Skip to content

Commit ad9e3f2

Browse files
committed
Overload to_geojson_geometry to support geometries
1 parent 83c53c2 commit ad9e3f2

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

lib/trino-geospatial-toolkit/src/main/java/io/trino/geospatial/GeometryUtils.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ public static org.locationtech.jts.geom.Geometry jtsGeometryFromJson(String json
179179

180180
public static String jsonFromJtsGeometry(org.locationtech.jts.geom.Geometry geometry)
181181
{
182-
return new GeoJsonWriter().write(geometry);
182+
GeoJsonWriter geoJsonWriter = new GeoJsonWriter();
183+
geoJsonWriter.setEncodeCRS(false);
184+
return geoJsonWriter.write(geometry);
183185
}
184186
}

plugin/trino-geospatial/src/main/java/io/trino/plugin/geospatial/GeoFunctions.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1426,7 +1426,16 @@ public static Slice fromGeoJsonGeometry(@SqlType(VARCHAR) Slice input)
14261426
@ScalarFunction("to_geojson_geometry")
14271427
@Description("Returns GeoJSON string based on the input spherical geography")
14281428
@SqlType(VARCHAR)
1429-
public static Slice toGeoJsonGeometry(@SqlType(StandardTypes.SPHERICAL_GEOGRAPHY) Slice input)
1429+
public static Slice toGeoJsonGeometryFromGeography(@SqlType(StandardTypes.SPHERICAL_GEOGRAPHY) Slice input)
1430+
{
1431+
return Slices.utf8Slice(jsonFromJtsGeometry(JtsGeometrySerde.deserialize(input)));
1432+
}
1433+
1434+
@SqlNullable
1435+
@ScalarFunction("to_geojson_geometry")
1436+
@Description("Returns GeoJSON string based on the input geometry")
1437+
@SqlType(VARCHAR)
1438+
public static Slice toGeoJsonGeometryFromGeometry(@SqlType(StandardTypes.GEOMETRY) Slice input)
14301439
{
14311440
return Slices.utf8Slice(jsonFromJtsGeometry(JtsGeometrySerde.deserialize(input)));
14321441
}

plugin/trino-geospatial/src/test/java/io/trino/plugin/geospatial/TestGeoFunctions.java

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2239,7 +2239,7 @@ private void assertGeometryFromHadoopShape(String hadoopHex, String expectedWkt)
22392239
}
22402240

22412241
@Test
2242-
public void testGeometryJsonConversion()
2242+
public void testSphericalGeographyJsonConversion()
22432243
{
22442244
// empty geometries should return empty
22452245
// empty geometries are represented by an empty JSON array in GeoJSON
@@ -2326,6 +2326,52 @@ private void assertInvalidGeometryJson(String json, String message)
23262326
.hasMessage(message);
23272327
}
23282328

2329+
@Test
2330+
public void testGeometryJsonConversion()
2331+
{
2332+
// empty geometries should return empty
2333+
// empty geometries are represented by an empty JSON array in GeoJSON
2334+
assertGeometryToAndFromJson("POINT EMPTY");
2335+
assertGeometryToAndFromJson("LINESTRING EMPTY");
2336+
assertGeometryToAndFromJson("POLYGON EMPTY");
2337+
assertGeometryToAndFromJson("MULTIPOINT EMPTY");
2338+
assertGeometryToAndFromJson("MULTILINESTRING EMPTY");
2339+
assertGeometryToAndFromJson("MULTIPOLYGON EMPTY");
2340+
assertGeometryToAndFromJson("GEOMETRYCOLLECTION EMPTY");
2341+
2342+
// valid nonempty geometries should return as is.
2343+
assertGeometryToAndFromJson("POINT (1 2)");
2344+
assertGeometryToAndFromJson("MULTIPOINT ((1 2), (3 4))");
2345+
assertGeometryToAndFromJson("LINESTRING (0 0, 1 2, 3 4)");
2346+
assertGeometryToAndFromJson("MULTILINESTRING (" +
2347+
"(1 1, 5 1), " +
2348+
"(2 4, 4 4))");
2349+
assertGeometryToAndFromJson("POLYGON (" +
2350+
"(0 0, 1 0, 1 1, 0 1, 0 0))");
2351+
assertGeometryToAndFromJson("POLYGON (" +
2352+
"(0 0, 3 0, 3 3, 0 3, 0 0), " +
2353+
"(1 1, 1 2, 2 2, 2 1, 1 1))");
2354+
assertGeometryToAndFromJson("MULTIPOLYGON (" +
2355+
"((1 1, 3 1, 3 3, 1 3, 1 1)), " +
2356+
"((2 4, 6 4, 6 6, 2 6, 2 4)))");
2357+
assertGeometryToAndFromJson("GEOMETRYCOLLECTION (" +
2358+
"POINT (1 2), " +
2359+
"LINESTRING (0 0, 1 2, 3 4), " +
2360+
"POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0)))");
2361+
2362+
// invalid geometries should return as is.
2363+
assertGeometryToAndFromJson("MULTIPOINT ((0 0), (0 1), (1 1), (0 1))");
2364+
assertGeometryToAndFromJson("LINESTRING (0 0, 0 1, 0 1, 1 1, 1 0, 0 0)");
2365+
assertGeometryToAndFromJson("LINESTRING (0 0, 1 1, 1 0, 0 1)");
2366+
}
2367+
2368+
private void assertGeometryToAndFromJson(String wkt)
2369+
{
2370+
assertThat(assertions.function("ST_AsText", "to_geometry(from_geojson_geometry(to_geojson_geometry(ST_GeometryFromText('%s'))))".formatted(wkt)))
2371+
.hasType(VARCHAR)
2372+
.isEqualTo(wkt);
2373+
}
2374+
23292375
@Test
23302376
public void testSTGeomFromKML()
23312377
{

0 commit comments

Comments
 (0)