Open zhihuawensc opened 7 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
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 .
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)));
}
@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. 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)));
}
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: <?xml version="1.0" encoding="UTF-8" standalone="yes"?>