2020
2121package org .neo4j .gis .spatial .functions ;
2222
23+ import static org .neo4j .gis .spatial .Constants .SRID_COORDINATES_2D ;
24+ import static org .neo4j .gis .spatial .Constants .SRID_COORDINATES_3D ;
25+
26+ import java .util .Arrays ;
27+ import java .util .Collection ;
28+ import org .locationtech .jts .geom .Coordinate ;
2329import org .locationtech .jts .geom .Geometry ;
2430import org .locationtech .jts .io .ParseException ;
2531import org .locationtech .jts .io .WKTReader ;
32+ import org .locationtech .jts .io .WKTWriter ;
2633import org .neo4j .gis .spatial .Layer ;
2734import org .neo4j .gis .spatial .procedures .SpatialProcedures .GeometryResult ;
2835import org .neo4j .gis .spatial .utilities .GeoJsonUtils ;
2936import org .neo4j .gis .spatial .utilities .SpatialApiBase ;
3037import org .neo4j .graphdb .Node ;
38+ import org .neo4j .graphdb .spatial .Point ;
3139import org .neo4j .procedure .Description ;
3240import org .neo4j .procedure .Name ;
3341import org .neo4j .procedure .UserFunction ;
@@ -58,7 +66,6 @@ public Object asGeometry(@Name("geometry") Object geometry) {
5866 return toNeo4jGeometry (null , geometry );
5967 }
6068
61-
6269 @ UserFunction ("spatial.wktToGeoJson" )
6370 @ Description ("Converts a WKT to GeoJson structure" )
6471 public Object wktToGeoJson (@ Name ("wkt" ) String wkt ) throws ParseException {
@@ -69,4 +76,38 @@ public Object wktToGeoJson(@Name("wkt") String wkt) throws ParseException {
6976 Geometry geometry = wktReader .read (wkt );
7077 return GeoJsonUtils .toGeoJsonStructure (geometry );
7178 }
79+
80+ @ UserFunction ("spatial.neo4jGeometryToWkt" )
81+ @ Description ("Converts a point or point array to WKT" )
82+ public String nativeToWkt (@ Name ("data" ) Object object ) {
83+ if (object instanceof Point point ) {
84+ var coordinate = convertToCoordinate (point );
85+ return WKTWriter .toPoint (coordinate );
86+ }
87+ if (object instanceof Point [] points ) {
88+ var coordinates = Arrays .stream (points ).map (SpatialFunctions ::convertToCoordinate )
89+ .toArray (Coordinate []::new );
90+ return WKTWriter .toLineString (coordinates );
91+ }
92+ if (object instanceof Collection <?> points ) {
93+ var coordinates = points .stream ()
94+ .filter (Point .class ::isInstance )
95+ .map (Point .class ::cast )
96+ .map (SpatialFunctions ::convertToCoordinate )
97+ .toArray (Coordinate []::new );
98+ return WKTWriter .toLineString (coordinates );
99+ }
100+ throw new IllegalArgumentException ("Unsupported type: " + object .getClass ());
101+ }
102+
103+ private static Coordinate convertToCoordinate (Point point ) {
104+ double [] coordinate = point .getCoordinate ().getCoordinate ();
105+ if (point .getCRS ().getCode () == SRID_COORDINATES_3D ) {
106+ return new Coordinate (coordinate [0 ], coordinate [1 ], coordinate [2 ]);
107+ } else if (point .getCRS ().getCode () == SRID_COORDINATES_2D ) {
108+ return new Coordinate (coordinate [0 ], coordinate [1 ]);
109+ } else {
110+ throw new IllegalArgumentException ("Unsupported CRS: " + point .getCRS ().getCode ());
111+ }
112+ }
72113}
0 commit comments