Skip to content

Commit

Permalink
SQL: Add ST_X, ST_Y and ST_GEOMETRY_TYPE functions (#41104)
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 #40908. This support will be added as soon as the
ShapeBuilder issues are resolved.

Relates to #29872
  • Loading branch information
imotov committed Apr 22, 2019
1 parent 07ad376 commit dd07273
Show file tree
Hide file tree
Showing 18 changed files with 598 additions and 23 deletions.
84 changes: 81 additions & 3 deletions docs/reference/sql/functions/geo.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Returns the WKT representation of the `geometry`. The return type is string.

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


Expand All @@ -53,7 +53,85 @@ Returns the geometry from WKT representation. The return type is geometry.

["source","sql",subs="attributes,macros"]
--------------------------------------------------
include-tagged::{sql-specs}/geo/docs.csv-spec[aswkt]
include-tagged::{sql-specs}/docs/geo.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, MULTIPOINT, LINESTRING, MULTILINESTRING, POLYGON, MULTIPOLYGON, GEOMETRYCOLLECTION, ENVELOPE or CIRCLE.

The return type is string.

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

[[sql-functions-geo-st-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}/docs/geo.csv-spec[x]
--------------------------------------------------

[[sql-functions-geo-st-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}/docs/geo.csv-spec[y]
--------------------------------------------------

[[sql-functions-geo-st-distance]]
Expand All @@ -78,5 +156,5 @@ Returns the distance between geometries in meters. Both geometries have to be po

["source","sql",subs="attributes,macros"]
--------------------------------------------------
include-tagged::{sql-specs}/geo/docs.csv-spec[distance]
include-tagged::{sql-specs}/docs/geo.csv-spec[distance]
--------------------------------------------------
5 changes: 4 additions & 1 deletion docs/reference/sql/functions/index.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,11 @@
** <<sql-functions-conditional-nvl>>
* <<sql-functions-geo>>
** <<sql-functions-geo-st-as-wkt>>
** <<sql-functions-geo-st-wkt-to-sql>>
** <<sql-functions-geo-st-distance>>
** <<sql-functions-geo-st-geometrytype>>
** <<sql-functions-geo-st-wkt-to-sql>>
** <<sql-functions-geo-st-x>>
** <<sql-functions-geo-st-y>>
* <<sql-functions-system>>
** <<sql-functions-system-database>>
** <<sql-functions-system-user>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static List<Object[]> readScriptSpec() throws Exception {
List<Object[]> tests = new ArrayList<>();
tests.addAll(readScriptSpec("/ogc/ogc.csv-spec", parser));
tests.addAll(readScriptSpec("/geo/geosql.csv-spec", parser));
tests.addAll(readScriptSpec("/geo/docs.csv-spec", parser));
tests.addAll(readScriptSpec("/docs/geo.csv-spec", parser));
return tests;
}

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
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
;
72 changes: 72 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,75 @@ 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
;


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

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
;

groupByNorthEastSortByEastNorth
SELECT COUNT(*) cnt, FLOOR(ST_Y(location)/45) north, FLOOR(ST_X(location)/90) east FROM geo GROUP BY north, east ORDER BY east, north;

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

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

city:s | x:d | y:d | lx:d | ly:d
Sydney |151.208629 |-33.863385 |151.20862897485495|-33.863385021686554
Singapore |103.855535 |1.295868 |103.8555349688977 |1.2958679627627134
Hong Kong |114.183925 |22.281397 |114.18392493389547|22.28139698971063
Tokyo |139.76402225 |35.669616 |139.76402222178876|35.66961596254259
Seoul |127.060851 |37.509132 |127.06085099838674|37.50913198571652
Munich |11.537505 |48.146321 |11.537504978477955|48.14632098656148
Paris |2.351773 |48.845538 |2.3517729341983795|48.84553796611726
Amsterdam |4.850312 |52.347557 |4.850311987102032 |52.347556999884546
Berlin |13.390889 |52.486701 |13.390888944268227|52.48670099303126
;

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

city:s | region:s | x:d
San Francisco |Americas |-122.39422800019383
Mountain View |Americas |-122.08384302444756
Phoenix |Americas |-111.97350500151515
Chicago |Americas |-87.63787407428026
New York |Americas |-73.9900270756334
;
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),
GEOMETRY_TYPE(GeoShape::getGeometryType),
X(GeoShape::getX),
Y(GeoShape::getY);

private final Function<Object, Object> apply;

Expand Down
Loading

0 comments on commit dd07273

Please sign in to comment.