这是多边形中的一个点。纬度和经度坐标。 GeoTools

问题描述 投票:0回答:1

[鉴于我也收到了该死的信息(以纬度,对数表示),给出了一个用纬度和经度坐标绘制的多边形。

我需要知道此点是否在我的多边形内

import org.geotools.geometry.jts.JTSFactoryFinder;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Polygon;
import org.locationtech.jts.geom.PrecisionModel;
import org.opengis.geometry.MismatchedDimensionException;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.NoSuchAuthorityCodeException;
import org.opengis.referencing.operation.TransformException;

    private void isPointInPolygon() {
        GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();

        Coordinate[] coords = new Coordinate[] { new Coordinate(40.174256, -3.483185),
                new Coordinate(40.173279, -3.481339), new Coordinate(40.172597, -3.482541),
                new Coordinate(40.173403, -3.485401), new Coordinate(40.174256, -3.483185) };

        Polygon polygon = geometryFactory.createPolygon(coords);

        System.out.println("polygon : " + polygon.isValid());

        double scale = Math.pow(10, 2);
        PrecisionModel pm = new PrecisionModel(scale);
        GeometryFactory gf = new GeometryFactory(pm);

        Geometry testPoint = gf.createPoint(new Coordinate(40.173597, -3.483798));
        System.out.println("polygon contains point : " + polygon.contains(testPoint));
    }

// MAVEN

    <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-geojson</artifactId>
            <version>24-SNAPSHOT</version>
        </dependency>

下面的代码是正确的,也就是说,它没有给出任何错误。

我怀疑其结果是否正确,或者是否必须进行某种转换才能使结果正确。如果我必须弄平它或它是代码,它已经知道如何很好地治愈。

geotools
1个回答
1
投票

[假设您的多边形相当小就可以了。

如果使用多边形:

  • 正在接近世界的一半,那么答案可能是错误的。
  • 越过子午线(180W / E),答案可能是错误的。
  • 如果多边形的边很长但上面没有点,那么答案很可能是错误的。
© www.soinside.com 2019 - 2024. All rights reserved.