给定一个我用纬度和经度坐标跟踪的多边形,给定一个我也收到的(在纬度,经度中)。
我需要知道这个点是否在我的多边形内
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));
}
马文
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geojson</artifactId>
<version>24-SNAPSHOT</version>
</dependency>
以下代码是正确的,即它没有给出任何错误。
我怀疑它的结果是否正确,或者我必须做某种转换才能使结果正确。如果我必须扁平化它,或者如果它是代码,它已经知道如何很好地治愈。
如果你的多边形相当小,那就没问题了。
如果您的多边形: