这是多边形中的一个点。纬度和经度坐标。地理工具
This is a point in a polygon. Latitude and Longitude Coordinates. GeoTools
给定一个我用纬度和经度坐标追踪的多边形,给定一个我也在(纬度、经度)中接收到的线。
我需要知道这个点是否在我的多边形内
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>
下面的代码是正确的,即没有报错
我怀疑它的结果是否正确,或者我是否必须进行某种转换才能使结果正确。如果我非要压平它或者是代码,它已经知道怎么治愈了。
只要您的多边形相当小就可以了。
如果您的多边形:
- 接近半个地球那么答案可能是错误的。
- 越过逆子午线(180W/E)则答案可能是错误的
- 如果多边形的边很长但上面的点很少,那么答案很可能是错误的。
给定一个我用纬度和经度坐标追踪的多边形,给定一个我也在(纬度、经度)中接收到的线。
我需要知道这个点是否在我的多边形内
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>
下面的代码是正确的,即没有报错
我怀疑它的结果是否正确,或者我是否必须进行某种转换才能使结果正确。如果我非要压平它或者是代码,它已经知道怎么治愈了。
只要您的多边形相当小就可以了。
如果您的多边形:
- 接近半个地球那么答案可能是错误的。
- 越过逆子午线(180W/E)则答案可能是错误的
- 如果多边形的边很长但上面的点很少,那么答案很可能是错误的。