Java - 如何检查 lat/lng 坐标是否在多边形内
Java - How to check if a lat/lng coords are inside a polygon
我想检查地图上的给定点(及其纬度和经度)是否在某个多边形内。我有多边形的顶点坐标(在 lat/long 中)。
我想创建一个多边形并检查点是否在里面,但它告诉我点总是在外面...也许多边形不适用于地理参考坐标?
Double[] xCoords = {40.842226, 40.829498, 40.833394, 40.84768, 40.858716}
Double[] yCoords = {14.211753, 14.229262, 14.26617, 14.278701, 14.27715}
Double[] myPoint = {40.86141, 14.279932};
Path2D myPolygon = new Path2D.Double();
myPolygon.moveTo(xCoords[0], yCoords[0]); //first point
for(int i = 1; i < xCoords.length; i ++) {
myPolygon.lineTo(xCoords[i], yCoords[i]); //draw lines
}
myPolygon.closePath(); //draw last line
if(myPolygon.contains(myPoint{0}, myPoint{1})) {
//it's inside;
}
这是 google 地图中的样子
它总是 return 错误...但是它在多边形内部...
无论多边形是什么形状,该点都不可能包含在该多边形中。
您的 right-most 坐标位于 40.858716
而该点的 x 值为 40.86141
,这意味着该点位于多边形的右侧。 y 也一样,多边形的最大 y 坐标是 14.278701
,而点在 14.279932
。也就是说点在外面.
还有,你把坐标颠倒了,我们心爱的城市的坐标是40.8518° N, 14.2681° E
,这意味着40
是y
,14
是x
.
Path2D
就可以了。我的观察只是告诉您该点不在多边形中,但检查极值并不是验证点是否在多边形内的通用解决方案。
public class CoordinatesDTO {
private Long id;
private double latitude;
private double longnitude;
}
public static boolean isLocationInsideTheFencing(CoordinatesDTO location, List<CoordinatesDTO> fencingCoordinates) { //this is important method for Checking the point exist inside the fence or not.
boolean blnIsinside = false;
List<CoordinatesDTO> lstCoordinatesDTO = fencingCoordinates;
Path2D myPolygon = new Path2D.Double();
myPolygon.moveTo(lstCoordinatesDTO.get(0).getLatitude(), lstCoordinatesDTO.get(0).getLongnitude()); // first
// point
for (int i = 1; i < lstCoordinatesDTO.size(); i++) {
myPolygon.lineTo(lstCoordinatesDTO.get(i).getLatitude(), lstCoordinatesDTO.get(i).getLongnitude()); // draw
// lines
}
myPolygon.closePath(); // draw last line
// myPolygon.contains(p);
Point2D P2D2 = new Point2D.Double();
P2D2.setLocation(location.getLatitude(), location.getLongnitude());
if (myPolygon.contains(P2D2)) {
blnIsinside = true;
} else {
blnIsinside = false;
}
return blnIsinside;
}
我想检查地图上的给定点(及其纬度和经度)是否在某个多边形内。我有多边形的顶点坐标(在 lat/long 中)。
我想创建一个多边形并检查点是否在里面,但它告诉我点总是在外面...也许多边形不适用于地理参考坐标?
Double[] xCoords = {40.842226, 40.829498, 40.833394, 40.84768, 40.858716}
Double[] yCoords = {14.211753, 14.229262, 14.26617, 14.278701, 14.27715}
Double[] myPoint = {40.86141, 14.279932};
Path2D myPolygon = new Path2D.Double();
myPolygon.moveTo(xCoords[0], yCoords[0]); //first point
for(int i = 1; i < xCoords.length; i ++) {
myPolygon.lineTo(xCoords[i], yCoords[i]); //draw lines
}
myPolygon.closePath(); //draw last line
if(myPolygon.contains(myPoint{0}, myPoint{1})) {
//it's inside;
}
这是 google 地图中的样子
它总是 return 错误...但是它在多边形内部...
无论多边形是什么形状,该点都不可能包含在该多边形中。
您的 right-most 坐标位于 40.858716
而该点的 x 值为 40.86141
,这意味着该点位于多边形的右侧。 y 也一样,多边形的最大 y 坐标是 14.278701
,而点在 14.279932
。也就是说点在外面.
还有,你把坐标颠倒了,我们心爱的城市的坐标是40.8518° N, 14.2681° E
,这意味着40
是y
,14
是x
.
Path2D
就可以了。我的观察只是告诉您该点不在多边形中,但检查极值并不是验证点是否在多边形内的通用解决方案。
public class CoordinatesDTO {
private Long id;
private double latitude;
private double longnitude;
}
public static boolean isLocationInsideTheFencing(CoordinatesDTO location, List<CoordinatesDTO> fencingCoordinates) { //this is important method for Checking the point exist inside the fence or not.
boolean blnIsinside = false;
List<CoordinatesDTO> lstCoordinatesDTO = fencingCoordinates;
Path2D myPolygon = new Path2D.Double();
myPolygon.moveTo(lstCoordinatesDTO.get(0).getLatitude(), lstCoordinatesDTO.get(0).getLongnitude()); // first
// point
for (int i = 1; i < lstCoordinatesDTO.size(); i++) {
myPolygon.lineTo(lstCoordinatesDTO.get(i).getLatitude(), lstCoordinatesDTO.get(i).getLongnitude()); // draw
// lines
}
myPolygon.closePath(); // draw last line
// myPolygon.contains(p);
Point2D P2D2 = new Point2D.Double();
P2D2.setLocation(location.getLatitude(), location.getLongnitude());
if (myPolygon.contains(P2D2)) {
blnIsinside = true;
} else {
blnIsinside = false;
}
return blnIsinside;
}