gpt4 book ai didi

java - 如何定义确定点是否在经纬度区域内?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:07:30 25 4
gpt4 key购买 nike

我有一个由一组地理点列表定义的区域,我需要知道坐标是否在该区域内

public class Region{
List<Coordinate> boundary;

}

public class Coordinate{

private double latitude;
private double longitude;

}

public static boolean isInsideRegion(Region region, Coordinate coordinate){


}

最佳答案

您可以申请 Point in polygon算法来自 Computational Geometry一系列问题。

Paul Bourke用C语言写了四种算法,可以看代码here .在 Processing Forum 中有对 Java 的改编, 以防万一你不能使用 Java7:

public class RegionUtil {

boolean coordinateInRegion(Region region, Coordinate coord) {
int i, j;
boolean isInside = false;
//create an array of coordinates from the region boundary list
Coordinate[] verts = (Coordinate)region.getBoundary().toArray(new Coordinate[region.size()]);
int sides = verts.length;
for (i = 0, j = sides - 1; i < sides; j = i++) {
//verifying if your coordinate is inside your region
if (
(
(
(verts[i].getLongitude() <= coord.getLongitude()) && (coord.getLongitude() < verts[j].getLongitude())
) || (
(verts[j].getLongitude() <= coord.getLongitude()) && (coord.getLongitude() < verts[i].getLongitude())
)
) &&
(coord.getLatitude() < (verts[j].getLatitude() - verts[i].getLatitude()) * (coord.getLongitude() - verts[i].getLongitude()) / (verts[j].getLongitude() - verts[i].getLongitude()) + verts[i].getLatitude())
) {
isInside = !isInside;
}
}
return isInside;
}
}

关于java - 如何定义确定点是否在经纬度区域内?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12083093/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com