简体   繁体   English

仅使用 OSM 地图中心的坐标和缩放系数计算区域边界

[英]Calculate area boundaries with only coordinates of the OSM map centre and the zoom factor

I'm using MapSCII (by rastapasta) for a project with python and nodejs.我正在使用MapSCII (由 rastapasta 编写)用于带有 python 和 nodejs 的项目。 I need to render some objects by their location within the MapSCII output.我需要通过它们在 MapSCII 输出中的位置来渲染一些对象。 MapSCII uses OSM tile layers to generate the ASCII map. MapSCII 使用 OSM 平铺层来生成 ASCII 地图。 I only know the coordinates of the center of the map, the zoom level as well as the number of rows/columns of the ASCII map.我只知道地图中心的坐标、缩放级别以及 ASCII 地图的行数/列数。

Do you have any hints on how to calculate the boundaries (upper left and lower right corner), so that I can map a local coordinate system onto the ACSII data?您对如何计算边界(左上角和右下角)有任何提示,以便我可以将局部坐标系映射到 ACSII 数据上吗?

Take these variables for example:以这些变量为例:

def calculate_boundaries(lat, lng, zoom, width, height) -> tuple:
    ...

lat = 51.5252178
lng = -0.0925642
zoom = 17
width = 80
height = 24
upper_left, lower_right = calculate_boundaries(lat, lng, zoom, width, height)

I stumbled across this wiki entry , but it does not seem to be helpful, as I do not work with the tile numbers but with latitude/longitude.我偶然发现了这个 wiki entry ,但它似乎没有帮助,因为我不使用瓷砖编号而是使用纬度/经度。

// Edit Is this even feasible? // 编辑 这甚至可行吗? Or would it be easier to note down, how much lat/lng change when moving in the 2D MapSCII array on each zoom level?或者更容易记下在每个缩放级别上移动 2D MapSCII 阵列时有多少纬度/经度变化?

Let's consider width and height in pixels.让我们考虑以像素为单位的宽度和高度。

Calculation of Meters per Pixel (m/px) The distance (S) represented by 01 (one) pixel is given by:计算每像素米数 (m/px) 由 01(一)像素表示的距离 (S) 由下式给出:

S=C*cos(y)/2^(z+8) Where: S=C*cos(y)/2^(z+8) 其中:

C is the circumference of the Earth at the Equator; C 是地球在赤道处的周长; z is the zoom level; z 是缩放级别; y is the latitude where you want to get the scale. y 是您想要获取比例尺的纬度。

Source: https://wiki.openstreetmap.org/wiki/Pt:Zoom_levels来源: https ://wiki.openstreetmap.org/wiki/Pt:Zoom_levels

There were a mistake while converting degree to radian.将度数转换为弧度时出错。 So I imported pi and convertion.所以我导入了 pi 和转换。

from math import cos, pi

def calculate_boundaries(lat, lng, zoom, width, height): # -> tuple:
    upper_left, lower_right = {}, {}
    C = 40075 # km - Equator distance around the world
    y = pi * lat / 180 # convert latitude degree to radian
    S = C * cos(y) / 2 ** (zoom + 8) # km distance of 1 px - https://wiki.openstreetmap.org/wiki/Pt:Zoom_levels
    S_deg = S * cos(y) / 100 # convert km (distance of 1 px) to degrees (coordinates)

    upper_left['lat'] = lat + height / 2 * S_deg
    upper_left['lng'] = lng - width / 2 * S_deg

    lower_right['lat'] = lat - height / 2 * S_deg
    lower_right['lng'] = lng + width / 2 * S_deg

    return upper_left, lower_right

# main
lat = 51.5252178
lng = -0.0925642
zoom = 17 # zoom
width = 80 # considered as pixels
height = 24
upper_left, lower_right = calculate_boundaries(lat, lng, zoom, width, height)

print(upper_left, lower_right)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM