Skip to content

Commit

Permalink
SQL: Add ST_X, ST_Y and ST_GEOMETRY_TYPE functions
Browse files Browse the repository at this point in the history
Adds 3 new geosql functions. The ST_Z functions as well as ST_X and
ST_Y functions for polygons are not supported because of the issues
described in elastic#40908. The suppor will be added as soon as the
ShapeBuilder issues are resolved.

Relates to elastic#29872
  • Loading branch information
imotov committed Apr 11, 2019
1 parent c6d611b commit c1721d3
Show file tree
Hide file tree
Showing 16 changed files with 544 additions and 16 deletions.
77 changes: 77 additions & 0 deletions docs/reference/sql/functions/geo.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,83 @@ Returns the geometry from WKT representation. The return type is geometry.
include-tagged::{sql-specs}/geo/docs.csv-spec[aswkt]
--------------------------------------------------

==== Geometry Properties

[[sql-functions-geo-st-geometrytype]]
===== `ST_GeometryType`

.Synopsis:
[source, sql]
--------------------------------------------------
ST_GeometryType(geometry<1>)
--------------------------------------------------

*Input*:

<1> geometry

*Output*: string

.Description:

Returns the type of the `geometry` such as "Point", "Polygon", "MultiPolygon", etc.
The return type is string.

["source","sql",subs="attributes,macros"]
--------------------------------------------------
include-tagged::{sql-specs}/geo/docs.csv-spec[geometrytype]
--------------------------------------------------

[[sql-functions-geo-x]]
===== `ST_X`

.Synopsis:
[source, sql]
--------------------------------------------------
ST_X(geometry<1>)
--------------------------------------------------

*Input*:

<1> geometry

*Output*: double

.Description:

Returns the longitude of the first point in the geometry.
The return type is double.

["source","sql",subs="attributes,macros"]
--------------------------------------------------
include-tagged::{sql-specs}/geo/docs.csv-spec[x]
--------------------------------------------------

[[sql-functions-geo-y]]
===== `ST_Y`

.Synopsis:
[source, sql]
--------------------------------------------------
ST_Y(geometry<1>)
--------------------------------------------------

*Input*:

<1> geometry

*Output*: double

.Description:

Returns the the latitude of the first point in the geometry.
The return type is double.

["source","sql",subs="attributes,macros"]
--------------------------------------------------
include-tagged::{sql-specs}/geo/docs.csv-spec[y]
--------------------------------------------------

[[sql-functions-geo-st-distance]]
===== `ST_Distance`

Expand Down
3 changes: 3 additions & 0 deletions docs/reference/sql/functions/index.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@
* <<sql-functions-geo>>
** <<sql-functions-geo-st-as-wkt>>
** <<sql-functions-geo-st-wkt-to-sql>>
** <<sql-functions-geo-st-geometrytype>>
** <<sql-functions-geo-st-x>>
** <<sql-functions-geo-st-y>>
** <<sql-functions-geo-st-distance>>
* <<sql-functions-system>>
** <<sql-functions-system-database>>
Expand Down
3 changes: 3 additions & 0 deletions x-pack/plugin/sql/qa/src/main/resources/command.csv-spec
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,11 @@ USER |SCALAR
ST_ASTEXT |SCALAR
ST_ASWKT |SCALAR
ST_DISTANCE |SCALAR
ST_GEOMETRYTYPE |SCALAR
ST_GEOMFROMTEXT |SCALAR
ST_WKTTOSQL |SCALAR
ST_X |SCALAR
ST_Y |SCALAR
SCORE |SCORE
;

Expand Down
3 changes: 3 additions & 0 deletions x-pack/plugin/sql/qa/src/main/resources/docs/docs.csv-spec
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,11 @@ USER |SCALAR
ST_ASTEXT |SCALAR
ST_ASWKT |SCALAR
ST_DISTANCE |SCALAR
ST_GEOMETRYTYPE |SCALAR
ST_GEOMFROMTEXT |SCALAR
ST_WKTTOSQL |SCALAR
ST_X |SCALAR
ST_Y |SCALAR
SCORE |SCORE
// end::showFunctions
;
Expand Down
33 changes: 33 additions & 0 deletions x-pack/plugin/sql/qa/src/main/resources/geo/docs.csv-spec
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,36 @@ SELECT ST_Distance(ST_WKTToSQL('POINT (10 20)'), ST_WKTToSQL('POINT (20 30)')) d
1499101.2889383635
// end::distance
;

///////////////////////////////
//
// Geometry Properties
//
///////////////////////////////

selectGeometryType
// tag::geometrytype
SELECT ST_GeometryType(ST_WKTToSQL('POINT (10 20)')) type;

type:s
POINT
// end::geometrytype
;

selectX
// tag::x
SELECT ST_X(ST_WKTToSQL('POINT (10 20)')) x;

x:d
10.0
// end::x
;

selectY
// tag::y
SELECT ST_Y(ST_WKTToSQL('POINT (10 20)')) y;

y:d
20.0
// end::y
;
52 changes: 52 additions & 0 deletions x-pack/plugin/sql/qa/src/main/resources/geo/geosql.csv-spec
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,55 @@ SELECT ST_Distance(ST_WktToSql(NULL), ST_WktToSQL('POINT (-71 42)')) shape;
shape:d
null
;

groupByGeometryType
SELECT COUNT(*) cnt, ST_GeometryType(location) gt FROM geo GROUP BY ST_GeometryType(location);

cnt:l | gt:s
15 |POINT
;

groupByEastWest
SELECT COUNT(*) cnt, FLOOR(ST_X(location)/90) east FROM geo GROUP BY east ORDER BY east;

cnt:l | east:l
3 |-2
3 |-1
4 |0
5 |1
;

groupByNorthSouth
SELECT COUNT(*) cnt, FLOOR(ST_Y(location)/45) north FROM geo GROUP BY north ORDER BY north;

cnt:l | north:l
1 |-1
9 |0
5 |1
;

selectFilterByXOfLocation
SELECT city, ST_X(shape) x, ST_Y(shape) y FROM geo WHERE ST_X(location) > 0 ORDER BY ST_Y(location);

city:s | x:d | y:d
Sydney |151.208629 |-33.863385
Singapore |103.855535 |1.295868
Hong Kong |114.183925 |22.281397
Tokyo |139.76402225 |35.669616
Seoul |127.060851 |37.509132
Munich |11.537505 |48.146321
Paris |2.351773 |48.845538
Amsterdam |4.850312 |52.347557
Berlin |13.390889 |52.486701
;

selectFilterByRegionPoint
SELECT city, region FROM geo WHERE ST_X(ST_WKTTOSQL(region_point)) < 0 ORDER BY ST_X(location);

city:s | region:s
San Francisco |Americas
Mountain View |Americas
Phoenix |Americas
Chicago |Americas
New York |Americas
;
52 changes: 52 additions & 0 deletions x-pack/plugin/sql/qa/src/main/resources/ogc/ogc.sql-spec
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,55 @@ SELECT fid, name, num_lanes, aliases, REPLACE(UCASE(ST_AsText(centerline)), '.0'

selectSinglePoint
SELECT ST_GeomFromText('point (10.0 12.0)') point;


//
// Geometry Property Functions
//
// H2GIS doesn't follow the standard here that mandates ST_Dimension returns SMALLINT
selectLakesProps
SELECT fid, UCASE(ST_GeometryType(shore)) type FROM lakes ORDER BY fid;
selectRoadSegmentsProps
SELECT fid, UCASE(ST_GeometryType(centerline)) type FROM road_segments ORDER BY fid;
selectDividedRoutesProps
SELECT fid, UCASE(ST_GeometryType(centerlines)) type FROM divided_routes ORDER BY fid;
selectForestsProps
SELECT fid, UCASE(ST_GeometryType(boundary)) type FROM forests ORDER BY fid;
selectBridgesProps
SELECT fid, UCASE(ST_GeometryType(position)) type FROM bridges ORDER BY fid;
selectStreamsProps
SELECT fid, UCASE(ST_GeometryType(centerline)) type FROM streams ORDER BY fid;
selectBuildingsProps
SELECT fid, UCASE(ST_GeometryType(position)) type1, UCASE(ST_GeometryType(footprint)) type2 FROM buildings ORDER BY fid;
selectPondsProps
SELECT fid, UCASE(ST_GeometryType(shores)) type FROM ponds ORDER BY fid;
selectNamedPlacesProps
SELECT fid, UCASE(ST_GeometryType(boundary)) type FROM named_places ORDER BY fid;
selectMapNeatLinesProps
SELECT fid, UCASE(ST_GeometryType(neatline)) type FROM map_neatlines ORDER BY fid;

// AwaitsFix https://github.com/elastic/elasticsearch/issues/40908
// selectLakesXY
// SELECT fid, ST_X(shore) x, ST_Y(shore) y FROM lakes ORDER BY fid;
selectRoadSegmentsXY
SELECT fid, ST_X(centerline) x, ST_Y(centerline) y FROM road_segments ORDER BY fid;
selectDividedRoutesXY
SELECT fid, ST_X(centerlines) x, ST_Y(centerlines) y FROM divided_routes ORDER BY fid;
// AwaitsFix https://github.com/elastic/elasticsearch/issues/40908
// selectForestsXY
// SELECT fid, ST_X(boundary) x, ST_Y(boundary) y FROM forests ORDER BY fid;
selectBridgesPositionsXY
SELECT fid, ST_X(position) x, ST_Y(position) y FROM bridges ORDER BY fid;
selectStreamsXY
SELECT fid, ST_X(centerline) x, ST_Y(centerline) y FROM streams ORDER BY fid;
selectBuildingsXY
SELECT fid, ST_X(position) x, ST_Y(position) y FROM buildings ORDER BY fid;
// AwaitsFix https://github.com/elastic/elasticsearch/issues/40908
// selectBuildingsFootprintsXY
// SELECT fid, ST_X(footprint) x, ST_Y(footprint) y FROM buildings ORDER BY fid;
// selectPondsXY
// SELECT fid, ST_X(shores) x, ST_Y(shores) y FROM ponds ORDER BY fid;
// selectNamedPlacesXY
// SELECT fid, ST_X(boundary) x, ST_Y(boundary) y FROM named_places ORDER BY fid;
// selectMapNeatLinesXY
// SELECT fid, ST_X(neatline) x, ST_Y(neatline) y FROM map_neatlines ORDER BY fid;
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@
import org.elasticsearch.xpack.sql.expression.function.scalar.datetime.Year;
import org.elasticsearch.xpack.sql.expression.function.scalar.geo.StAswkt;
import org.elasticsearch.xpack.sql.expression.function.scalar.geo.StDistance;
import org.elasticsearch.xpack.sql.expression.function.scalar.geo.StGeometryType;
import org.elasticsearch.xpack.sql.expression.function.scalar.geo.StWkttosql;
import org.elasticsearch.xpack.sql.expression.function.scalar.geo.StX;
import org.elasticsearch.xpack.sql.expression.function.scalar.geo.StY;
import org.elasticsearch.xpack.sql.expression.function.scalar.math.ACos;
import org.elasticsearch.xpack.sql.expression.function.scalar.math.ASin;
import org.elasticsearch.xpack.sql.expression.function.scalar.math.ATan;
Expand Down Expand Up @@ -258,7 +261,10 @@ private void defineDefaultFunctions() {
// Geo Functions
addToMap(def(StAswkt.class, StAswkt::new, "ST_ASWKT", "ST_ASTEXT"),
def(StWkttosql.class, StWkttosql::new, "ST_WKTTOSQL", "ST_GEOMFROMTEXT"),
def(StDistance.class, StDistance::new, "ST_DISTANCE")
def(StDistance.class, StDistance::new, "ST_DISTANCE"),
def(StX.class, StX::new, "ST_X"),
def(StY.class, StY::new, "ST_Y"),
def(StGeometryType.class, StGeometryType::new, "ST_GEOMETRYTYPE")
);

// Special
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ default R apply(Object o) {
}

public enum GeoOperation {
ASWKT(GeoShape::toString);
ASWKT(GeoShape::toString),
X(GeoShape::getX),
Y(GeoShape::getY),
GEOMETRY_TYPE(GeoShape::getGeometryType);

private final Function<Object, Object> apply;

Expand Down
Loading

0 comments on commit c1721d3

Please sign in to comment.