Skip to content

Commit

Permalink
fix: added GeometryCollection to GeoJSONUtils (#556)
Browse files Browse the repository at this point in the history
  • Loading branch information
KiwiKilian authored Dec 27, 2024
1 parent 8c605d4 commit e6b7a66
Showing 1 changed file with 49 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableNativeArray;
import com.facebook.react.bridge.WritableNativeMap;
import com.google.gson.JsonObject;

import org.maplibre.geojson.Feature;
import org.maplibre.geojson.FeatureCollection;
import org.maplibre.geojson.Geometry;
Expand All @@ -19,46 +21,55 @@
import org.maplibre.android.geometry.LatLng;
import org.maplibre.android.geometry.LatLngBounds;
import org.maplibre.android.geometry.LatLngQuad;
import org.maplibre.android.style.light.Position;
import org.maplibre.android.log.Logger;
import org.maplibre.turf.TurfMeasurement;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class GeoJSONUtils {
public static final String LOG_TAG = "GeoJSONUtils";

public static WritableMap fromFeature(Feature feature) {
WritableMap map = Arguments.createMap();
map.putString("type", "Feature");
map.putString("id", feature.id());

WritableMap geometry = fromGeometry(feature.geometry());
map.putMap("geometry", geometry);
Geometry geometry = feature.geometry();
if (geometry == null) {
map.putNull("geometry");
} else {
map.putMap("geometry", fromGeometry(geometry));
}

WritableMap properties = ConvertUtils.toWritableMap(feature.properties());
map.putMap("properties", properties);
JsonObject properties = feature.properties();
if(properties == null) {
map.putNull("properties");
} else {
map.putMap("properties", ConvertUtils.toWritableMap(properties));
}

return map;
}

public static WritableMap fromGeometry(Geometry geometry) {
final String type = geometry.type();

switch (type) {
case "Point":
return fromPoint((Point) geometry);
case "LineString":
return fromLineString((LineString) geometry);
case "Polygon":
return fromPolygon((Polygon) geometry);
case "MultiPoint":
return fromMultiPoint((MultiPoint) geometry);
case "MultiLineString":
return fromMultiLineString((MultiLineString) geometry);
case "MultiPolygon":
return fromMultiPolygon((MultiPolygon) geometry);
default:
return null;
}
return switch (type) {
case "Point" -> fromPoint((Point) geometry);
case "LineString" -> fromLineString((LineString) geometry);
case "Polygon" -> fromPolygon((Polygon) geometry);
case "MultiPoint" -> fromMultiPoint((MultiPoint) geometry);
case "MultiLineString" -> fromMultiLineString((MultiLineString) geometry);
case "MultiPolygon" -> fromMultiPolygon((MultiPolygon) geometry);
case "GeometryCollection" -> fromGeometryCollection((GeometryCollection) geometry);
default -> {
Logger.w(LOG_TAG, "GeoJSONUtils.fromGeometry unsupported type: \"" + type + "\"");

yield null;
}
};
}

public static WritableMap fromPoint(Point point) {
Expand Down Expand Up @@ -103,6 +114,23 @@ public static WritableMap fromMultiPolygon(MultiPolygon multiPolygon) {
return map;
}

public static WritableMap fromGeometryCollection(GeometryCollection geometryCollection) {
WritableMap map = Arguments.createMap();
map.putString("type", "GeometryCollection");

map.putArray("geometries",
Arguments.fromList(
geometryCollection
.geometries()
.stream()
.map(GeoJSONUtils::fromGeometry)
.collect(Collectors.toList())
)
);

return map;
}

public static WritableArray getCoordinates(Point point) {
return Arguments.fromArray(pointToDoubleArray(point));
}
Expand All @@ -122,9 +150,6 @@ public static WritableArray getCoordinates(Polygon polygon) {
WritableArray array = Arguments.createArray();

List<List<Point>> points = polygon.coordinates();
if (points == null) {
return array;
}

for (List<Point> curPoint : points) {
WritableArray innerArray = Arguments.createArray();
Expand Down

0 comments on commit e6b7a66

Please sign in to comment.