5
5
*/
6
6
package org .elasticsearch .xpack .sql .expression .function .scalar .geo ;
7
7
8
+ import org .elasticsearch .ElasticsearchParseException ;
9
+ import org .elasticsearch .common .bytes .BytesReference ;
8
10
import org .elasticsearch .common .geo .GeoUtils ;
9
- import org .elasticsearch .common .geo .builders .PointBuilder ;
10
- import org .elasticsearch .common .geo .builders .ShapeBuilder ;
11
- import org .elasticsearch .common .geo .parsers .ShapeParser ;
11
+ import org .elasticsearch .common .geo .GeometryParser ;
12
12
import org .elasticsearch .common .io .stream .NamedWriteable ;
13
13
import org .elasticsearch .common .io .stream .StreamInput ;
14
14
import org .elasticsearch .common .io .stream .StreamOutput ;
15
+ import org .elasticsearch .common .xcontent .LoggingDeprecationHandler ;
16
+ import org .elasticsearch .common .xcontent .NamedXContentRegistry ;
15
17
import org .elasticsearch .common .xcontent .ToXContentFragment ;
16
18
import org .elasticsearch .common .xcontent .XContentBuilder ;
19
+ import org .elasticsearch .common .xcontent .XContentParser ;
20
+ import org .elasticsearch .common .xcontent .json .JsonXContent ;
17
21
import org .elasticsearch .geo .geometry .Circle ;
18
22
import org .elasticsearch .geo .geometry .Geometry ;
19
23
import org .elasticsearch .geo .geometry .GeometryCollection ;
26
30
import org .elasticsearch .geo .geometry .Point ;
27
31
import org .elasticsearch .geo .geometry .Polygon ;
28
32
import org .elasticsearch .geo .geometry .Rectangle ;
33
+ import org .elasticsearch .geo .utils .WellKnownText ;
29
34
import org .elasticsearch .xpack .sql .SqlIllegalArgumentException ;
30
35
31
36
import java .io .IOException ;
37
+ import java .io .InputStream ;
38
+ import java .text .ParseException ;
32
39
import java .util .Objects ;
33
40
34
41
/**
@@ -41,41 +48,49 @@ public class GeoShape implements ToXContentFragment, NamedWriteable {
41
48
42
49
public static final String NAME = "geo" ;
43
50
44
- private final ShapeBuilder <?, ?, ?> shapeBuilder ;
51
+ private final Geometry shape ;
45
52
46
53
public GeoShape (double lon , double lat ) {
47
- shapeBuilder = new PointBuilder ( lon , lat );
54
+ shape = new Point ( lat , lon );
48
55
}
49
56
50
57
public GeoShape (Object value ) throws IOException {
51
- shapeBuilder = ShapeParser .parse (value );
58
+ try {
59
+ shape = parse (value );
60
+ } catch (ParseException ex ) {
61
+ throw new ElasticsearchParseException ("cannot load shape" , ex );
62
+ }
52
63
}
53
64
54
65
public GeoShape (StreamInput in ) throws IOException {
55
- shapeBuilder = ShapeParser .parse (in .readString ());
66
+ try {
67
+ shape = parse (in .readString ());
68
+ } catch (ParseException ex ) {
69
+ throw new ElasticsearchParseException ("cannot load shape" , ex );
70
+ }
56
71
}
57
72
58
73
@ Override
59
74
public void writeTo (StreamOutput out ) throws IOException {
60
- out .writeString (shapeBuilder .toWKT ());
75
+ out .writeString (WellKnownText .toWKT (shape ));
61
76
}
62
77
63
78
@ Override
64
79
public String toString () {
65
- return shapeBuilder .toWKT ();
80
+ return WellKnownText .toWKT (shape );
66
81
}
67
82
68
83
@ Override
69
84
public XContentBuilder toXContent (XContentBuilder builder , Params params ) throws IOException {
70
- return builder .value (shapeBuilder .toWKT ());
85
+ return builder .value (WellKnownText .toWKT (shape ));
71
86
}
72
87
73
88
public Geometry toGeometry () {
74
- return shapeBuilder . buildGeometry () ;
89
+ return shape ;
75
90
}
76
91
77
92
public Point firstPoint () {
78
- return shapeBuilder . buildGeometry () .visit (new GeometryVisitor <Point , RuntimeException >() {
93
+ return shape .visit (new GeometryVisitor <Point , RuntimeException >() {
79
94
@ Override
80
95
public Point visit (Circle circle ) {
81
96
return new Point (circle .getLat (), circle .getLon (), circle .hasAlt () ? circle .getAlt () : Double .NaN );
@@ -149,16 +164,16 @@ public String getGeometryType() {
149
164
}
150
165
151
166
public static double distance (GeoShape shape1 , GeoShape shape2 ) {
152
- if (shape1 .shapeBuilder instanceof PointBuilder == false ) {
167
+ if (shape1 .shape instanceof Point == false ) {
153
168
throw new SqlIllegalArgumentException ("distance calculation is only supported for points; received [{}]" , shape1 );
154
169
}
155
- if (shape2 .shapeBuilder instanceof PointBuilder == false ) {
170
+ if (shape2 .shape instanceof Point == false ) {
156
171
throw new SqlIllegalArgumentException ("distance calculation is only supported for points; received [{}]" , shape2 );
157
172
}
158
- double srcLat = ((PointBuilder ) shape1 .shapeBuilder ). latitude ();
159
- double srcLon = ((PointBuilder ) shape1 .shapeBuilder ). longitude ();
160
- double dstLat = ((PointBuilder ) shape2 .shapeBuilder ). latitude ();
161
- double dstLon = ((PointBuilder ) shape2 .shapeBuilder ). longitude ();
173
+ double srcLat = ((Point ) shape1 .shape ). getLat ();
174
+ double srcLon = ((Point ) shape1 .shape ). getLon ();
175
+ double dstLat = ((Point ) shape2 .shape ). getLat ();
176
+ double dstLon = ((Point ) shape2 .shape ). getLon ();
162
177
return GeoUtils .arcDistance (srcLat , srcLon , dstLat , dstLon );
163
178
}
164
179
@@ -171,17 +186,32 @@ public boolean equals(Object o) {
171
186
return false ;
172
187
}
173
188
GeoShape geoShape = (GeoShape ) o ;
174
- return shapeBuilder .equals (geoShape .shapeBuilder );
189
+ return shape .equals (geoShape .shape );
175
190
}
176
191
177
192
@ Override
178
193
public int hashCode () {
179
- return Objects .hash (shapeBuilder );
194
+ return Objects .hash (shape );
180
195
}
181
196
182
197
@ Override
183
198
public String getWriteableName () {
184
199
return NAME ;
185
200
}
186
201
202
+ private static Geometry parse (Object value ) throws IOException , ParseException {
203
+ XContentBuilder content = JsonXContent .contentBuilder ();
204
+ content .startObject ();
205
+ content .field ("value" , value );
206
+ content .endObject ();
207
+
208
+ try (InputStream stream = BytesReference .bytes (content ).streamInput ();
209
+ XContentParser parser = JsonXContent .jsonXContent .createParser (
210
+ NamedXContentRegistry .EMPTY , LoggingDeprecationHandler .INSTANCE , stream )) {
211
+ parser .nextToken (); // start object
212
+ parser .nextToken (); // field name
213
+ parser .nextToken (); // field value
214
+ return GeometryParser .parse (parser , true , true , true );
215
+ }
216
+ }
187
217
}
0 commit comments