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

Geospatial function ST_GeomFromKML #24297

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
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
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 @@ -317,6 +318,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(GEOMETRY_TYPE_NAME)
Expand Down Expand Up @@ -1551,6 +1560,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>");
}
}
Loading