40
40
import org .elasticsearch .common .xcontent .XContentBuilder ;
41
41
import org .elasticsearch .common .xcontent .XContentFactory ;
42
42
import org .elasticsearch .common .xcontent .XContentParser ;
43
+ import org .elasticsearch .geo .geometry .Geometry ;
43
44
import org .elasticsearch .geo .geometry .Line ;
44
45
import org .elasticsearch .geo .geometry .MultiLine ;
45
46
import org .elasticsearch .geo .geometry .MultiPoint ;
@@ -112,27 +113,39 @@ public void testParsePoint() throws IOException {
112
113
113
114
@ Override
114
115
public void testParseMultiPoint () throws IOException {
115
- int numPoints = randomIntBetween (2 , 100 );
116
+ int numPoints = randomIntBetween (0 , 100 );
116
117
List <Coordinate > coordinates = new ArrayList <>(numPoints );
117
118
for (int i = 0 ; i < numPoints ; ++i ) {
118
119
coordinates .add (new Coordinate (GeoTestUtil .nextLongitude (), GeoTestUtil .nextLatitude ()));
119
120
}
120
121
121
- Shape [] shapes = new Shape [ numPoints ] ;
122
+ List < org . elasticsearch . geo . geometry . Point > points = new ArrayList <>( numPoints ) ;
122
123
for (int i = 0 ; i < numPoints ; ++i ) {
123
124
Coordinate c = coordinates .get (i );
124
- shapes [ i ] = SPATIAL_CONTEXT . makePoint (c .x , c .y );
125
+ points . add ( new org . elasticsearch . geo . geometry . Point (c .y , c .x ) );
125
126
}
126
- ShapeCollection <?> expected = shapeCollection (shapes );
127
- assertExpected (expected , new MultiPointBuilder (coordinates ), true );
128
127
129
- List <org .elasticsearch .geo .geometry .Point > points = new ArrayList <>(numPoints );
128
+ Geometry expectedGeom ;
129
+ MultiPointBuilder actual ;
130
+ if (numPoints == 0 ) {
131
+ expectedGeom = MultiPoint .EMPTY ;
132
+ actual = new MultiPointBuilder ();
133
+ } else {
134
+ expectedGeom = new MultiPoint (points );
135
+ actual = new MultiPointBuilder (coordinates );
136
+ }
137
+
138
+ assertExpected (expectedGeom , actual , false );
139
+ assertMalformed (actual );
140
+
141
+ assumeTrue ("JTS test path cannot handle empty multipoints" , numPoints > 1 );
142
+ Shape [] shapes = new Shape [numPoints ];
130
143
for (int i = 0 ; i < numPoints ; ++i ) {
131
144
Coordinate c = coordinates .get (i );
132
- points . add ( new org . elasticsearch . geo . geometry . Point (c .y , c .x ) );
145
+ shapes [ i ] = SPATIAL_CONTEXT . makePoint (c .x , c .y );
133
146
}
134
- assertExpected ( new MultiPoint ( points ), new MultiPointBuilder ( coordinates ), false );
135
- assertMalformed ( new MultiPointBuilder (coordinates ));
147
+ ShapeCollection <?> expected = shapeCollection ( shapes );
148
+ assertExpected ( expected , new MultiPointBuilder (coordinates ), true );
136
149
}
137
150
138
151
private List <Coordinate > randomLineStringCoords () {
@@ -163,7 +176,7 @@ public void testParseLineString() throws IOException {
163
176
164
177
@ Override
165
178
public void testParseMultiLineString () throws IOException {
166
- int numLineStrings = randomIntBetween (2 , 8 );
179
+ int numLineStrings = randomIntBetween (0 , 8 );
167
180
List <LineString > lineStrings = new ArrayList <>(numLineStrings );
168
181
MultiLineStringBuilder builder = new MultiLineStringBuilder ();
169
182
for (int j = 0 ; j < numLineStrings ; ++j ) {
@@ -173,18 +186,27 @@ public void testParseMultiLineString() throws IOException {
173
186
builder .linestring (new LineStringBuilder (lsc ));
174
187
}
175
188
176
- MultiLineString expected = GEOMETRY_FACTORY .createMultiLineString (
177
- lineStrings .toArray (new LineString [lineStrings .size ()]));
178
- assertExpected (jtsGeom (expected ), builder , true );
179
-
180
189
List <Line > lines = new ArrayList <>(lineStrings .size ());
181
190
for (int j = 0 ; j < lineStrings .size (); ++j ) {
182
191
Coordinate [] c = lineStrings .get (j ).getCoordinates ();
183
192
lines .add (new Line (Arrays .stream (c ).mapToDouble (i ->i .y ).toArray (),
184
193
Arrays .stream (c ).mapToDouble (i ->i .x ).toArray ()));
185
194
}
186
- assertExpected (new MultiLine (lines ), builder , false );
195
+ Geometry expectedGeom ;
196
+ if (lines .isEmpty ()) {
197
+ expectedGeom = MultiLine .EMPTY ;
198
+ } else if (lines .size () == 1 ) {
199
+ expectedGeom = new Line (lines .get (0 ).getLats (), lines .get (0 ).getLons ());
200
+ } else {
201
+ expectedGeom = new MultiLine (lines );
202
+ }
203
+ assertExpected (expectedGeom , builder , false );
187
204
assertMalformed (builder );
205
+
206
+ MultiLineString expected = GEOMETRY_FACTORY .createMultiLineString (
207
+ lineStrings .toArray (new LineString [lineStrings .size ()]));
208
+ assumeTrue ("JTS test path cannot handle empty multilinestrings" , numLineStrings > 1 );
209
+ assertExpected (jtsGeom (expected ), builder , true );
188
210
}
189
211
190
212
@ Override
@@ -201,7 +223,7 @@ public void testParsePolygon() throws IOException {
201
223
202
224
@ Override
203
225
public void testParseMultiPolygon () throws IOException {
204
- int numPolys = randomIntBetween (2 , 8 );
226
+ int numPolys = randomIntBetween (0 , 8 );
205
227
MultiPolygonBuilder builder = new MultiPolygonBuilder ();
206
228
PolygonBuilder pb ;
207
229
Coordinate [] coordinates ;
@@ -214,7 +236,7 @@ public void testParseMultiPolygon() throws IOException {
214
236
shell = GEOMETRY_FACTORY .createLinearRing (coordinates );
215
237
shapes [i ] = GEOMETRY_FACTORY .createPolygon (shell , null );
216
238
}
217
-
239
+ assumeTrue ( "JTS test path cannot handle empty multipolygon" , numPolys > 1 );
218
240
Shape expected = shapeCollection (shapes );
219
241
assertExpected (expected , builder , true );
220
242
assertMalformed (builder );
@@ -429,7 +451,6 @@ public void testInvalidGeometryType() throws IOException {
429
451
assertValidException (builder , IllegalArgumentException .class );
430
452
}
431
453
432
- @ AwaitsFix (bugUrl = "https://github.com/elastic/elasticsearch/issues/37894" )
433
454
@ Override
434
455
public void testParseGeometryCollection () throws IOException {
435
456
if (rarely ()) {
0 commit comments