0

我正在尝试从 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'
4

2 回答 2

2

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

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

x1new= 100000
x2new = 100500
y1new = 450000
y2new = 450500

layer= 'ahn3_05m_dtm'


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

with rasterio.open(BytesIO(img.read())) as r:
    thing = r.read()
    show(thing)
于 2020-07-17T13:54:41.403 回答
1

您可以使用 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. 这是上面示例的输出:

在此处输入图像描述

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

于 2020-06-05T09:03:59.750 回答