Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog/136309.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 136309
summary: Adds ST_SIMPLIFY geo spatial function
area: ES|QL
type: enhancement
issues: []
1 change: 1 addition & 0 deletions x-pack/plugin/esql-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ base {
dependencies {
api "org.antlr:antlr4-runtime:${versions.antlr4}"
api project(path: xpackModule('mapper-version'))
api "org.locationtech.jts:jts-core:${versions.jts}"
compileOnly project(path: xpackModule('core'))
testApi(project(xpackModule('esql-core:test-fixtures'))) {
exclude group: 'org.elasticsearch.plugin', module: 'esql-core'
Expand Down
31 changes: 31 additions & 0 deletions x-pack/plugin/esql-core/licenses/jts-core-LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Eclipse Distribution License - v 1.0

Copyright (c) 2007, Eclipse Foundation, Inc. and its licensors.

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

Neither the name of the Eclipse Foundation, Inc. nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

1 change: 1 addition & 0 deletions x-pack/plugin/esql-core/licenses/jts-core-NOTICE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
import org.elasticsearch.geometry.utils.GeometryValidator;
import org.elasticsearch.geometry.utils.WellKnownBinary;
import org.elasticsearch.geometry.utils.WellKnownText;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;
import org.locationtech.jts.io.WKTWriter;

import java.nio.ByteOrder;

Expand Down Expand Up @@ -125,4 +128,17 @@ public String wkbToWkt(BytesRef wkb) {
public Geometry wkbToGeometry(BytesRef wkb) {
return WellKnownBinary.fromWKB(validator(), false, wkb.bytes, wkb.offset, wkb.length);
}

public org.locationtech.jts.geom.Geometry wkbToJtsGeometry(BytesRef wkb) throws ParseException {
String wkt = wkbToWkt(wkb);
WKTReader reader = new WKTReader();
return reader.read(wkt);
}

public BytesRef jtsGeometryToWkb(org.locationtech.jts.geom.Geometry jtsGeometry) {
WKTWriter writer = new WKTWriter();
String wkt = writer.write(jtsGeometry);
return wktToWkb(wkt);
}

}
120 changes: 120 additions & 0 deletions x-pack/plugin/esql/qa/testFixtures/src/main/resources/spatial.csv-spec
Original file line number Diff line number Diff line change
Expand Up @@ -3024,3 +3024,123 @@ wkt:keyword |pt:cartesian_point
"POINT(111)" |null
// end::to_cartesianpoint-str-parse-error-result[]
;

###############################################
# Tests for ST_SIMPLIFY
###############################################

stSimplifyMultiRow
required_capability: st_simplify

FROM airports
| SORT name
| LIMIT 5
| EVAL result = st_simplify(TO_GEOSHAPE("POLYGON((-10 -60, 120 -60, 120 60, -10 60, -10 -60))"), 1.0)
| KEEP name, result
;

name:text | result:geo_shape
Aba Tenna D. Yilma Int'l | POLYGON ((-10.0 -60.0, -10.0 60.0, 120.0 60.0, 120.0 -60.0, -10.0 -60.0))
Abdul Rachman Saleh | POLYGON ((-10.0 -60.0, -10.0 60.0, 120.0 60.0, 120.0 -60.0, -10.0 -60.0))
Abidjan Port Bouet | POLYGON ((-10.0 -60.0, -10.0 60.0, 120.0 60.0, 120.0 -60.0, -10.0 -60.0))
Abu Dhabi Int'l | POLYGON ((-10.0 -60.0, -10.0 60.0, 120.0 60.0, 120.0 -60.0, -10.0 -60.0))
Abuja Int'l | POLYGON ((-10.0 -60.0, -10.0 60.0, 120.0 60.0, 120.0 -60.0, -10.0 -60.0))
;

stSimplifyMultiRowWithPoints
required_capability: st_simplify

FROM airports
| SORT name
| LIMIT 5
| EVAL result = st_simplify(location, 0.0)
| KEEP location, result
;

location:geo_point | result:geo_shape
POINT (41.857756722253 9.61267784753569) | POINT (41.857756722253 9.61267784753569)
POINT (112.711418617258 -7.92998002840567) | POINT (112.711418617258 -7.92998002840567)
POINT (-3.93221929167636 5.2543984451492) | POINT (-3.93221929167636 5.2543984451492)
POINT (54.6463293225558 24.4272271529764) | POINT (54.6463293225558 24.4272271529764)
POINT (7.27025993974356 9.00437659781094) | POINT (7.27025993974356 9.00437659781094)
;

stSimplifyNoSimplification
required_capability: st_simplify

ROW geo_shape = TO_GEOSHAPE("POLYGON((0 0, 1 0.1, 2 0, 2 2, 1 1.9, 0 2, 0 0))")
| EVAL result = st_simplify(geo_shape, 0.01)
| KEEP result
;

result:geo_shape
POLYGON ((0.0 0.0, 0.0 2.0, 1.0 1.9, 2.0 2.0, 2.0 0.0, 1.0 0.1, 0.0 0.0))
;

stSimplifyWithSimplification
required_capability: st_simplify

ROW geo_shape = TO_GEOSHAPE("POLYGON((0 0, 1 0.1, 2 0, 2 2, 1 1.9, 0 2, 0 0))")
| EVAL result = st_simplify(geo_shape, 0.2)
| KEEP result
;

result:geo_shape
POLYGON ((0.0 0.0, 0.0 2.0, 2.0 2.0, 2.0 0.0, 0.0 0.0))
;

stSimplifyEmptySimplification
required_capability: st_simplify

ROW geo_shape = TO_GEOSHAPE("POLYGON((0 0, 1 0.1, 2 0, 2 2, 1 1.9, 0 2, 0 0))")
| EVAL result = st_simplify(geo_shape, 2.0)
| KEEP result
;

result:geo_shape
POLYGON EMPTY
;

stSimplifyNull
required_capability: st_simplify

ROW geo_shape = NULL
| EVAL result = st_simplify(geo_shape, 2.0)
| KEEP result
;

result:geo_shape
NULL
;

stSimplifyCartesianPoint
required_capability: st_simplify

# TODO Why cannot we use a latitud outside of -90, 90 when there are tests
# that use the TO_CARTESIANPOINT that are doing it already?
ROW wkt = ["POINT(97.11 75.53)", "POINT(80.93 72.77)"]
| MV_EXPAND wkt
| EVAL pt = TO_CARTESIANPOINT(wkt)
| EVAL result = st_simplify(pt, 2.0)
| KEEP result
;

result:geo_shape
POINT (97.11 75.53)
POINT (80.93 72.77)
;

stSimplifyCartesianShape
required_capability: st_simplify

ROW wkt = ["POINT(97.11 75.53)", "POLYGON((0 0, 1 0.1, 2 0, 2 2, 1 1.9, 0 2, 0 0))"]
| MV_EXPAND wkt
| EVAL geom = TO_CARTESIANSHAPE(wkt)
| EVAL result = st_simplify(geom, 0.2)
| KEEP result
;

result:geo_shape
POINT (97.11 75.53)
POLYGON ((0.0 0.0, 0.0 2.0, 2.0 2.0, 2.0 0.0, 0.0 0.0))
;

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading