Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 56 additions & 5 deletions src/main/java/il/org/osm/israelhiking/BBoxDocument.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,71 @@
package il.org.osm.israelhiking;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.locationtech.jts.geom.Envelope;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.LineString;
import org.locationtech.jts.geom.MultiPolygon;
import org.locationtech.jts.geom.Polygon;


public class BBoxDocument {
public Map<String, String> name = new HashMap<String,String>();
public Map<String, Object> bbox;
public double area;

public void setBBox(Envelope envelope) {
public void setBBox(Geometry geometry) {
bbox = new HashMap<String, Object>();
bbox.put("type", "envelope");
double[][] coordinates = new double[][]{{envelope.getMinX(), envelope.getMaxY()}, {envelope.getMaxX(), envelope.getMinY()}};
bbox.put("type", geometry.getGeometryType().toLowerCase());
bbox.put("coordinates", convertJTSToGeoJson(geometry));
}

private Object convertJTSToGeoJson(Geometry geometry) {
if (geometry instanceof Polygon) {
return convertPolygonToGeoJson((Polygon) geometry);
} else if (geometry instanceof MultiPolygon) {
return convertMultiPolygonToGeoJson((MultiPolygon) geometry);
}
throw new UnsupportedOperationException("Geometry type not supported: " + geometry.getGeometryType());

Check warning on line 33 in src/main/java/il/org/osm/israelhiking/BBoxDocument.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/il/org/osm/israelhiking/BBoxDocument.java#L33

Added line #L33 was not covered by tests
}

private List<List<List<Double>>> convertPolygonToGeoJson(Polygon polygon) {
List<List<List<Double>>> polygonCoordinates = new ArrayList<>();

// Add exterior ring
polygonCoordinates.add(convertLinearRingToList(polygon.getExteriorRing()));

// Add interior rings (holes)
for (int i = 0; i < polygon.getNumInteriorRing(); i++) {
polygonCoordinates.add(convertLinearRingToList(polygon.getInteriorRingN(i)));
}

return polygonCoordinates;
}

private List<List<List<List<Double>>>> convertMultiPolygonToGeoJson(MultiPolygon multiPolygon) {
List<List<List<List<Double>>>> multiPolygonCoordinates = new ArrayList<>();

for (int i = 0; i < multiPolygon.getNumGeometries(); i++) {
Polygon polygon = (Polygon) multiPolygon.getGeometryN(i);
multiPolygonCoordinates.add(convertPolygonToGeoJson(polygon));
}

return multiPolygonCoordinates;
}

private List<List<Double>> convertLinearRingToList(LineString ring) {
List<List<Double>> coordinates = new ArrayList<>();
Coordinate[] coords = ring.getCoordinates();

for (Coordinate coord : coords) {
coordinates.add(Arrays.asList(coord.x, coord.y));
}

bbox.put("coordinates", coordinates);
return coordinates;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -506,17 +506,17 @@ private void insertPointToElasticsearch(PointDocument pointDocument, String docI
}

private void insertBboxToElasticsearch(SourceFeature feature, String[] supportedLanguages) {
Envelope envelope;
Geometry polygon;
try {
envelope = feature.polygon().getEnvelopeInternal();
polygon = GeoUtils.worldToLatLonCoords(feature.polygon());
} catch (GeometryException e) {
return;
}
try {
var bbox = new BBoxDocument();
bbox.area = feature.areaMeters();

bbox.setBBox(GeoUtils.toLatLonBoundsBounds(envelope));
bbox.setBBox(polygon);
for (String lang : supportedLanguages) {
CoalesceIntoMap(bbox.name, lang, feature.getString("name:" + lang), feature.getString("name"));
}
Expand Down