Comments (4)
from s2-geometry-library-java.
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)));
}
from s2-geometry-library-java.
@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.
from s2-geometry-library-java.
@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)));
}
from s2-geometry-library-java.
Related Issues (20)
- S2CellCreation HOT 3
- Retrieve cell based on shortest distance
- Create cell by mentioning level HOT 1
- Support adding an offset to an S2LatLng, compute the heading/angle, etc.
- [BUG] OOM happened when S2RegionCoverer try to getCovering !!!
- [BUG] OOM happened when S2RegionCoverer try to getCovering !!! HOT 6
- bug in initToUnion HOT 3
- Query on polylines HOT 1
- CellID lost when S2RegionCoverer.getCovering() HOT 6
- S2RegionCoverer.getCoveringInternal Tries to cover whole globe! HOT 1
- Adding additional attribute to S2Polygon class for custom feature? HOT 4
- does S2 support maven version in repository? HOT 2
- Information: Library S2 Geometry in Kotlin exists
- Java port for S2RegionTermIndexer (c++) HOT 3
- S1Angle.fromEarthDistance HOT 1
- [FR] Are there J2CL annotations for this library? HOT 4
- Latest version in maven repo? HOT 8
- S2ContainsPointQuery - Different Result between Java and CPP HOT 1
- What's the unit of area being calculated in S2Cell class? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from s2-geometry-library-java.