Skip to content

Commit faf7b7b

Browse files
committed
Reshapes the code a bit
1 parent dd2933e commit faf7b7b

File tree

6 files changed

+26
-19
lines changed

6 files changed

+26
-19
lines changed

x-pack/plugin/esql-core/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ base {
1616
dependencies {
1717
api "org.antlr:antlr4-runtime:${versions.antlr4}"
1818
api project(path: xpackModule('mapper-version'))
19+
api "org.locationtech.jts:jts-core:${versions.jts}"
1920
compileOnly project(path: xpackModule('core'))
2021
testApi(project(xpackModule('esql-core:test-fixtures'))) {
2122
exclude group: 'org.elasticsearch.plugin', module: 'esql-core'
File renamed without changes.
File renamed without changes.

x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/util/SpatialCoordinateTypes.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
import org.elasticsearch.geometry.utils.GeometryValidator;
1717
import org.elasticsearch.geometry.utils.WellKnownBinary;
1818
import org.elasticsearch.geometry.utils.WellKnownText;
19+
import org.locationtech.jts.io.ParseException;
20+
import org.locationtech.jts.io.WKTReader;
21+
import org.locationtech.jts.io.WKTWriter;
1922

2023
import java.nio.ByteOrder;
2124

@@ -125,4 +128,17 @@ public String wkbToWkt(BytesRef wkb) {
125128
public Geometry wkbToGeometry(BytesRef wkb) {
126129
return WellKnownBinary.fromWKB(validator(), false, wkb.bytes, wkb.offset, wkb.length);
127130
}
131+
132+
public org.locationtech.jts.geom.Geometry wkbToJtsGeometry(BytesRef wkb) throws ParseException {
133+
String wkt = wkbToWkt(wkb);
134+
WKTReader reader = new WKTReader();
135+
return reader.read(wkt);
136+
}
137+
138+
public BytesRef jtsGeometryToWkb(org.locationtech.jts.geom.Geometry jtsGeometry) {
139+
WKTWriter writer = new WKTWriter();
140+
String wkt = writer.write(jtsGeometry);
141+
return wktToWkb(wkt);
142+
}
143+
128144
}

x-pack/plugin/esql/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ dependencies {
3737
compileOnly project(xpackModule('ml'))
3838
compileOnly project(path: xpackModule('mapper-aggregate-metric'))
3939
compileOnly project(path: xpackModule('downsample'))
40-
implementation "org.locationtech.jts:jts-core:${versions.jts}"
4140
implementation project(xpackModule('kql'))
4241
implementation project('compute')
4342
implementation project('compute:ann')

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/spatial/StSimplify.java

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
import org.elasticsearch.xpack.esql.expression.function.Param;
2424
import org.elasticsearch.xpack.esql.expression.function.scalar.EsqlScalarFunction;
2525
import org.elasticsearch.xpack.esql.io.stream.PlanStreamInput;
26-
import org.locationtech.jts.io.WKTReader;
27-
import org.locationtech.jts.io.WKTWriter;
2826
import org.locationtech.jts.simplify.DouglasPeuckerSimplifier;
2927

3028
import java.io.IOException;
@@ -93,20 +91,13 @@ public void writeTo(StreamOutput out) throws IOException {
9391
out.writeNamedWriteable(tolerance);
9492
}
9593

96-
private static class GeoSimplifier {
97-
static WKTReader reader = new WKTReader();
98-
static WKTWriter writer = new WKTWriter();
99-
100-
public static BytesRef geoSourceAndConstantTolerance(BytesRef inputGeometry, @Fixed double inputTolerance) {
101-
String wkt = UNSPECIFIED.wkbToWkt(inputGeometry);
102-
try {
103-
org.locationtech.jts.geom.Geometry jtsGeometry = reader.read(wkt);
104-
org.locationtech.jts.geom.Geometry simplifiedGeometry = DouglasPeuckerSimplifier.simplify(jtsGeometry, inputTolerance);
105-
String simplifiedWkt = writer.write(simplifiedGeometry);
106-
return UNSPECIFIED.wktToWkb(simplifiedWkt);
107-
} catch (Exception e) {
108-
throw new RuntimeException(e);
109-
}
94+
private static BytesRef geoSourceAndConstantTolerance(BytesRef inputGeometry, double inputTolerance) {
95+
try {
96+
org.locationtech.jts.geom.Geometry jtsGeometry = UNSPECIFIED.wkbToJtsGeometry(inputGeometry);
97+
org.locationtech.jts.geom.Geometry simplifiedGeometry = DouglasPeuckerSimplifier.simplify(jtsGeometry, inputTolerance);
98+
return UNSPECIFIED.jtsGeometryToWkb(simplifiedGeometry);
99+
} catch (Exception e) {
100+
throw new RuntimeException(e);
110101
}
111102
}
112103

@@ -128,11 +119,11 @@ public EvalOperator.ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvalua
128119

129120
@Evaluator(extraName = "NonFoldableGeoAndConstantTolerance", warnExceptions = { IllegalArgumentException.class })
130121
static BytesRef processNonFoldableGeoAndConstantTolerance(BytesRef inputGeometry, @Fixed double inputTolerance) {
131-
return GeoSimplifier.geoSourceAndConstantTolerance(inputGeometry, inputTolerance);
122+
return geoSourceAndConstantTolerance(inputGeometry, inputTolerance);
132123
}
133124

134125
@Evaluator(extraName = "FoldableGeoAndConstantTolerance", warnExceptions = { IllegalArgumentException.class })
135126
static BytesRef processFoldableGeoAndConstantTolerance(@Fixed BytesRef inputGeometry, @Fixed double inputTolerance) {
136-
return GeoSimplifier.geoSourceAndConstantTolerance(inputGeometry, inputTolerance);
127+
return geoSourceAndConstantTolerance(inputGeometry, inputTolerance);
137128
}
138129
}

0 commit comments

Comments
 (0)