Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: added GeometryCollection to GeoJSONUtils #556

Open
wants to merge 2 commits into
base: beta
Choose a base branch
from
Open
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
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
Loading