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
Comments
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<S2Point> 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 ***@***.***> 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
<#10 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AIihJxI7nxaZHGpKv7t6lP820YhjjIDmks5syWbmgaJpZM4QPNx4>
.
|
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. Edge length 10 centroid in polygon false distance to real center 119.20291197164347
|
@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. |
@daudrain Yes, I tried to remove the last point and the results were same.
|
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?
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.167557150782145The text was updated successfully, but these errors were encountered: