11package com .opendatasoft .elasticsearch .odsscript ;
22
3- import com .vividsolutions .jts .geom .Coordinate ;
43import com .vividsolutions .jts .geom .Geometry ;
54import com .vividsolutions .jts .geom .GeometryFactory ;
65import com .vividsolutions .jts .io .ParseException ;
1211import org .apache .lucene .index .AtomicReaderContext ;
1312import org .apache .lucene .util .BytesRef ;
1413import org .elasticsearch .common .Base64 ;
15- import org .elasticsearch .index .fielddata .ScriptDocValues ;
1614import org .elasticsearch .index .fielddata .SortedBinaryDocValues ;
1715import org .elasticsearch .index .mapper .FieldMapper ;
1816import org .elasticsearch .script .AbstractSearchScript ;
2220import java .util .HashMap ;
2321import 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}
0 commit comments