Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Circle to Geometry conversion is highly inaccurate for GeoCircle #177

Closed
hsbakshi opened this issue Mar 7, 2020 · 1 comment
Closed

Comments

@hsbakshi
Copy link
Contributor

hsbakshi commented Mar 7, 2020

Problem: JtsShapeFactory has a 'getGeometryFrom' method that allows converting a Shape object to a Geometry object. This method can be used to transform a Circle to a Geometry (Polygon). This conversion currently returns a highly inaccurate result when

Test case:

@Test
public void testCircleGeometryConversions() {
  // Nunavat (Far North)
  circleGeometryConversionTest(-83.10, 70.30, 100);
}
                                                                                                             
private void circleGeometryConversionTest(double x, double y, double radiusKm) {
  Point circleCenter = new PointImpl(x, y, SpatialContext.GEO);
  double radiusDeg = DistanceUtils.dist2Degrees(radiusKm, DistanceUtils.EARTH_EQUATORIAL_RADIUS_KM);
  GeoCircle geoCircle = new GeoCircle(circleCenter, radiusDeg, SpatialContext.GEO);
                                                                                                             
  JtsShapeFactory shapeFactory = JtsSpatialContext.GEO.getShapeFactory();
  // Let's ensure the circle-to-polygon conversion is accurate accounting for geodesics.
  Geometry geometry = shapeFactory.getGeometryFrom(geoCircle);
  Assert.assertTrue(geometry instanceof Polygon);
  Polygon polygon = (Polygon) geometry;
  Coordinate[] coordinates = polygon.getExteriorRing().getCoordinates();
  int size = coordinates.length;
  Assert.assertTrue(size >= 100);
  GeodesicSphereDistCalc distCalc = new GeodesicSphereDistCalc.Haversine();
  for (Coordinate coordinate : coordinates) {
    // Check distance from center of each point
    Point point = new PointImpl(coordinate.x, coordinate.y, SpatialContext.GEO);
    double distance = distCalc.distance(point, circleCenter);
    double distanceKm = DistanceUtils.degrees2Dist(distance, DistanceUtils.EARTH_MEAN_RADIUS_KM);
    double expectedPrecision = radiusKm / 100;
    //Assert.assertEquals("Distance mismatch:  Point:" + point, radiusKm, distanceKm, expectedPrecision);
    System.out.println(String.format("Distance from point to center: %.2f km. Expected: %.2f km", distanceKm,
            radiusKm));
  }
}

