2012-05-13 35 views
1

我有大量的經度和緯度,我想快速找出哪些在某個經度緯度的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點是正確的,但我不能完全肯定,如果我有計算正確。

我創建了一個自定義的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地圖上的距離,它應該只返回點「2」。

謝謝

+6

歡迎來到Stack Overflow!要求陌生人通過檢查發現代碼中的錯誤並不是富有成效的。您應該使用調試器或打印語句來識別(或至少隔離)問題,然後回來一個更具體的問題(一旦您將其縮小到10行[測試案例](http:///sscce.org))。 –

+0

是什麼讓你覺得你的代碼有問題? –

+0

你問你如何檢查你的代碼是否正確? –

回答

0

從上面的評論,你不知道你的公式是否準確。

第一步是弄清楚。

選擇在地球上的一些知名點(北極,南極,格林威治英國等),並創建與這些座標調用convertSphericaltoCartesian()測試用例。檢查結果,看看它們是否有意義。

+0

Vector2類可以接受一個允許它執行轉換爲笛卡爾的布爾值。 Vector2 A =新的Vector2(srclon,srclat,true);這種方法然後採取球形CoOrdinates並將其轉換爲笛卡爾。我遵循:http://rbrundritt.wordpress.com/2008/10/14/conversion-between-spherical-and-cartesian-coordinates-systems/ – Sonil

+0

然後,我將Cartesian CoOrdinates作爲X,Y存儲在Vector類中。 (保持多拉特僅供我自己參考) – Sonil

+1

好的,我們都從互聯網複製粘貼代碼。但是,您的代碼無法正常工作,您將不得不爬過代碼並找到缺陷。開發這項技能與學習如何編寫代碼一樣重要。 –