google / s2-geometry-library-java

Automatically exported from code.google.com/p/s2-geometry-library-java
Apache License 2.0
533 stars 230 forks source link

S2polygon.getCentroid seems wrong in some case? #10

Open zhihuawensc opened 6 years ago

zhihuawensc commented 6 years ago

I encountered a problem where the centroid seems wrong, i.e., the centroid is outside the polygon even when the polygon itself is a convex.

The code below plots a hexagon but the centroid seems to be way off, did I do anything wrong, thanks a lot for help?

  double[] coordinates = new double[]{
            47.167511,-122.1521,
            47.167568,-122.151952,
            47.167684,-122.151952,
            47.167746,-122.1521,
            47.167691,-122.152248,
            47.16757,-122.152248,
            47.167511,-122.1521,
    };
    List<S2Point> s2Points = new ArrayList<>();
    for (int i = 0; i < coordinates.length /2; i++) {
        double lat = coordinates[2 * i];
        double lng = coordinates[2 * i + 1];
        System.out.println(lat + "," + lng + ",");
        S2LatLng s2LatLng = S2LatLng.fromDegrees(lat, lng);
        s2Points.add(s2LatLng.toPoint());
    }
    S2Loop s2Loop  = new S2Loop(s2Points);
    s2Loop.normalize();
    S2Polygon s2Polygon = new S2Polygon(s2Loop);
    S2Point s2PolygonCentroid = s2Polygon.getCentroid();
    System.out.println("Center: " + s2PolygonCentroid.toDegreesString() + " in_polygon " + s2Polygon
            .contains(s2PolygonCentroid));

Output: Center: (47.167557150782145, -122.15163675765044) in_polygon false

Kml file: <?xml version="1.0" encoding="UTF-8" standalone="yes"?>

layer normal #style-polygon-normal highlight #style-polygon-highlight normal #style-s2cell-normal highlight #style-s2cell-highlight test test #style-polygon -122.15210000000002,47.167511 -122.151952,47.16756800000001 -122.15195200000002,47.16768400000001 -122.15210000000002,47.167746 -122.15224799999999,47.167691000000005 -122.15224799999999,47.16757 -122.15210000000002,47.167511 -122.15163675765044,47.167557150782145 screen shot 2017-11-01 at 11 19 06 pm
zhihuawensc commented 6 years ago

I tried it, and it didn't make any difference. And this thing seems only happen to very small polygons, i.e., are size < 1000 square meters.

double[] coordinates = new double[]{ 47.167511,-122.1521, 47.167568,-122.151952, 47.167684,-122.151952, 47.167746,-122.1521, 47.167691,-122.152248, 47.16757,-122.152248, 47.167511,-122.1521, };

List s2Points = new ArrayList<>(); //for (int i = 0; i < coordinates.length /2; i++) { for (int i = coordinates.length /2 - 1 ; i >= 0; i--) { double lat = coordinates[2 i]; double lng = coordinates[2 i + 1]; S2LatLng s2LatLng = S2LatLng.fromDegrees(lat, lng); s2Points.add(s2LatLng.toPoint()); } S2Loop s2Loop = new S2Loop(s2Points); s2Loop.normalize(); S2Polygon s2Polygon = new S2Polygon(s2Loop); S2Point s2PolygonCentroid = s2Polygon.getCentroid();

System.out.println("Area: " + s2Polygon .getArea() S2LatLng.EARTH_RADIUS_METERS S2LatLng.EARTH_RADIUS_METERS + " Center: " + s2PolygonCentroid .toDegreesString() + " in_polygon " + s2Polygon.contains(s2PolygonCentroid));

On Wed, Nov 1, 2017 at 11:53 PM Ryan Leach notifications@github.com wrote:

I've got a sneaking suspicion that your points may be in a different rotation? Try reversing that list?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/google/s2-geometry-library-java/issues/10#issuecomment-341333085, or mute the thread https://github.com/notifications/unsubscribe-auth/AIihJxI7nxaZHGpKv7t6lP820YhjjIDmks5syWbmgaJpZM4QPNx4 .

zhihuawensc commented 6 years ago

I found the s2polygon.getCentroid() is really far away from the real centroid when the polygon is small, but it becomes more accurate when the polygon is large. In the code snippet below, I generated a square with a edge length below, I found that if edge length is only 10 meters, the generated centroid is 120 meters away from the real center. But if the edge length is 100 meters, then it's only 1 meter from the real center. Please take a look to see if this is a real bug.

Edge length 10 centroid in polygon false distance to real center 119.20291197164347 Edge length 100 centroid in polygon true distance to real center 1.0399613891675528

private static void getSquare(int edgeLengthInMeters) {
    final double DEG_OF_ONE_METER = 0.0000089944;
    final S2LatLng realCenter = S2LatLng.fromDegrees(40, 120);
    S2LatLngRect s2LatLngRect = S2LatLngRect.fromPoint(realCenter)
            .convolveWithCap(S1Angle.degrees(DEG_OF_ONE_METER * edgeLengthInMeters / 2));

    final List<S2Point> points = new ArrayList<>();
    for (int i = 0; i < 4; i++) {
        points.add(s2LatLngRect.getVertex(i).toPoint());
    }
    final S2Polygon s2Polygon = new S2Polygon(new S2Loop(points));
    final S2Point centroid = s2Polygon.getCentroid();
    System.out.println("Centroid in polygon " + s2Polygon
            .contains(centroid) + " distance to real center " + realCenter
            .getEarthDistance(new S2LatLng(centroid)));
}
daudrain commented 6 years ago

@zhihuawensc In your original example, the first and last point are duplicates, did you try omitting the last one?

As mentioned in the S2Loop documentation, The last vertex is implicitly connected to the first.

zhihuawensc commented 6 years ago

@daudrain Yes, I tried to remove the last point and the results were same. You can try my code snippet with really small radius as well.

private static void getSquare(int edgeLengthInMeters) {
    final double DEG_OF_ONE_METER = 0.0000089944;
    final S2LatLng realCenter = S2LatLng.fromDegrees(40, 120);
    S2LatLngRect s2LatLngRect = S2LatLngRect.fromPoint(realCenter)
            .convolveWithCap(S1Angle.degrees(DEG_OF_ONE_METER * edgeLengthInMeters / 2));

    final List<S2Point> points = new ArrayList<>();
    for (int i = 0; i < 4; i++) {
        points.add(s2LatLngRect.getVertex(i).toPoint());
    }
    final S2Polygon s2Polygon = new S2Polygon(new S2Loop(points));
    final S2Point centroid = s2Polygon.getCentroid();
    System.out.println("Centroid in polygon " + s2Polygon
            .contains(centroid) + " distance to real center " + realCenter
            .getEarthDistance(new S2LatLng(centroid)));
}