[英]How to quickly calculate what points are within a given distance of my point
我有大量的經度和緯度,我想快速找出哪些位於某個經度緯度的5公里半徑內。
而不是使用數據結構(這可能會過大),我應該能夠非常快速地執行n種產品。 我做錯了什么,似乎看不到什么。
我一直試圖在Java中實現這一點:
final List<CoOrds> coOrds = Create20x20Grid();
// Determine point X (centre of earth)
final Vector2 X = new Vector2(0,0);
// My CoOrd I want to check
final double srclon = coOrds.get(0).getLongitude();
final double srclat = coOrds.get(0).getLatitude();
final Vector2 A = new Vector2(srclon, srclat, true);
final double brng = 0;
final double d = 5;
final double R = 6371.1;
double dist = 0;
dist = d / R; // convert dist to angular distance in radians
final double lat1 = Math.toRadians(srclat);
final double lon1 = Math.toRadians(srclon);
final double lat2 = Math.asin(Math.sin(lat1) * Math.cos(dist)+ Math.cos(lat1) * Math.sin(dist) * Math.cos(brng));
double lon2 = lon1 + Math.atan2(Math.sin(brng) * Math.sin(dist) * Math.cos(lat1),Math.cos(dist) - Math.sin(lat1) * Math.sin(lat2));
// normalise to -180..+180º
lon2 = (lon2 + 3 * Math.PI) % (2 * Math.PI) - Math.PI;
//Create another point which is the distance is d away from your point
final Vector2 B = new Vector2(Math.toDegrees(lon2),Math.toDegrees(lat2), true);
// Create a vector from X->A
final Vector2 X_A = new Vector2((A.getX() - X.getX()),(A.getY() - X.getY()));
// Create a vector from X->B
final Vector2 X_B = new Vector2((B.getX() - X.getX()),(B.getY() - X.getY()));
// Normalize XA
final Vector2 nX_A = X_A.normalize();
// Normalize XB
final Vector2 nX_B = X_B.normalize();
// Calculate the Dot Product
final Double Alpha = nX_A.dot(nX_B);
int count = 0;
for (final CoOrds c : coOrds) {
final Vector2 P = c.getPosition();
final Vector2 X_P = new Vector2((P.getX() - X.getX()),(P.getY() - X.getY()));
final Vector2 nX_P = X_P.normalize());
final Double Beta = nX_A.dot(nX_P);
if (Beta < Alpha) {
System.out.println(count + " -- " + Beta + " : " + Alpha);
count++;
}
}
System.out.println("Number of CoOrds within Distance : " + count);
當我將新點P加載到Google Maps中時,它是正確的,但是我不能完全確定我的計算是否正確。
我創建了一個自定義Vector2類,該類存儲經度和緯度。 它還將它們隱蔽為笛卡爾坐標:
private void convertSphericalToCartesian(final double latitude, final double longitude) {
x = (earthRadius * Math.cos(latitude) * Math.cos(longitude)) ;
y = (earthRadius * Math.cos(latitude) * Math.sin(longitude)) ;
}
點產品:
public double dot(final Vector2 v2) {
return ((getX() * v2.getX()) + (getY() * v2.getY()));
}
規范化:
public Vector2 normalize() {
final double num2 = (getX() * getX()) + (getY() * getY());
final double num = 1d / Math.sqrt(num2);
double a = x;
double b = y;
a *= num;
b *= num;
return new Vector2(a, b);
}
任何幫助,將不勝感激
我使用了以下網站: http : //www.movable-type.co.uk/scripts/latlong.html為了幫助我計算B點。
我使用了以下網站: http ://rbrundritt.wordpress.com/2008/10/14/conversion-between-spherical-and-cartesian-coordinates-systems/幫助我將球坐標系轉換為笛卡爾坐標系。
謝謝
[編輯]
我當前正在運行的測試用例是:
0-0-0
2-2-0
1-2-0
上面是9點的網格。 我要檢查的點是“ 1”。 我希望它返回所有點“ 2”。 但是它正在返回網格中的所有點。 我已經手動檢查了Google Maps上的距離,它應該只返回點“ 2”。
謝謝
從上面的注釋中,您不確定公式是否正確。
第一步是弄清楚這一點。
選擇地球上一些著名的點(北極,南極,英國格林威治等),然后創建一個測試用例,用這些坐標調用convertSphericaltoCartesian()。 檢查結果,看它們是否有意義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.