Skip to content

Commit d2228b7

Browse files
author
Julien Delbourgo
committed
Refactor and rename GeoScript to GeoSimplifyScript
1 parent b856e0c commit d2228b7

File tree

4 files changed

+82
-91
lines changed

4 files changed

+82
-91
lines changed

README.md

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -292,17 +292,15 @@ Example :
292292
}
293293
```
294294

295-
## Geo preview with shape simplification and conversion
295+
## Geo Simplify (geo shape simplification script)
296296

297-
This script adds a generated field containing the simplified or converted geoshape for the requested field.
297+
This script adds a generated field containing the simplified geoshape for the requested field.
298298

299299
It takes parameters that can passed in a `POST` request body:
300-
- `geoshapestringfield` (mandatory): `geo` field name. Must be of type `geo`
301-
- `simplify` : boolean for whether or not the shape should be simplified according to zoom level
302-
- `zoom` : the zoom level for simplification (1 giving the most simplified result, mandatory if simplify=true)
303-
- `output_format` : format of the output (can be `geojson`, `wkt` or `wkb`, defaults to `geojson`)
300+
- `field` (mandatory): `geo` field name. Must be of type `geo`
301+
- `zoom` (mandatory) : the zoom level for simplification, 1 giving the most simplified result
302+
- `output_format` : format of the output, can be `geojson` (default), `wkt` or `wkb`
304303
- `algorithm` : algorithm used for simplification, can be `douglas_peucker` (default) or `topology_preserving`
305-
- `is_point` : boolean (defaults to false) used to indicate the shape is a point
306304

307305
Example:
308306

@@ -312,12 +310,11 @@ Example:
312310
{
313311
"script_fields": {
314312
"simplified": {
315-
"script": "geopreview",
313+
"script": "geo_simplify",
316314
"lang":"native",
317315
"params": {
318-
"geoshapestringfield": "geo_shape",
319-
"zoom": 3,
320-
"simplify": true
316+
"field": "geo_shape",
317+
"zoom": 3
321318
}
322319
}
323320
}
@@ -334,9 +331,35 @@ Example:
334331
"failed": 0
335332
},
336333
"hits": {
337-
"total": 1,
334+
"total": 3,
338335
"max_score": 1,
339336
"hits": [
337+
{
338+
"_index": "test_index_geo",
339+
"_type": "records",
340+
"_id": "AVrxb8fMCjSOdtKpLj4q",
341+
"_score": 1,
342+
"fields": {
343+
"simplified": [
344+
{
345+
"real_type": "Point",
346+
"geom": "{\"type\":\"Point\",\"coordinates\":[-14,20]}",
347+
"type": "Point"
348+
}
349+
]
350+
}
351+
},
352+
{
353+
"_index": "test_index_geo",
354+
"_type": "records",
355+
"_id": "AVrxcBv4CjSOdtKpLj4r",
356+
"_score": 1,
357+
"fields": {
358+
"simplified": [
359+
{}
360+
]
361+
}
362+
},
340363
{
341364
"_index": "test_index_geo",
342365
"_type": "records",
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.opendatasoft.elasticsearch.odsscript;
22

3-
import com.vividsolutions.jts.geom.Coordinate;
43
import com.vividsolutions.jts.geom.Geometry;
54
import com.vividsolutions.jts.geom.GeometryFactory;
65
import com.vividsolutions.jts.io.ParseException;
@@ -12,7 +11,6 @@
1211
import org.apache.lucene.index.AtomicReaderContext;
1312
import org.apache.lucene.util.BytesRef;
1413
import org.elasticsearch.common.Base64;
15-
import org.elasticsearch.index.fielddata.ScriptDocValues;
1614
import org.elasticsearch.index.fielddata.SortedBinaryDocValues;
1715
import org.elasticsearch.index.mapper.FieldMapper;
1816
import org.elasticsearch.script.AbstractSearchScript;
@@ -22,21 +20,15 @@
2220
import java.util.HashMap;
2321
import java.util.Map;
2422

25-
/*
26-
* Copied from elasticsearch-ext-opendatasoft/GeoPreviewPlugin
27-
*/
28-
class GeoScript extends AbstractSearchScript {
29-
30-
private final String geoShapeStringFieldName;
23+
class GeoSimplifyScript extends AbstractSearchScript {
3124

25+
private final String fieldName;
3226
private final double epsilon;
3327
private final WKTWriter wktWriter;
3428
private final WKBWriter wkbWriter;
3529
private final GeometryJSON geometryJSON;
3630
private final OutputFormat outputFormat;
3731
private final Algorithm algorithm;
38-
private final boolean isPoint;
39-
private final boolean simplify;
4032
private AtomicReaderContext currentContext;
4133
private int docId;
4234

@@ -51,16 +43,14 @@ enum Algorithm {
5143
TOPOLOGY_PRESERVING
5244
}
5345

54-
public GeoScript(String geoShapeStringField, double epsilon, int coordDecimalsTrunc, OutputFormat outputFormat, Algorithm algorithm, boolean isPoint, boolean simplify) {
55-
this.geoShapeStringFieldName = geoShapeStringField;
46+
public GeoSimplifyScript(String field, double epsilon, int coordDecimalsTrunc, OutputFormat outputFormat, Algorithm algorithm) {
47+
this.fieldName = field;
5648
this.epsilon = epsilon;
5749
this.wktWriter = new WKTWriter();
5850
this.wkbWriter = new WKBWriter();
5951
this.geometryJSON = new GeometryJSON(coordDecimalsTrunc);
6052
this.outputFormat = outputFormat;
6153
this.algorithm = algorithm;
62-
this.isPoint = isPoint;
63-
this.simplify = simplify;
6454
}
6555

6656
private String outputGeometry(Geometry geo) {
@@ -102,44 +92,39 @@ public Object run() {
10292
GeometryFactory geometryFactory = new GeometryFactory();
10393
Map<String, String> resMap = new HashMap<>();
10494

105-
if (isPoint) {
106-
ScriptDocValues.GeoPoints geoPoints = (ScriptDocValues.GeoPoints) doc().get(this.geoShapeStringFieldName);
107-
Geometry geom = geometryFactory.createPoint(new Coordinate(geoPoints.getLon(), geoPoints.getLat()));
108-
109-
resMap.put("geom", outputGeometry(geom));
110-
resMap.put("type", geom.getGeometryType());
111-
resMap.put("real_type", geom.getGeometryType());
112-
113-
114-
} else {
115-
FieldMapper mapper = doc().mapperService().smartNameFieldMapper(this.geoShapeStringFieldName + ".wkb");
116-
SortedBinaryDocValues bytes = doc().fieldDataService().getForField(mapper).load(currentContext).getBytesValues();
117-
bytes.setDocument(docId);
118-
BytesRef wkb = bytes.valueAt(0);
119-
WKBReader reader = new WKBReader();
120-
121-
try {
122-
Geometry geometry = reader.read(wkb.bytes);
123-
String realType = geometry.getGeometryType();
124-
if (simplify) {
125-
geometry = getSimplifiedShape(geometry);
126-
}
127-
if (!geometry.isEmpty()) {
128-
resMap.put("geom", outputGeometry(geometry));
129-
resMap.put("type", geometry.getGeometryType());
130-
resMap.put("real_type", realType);
131-
} else {
132-
// If the simplified polygon is empty because it was too small, return a point
133-
134-
Geometry point = geometryFactory.createPoint(geometry.getCoordinate());
135-
136-
resMap.put("geom", outputGeometry(point));
137-
resMap.put("type", "SimplificationPoint");
138-
}
139-
} catch (ParseException e) {
140-
throw new ScriptException("Can't parse WKB", e);
95+
FieldMapper mapper = doc().mapperService().smartNameFieldMapper(this.fieldName + ".wkb");
96+
if (mapper == null) {
97+
return resMap;
98+
}
99+
SortedBinaryDocValues bytes = doc().fieldDataService().getForField(mapper).load(currentContext).getBytesValues();
100+
bytes.setDocument(docId);
101+
102+
BytesRef wkb;
103+
try {
104+
wkb = bytes.valueAt(0);
105+
} catch (ArrayIndexOutOfBoundsException e) {
106+
return resMap;
107+
}
108+
WKBReader reader = new WKBReader();
109+
110+
try {
111+
Geometry geometry = reader.read(wkb.bytes);
112+
String realType = geometry.getGeometryType();
113+
geometry = getSimplifiedShape(geometry);
114+
if (!geometry.isEmpty()) {
115+
resMap.put("geom", outputGeometry(geometry));
116+
resMap.put("type", geometry.getGeometryType());
117+
resMap.put("real_type", realType);
118+
} else {
119+
// If the simplified polygon is empty because it was too small, return a point
120+
Geometry point = geometryFactory.createPoint(geometry.getCoordinate());
121+
resMap.put("geom", outputGeometry(point));
122+
resMap.put("type", "SimplificationPoint");
141123
}
124+
} catch (ParseException e) {
125+
throw new ScriptException("Can't parse WKB", e);
142126
}
127+
143128
return resMap;
144129
}
145130
}

src/main/java/com/opendatasoft/elasticsearch/odsscript/OdsScriptFactory.java

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,25 @@
77

88
import java.util.Map;
99

10-
/*
11-
* Copied from elasticsearch-ext-opendatasoft/GeoPreviewPlugin
12-
*/
1310
public class OdsScriptFactory implements NativeScriptFactory {
1411

1512
@Override
1613
public ExecutableScript newScript(@Nullable Map<String, Object> params) {
17-
// geoshapestringfield and zoom are mandatory
1814

1915
if (params == null) {
20-
// throw npe error
16+
throw new ElasticsearchIllegalArgumentException("'params' field is mandatory");
2117
}
2218

23-
String geoShapeStringField = (String) params.get("geoshapestringfield");
19+
String field = (String) params.get("field");
2420

25-
26-
if (geoShapeStringField == null) {
27-
throw new ElasticsearchIllegalArgumentException("Missing the geoshapestringfield parameter");
28-
}
29-
30-
Boolean simplify = (Boolean) params.get("simplify");
31-
32-
if (simplify == null) {
33-
simplify = false;
21+
if (field == null) {
22+
throw new ElasticsearchIllegalArgumentException("'field' parameter is mandatory");
3423
}
3524

3625
Integer zoom = (Integer) params.get("zoom");
3726

38-
if (simplify && zoom == null) {
39-
throw new ElasticsearchIllegalArgumentException("Missing the zoom parameter");
27+
if (zoom == null) {
28+
throw new ElasticsearchIllegalArgumentException("'zoom' parameter is mandatory");
4029
}
4130

4231
double tolerance = 360 / (256 * Math.pow(zoom, 3));
@@ -45,26 +34,20 @@ public ExecutableScript newScript(@Nullable Map<String, Object> params) {
4534

4635
String stringOutputFormat = (String) params.get("output_format");
4736

48-
GeoScript.OutputFormat outputFormat = GeoScript.OutputFormat.GEOJSON;
37+
GeoSimplifyScript.OutputFormat outputFormat = GeoSimplifyScript.OutputFormat.GEOJSON;
4938

5039
if (stringOutputFormat != null) {
51-
outputFormat = GeoScript.OutputFormat.valueOf(stringOutputFormat.toUpperCase());
40+
outputFormat = GeoSimplifyScript.OutputFormat.valueOf(stringOutputFormat.toUpperCase());
5241
}
5342

5443
String algorithmString = (String) params.get("algorithm");
5544

56-
GeoScript.Algorithm algorithm = GeoScript.Algorithm.DOUGLAS_PEUCKER;
45+
GeoSimplifyScript.Algorithm algorithm = GeoSimplifyScript.Algorithm.DOUGLAS_PEUCKER;
5746

5847
if (algorithmString != null) {
59-
algorithm = GeoScript.Algorithm.valueOf(algorithmString.toUpperCase());
60-
}
61-
62-
Boolean isPoint = (Boolean) params.get("is_point");
63-
64-
if (isPoint == null) {
65-
isPoint = false;
48+
algorithm = GeoSimplifyScript.Algorithm.valueOf(algorithmString.toUpperCase());
6649
}
6750

68-
return new GeoScript(geoShapeStringField, tolerance, nbDecimals, outputFormat, algorithm, isPoint, simplify);
51+
return new GeoSimplifyScript(field, tolerance, nbDecimals, outputFormat, algorithm);
6952
}
7053
}

src/main/java/com/opendatasoft/elasticsearch/plugin/geo/GeoPlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,6 @@ public void onModule(AggregationModule aggModule) {
6565
}
6666

6767
public void onModule(ScriptModule module) {
68-
module.registerScript("geopreview", OdsScriptFactory.class);
68+
module.registerScript("geo_simplify", OdsScriptFactory.class);
6969
}
7070
}

0 commit comments

Comments
 (0)