Skip to content

S2polygon.getCentroid seems wrong in some case? #10

Description

@zhihuawensc

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions