2017-01-09 65 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

相关问题