0

我是 geopandas 和 rasterio 的新手。我已经设法用我的掩码 geotiff 数据绘制了一个图,并使用转换来获取轴上的正确坐标值。我现在想根据上下文添加底图,但我很挣扎。

到目前为止,这是我的代码...

import geopandas
import matplotlib.pyplot as plt
import rasterio
import rasterio.plot
from rasterio import mask
import numpy as np
import contextily as ctx

shapes = [
    { "type": "Polygon", "coordinates": [ [ [ -4.034278075119148, 50.408709729045114 ], [ -4.017163720775412, 50.412558554591797 ], [ -4.004531592262922, 50.405432114946152 ], [ -4.014160721995826, 50.397385783476295 ], [ -4.02723873431488, 50.395358910428506 ], [ -4.03522008284683, 50.40227262174929 ], [ -4.034278075119148, 50.408709729045114 ] ] ] } 
]


with rasterio.open("/home/xxx/Desktop/xxx/dataprocess/data/sx-landcover-84.tif") as src:
    lc_out_image, lc_out_transform = rasterio.mask.mask(src, shapes, crop=True)
    out_meta = src.meta
    out_meta.update({"driver": "GTiff",
                 "height": lc_out_image.shape[0],
                 "width": lc_out_image.shape[1],
                 "transform": lc_out_transform})
    lc_out_image[lc_out_image <= 0] = np.nan
    fig, ax = plt.subplots()
    rasterio.plot.show(lc_out_image, ax=ax, transform=lc_out_transform)

我认为contexily可以做到这一点,但我在两个图书馆之间迷路了。你能帮忙吗?

4

0 回答 0