7
7
package org .elasticsearch .xpack .spatial ;
8
8
9
9
import org .apache .lucene .util .SloppyMath ;
10
+ import org .elasticsearch .geo .GeometryTestUtils ;
10
11
import org .elasticsearch .geometry .Circle ;
11
12
import org .elasticsearch .geometry .LinearRing ;
12
13
import org .elasticsearch .geometry .Polygon ;
13
14
import org .elasticsearch .test .ESTestCase ;
14
15
15
16
import static org .hamcrest .Matchers .closeTo ;
17
+ import static org .hamcrest .Matchers .containsString ;
16
18
import static org .hamcrest .Matchers .equalTo ;
17
19
18
20
public class SpatialUtilsTests extends ESTestCase {
19
21
20
22
public void testCreateRegularGeoShapePolygon () {
21
- double lon = randomDoubleBetween (-20 , 20 , true );
22
- double lat = randomDoubleBetween (-20 , 20 , true );
23
- double radiusMeters = randomDoubleBetween (10 , 10000 , true );
24
- Circle circle = new Circle (lon , lat , radiusMeters );
23
+ final Circle circle = randomValueOtherThanMany (
24
+ c -> SloppyMath .haversinMeters (c .getLat (), c .getLon (), 90 , 0 ) < c .getRadiusMeters ()
25
+ || SloppyMath .haversinMeters (c .getLat (), c .getLon (), -90 , 0 ) < c .getRadiusMeters (),
26
+ () -> GeometryTestUtils .randomCircle (true ));
27
+ doRegularGeoShapePolygon (circle );
28
+ }
29
+
30
+ public void testCircleContainsNorthPole () {
31
+ final Circle circle = randomValueOtherThanMany (
32
+ c -> SloppyMath .haversinMeters (c .getLat (), c .getLon (), 90 , 0 ) >= c .getRadiusMeters (),
33
+ () -> GeometryTestUtils .randomCircle (true ));
34
+ IllegalArgumentException ex = expectThrows (IllegalArgumentException .class , () -> doRegularGeoShapePolygon (circle ));
35
+ assertThat (ex .getMessage (), containsString ("contains the north pole" ));
36
+ }
37
+
38
+ public void testCircleContainsSouthPole () {
39
+ final Circle circle = randomValueOtherThanMany (
40
+ c -> SloppyMath .haversinMeters (c .getLat (), c .getLon (), -90 , 0 ) >= c .getRadiusMeters (),
41
+ () -> GeometryTestUtils .randomCircle (true ));
42
+ IllegalArgumentException ex = expectThrows (IllegalArgumentException .class , () -> doRegularGeoShapePolygon (circle ));
43
+ assertThat (ex .getMessage (), containsString ("contains the south pole" ));
44
+ }
45
+
46
+ private void doRegularGeoShapePolygon (Circle circle ) {
25
47
int numSides = randomIntBetween (4 , 1000 );
26
48
Polygon polygon = SpatialUtils .createRegularGeoShapePolygon (circle , numSides );
27
49
LinearRing outerShell = polygon .getPolygon ();
@@ -35,7 +57,7 @@ public void testCreateRegularGeoShapePolygon() {
35
57
for (int i = 0 ; i < numPoints ; i ++) {
36
58
double actualDistance = SloppyMath
37
59
.haversinMeters (circle .getY (), circle .getX (), outerShell .getY (i ), outerShell .getX (i ));
38
- assertThat (actualDistance , closeTo (radiusMeters , 0.1 ));
60
+ assertThat (actualDistance , closeTo (circle . getRadiusMeters () , 0.1 ));
39
61
}
40
62
}
41
63
0 commit comments