Git Product home page Git Product logo

Comments (4)

zhihuawensc avatar zhihuawensc commented on May 3, 2024

from s2-geometry-library-java.

zhihuawensc avatar zhihuawensc commented on May 3, 2024

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.

daudrain avatar daudrain commented on May 3, 2024

@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.

zhihuawensc avatar zhihuawensc commented on May 3, 2024

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.