Skip to content

Commit c1721d3

Browse files
committed
SQL: Add ST_X, ST_Y and ST_GEOMETRY_TYPE functions
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
1 parent c6d611b commit c1721d3

File tree

16 files changed

+544
-16
lines changed

16 files changed

+544
-16
lines changed

docs/reference/sql/functions/geo.asciidoc

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,83 @@ Returns the geometry from WKT representation. The return type is geometry.
5656
include-tagged::{sql-specs}/geo/docs.csv-spec[aswkt]
5757
--------------------------------------------------
5858

59+
==== Geometry Properties
60+
61+
[[sql-functions-geo-st-geometrytype]]
62+
===== `ST_GeometryType`
63+
64+
.Synopsis:
65+
[source, sql]
66+
--------------------------------------------------
67+
ST_GeometryType(geometry<1>)
68+
--------------------------------------------------
69+
70+
*Input*:
71+
72+
<1> geometry
73+
74+
*Output*: string
75+
76+
.Description:
77+
78+
Returns the type of the `geometry` such as "Point", "Polygon", "MultiPolygon", etc.
79+
The return type is string.
80+
81+
["source","sql",subs="attributes,macros"]
82+
--------------------------------------------------
83+
include-tagged::{sql-specs}/geo/docs.csv-spec[geometrytype]
84+
--------------------------------------------------
85+
86+
[[sql-functions-geo-x]]
87+
===== `ST_X`
88+
89+
.Synopsis:
90+
[source, sql]
91+
--------------------------------------------------
92+
ST_X(geometry<1>)
93+
--------------------------------------------------
94+
95+
*Input*:
96+
97+
<1> geometry
98+
99+
*Output*: double
100+
101+
.Description:
102+
103+
Returns the longitude of the first point in the geometry.
104+
The return type is double.
105+
106+
["source","sql",subs="attributes,macros"]
107+
--------------------------------------------------
108+
include-tagged::{sql-specs}/geo/docs.csv-spec[x]
109+
--------------------------------------------------
110+
111+
[[sql-functions-geo-y]]
112+
===== `ST_Y`
113+
114+
.Synopsis:
115+
[source, sql]
116+
--------------------------------------------------
117+
ST_Y(geometry<1>)
118+
--------------------------------------------------
119+
120+
*Input*:
121+
122+
<1> geometry
123+
124+
*Output*: double
125+
126+
.Description:
127+
128+
Returns the the latitude of the first point in the geometry.
129+
The return type is double.
130+
131+
["source","sql",subs="attributes,macros"]
132+
--------------------------------------------------
133+
include-tagged::{sql-specs}/geo/docs.csv-spec[y]
134+
--------------------------------------------------
135+
59136
[[sql-functions-geo-st-distance]]
60137
===== `ST_Distance`
61138

docs/reference/sql/functions/index.asciidoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@
137137
* <<sql-functions-geo>>
138138
** <<sql-functions-geo-st-as-wkt>>
139139
** <<sql-functions-geo-st-wkt-to-sql>>
140+
** <<sql-functions-geo-st-geometrytype>>
141+
** <<sql-functions-geo-st-x>>
142+
** <<sql-functions-geo-st-y>>
140143
** <<sql-functions-geo-st-distance>>
141144
* <<sql-functions-system>>
142145
** <<sql-functions-system-database>>

x-pack/plugin/sql/qa/src/main/resources/command.csv-spec

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,11 @@ USER |SCALAR
135135
ST_ASTEXT |SCALAR
136136
ST_ASWKT |SCALAR
137137
ST_DISTANCE |SCALAR
138+
ST_GEOMETRYTYPE |SCALAR
138139
ST_GEOMFROMTEXT |SCALAR
139140
ST_WKTTOSQL |SCALAR
141+
ST_X |SCALAR
142+
ST_Y |SCALAR
140143
SCORE |SCORE
141144
;
142145

x-pack/plugin/sql/qa/src/main/resources/docs/docs.csv-spec

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,11 @@ USER |SCALAR
312312
ST_ASTEXT |SCALAR
313313
ST_ASWKT |SCALAR
314314
ST_DISTANCE |SCALAR
315+
ST_GEOMETRYTYPE |SCALAR
315316
ST_GEOMFROMTEXT |SCALAR
316317
ST_WKTTOSQL |SCALAR
318+
ST_X |SCALAR
319+
ST_Y |SCALAR
317320
SCORE |SCORE
318321
// end::showFunctions
319322
;

x-pack/plugin/sql/qa/src/main/resources/geo/docs.csv-spec

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,36 @@ SELECT ST_Distance(ST_WKTToSQL('POINT (10 20)'), ST_WKTToSQL('POINT (20 30)')) d
3535
1499101.2889383635
3636
// end::distance
3737
;
38+
39+
///////////////////////////////
40+
//
41+
// Geometry Properties
42+
//
43+
///////////////////////////////
44+
45+
selectGeometryType
46+
// tag::geometrytype
47+
SELECT ST_GeometryType(ST_WKTToSQL('POINT (10 20)')) type;
48+
49+
type:s
50+
POINT
51+
// end::geometrytype
52+
;
53+
54+
selectX
55+
// tag::x
56+
SELECT ST_X(ST_WKTToSQL('POINT (10 20)')) x;
57+
58+
x:d
59+
10.0
60+
// end::x
61+
;
62+
63+
selectY
64+
// tag::y
65+
SELECT ST_Y(ST_WKTToSQL('POINT (10 20)')) y;
66+
67+
y:d
68+
20.0
69+
// end::y
70+
;

x-pack/plugin/sql/qa/src/main/resources/geo/geosql.csv-spec

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,55 @@ SELECT ST_Distance(ST_WktToSql(NULL), ST_WktToSQL('POINT (-71 42)')) shape;
214214
shape:d
215215
null
216216
;
217+
218+
groupByGeometryType
219+
SELECT COUNT(*) cnt, ST_GeometryType(location) gt FROM geo GROUP BY ST_GeometryType(location);
220+
221+
cnt:l | gt:s
222+
15 |POINT
223+
;
224+
225+
groupByEastWest
226+
SELECT COUNT(*) cnt, FLOOR(ST_X(location)/90) east FROM geo GROUP BY east ORDER BY east;
227+
228+
cnt:l | east:l
229+
3 |-2
230+
3 |-1
231+
4 |0
232+
5 |1
233+
;
234+
235+
groupByNorthSouth
236+
SELECT COUNT(*) cnt, FLOOR(ST_Y(location)/45) north FROM geo GROUP BY north ORDER BY north;
237+
238+
cnt:l | north:l
239+
1 |-1
240+
9 |0
241+
5 |1
242+
;
243+
244+
selectFilterByXOfLocation
245+
SELECT city, ST_X(shape) x, ST_Y(shape) y FROM geo WHERE ST_X(location) > 0 ORDER BY ST_Y(location);
246+
247+
city:s | x:d | y:d
248+
Sydney |151.208629 |-33.863385
249+
Singapore |103.855535 |1.295868
250+
Hong Kong |114.183925 |22.281397
251+
Tokyo |139.76402225 |35.669616
252+
Seoul |127.060851 |37.509132
253+
Munich |11.537505 |48.146321
254+
Paris |2.351773 |48.845538
255+
Amsterdam |4.850312 |52.347557
256+
Berlin |13.390889 |52.486701
257+
;
258+
259+
selectFilterByRegionPoint
260+
SELECT city, region FROM geo WHERE ST_X(ST_WKTTOSQL(region_point)) < 0 ORDER BY ST_X(location);
261+
262+
city:s | region:s
263+
San Francisco |Americas
264+
Mountain View |Americas
265+
Phoenix |Americas
266+
Chicago |Americas
267+
New York |Americas
268+
;

x-pack/plugin/sql/qa/src/main/resources/ogc/ogc.sql-spec

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,55 @@ SELECT fid, name, num_lanes, aliases, REPLACE(UCASE(ST_AsText(centerline)), '.0'
3434

3535
selectSinglePoint
3636
SELECT ST_GeomFromText('point (10.0 12.0)') point;
37+
38+
39+
//
40+
// Geometry Property Functions
41+
//
42+
// H2GIS doesn't follow the standard here that mandates ST_Dimension returns SMALLINT
43+
selectLakesProps
44+
SELECT fid, UCASE(ST_GeometryType(shore)) type FROM lakes ORDER BY fid;
45+
selectRoadSegmentsProps
46+
SELECT fid, UCASE(ST_GeometryType(centerline)) type FROM road_segments ORDER BY fid;
47+
selectDividedRoutesProps
48+
SELECT fid, UCASE(ST_GeometryType(centerlines)) type FROM divided_routes ORDER BY fid;
49+
selectForestsProps
50+
SELECT fid, UCASE(ST_GeometryType(boundary)) type FROM forests ORDER BY fid;
51+
selectBridgesProps
52+
SELECT fid, UCASE(ST_GeometryType(position)) type FROM bridges ORDER BY fid;
53+
selectStreamsProps
54+
SELECT fid, UCASE(ST_GeometryType(centerline)) type FROM streams ORDER BY fid;
55+
selectBuildingsProps
56+
SELECT fid, UCASE(ST_GeometryType(position)) type1, UCASE(ST_GeometryType(footprint)) type2 FROM buildings ORDER BY fid;
57+
selectPondsProps
58+
SELECT fid, UCASE(ST_GeometryType(shores)) type FROM ponds ORDER BY fid;
59+
selectNamedPlacesProps
60+
SELECT fid, UCASE(ST_GeometryType(boundary)) type FROM named_places ORDER BY fid;
61+
selectMapNeatLinesProps
62+
SELECT fid, UCASE(ST_GeometryType(neatline)) type FROM map_neatlines ORDER BY fid;
63+
64+
// AwaitsFix https://github.com/elastic/elasticsearch/issues/40908
65+
// selectLakesXY
66+
// SELECT fid, ST_X(shore) x, ST_Y(shore) y FROM lakes ORDER BY fid;
67+
selectRoadSegmentsXY
68+
SELECT fid, ST_X(centerline) x, ST_Y(centerline) y FROM road_segments ORDER BY fid;
69+
selectDividedRoutesXY
70+
SELECT fid, ST_X(centerlines) x, ST_Y(centerlines) y FROM divided_routes ORDER BY fid;
71+
// AwaitsFix https://github.com/elastic/elasticsearch/issues/40908
72+
// selectForestsXY
73+
// SELECT fid, ST_X(boundary) x, ST_Y(boundary) y FROM forests ORDER BY fid;
74+
selectBridgesPositionsXY
75+
SELECT fid, ST_X(position) x, ST_Y(position) y FROM bridges ORDER BY fid;
76+
selectStreamsXY
77+
SELECT fid, ST_X(centerline) x, ST_Y(centerline) y FROM streams ORDER BY fid;
78+
selectBuildingsXY
79+
SELECT fid, ST_X(position) x, ST_Y(position) y FROM buildings ORDER BY fid;
80+
// AwaitsFix https://github.com/elastic/elasticsearch/issues/40908
81+
// selectBuildingsFootprintsXY
82+
// SELECT fid, ST_X(footprint) x, ST_Y(footprint) y FROM buildings ORDER BY fid;
83+
// selectPondsXY
84+
// SELECT fid, ST_X(shores) x, ST_Y(shores) y FROM ponds ORDER BY fid;
85+
// selectNamedPlacesXY
86+
// SELECT fid, ST_X(boundary) x, ST_Y(boundary) y FROM named_places ORDER BY fid;
87+
// selectMapNeatLinesXY
88+
// SELECT fid, ST_X(neatline) x, ST_Y(neatline) y FROM map_neatlines ORDER BY fid;

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/FunctionRegistry.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@
4848
import org.elasticsearch.xpack.sql.expression.function.scalar.datetime.Year;
4949
import org.elasticsearch.xpack.sql.expression.function.scalar.geo.StAswkt;
5050
import org.elasticsearch.xpack.sql.expression.function.scalar.geo.StDistance;
51+
import org.elasticsearch.xpack.sql.expression.function.scalar.geo.StGeometryType;
5152
import org.elasticsearch.xpack.sql.expression.function.scalar.geo.StWkttosql;
53+
import org.elasticsearch.xpack.sql.expression.function.scalar.geo.StX;
54+
import org.elasticsearch.xpack.sql.expression.function.scalar.geo.StY;
5255
import org.elasticsearch.xpack.sql.expression.function.scalar.math.ACos;
5356
import org.elasticsearch.xpack.sql.expression.function.scalar.math.ASin;
5457
import org.elasticsearch.xpack.sql.expression.function.scalar.math.ATan;
@@ -258,7 +261,10 @@ private void defineDefaultFunctions() {
258261
// Geo Functions
259262
addToMap(def(StAswkt.class, StAswkt::new, "ST_ASWKT", "ST_ASTEXT"),
260263
def(StWkttosql.class, StWkttosql::new, "ST_WKTTOSQL", "ST_GEOMFROMTEXT"),
261-
def(StDistance.class, StDistance::new, "ST_DISTANCE")
264+
def(StDistance.class, StDistance::new, "ST_DISTANCE"),
265+
def(StX.class, StX::new, "ST_X"),
266+
def(StY.class, StY::new, "ST_Y"),
267+
def(StGeometryType.class, StGeometryType::new, "ST_GEOMETRYTYPE")
262268
);
263269

264270
// Special

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/GeoProcessor.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ default R apply(Object o) {
2828
}
2929

3030
public enum GeoOperation {
31-
ASWKT(GeoShape::toString);
31+
ASWKT(GeoShape::toString),
32+
X(GeoShape::getX),
33+
Y(GeoShape::getY),
34+
GEOMETRY_TYPE(GeoShape::getGeometryType);
3235

3336
private final Function<Object, Object> apply;
3437

0 commit comments

Comments
 (0)