Skip to content

Commit

Permalink
Geospatial function ST_GeomFromKML
Browse files Browse the repository at this point in the history
  • Loading branch information
umartin authored and wendigo committed Jan 10, 2025
1 parent 218f3ed commit 88846bf
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/src/main/sphinx/functions/geospatial.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ Returns a geometry type object from WKT representation.
Returns a geometry type object from WKB or EWKB representation.
:::

:::{function} ST_GeomFromKML(varchar) -> Geometry
Returns a geometry type object from KML representation.
:::

:::{function} geometry_from_hadoop_shape(varbinary) -> Geometry
Returns a geometry type object from Spatial Framework for Hadoop representation.
:::
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKBReader;
import org.locationtech.jts.io.kml.KMLReader;
import org.locationtech.jts.linearref.LengthIndexedLine;
import org.locationtech.jts.operation.distance.DistanceOp;

Expand Down Expand Up @@ -315,6 +316,14 @@ public static Slice stGeomFromBinary(@SqlType(VARBINARY) Slice input)
return serialize(geomFromBinary(input));
}

@Description("Returns a Geometry type object from OGC KML representation")
@ScalarFunction("ST_GeomFromKML")
@SqlType(GEOMETRY_TYPE_NAME)
public static Slice stGeomFromKML(@SqlType(VARCHAR) Slice input)
{
return serialize(geomFromKML(input));
}

@Description("Returns a Geometry type object from Spatial Framework for Hadoop representation")
@ScalarFunction("geometry_from_hadoop_shape")
@SqlType(StandardTypes.GEOMETRY)
Expand Down Expand Up @@ -1549,6 +1558,16 @@ private static Geometry geomFromBinary(Slice input)
}
}

private static Geometry geomFromKML(Slice input)
{
try {
return new KMLReader().read(input.toStringUtf8());
}
catch (ParseException e) {
throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "Invalid KML: " + input.toStringUtf8(), e);
}
}

private static ByteBuffer getShapeByteBuffer(Slice input)
{
int offset = HADOOP_SHAPE_SIZE_WKID + HADOOP_SHAPE_SIZE_TYPE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2325,4 +2325,16 @@ private void assertInvalidGeometryJson(String json, String message)
assertTrinoExceptionThrownBy(assertions.function("from_geojson_geometry", "'%s'".formatted(json))::evaluate)
.hasMessage(message);
}

@Test
public void testSTGeomFromKML()
{
assertThat(assertions.expression("ST_AsText(ST_GeomFromKML(geometry))")
.binding("geometry", "'<Point><coordinates>-2,2</coordinates></Point>'"))
.hasType(VARCHAR)
.isEqualTo("POINT (-2 2)");

assertTrinoExceptionThrownBy(assertions.function("ST_GeomFromKML", "'<Point>'")::evaluate)
.hasMessage("Invalid KML: <Point>");
}
}

0 comments on commit 88846bf

Please sign in to comment.