2017-01-09 27 views
0

我有一個邊框:轉換緯度/長到X,內Y位置邊框

Left -122.27671 
Bottom 37.80445 
Right -122.26673 
Top 37.81449 

它也可以轉化爲NE緯度/經度和SW緯度/龍

在該邊界框,我想找到特定緯度/經度的X,Y位置。這將使用墨卡託投影。

我見過的答案是找到X,Y採用墨卡託投影的世界地圖上的位置,而不是特定的經/緯度範圍內。

任何幫助表示讚賞!

UPDATE 把這個從我看到的另一個問題放在一起。任何人都可以驗證,如果這看起來合法?

map_width = 1240 
map_height = 1279 

map_lon_left = -122.296916 
map_lon_right = -122.243380 
map_lon_delta = map_lon_right - map_lon_left 

map_lat_bottom = 37.782368 
map_lat_bottom_degree = map_lat_bottom * Math::PI/180 

def convert_geo_to_pixel(lat, long) 
    x = (long - map_lon_left) * (map_width/map_lon_delta) 

    lat = lat * Math::PI/180 
    world_map_width = ((map_width/map_lon_delta) * 360)/(2 * Math::PI) 
    map_offset_y = (world_map_width/2 * Math.log((1 + Math.sin(map_lat_bottom_degree))/(1 - Math.sin(map_lat_bottom_degree)))) 
    y = map_height - ((world_map_width/2 * Math.log((1 + Math.sin(lat))/(1 - Math.sin(lat)))) - map_offset_y) 

    return [x, y] 
end 

回答

0

找到一個我已經測試和驗證的更好的解決方案。將此發佈給任何可能會發現它有用的人。它用Ruby編寫,但易於轉換爲任何其他語言

@north = to_radians(37.81449) 
@south = to_radians(37.80445) 
@east = to_radians(-122.26673) 
@west = to_radians(-122.27671) 
# Coordinates above are a subsection of Oakland, CA 

@map_width = map_width 
@map_height = map_height 

def location_to_pixel(lat:, lon:) 
    lat = to_radians(lat) 
    lon = to_radians(lon) 
    ymin = mercator_y(@south) 
    ymax = mercator_y(@north) 
    x_factor = @map_width/(@east - @west) 
    y_factor = @map_height/(ymax - ymin) 

    y = mercator_y(lat); 
    x = (lon - @west) * x_factor 
    y = (ymax - y) * y_factor 
    [x, y] 
end 

def to_radians(deg) 
    deg * Math::PI/180 
end 

def mercator_y(lat) 
    Math.log(
     Math.tan(lat/2 + Math::PI/4) 
    ) 
end 
0

讓我們s是地圖的世界空間移位,以弧度爲單位乙底部緯度,頂部緯度T.(I假設Y = 0是底部)

enter image description here

C * Sin(B) = 0 + s 
C * Sin(T) = map_height + s 
=> 
C = map_height/(Sin(T) - Sin(B)) 
s = C * Sin(B) 
y = C * Sin(Lat) - s = 
    C * Sin(Lat) - C * Sin(B) = 
    C * (Sin(Lat) - Sin(B)) = 
    map_height * (Sin(Lat) - Sin(B)/(Sin(T) - Sin(B)) 

     // note - resembles linear interpolation is sine space 
+0

是我在上面使用的代碼不正確嗎?有您的例子輸出'X,y'當一個邊框內給予緯度/經度很難理解? – theartofbeing

+0

您的x計算是正確的。你似乎錯了。我的緯度相當於你的緯度,B = map_lat_bottom,T = map_lat_top(沒有看到它在你的代碼) – MBo

+0

我從http://stackoverflow.com/questions/2103924/mercator-longitude-and-latitude-抄我的解決辦法計算到的X和Y型上一個裁剪地圖的最/ 10401734#10401734 我相信map_lat左和右是左上角和右上角。既然我們知道角落的位置,我們需要的只是底部的緯度來完成正方形。這就是我解釋這個解決方案的方式? – theartofbeing

相關問題