Can I read a raster map which has been constructed in mapserver as WMS with rasterio?
I tried :
from rasterio.io import MemoryFile
def print_metadata(dataset):
print(dataset.profile)
url ='http://localhost:8080/?map=/maps/ivm3.map&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&LAYERS=tr_ivm_gray&STYLES=&SRS=epsg:3857&BBOX=2857613.741389,4274927.875099,4989229.633477,5176940.449967&WIDTH=1160&HEIGHT=540&FORMAT=image/tiff'
tif_bytes = open(url,'rb').read()
with MemoryFile(tif_bytes) as memfile:
with memfile.open() as dataset:
print_metadata(dataset)
and it gives :
FileNotFoundError: [Errno 2] No such file or directory: 'http://localhost:8080/?map=/maps/ivm3.map&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&LAYERS=tr_ivm_gray&STYLES=&SRS=epsg:3857&BBOX=2857613.741389,4274927.875099,4989229.633477,5176940.449967&WIDTH=1160&HEIGHT=540&FORMAT=image/tiff'
another code :
raster=rasterio.open(url)
print (type(raster))
print ( raster.meta )
print ( raster.count )
array=raster.read()
this gives:
<class 'rasterio.io.DatasetReader'>
{'driver': 'WMS', 'dtype': 'uint8', 'nodata': None, 'width': 1073741824, 'height': 454363579, 'count': 3, 'crs': CRS({'init': 'epsg:3857'}), 'transform': Affine(0.001985222000710666, 0.0, 2857613.741389,
0.0, -0.0019852220040462356, 5176940.449967)}
3
Traceback (most recent call last):
File "rio1.py", line 20, in <module>
array=raster.read()
File "rasterio/_io.pyx", line 322, in rasterio._io.DatasetReaderBase.read
MemoryError
raster.read() tries to read too much data.
What can I do here?
Best Answer
In case someone else runs into the same difficulty.
The
MemoryFile
class is indeed the way to go withrasterio
.In the first example you use
MemoryFile
correctly, but you're trying to request data from an url using python'sopen()
built-in function, which is not possible as far as I know. The second example seems correct, but judging from the memory error you get and as indicated by @user2856, you're probably just trying to read too much data.Here are a couple of working examples.
1) Reproducing the first example in the question using
urllib
andrasterio.MemoryFile
:2) Alternatively you can use
owslib
, which comes with a bunch of functionalities to query OGC web services:3) Finally you can also directly read the data with
rasterio.open()
, as in the second example in the question:And here is the result :)