From b1f53fd4e2f10fdfa97c0eaca386992f9df8f985 Mon Sep 17 00:00:00 2001 From: Kilian Finger Date: Sun, 15 Dec 2024 11:03:42 +0100 Subject: [PATCH 1/2] feat: implement `fromGeometryCollection` in GeoJSONUtils --- .../reactnative/utils/GeoJSONUtils.java | 52 +++++++++++++------ 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/android/src/main/java/org/maplibre/reactnative/utils/GeoJSONUtils.java b/android/src/main/java/org/maplibre/reactnative/utils/GeoJSONUtils.java index 7fe1b0a8..df8cd851 100644 --- a/android/src/main/java/org/maplibre/reactnative/utils/GeoJSONUtils.java +++ b/android/src/main/java/org/maplibre/reactnative/utils/GeoJSONUtils.java @@ -19,13 +19,16 @@ 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"); @@ -43,22 +46,20 @@ public static WritableMap fromFeature(Feature feature) { 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) { @@ -103,6 +104,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)); } From 926029f05a08764e5c627c1c0ad517d3f28fbb44 Mon Sep 17 00:00:00 2001 From: Kilian Finger Date: Sun, 15 Dec 2024 11:03:59 +0100 Subject: [PATCH 2/2] fix: improve nullable checks in GeoJSONUtils --- .../reactnative/utils/GeoJSONUtils.java | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/android/src/main/java/org/maplibre/reactnative/utils/GeoJSONUtils.java b/android/src/main/java/org/maplibre/reactnative/utils/GeoJSONUtils.java index df8cd851..797c0372 100644 --- a/android/src/main/java/org/maplibre/reactnative/utils/GeoJSONUtils.java +++ b/android/src/main/java/org/maplibre/reactnative/utils/GeoJSONUtils.java @@ -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; @@ -34,11 +36,19 @@ public static WritableMap fromFeature(Feature feature) { 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; } @@ -140,9 +150,6 @@ public static WritableArray getCoordinates(Polygon polygon) { WritableArray array = Arguments.createArray(); List> points = polygon.coordinates(); - if (points == null) { - return array; - } for (List curPoint : points) { WritableArray innerArray = Arguments.createArray();