Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Math 在边界框内将横向/纵向转换为X,Y位置_Math_Maps_2d_Mercator - Fatal编程技术网

Math 在边界框内将横向/纵向转换为X,Y位置

Math 在边界框内将横向/纵向转换为X,Y位置,math,maps,2d,mercator,Math,Maps,2d,Mercator,我有一个边界框: Left -122.27671 Bottom 37.80445 Right -122.26673 Top 37.81449 它也可以转换为NE Lat/Long和SW Lat/Long 在该边界框中,我想找到特定Lat/Long的X,Y位置。这将使用墨卡托投影 我见过一些答案,用墨卡托在世界地图上找到某个位置的X,Y,但不在特定的纬度/经度范围内 感谢您的帮助 更新 把我看到的另一个问题放在一起。有人能证实这是否合法吗 map_width = 1240 map_height

我有一个边界框:

Left -122.27671
Bottom 37.80445
Right -122.26673
Top 37.81449
它也可以转换为NE Lat/Long和SW Lat/Long

在该边界框中,我想找到特定Lat/Long的X,Y位置。这将使用墨卡托投影

我见过一些答案,用墨卡托在世界地图上找到某个位置的X,Y,但不在特定的纬度/经度范围内

感谢您的帮助

更新 把我看到的另一个问题放在一起。有人能证实这是否合法吗

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

让我们看
s
是地图在世界空间中的移动,底部纬度以弧度B表示,顶部纬度T。(我假设y=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

我上面使用的代码是否不正确?当在边界框中给定lat/lon时,无法理解示例输出的
x,y
?您的x计算是正确的。你似乎错了。我的Lat对应于你的Lat,B=map\u Lat\u bottom,T=map\u Lat\u top(在你的代码中没有看到它)我从中复制了我的解决方案,我相信map\u Lat left和right是左上角和右上角。既然我们知道角落在哪里,我们所需要的就是底部的板条来完成正方形。这就是我解释这个解决方案的方式?上面发布了一个重构版本的新答案。我发布的两段代码都有效,但我选择的答案更清晰,因为它指定了NSEW
@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