Skip to content
Snippets Groups Projects
Commit 0f135d33 authored by Luís de Sousa's avatar Luís de Sousa
Browse files

Adapted to 2019 services

parent 750d0ba8
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
This time a connection is made to the service for soil pH:
%% Cell type:code id: tags:
``` python
from owslib.wcs import WebCoverageService
wcs = WebCoverageService('http://maps.isric.org/mapserv?map=/map/phh2o.map', version='1.0.0')
```
%% Cell type:markdown id: tags:
A bounding box broadly matching Senegal:
%% Cell type:code id: tags:
``` python
bbox = (-1784000, 1356000, -1140000, 1863000)
```
%% Cell type:markdown id: tags:
Now fetch the coverage for Senegal:
%% Cell type:code id: tags:
``` python
response = wcs.getCoverage(
identifier='phh20_0-5cm_mean',
crs='urn:ogc:def:crs:EPSG::152160',
bbox=bbox,
resx=1, resy=1,
format='GeoTIFF')
```
%% Cell type:code id: tags:
``` python
with open('./data/Senegal_pH_0-5_mean.tif', 'wb') as file:
file.write(response.read())
```
%% Cell type:markdown id: tags:
With the data on the client side some regular interaction can be started with a library like resterIO:
%% Cell type:code id: tags:
``` python
import rasterio
CEC = rasterio.open("./data/Senegal_CEC0m.tif", driver="GTiff")
show(CEC, title='Cation Exchange Capacity at 0 metres in Senegal', cmap='gist_ncar')
```
%% Output
---------------------------------------------------------------------------
CPLE_OpenFailedError Traceback (most recent call last)
rasterio/_base.pyx in rasterio._base.DatasetBase.__init__()
rasterio/_shim.pyx in rasterio._shim.open_dataset()
rasterio/_err.pyx in rasterio._err.exc_wrap_pointer()
CPLE_OpenFailedError: './data/Senegal_CEC0m.tif' not recognized as a supported file format.
During handling of the above exception, another exception occurred:
RasterioIOError Traceback (most recent call last)
<ipython-input-14-3c4dada614d1> in <module>()
1 import rasterio
----> 2 CEC = rasterio.open("./data/Senegal_CEC0m.tif", driver="GTiff")
3 show(CEC, title='Cation Exchange Capacity at 0 metres in Senegal', cmap='gist_ncar')
/usr/lib/python3/dist-packages/rasterio/env.py in wrapper(*args, **kwds)
428
429 with env_ctor(session=session):
--> 430 return f(*args, **kwds)
431
432 return wrapper
/usr/lib/python3/dist-packages/rasterio/__init__.py in open(fp, mode, driver, width, height, count, crs, transform, dtype, nodata, sharing, **kwargs)
214 # None.
215 if mode == 'r':
--> 216 s = DatasetReader(path, driver=driver, sharing=sharing, **kwargs)
217 elif mode == 'r+':
218 s = get_writer_for_path(path)(path, mode, driver=driver, sharing=sharing, **kwargs)
rasterio/_base.pyx in rasterio._base.DatasetBase.__init__()
RasterioIOError: './data/Senegal_CEC0m.tif' not recognized as a supported file format.
%% Cell type:markdown id: tags:
Next steps:
https://publicwiki.deltares.nl/display/OET/WCS+primer
https://geoscripting-wur.github.io/PythonRaster/
This diff is collapsed.
%% Cell type:markdown id: tags:
First load the `WebCoverageService` class from the OWSLib and create a connection to a service:
%% Cell type:code id: tags:
``` python
from owslib.wcs import WebCoverageService
wcs = WebCoverageService('http://maps.isric.org/mapserv?map=/map/bdod.map', version='1.0.0')
```
%% Cell type:markdown id: tags:
A bounding box broadly matching Senegal:
%% Cell type:markdown id: tags:
The absense of errors means that a connection was sucessfully established. To get more information about this service, start by identifying the operations available from this service:
%% Cell type:code id: tags:
``` python
print([op.name for op in wcs.operations])
```
%% Output
['GetCapabilities', 'DescribeCoverage', 'GetCoverage']
%% Cell type:markdown id: tags:
The full list of coverages available from this service is in the `contents` property:
%% Cell type:code id: tags:
``` python
print(list(wcs.contents))
```
%% Output
['bdod_15-30cm_Q0.95', 'bdod_0-5cm_mean', 'bdod_5-15cm_Q0.95', 'bdod_15-30cm_Q0.5', 'bdod_30-60cm_Q0.05', 'bdod_100-200cm_Q0.05', 'bdod_15-30cm_Q0.05', 'bdod_100-200cm_Q0.95', 'bdod_0-5cm_Q0.5', 'bdod_0-5cm_Q0.05', 'bdod_5-15cm_Q0.5', 'bdod_100-200cm_mean', 'bdod_60-100cm_Q0.95', 'bdod_5-15cm_Q0.05', 'bdod_30-60cm_mean', 'bdod_5-15cm_mean', 'bdod_100-200cm_Q0.5', 'bdod_30-60cm_Q0.95', 'bdod_15-30cm_mean', 'bdod_0-5cm_Q0.95', 'bdod_60-100cm_Q0.05', 'bdod_60-100cm_Q0.5', 'bdod_60-100cm_mean', 'bdod_30-60cm_Q0.5']
%% Cell type:markdown id: tags:
That is a large set of coverages, but it is easy to filter the dictionary. For instance, to get the name of all coverages for the 0 cm to 5 cm depth interval:
%% Cell type:code id: tags:
``` python
names = [k for k in wcs.contents.keys() if k.startswith("bdod_0-5cm")]
print(names)
```
%% Output
['bdod_0-5cm_mean', 'bdod_0-5cm_Q0.5', 'bdod_0-5cm_Q0.05', 'bdod_0-5cm_Q0.95']
%% Cell type:markdown id: tags:
Or to search for all the coverages reporting the median prediction:
%% Cell type:code id: tags:
``` python
q0_5_covs = [k for k in wcs.contents.keys() if k.find("Q0.5") != -1]
print(q0_5_covs)
```
%% Output
['bdod_15-30cm_Q0.5', 'bdod_0-5cm_Q0.5', 'bdod_5-15cm_Q0.5', 'bdod_100-200cm_Q0.5', 'bdod_60-100cm_Q0.5', 'bdod_30-60cm_Q0.5']
%% Cell type:markdown id: tags:
These are the SoilGrids predictions for bulk density for the six standard depths defined in the GlobalSoilMap specifications.
The details for one of these coverages can be inspected using the identifiers above:
%% Cell type:code id: tags:
``` python
bdod_5_15_median = wcs.contents['bdod_5-15cm_Q0.5']
bdod_5_15_median.supportedCRS
```
%% Output
[urn:ogc:def:crs:EPSG::152160, urn:ogc:def:crs:EPSG::152160]
%% Cell type:code id: tags:
``` python
bdod_5_15_median.supportedFormats
```
%% Output
['GEOTIFF_16']
%% Cell type:code id: tags:
``` python
bbox = (-17.1, 12.65, -13.15, 14.95)
```
%% Cell type:markdown id: tags:
Now fetch the coverage for Senegal:
%% Cell type:code id: tags:
``` python
response = wcs.getCoverage(
identifier='phh20_0-5cm_mean',
crs='urn:ogc:def:crs:EPSG::152160',
bbox=bbox,
resx=1, resy=1,
format='GeoTIFF')
```
%% Cell type:code id: tags:
``` python
with open('./data/Senegal_CEC0m.tif', 'wb') as file:
file.write(response.read())
```
%% Cell type:markdown id: tags:
With the data on the client side some regular interaction can be started with a library like resterIO:
%% Cell type:code id: tags:
``` python
import rasterio
CEC = rasterio.open("./data/Senegal_CEC0m.tif", driver="GTiff")
show(CEC, title='Cation Exchange Capacity at 0 metres in Senegal', cmap='gist_ncar')
```
%% Output
---------------------------------------------------------------------------
CPLE_OpenFailedError Traceback (most recent call last)
rasterio/_base.pyx in rasterio._base.DatasetBase.__init__()
rasterio/_shim.pyx in rasterio._shim.open_dataset()
rasterio/_err.pyx in rasterio._err.exc_wrap_pointer()
CPLE_OpenFailedError: './data/Senegal_CEC0m.tif' not recognized as a supported file format.
During handling of the above exception, another exception occurred:
RasterioIOError Traceback (most recent call last)
<ipython-input-14-3c4dada614d1> in <module>()
1 import rasterio
----> 2 CEC = rasterio.open("./data/Senegal_CEC0m.tif", driver="GTiff")
3 show(CEC, title='Cation Exchange Capacity at 0 metres in Senegal', cmap='gist_ncar')
/usr/lib/python3/dist-packages/rasterio/env.py in wrapper(*args, **kwds)
428
429 with env_ctor(session=session):
--> 430 return f(*args, **kwds)
431
432 return wrapper
/usr/lib/python3/dist-packages/rasterio/__init__.py in open(fp, mode, driver, width, height, count, crs, transform, dtype, nodata, sharing, **kwargs)
214 # None.
215 if mode == 'r':
--> 216 s = DatasetReader(path, driver=driver, sharing=sharing, **kwargs)
217 elif mode == 'r+':
218 s = get_writer_for_path(path)(path, mode, driver=driver, sharing=sharing, **kwargs)
rasterio/_base.pyx in rasterio._base.DatasetBase.__init__()
RasterioIOError: './data/Senegal_CEC0m.tif' not recognized as a supported file format.
%% Cell type:markdown id: tags:
Next steps:
https://publicwiki.deltares.nl/display/OET/WCS+primer
https://geoscripting-wur.github.io/PythonRaster/
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment