2014-07-21 94 views
1

我拍攝了Google地球的圖像,它的所有4個角的經度/緯度都是已知的。我正在使用GPS傳感器捕捉緯度/經度。我必須使用java將這些捕獲的緯度/經度轉換爲圖像座標(像素座標)。我將使用圖像座標模擬汽車在靜態地圖上移動(從Google地球拍攝的圖像)。在簡單的圓柱投影上將緯度/經度轉換爲圖像座標(像素座標)

我發現了這個公式,並試圖實現它

  1. 確定在1653x1012的圖像最左邊的經度(X)
  2. 確定東最經度在1653x1012的圖像(Y)
  3. 確定經度差速器(Z = Y - X)
  4. 確定南北緯度大部分在1653x1012的圖像(A)
  5. 確定南緯度大部分在1653x1012的圖像(B)
  6. 確定緯度的Diff(C = A - B) 給定緯度和經度,以確定他們點擊哪個像素:

J =輸入的經度 K =輸入緯度

計算X-像素

XPixel = CInt(((Y - J)/CDbl(Z)) * 1653) 

計算的Y像素

YPixel = CInt(((A - K)/CDbl(C)) * 1012) 

這是我使用的代碼。

import java.awt.geom.Point2D; 
    import java.io.BufferedReader; 
    import java.io.File; 
    import java.io.FileReader; 
    import java.util.ArrayList; 
    import java.util.List; 

    public class LatLongService { 
     private static LatLongService latLangService; 
     private BufferedReader reader = null; 
     private String st; 

     private LatLongService() { 
      try { 
       reader = new BufferedReader(new FileReader(new File(
         "resources/GPS_lat_long_2.txt"))); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 

     public static LatLongService getInstance() { 
      if (latLangService == null) 
       latLangService = new LatLongService(); 
      return latLangService; 
     } 

     public List<Point2D> readLatLongList() { 
      List<Point2D> pointList = new ArrayList<Point2D>(); 
      StringBuffer xStr; 
      StringBuffer yStr = new StringBuffer(); 
      try { 
       while ((st = reader.readLine()) != null) { 
        xStr = new StringBuffer(st.substring(0, st.indexOf(','))); 
        yStr = new StringBuffer(st.substring(st.indexOf(',') + 2, 
          st.length())); 
        Point2D pt = new Point2D.Double(
          new Double(xStr.toString()).doubleValue(), new Double(
            yStr.toString()).doubleValue()); 
        pointList.add(pt); 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
       try { 
        reader.close(); 
       } catch (Exception e2) { 
        e.printStackTrace(); 
       } 
      } 
      return pointList; 
     } 


    public List<Point2D> convertLatLongToCoord(List<Point2D> coordinate) { 
      List<Point2D> latLong = new ArrayList<Point2D>(); 
      double westMostLong = -79.974642; 
      double eastMostLong = -79.971244; 
      double longDiff = eastMostLong - westMostLong; // (rightmost_longitude - 
                  // leftmost_longitude) 

      double northMostLat = 39.647556; 
      double southMostLat = 39.644675; 
      double latDiff = northMostLat - southMostLat; // (topmost_latitude - 
                  // bottommost_latitude) 
      for (Point2D coord : coordinate) { 
       double j = coord.getY(); 
       double k = coord.getX(); 

       double XPixel = (((eastMostLong - j)/longDiff) * 1653); 
       double YPixel = (((northMostLat - k)/latDiff) * 1012); 

       Point2D actualCoord = new Point2D.Double(XPixel, YPixel); 
       latLong.add(actualCoord); 
      } 
      return latLong; 
     } 
    } 

一些GPS的緯度/經度我從GPS傳感器得到

輸入緯度輸入的經度 (39.64581,-79.97168) (39.64651,-79.97275) (39.646915,-79.97342) ( 39.646538,-79.97279)

[IMG] http://i59.tinypic.com/nbqkk3.png[/IMG]

圖中的紅色線表示的路徑時,隨後取GPS座標通過傳感器。

但是,當我使用此公式將Lat/Long座標轉換爲像素座標時。像素座標轉換之後並不一致,你可以看到下面的輸出:

圖像X圖像Y (212.0977045,613.3120444) (732.6127134,367.4251996) (1058.542672,225.1620965) (752.0712184,357.5897258)

X,Y(像素)座標的變化太大。因此,當我嘗試根據像素座標移動車輛時,車輛不會沿着紅線或至少靠近該線。

車輛移動到紅線以上或線下,但不在線上。

對於基於像素的車輛的運動平穩協調,最好我期望從轉換緯度/經度對圖像座標是這樣的:

所需的圖像X必需圖像Y (1290,409) (1289,409) (1288,409) (1287,409)

但我得到這個

圖像X圖像Y (212.0977045,613.3120444) (732.6127134,367.4251996) (1058.542672,225.1620965) (752.0712184,357.5897258)

我希望我能夠傳達我的問題。

+0

你有你的java源代碼和一些關於算法的例子嗎? –

+0

「它跳」是什麼意思?你需要使用動畫框架嗎?或者你認爲座標是完全錯誤的?並且您確定輸入座標軟件車輛不會跳躍(這是當座標通過GPS測量並且車輛或人員靜止時的情況) – AlexWien

+0

我編輯了我的問題,希望有所幫助。 – Nemesis

回答

0

這是一個代碼,編譯和回答你的問題,因爲

[1] (39.64581,-79.97168) -> 102,363 
[2] (39.64651,-79.97275) -> 354,217 
[3] (39.646915,-79.97342) -> 512,133 
[4] (39.646538,-79.97279) -> 363,212 
[5] (39.646458,-79.97264) -> 328,228 

您可能已經交換了x-y座標。在這種情況下,x ==經度,y =緯度。

import java.util.*; 
import java.awt.geom.*; 

public class LatLong { 

     private int imageW, imageH; 
     private final static double west = -79.974642,  north = 39.647556, 
             east = -79.971244,  south = 39.644675; 

     public LatLong (int w, int h) { 
       imageW = w; 
       imageH = h; 
     } 

     public List<Point2D> convertLatLongToCoord (List<Point2D> coordinate) { 
      List<Point2D> latLong = new ArrayList<Point2D>(); 
      for (Point2D coord : coordinate) { 
       double x = coord.getY(),  px = imageW * (x-east)/(west-east), 
         y = coord.getX(),  py = imageH * (y-north)/(south-north); 
       latLong.add (new Point2D.Double(px,py)); 
      } 
      return latLong; 
     } 

     public static void main (String[] args) { 
       double[] latit = {39.64581, 39.64651, 39.646915, 39.646538, 39.646458}, 
         longit = {-79.97168, -79.97275, -79.97342, -79.97279, -79.97264}; 

       List<Point2D> pointList = new ArrayList<Point2D>(); 
       for (int i = 0 ; i < latit.length ; i++) 
         pointList.add (new Point2D.Double(latit[i], longit[i])); 

       List<Point2D> pixels = new LatLong (800,600).convertLatLongToCoord (pointList); 

       for (int i = 0 ; i < latit.length ; i++) 
         System.out.println ("[" + (i+1) + "]\t(" + latit[i] + "," + longit[i] + ") -> " + 
           (int) (pixels.get(i).getX()) + "," + (int) (pixels.get(i).getY())); 
}} 
+0

如果我們在Google地球中繪製所有緯度/經度座標,它幾乎會形成直線。然而,當我們使用上面的公式將這個緯度/經度轉換爲像素座標時,這些都是在這個地方。像素座標中的增量/減量應該是連續的,這在當前是不存在的。 – Nemesis

+0

該公式假定由經度和經度形成的矩形的邊界爲矩形。對於這樣的小部分,我不認爲測地變換是相關的。 –

+0

我認爲這些轉換可能是相關的,因爲我嘗試過不同的轉換公式。但我沒有得到我需要的圖像座標。 – Nemesis

1

緯度和經度不是距離。

http://geography.about.com/cs/latitudelongitude/a/latlong.htm

我最近曾在其上使用GPS的Arduino項目。我遵循將經緯度轉換爲北向和東向(UTM)的minigeo API方法。

中的鏈接使用庫,你可以做到這一點皈依: http://www.ibm.com/developerworks/library/j-coordconvert/

比獲得最大的東向和北向和計算比例

private synchronized void scale() { 
     int w = 800; 
     int h = 600; 

     this.scale = Math.min(
       w/(maxEasting - minEasting), 
       h/(maxNorthing - minNorthing)); 

     oEasting = minEasting; 
     oNorthing = minNorthing; 
    } 

不是轉換成X和Y

private int applyScale(double km) { 
     return (int) (km * scale); 
    } 

    private int convertX(double easting) { 
     return applyScale(easting - oEasting); 
    } 

    private int convertY(double northing, int height) { 
     return 600/*height*/ - applyScale(northing - oNorthing); 
    } 

來源:Minigeo

+0

我正在使用從Google Earth捕獲的靜態圖像(GE使用簡單的圓柱投影)。爲什麼我需要再次轉換爲UTM座標?你可以解釋嗎 ? – Nemesis

+0

看到最好的答案,它很好地解釋:http:// stackoverflow。com/questions/14329691/covert-latitude-longitude-point-to-a-pixels-xy-on-mercator-projection?rq = 1 –

相關問題