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

S2polygon.getCentroid seems wrong in some case? #10

Open
zhihuawensc opened this issue Nov 2, 2017 · 4 comments
Open

S2polygon.getCentroid seems wrong in some case? #10

zhihuawensc opened this issue Nov 2, 2017 · 4 comments

Comments

@zhihuawensc
Copy link

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:

layer <Style id="style-polygon-normal"> ff000000 1.2000000476837158 4d000000 1 1 <![CDATA[<h3>$[name]</h3>]]> </Style> <Style id="style-polygon-highlight"> ff000000 1.7999999523162842 4d000000 1 1 <![CDATA[<h3>$[name]</h3>]]> </Style> normal #style-polygon-normal highlight #style-polygon-highlight <Style id="style-s2cell-normal"> ff000000 1.2000000476837158 4d000000 1 1 <![CDATA[<h3>$[name]</h3>]]> </Style> <Style id="style-s2cell-highlight"> ff000000 1.7999999523162842 4d000000 1 1 <![CDATA[<h3>$[name]</h3>]]> </Style> 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
Copy link
Author

zhihuawensc commented Nov 2, 2017 via email

@zhihuawensc
Copy link
Author

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
Copy link

daudrain commented Jan 16, 2018

@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
Copy link
Author

@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)));
}

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