Console output:
Distance from point to center: 99.91 km. Expected: 100.00 km
Distance from point to center: 101.04 km. Expected: 100.00 km
Distance from point to center: 105.10 km. Expected: 100.00 km
Distance from point to center: 111.72 km. Expected: 100.00 km
Distance from point to center: 120.40 km. Expected: 100.00 km
Distance from point to center: 130.60 km. Expected: 100.00 km
Distance from point to center: 141.86 km. Expected: 100.00 km
Distance from point to center: 153.78 km. Expected: 100.00 km
Distance from point to center: 166.06 km. Expected: 100.00 km
Distance from point to center: 178.45 km. Expected: 100.00 km
Distance from point to center: 190.76 km. Expected: 100.00 km
Distance from point to center: 202.81 km. Expected: 100.00 km
Distance from point to center: 214.49 km. Expected: 100.00 km
Distance from point to center: 225.66 km. Expected: 100.00 km
Distance from point to center: 236.24 km. Expected: 100.00 km
Distance from point to center: 246.15 km. Expected: 100.00 km
Distance from point to center: 255.32 km. Expected: 100.00 km
Distance from point to center: 263.67 km. Expected: 100.00 km
Distance from point to center: 271.16 km. Expected: 100.00 km
Distance from point to center: 277.75 km. Expected: 100.00 km
Distance from point to center: 283.38 km. Expected: 100.00 km
Distance from point to center: 288.04 km. Expected: 100.00 km
Distance from point to center: 291.69 km. Expected: 100.00 km
Distance from point to center: 294.31 km. Expected: 100.00 km
Distance from point to center: 295.89 km. Expected: 100.00 km
Distance from point to center: 296.42 km. Expected: 100.00 km
Distance from point to center: 295.89 km. Expected: 100.00 km
Distance from point to center: 294.31 km. Expected: 100.00 km
Distance from point to center: 291.69 km. Expected: 100.00 km
Distance from point to center: 288.04 km. Expected: 100.00 km
Distance from point to center: 283.38 km. Expected: 100.00 km
Distance from point to center: 277.75 km. Expected: 100.00 km
Distance from point to center: 271.16 km. Expected: 100.00 km
Distance from point to center: 263.67 km. Expected: 100.00 km
Distance from point to center: 255.32 km. Expected: 100.00 km
Distance from point to center: 246.15 km. Expected: 100.00 km
Distance from point to center: 236.24 km. Expected: 100.00 km
Distance from point to center: 225.66 km. Expected: 100.00 km
Distance from point to center: 214.49 km. Expected: 100.00 km
Distance from point to center: 202.81 km. Expected: 100.00 km
Distance from point to center: 190.76 km. Expected: 100.00 km
Distance from point to center: 178.45 km. Expected: 100.00 km
Distance from point to center: 166.06 km. Expected: 100.00 km
Distance from point to center: 153.78 km. Expected: 100.00 km
Distance from point to center: 141.86 km. Expected: 100.00 km
Distance from point to center: 130.60 km. Expected: 100.00 km
Distance from point to center: 120.40 km. Expected: 100.00 km
Distance from point to center: 111.72 km. Expected: 100.00 km
Distance from point to center: 105.10 km. Expected: 100.00 km
Distance from point to center: 101.04 km. Expected: 100.00 km
Distance from point to center: 99.91 km. Expected: 100.00 km
Distance from point to center: 101.84 km. Expected: 100.00 km
Distance from point to center: 106.61 km. Expected: 100.00 km
Distance from point to center: 113.80 km. Expected: 100.00 km
Distance from point to center: 122.89 km. Expected: 100.00 km
Distance from point to center: 133.35 km. Expected: 100.00 km
Distance from point to center: 144.74 km. Expected: 100.00 km
Distance from point to center: 156.69 km. Expected: 100.00 km
Distance from point to center: 168.93 km. Expected: 100.00 km
Distance from point to center: 181.21 km. Expected: 100.00 km
Distance from point to center: 193.36 km. Expected: 100.00 km
Distance from point to center: 205.22 km. Expected: 100.00 km
Distance from point to center: 216.67 km. Expected: 100.00 km
Distance from point to center: 227.62 km. Expected: 100.00 km
Distance from point to center: 237.96 km. Expected: 100.00 km
Distance from point to center: 247.62 km. Expected: 100.00 km
Distance from point to center: 256.54 km. Expected: 100.00 km
Distance from point to center: 264.67 km. Expected: 100.00 km
Distance from point to center: 271.95 km. Expected: 100.00 km
Distance from point to center: 278.34 km. Expected: 100.00 km
Distance from point to center: 283.80 km. Expected: 100.00 km
Distance from point to center: 288.31 km. Expected: 100.00 km
Distance from point to center: 291.84 km. Expected: 100.00 km
Distance from point to center: 294.38 km. Expected: 100.00 km
Distance from point to center: 295.91 km. Expected: 100.00 km
Distance from point to center: 296.42 km. Expected: 100.00 km
Distance from point to center: 295.91 km. Expected: 100.00 km
Distance from point to center: 294.38 km. Expected: 100.00 km
Distance from point to center: 291.84 km. Expected: 100.00 km
Distance from point to center: 288.31 km. Expected: 100.00 km
Distance from point to center: 283.80 km. Expected: 100.00 km
Distance from point to center: 278.34 km. Expected: 100.00 km
Distance from point to center: 271.95 km. Expected: 100.00 km
Distance from point to center: 264.67 km. Expected: 100.00 km
Distance from point to center: 256.54 km. Expected: 100.00 km
Distance from point to center: 247.62 km. Expected: 100.00 km
Distance from point to center: 237.96 km. Expected: 100.00 km
Distance from point to center: 227.62 km. Expected: 100.00 km
Distance from point to center: 216.67 km. Expected: 100.00 km
Distance from point to center: 205.22 km. Expected: 100.00 km
Distance from point to center: 193.36 km. Expected: 100.00 km
Distance from point to center: 181.21 km. Expected: 100.00 km
Distance from point to center: 168.93 km. Expected: 100.00 km
Distance from point to center: 156.69 km. Expected: 100.00 km
Distance from point to center: 144.74 km. Expected: 100.00 km
Distance from point to center: 133.35 km. Expected: 100.00 km
Distance from point to center: 122.89 km. Expected: 100.00 km
Distance from point to center: 113.80 km. Expected: 100.00 km
Distance from point to center: 106.61 km. Expected: 100.00 km
Distance from point to center: 101.84 km. Expected: 100.00 km
Distance from point to center: 99.91 km. Expected: 100.00 km

hsbakshi added a commit to hsbakshi/spatial4j that referenced this issue Mar 7, 2020
Details of the problem are explained here: locationtech#177

The new test-case fails without the code change and passes after the code change.
hsbakshi added a commit to hsbakshi/spatial4j that referenced this issue Mar 7, 2020
Details of the problem are explained here: locationtech#177

The new test-case fails without the code change and passes after the code change.
hsbakshi added a commit to hsbakshi/spatial4j that referenced this issue Mar 8, 2020
Details of the problem are explained here: locationtech#177

The new test-case fails without the code change and passes after the code change.
hsbakshi added a commit to hsbakshi/spatial4j that referenced this issue Mar 8, 2020
Details of the problem are explained here: locationtech#177

The new test-case fails without the code change and passes after the code change.

Signed-off-by: Hrishi Bakshi <hrishi@amazon.com>
dsmiley pushed a commit that referenced this issue Mar 9, 2020
Details of the problem are explained here: #177

The new test-case fails without the code change and passes after the code change.

Signed-off-by: Hrishi Bakshi <hrishi@amazon.com>
@dsmiley
Copy link
Contributor

dsmiley commented Mar 9, 2020

Thanks for contributing!

@dsmiley dsmiley closed this as completed Mar 9, 2020
abrokenjester pushed a commit to abrokenjester/spatial4j that referenced this issue Mar 12, 2020
Details of the problem are explained here: locationtech#177

The new test-case fails without the code change and passes after the code change.

Signed-off-by: Hrishi Bakshi <hrishi@amazon.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants