Skip to content

Commit

Permalink
Support reading geometries in EWKB format
Browse files Browse the repository at this point in the history
  • Loading branch information
umartin authored and wendigo committed Nov 4, 2024
1 parent 579e8ac commit f61483d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/src/main/sphinx/functions/geospatial.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Returns a geometry type object from WKT representation.
:::

:::{function} ST_GeomFromBinary(varbinary) -> Geometry
Returns a geometry type object from WKB representation.
Returns a geometry type object from WKB or EWKB representation.
:::

:::{function} geometry_from_hadoop_shape(varbinary) -> Geometry
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryCollection;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKBReader;
import org.locationtech.jts.linearref.LengthIndexedLine;
import org.locationtech.jts.operation.distance.DistanceOp;

Expand Down Expand Up @@ -175,6 +177,8 @@ public final class GeoFunctions
private static final EnumSet<GeometryType> VALID_TYPES_FOR_ST_POINTS = EnumSet.of(
LINE_STRING, POLYGON, POINT, MULTI_POINT, MULTI_LINE_STRING, MULTI_POLYGON, GEOMETRY_COLLECTION);

private static final WKBReader WKB_READER = new WKBReader();

private GeoFunctions() {}

@Description("Returns a Geometry type LineString object from Well-Known Text representation (WKT)")
Expand Down Expand Up @@ -1538,18 +1542,15 @@ private static OGCGeometry geometryFromText(Slice input)
return geometry;
}

private static OGCGeometry geomFromBinary(Slice input)
private static Geometry geomFromBinary(Slice input)
{
requireNonNull(input, "input is null");
OGCGeometry geometry;
try {
geometry = OGCGeometry.fromBinary(input.toByteBuffer().slice());
return WKB_READER.read(input.getBytes());
}
catch (IllegalArgumentException | IndexOutOfBoundsException e) {
catch (ParseException e) {
throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "Invalid WKB", e);
}
geometry.setSpatialReference(null);
return geometry;
}

private static ByteBuffer getShapeByteBuffer(Slice input)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2159,6 +2159,12 @@ public void testSTGeometryFromBinary()
assertGeomFromBinary("MULTIPOLYGON (((1 1, 3 1, 3 3, 1 3, 1 1)), ((2 4, 6 4, 6 6, 2 6, 2 4)))");
assertGeomFromBinary("GEOMETRYCOLLECTION (POINT (1 2), LINESTRING (0 0, 1 2, 3 4), POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0)))");

// The EWKB representation of "SRID=4326;POINT (1 1)".
assertThat(assertions.expression("ST_AsText(ST_GeomFromBinary(wkb))")
.binding("wkb", "x'0101000020E6100000000000000000F03F000000000000F03F'"))
.hasType(VARCHAR)
.isEqualTo("POINT (1 1)");

// array of geometries
assertThat(assertions.expression("transform(a, wkb -> ST_AsText(ST_GeomFromBinary(wkb)))")
.binding("a", "ARRAY[ST_AsBinary(ST_Point(1, 2)), ST_AsBinary(ST_Point(3, 4))]"))
Expand Down

0 comments on commit f61483d

Please sign in to comment.