多数の経度と緯度があり、特定の緯度経度から半径 5 km 以内にある経度と緯度をすばやく見つけたいと考えています。
データ構造を使用する代わりに (これはやり過ぎです)、n do 製品を非常に迅速に実行できるはずです。私は何か間違ったことをしただけで、何が何なのかわかりません。
私は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 マップに読み込んだので正しいですが、計算が正しいかどうかは完全にはわかりません。
経度と緯度を格納するカスタム 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);
}
これについての助けは本当にありがたいです
この Web サイトを使用しました: 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マップで距離を手動で確認しましたが、ポイント「2」のみが返されるはずです。
ありがとう