使用Python发出WMS请求后如何读取GeoTIFF

时间:2019-06-25 08:21:34

标签: python api rasterio

我正在尝试从WMS服务读取海拔GeoTIFFS。我知道如果输出格式为JPEG时如何使用BytesIO进行此操作,但是对rasterio应用相同的技巧似乎不起作用。有人有建议吗?

url = 'http://geodata.nationaalgeoregister.nl/ahn3/wms?service=wms&version=1.3.0'
wms = WebMapService(url)

x1new= 51
x2new = 51.1
y1new = 5
y2new = 5.1

layer= 'ahn3_05m_dtm'



img = wms.getmap(layers = [layer], srs = 'EPSG:3857', bbox = [x1new,y1new,x2new,y2new] , size = (width,height), format= 'image/GeoTIFF')


r = rasterio.open(BytesIO(img.read()))
#this last step produces an error
r.read()

最后一步让我错了

AttributeError: '_GeneratorContextManager' object has no attribute 'read'

2 个答案:

答案 0 :(得分:2)

您可能已经找到了解决方案,但为了便于记录,此代码可以正常工作。问题出在您打开文件的方式上。

toMin (SecX 20) == MinX 0

答案 1 :(得分:0)

您可以使用rasterio的MemoryFile类:

from owslib.wms import WebMapService
from rasterio import MemoryFile
from rasterio.plot import show

url = 'https://services.terrascope.be/wms/v2?'
wms = WebMapService(url)

x_min = 556945.9710290054
y_min = 6657998.9149440415
x_max = 575290.8578174476
y_max = 6663655.255037144

layer = 'CGS_S2_RADIOMETRY'

img = wms.getmap(
    layers = [layer],
    srs = 'EPSG:3857',
    bbox = (x_min, y_min, x_max, y_max),
    size = (1920, 592),
    format = 'image/png',
    time = '2020-06-01'
)

with MemoryFile(img) as memfile:
     with memfile.open() as dataset:
            show(dataset)
            print(dataset.profile)

不过,我没有设法重现您的示例。请注意,您似乎在使用纬度较长的坐标,但在EPSG:3857中查询wms。这是上面示例的输出:

enter image description here

PS:对于此类特定于GIS的问题,gis.stackexchange可能更相关。