微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在瓦片中查找世界/地理坐标的像素坐标

如何解决在瓦片中查找世界/地理坐标的像素坐标

我正在尝试使用 Mapbox Terrain RGB获取空间中特定点的高程。我使用 mercantile.tile获取包含我在缩放级别 15 的点的图块的坐标,对于 -43º,-22º(为简单起见)是 12454,18527,然后 mercantile.xy 得到对应的世界坐标:-4806237.7150042495,-2621281.2257876047.

-4806237.7150042495 / 256 的整数部分(图块大小)不应该等于包含该点的图块的 x 坐标,即 12454?如果这个计算被检查出来,我想我正在寻找与结果的小数部分相对应的像素列(x 轴),例如 12454,5 的列 127(256 * 0,5)。然而,除法的结果是 -18774.366,(奇怪地接近瓷砖的 y 坐标,但它看起来像是巧合)。我在这里错过了什么?

作为替代方案,我想到了使用 mercantile.bounds,将第一个和最后一个像素列分配给最西和最东的经度,并通过插值找到我的位置,但我想检查我是否这样做了正确/推荐的方式。我对点高程感兴趣,所以这里所说的一切也适用于 Y 轴。

解决方法

这是我目前得到的:

def correct_altitude_mode(kml):
    with open(kml,"r+") as f:
        txt = f.read()
        if re.search("(?<=<altitudeMode>)relative(?=<\/altitudeMode>)",txt):
            lat = round(float(find_with_re("latitude",txt)),5)
            lng = round(float(find_with_re("longitude",5)
            alt = round(float(find_with_re("altitude",5)
        z = 15
        tile = mercantile.tile(lng,lat,z)            
        westmost,southmost,eastmost,northmost = mercantile.bounds(tile)
        pixel_column = np.interp(lng,[westmost,eastmost],[0,256])
        pixel_row = np.interp(lat,[southmost,northmost],[256,0])
        response = requests.get(f"https://api.mapbox.com/v4/mapbox.terrain-rgb/{z}/{tile.x}/{tile.y}.pngraw?access_token=pk.eyJ1IjoibWFydGltcGFzc29zIiwiYSI6ImNra3pmN2QxajBiYWUycW55N3E1dG1tcTEifQ.JFKSI85oP7M2gbeUTaUfQQ")
        buffer = BytesIO(response.content)
        tile_img = png.read_png_int(buffer)
        _,R,G,B = (tile_img[int(pixel_row),int(pixel_column)])
        print(tile_img[int(pixel_row),int(pixel_column)])
        height = -10000 + ((R * 256 * 256 + G * 256 + B) * 0.1)
        print(f"R:{R},G:{G},B:{B}\n{height}")
        plt.hlines(pixel_row,0.0,256.0,colors="r")
        plt.vlines(pixel_column,colors="r")
        plt.imshow(tile_img)

The height of 880163m doesn't make sense for any point on Earth

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。