From b4921166cfd468d0de2e849d04c9a22004a8753b Mon Sep 17 00:00:00 2001 From: Hazimi Asyraf Date: Fri, 13 Dec 2024 19:06:23 +0800 Subject: [PATCH] Add missing functions for multipolygon, multiline and multipoint --- .../reactnative/utils/GeoJSONUtils.java | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) 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 74a67d436..7fe1b0a8a 100644 --- a/android/src/main/java/org/maplibre/reactnative/utils/GeoJSONUtils.java +++ b/android/src/main/java/org/maplibre/reactnative/utils/GeoJSONUtils.java @@ -10,9 +10,11 @@ import org.maplibre.geojson.FeatureCollection; import org.maplibre.geojson.Geometry; import org.maplibre.geojson.GeometryCollection; +import org.maplibre.geojson.MultiLineString; import org.maplibre.geojson.LineString; import org.maplibre.geojson.MultiPoint; import org.maplibre.geojson.Point; +import org.maplibre.geojson.MultiPolygon; import org.maplibre.geojson.Polygon; import org.maplibre.android.geometry.LatLng; import org.maplibre.android.geometry.LatLngBounds; @@ -48,6 +50,12 @@ public static WritableMap fromGeometry(Geometry geometry) { 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; } @@ -74,6 +82,27 @@ public static WritableMap fromPolygon(Polygon polygon) { return map; } + public static WritableMap fromMultiPoint(MultiPoint multiPoint) { + WritableMap map = Arguments.createMap(); + map.putString("type", "MultiPoint"); + map.putArray("coordinates", getCoordinates(multiPoint)); + return map; + } + + public static WritableMap fromMultiLineString(MultiLineString multiLineString) { + WritableMap map = Arguments.createMap(); + map.putString("type", "MultiLineString"); + map.putArray("coordinates", getCoordinates(multiLineString)); + return map; + } + + public static WritableMap fromMultiPolygon(MultiPolygon multiPolygon) { + WritableMap map = Arguments.createMap(); + map.putString("type", "MultiPolygon"); + map.putArray("coordinates", getCoordinates(multiPolygon)); + return map; + } + public static WritableArray getCoordinates(Point point) { return Arguments.fromArray(pointToDoubleArray(point)); } @@ -110,6 +139,57 @@ public static WritableArray getCoordinates(Polygon polygon) { return array; } + public static WritableArray getCoordinates(MultiPoint multiPoint) { + WritableArray array = Arguments.createArray(); + + List points = multiPoint.coordinates(); + for (Point point : points) { + array.pushArray(Arguments.fromArray(pointToDoubleArray(point))); + } + + return array; + } + + public static WritableArray getCoordinates(MultiLineString multiLineString) { + WritableArray array = Arguments.createArray(); + + List> lines = multiLineString.coordinates(); + for (List line : lines) { + WritableArray lineArray = Arguments.createArray(); + + for (Point point : line) { + lineArray.pushArray(Arguments.fromArray(pointToDoubleArray(point))); + } + + array.pushArray(lineArray); + } + + return array; + } + + public static WritableArray getCoordinates(MultiPolygon multiPolygon) { + WritableArray array = Arguments.createArray(); + + List>> polygons = multiPolygon.coordinates(); + for (List> polygon : polygons) { + WritableArray polygonArray = Arguments.createArray(); + + for (List ring : polygon) { + WritableArray ringArray = Arguments.createArray(); + + for (Point point : ring) { + ringArray.pushArray(Arguments.fromArray(pointToDoubleArray(point))); + } + + polygonArray.pushArray(ringArray); + } + + array.pushArray(polygonArray); + } + + return array; + } + public static WritableMap toPointFeature(LatLng latLng, WritableMap properties) { WritableMap map = new WritableNativeMap(); map.putString("type", "Feature");