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

Add a spatial function to convert WKT to Geo-JSON #406

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -20,8 +20,12 @@

package org.neo4j.gis.spatial.functions;

import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;
import org.neo4j.gis.spatial.Layer;
import org.neo4j.gis.spatial.procedures.SpatialProcedures.GeometryResult;
import org.neo4j.gis.spatial.utilities.GeoJsonUtils;
import org.neo4j.gis.spatial.utilities.SpatialApiBase;
import org.neo4j.graphdb.Node;
import org.neo4j.procedure.Description;
Expand Down Expand Up @@ -55,4 +59,14 @@ public Object asGeometry(@Name("geometry") Object geometry) {
}


@UserFunction("spatial.convert.wktToGeoJson")
Andy2003 marked this conversation as resolved.
Show resolved Hide resolved
@Description("Converts a WKT to GeoJson structure")
public Object wktToGeoJson(@Name("wkt") String wkt) throws ParseException {
if (wkt == null) {
return null;
}
WKTReader wktReader = new WKTReader();
Geometry geometry = wktReader.read(wkt);
return GeoJsonUtils.toGeoJsonStructure(geometry);
}
}
82 changes: 82 additions & 0 deletions src/main/java/org/neo4j/gis/spatial/utilities/GeoJsonUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j Spatial.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.neo4j.gis.spatial.utilities;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryCollection;
import org.locationtech.jts.geom.LineString;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.Polygon;


public class GeoJsonUtils {

private GeoJsonUtils() {
}

public static Map<String, Object> toGeoJsonStructure(Geometry geometry) {
return Map.of(
"type", geometry.getGeometryType(),
"coordinates", getCoordinates(geometry)
);
}

private static List<?> getCoordinates(Geometry geometry) {
if (geometry instanceof Point point) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are missing types here, like MultiPoint, MultiLineString, MultiPolygon. Should be easy enough to add these.

Copy link
Collaborator Author

@Andy2003 Andy2003 May 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No all these types are covered by inheritance:
image

return getPoint(point.getCoordinate());
}
if (geometry instanceof LineString lineString) {
return Arrays.stream(lineString.getCoordinates()).map(GeoJsonUtils::getPoint).toList();
}
if (geometry instanceof Polygon polygon) {
return Stream.concat(
Stream.of(polygon.getExteriorRing()),
IntStream.range(0, polygon.getNumInteriorRing())
.mapToObj(polygon::getInteriorRingN)
)
.map(GeoJsonUtils::getCoordinates)
.toList();
}
if (geometry instanceof GeometryCollection geometryCollection) {
return IntStream.range(0, geometryCollection.getNumGeometries())
.mapToObj(geometryCollection::getGeometryN)
.map(GeoJsonUtils::getCoordinates)
.toList();
}
throw new IllegalArgumentException("Unsupported geometry type: " + geometry.getGeometryType());
}

private static List<Object> getPoint(Coordinate coordinate) {
if (Double.isNaN(coordinate.getZ())) {
return List.of(coordinate.getX(), coordinate.getY());
}
return List.of(
coordinate.getX(),
coordinate.getY(),
coordinate.getZ());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@

package org.neo4j.gis.spatial.functions;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.closeTo;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;

import java.util.List;
import java.util.Map;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -69,4 +72,24 @@ public void literal_geometry_return() {
"WITH spatial.asGeometry({latitude: 5.0, longitude: 4.0}) AS geometry RETURN geometry", "geometry");
assertInstanceOf(Geometry.class, geometry, "Should be Geometry type");
}

@Test
public void wktToGeoJson() {
String wkt = "MULTIPOLYGON(((15.3 60.2, 15.3 60.4, 15.7 60.4, 15.7 60.2, 15.3 60.2)))";
Andy2003 marked this conversation as resolved.
Show resolved Hide resolved
Object json = executeObject("return spatial.convert.wktToGeoJson($wkt) as json", Map.of("wkt", wkt), "json");
assertThat(json, equalTo(Map.of(
"type", "MultiPolygon",
"coordinates", List.of( // MultiPolygon
List.of( // Polygon
List.of( // LineString
List.of(15.3, 60.2),
List.of(15.3, 60.4),
List.of(15.7, 60.4),
List.of(15.7, 60.2),
List.of(15.3, 60.2)
)
)
)
)));
}
}