Skip to content

Geometry Input

Idan Sheinberg edited this page Dec 16, 2022 · 4 revisions

ST_GeomFromText

Description: Deserialize WKT to geometry format
Input Argument #1: Text (WKT)
Input Argument #2 (Optional): Int (SRID)
Output: Geometry (WKB)

SELECT ST_GeomFromText(
  'LINESTRING (100.0 92.5, 100.0 8.2, 26.0 8.2)'
)

Result

AQIAAAADAAAAAAAAAAAAWUAAAAAAACBXQAAAAAAAAFlAZmZmZmZmIEAAAAAAAAA6QGZmZmZmZiBA

Note
The actual result is a binary bytestring that may contain non printable characters.
Dremio Base64 encodes the data in order to properly display it in the UI.

ST_GeomFromEWKT

Description: Deserialize EWKT to geometry format
Input Argument #1: Text (EWKT)
Output: Geometry (WKB) with SRID set

SELECT ST_GeomFromEWKT(
  'SRID=4326;LINESTRING (100.0 92.5, 100.0 8.2, 26.0 8.2)'
)

Result

ACAAAAIAABDmAAAAA0BZAAAAAAAAQFcgAAAAAABAWQAAAAAAAEAgZmZmZmZmQDoAAAAAAABAIGZmZmZmZg==

Note
The actual result is a binary bytestring that may contain non printable characters.
Dremio Base64 encodes the data in order to properly display it in the UI.

ST_GeomFromWKB

Description: Deserialize WKB to geometry format
Input Argument #1: Binary (WKB)
Input Argument #2 (Optional): Int (SRID)
Output: Geometry (WKB)

SELECT ST_AsGeoJson(
  ST_GeomFromWKB(
    FROM_HEX(
      '01020000000300000000000000000059400000000000205740000000000000594066666666666620400000000000003A406666666666662040'
    ),
    3857
  )
)

Result

{"type":"LineString","coordinates":[[100,92.5],[100,8.2],[26,8.2]],"crs":{"type":"name","properties":{"name":"EPSG:3857"}}}

Note
There no need to use this function unless you're setting SRID, as the GIS functions already de/serialize data as EWKB.
Only FROM_HEX is mandatory when the input is a HEX encoded WKB. Calling without SRID,
will simply output the binary data for the enclosing functions

FROM_HEX

Description: Decode WKB/EWKB HEX Strings (such as those returned when querying geometry columns from PostGIS) to geometry format
Input Argument #1: Hex encoded WKB/EWKB (WKB)
Output: Geometry (WKB)

SELECT ST_AsText(
  FROM_HEX(
    '0101000020E610000054E57B4622E828408B074AC09EF34440'
  )
)

Result

POINT (12.4533865 41.9032822)

Note #1:
This is a Dremio built in function.

Note #2:
PostGIS<->Dremio bindings map the PostGIS geometry type as a string, this it serialized as as a HEX encoded bytestring.

Note #3:
A more concrete usage example (when querying from PostGIS, for example), would be:

SELECT FROM_HEX(<geom_column_name>) AS geometry_wkt 
FROM "<postgis_data_source>"."<schema>"."<table>" 
WHERE ...