Skip to content

Commit c4ea05f

Browse files
Merge pull request #30 from geo-engine/no-cartopy
remove cartopy and add its usage only to the WMS example
2 parents 5e24622 + ac2444b commit c4ea05f

File tree

6 files changed

+228
-357
lines changed

6 files changed

+228
-357
lines changed
-108 KB
Binary file not shown.

examples/wms.ipynb

Lines changed: 225 additions & 207 deletions
Large diffs are not rendered by default.

geoengine/workflow.py

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@
1313

1414
import requests as req
1515
import geopandas as gpd
16-
import matplotlib.pyplot as plt
17-
import cartopy.crs as ccrs
1816
from owslib.util import Authentication
1917
from owslib.wcs import WebCoverageService
20-
from owslib.wms import WebMapService
2118
import rasterio.io
2219
from vega import VegaLite
2320
import numpy as np
@@ -192,56 +189,6 @@ def geo_json_with_time_to_geopandas(geo_json):
192189

193190
return geo_json_with_time_to_geopandas(data)
194191

195-
def plot_image(self, bbox: QueryRectangle, ax: plt.Axes = None, timeout=3600) -> plt.Axes:
196-
'''
197-
Query a workflow and return the WMS result as a matplotlib image
198-
199-
Params:
200-
timeout - - HTTP request timeout in seconds
201-
'''
202-
203-
if not self.__result_descriptor.is_raster_result():
204-
raise MethodNotCalledOnRasterException()
205-
206-
session = get_session()
207-
208-
wms_url = f'{session.server_url}/wms/{str(self)}'
209-
210-
def srs_to_projection(srs: str) -> ccrs.Projection:
211-
fallback = ccrs.PlateCarree()
212-
213-
[authority, code] = srs.split(':')
214-
215-
if authority != 'EPSG':
216-
return fallback
217-
try:
218-
return ccrs.epsg(code)
219-
except ValueError:
220-
return fallback
221-
222-
if ax is None:
223-
ax = plt.axes(projection=srs_to_projection(bbox.srs))
224-
225-
wms = WebMapService(wms_url,
226-
version='1.3.0',
227-
auth=Authentication(auth_delegate=session.requests_bearer_auth()),
228-
timeout=timeout)
229-
230-
# TODO: incorporate spatial resolution (?)
231-
232-
ax.add_wms(wms,
233-
layers=[str(self)],
234-
wms_kwargs={
235-
'time': urllib.parse.quote(bbox.time_str),
236-
# 'bbox': bbox.bbox_str
237-
'crs': bbox.srs
238-
})
239-
240-
ax.set_xlim(bbox.xmin, bbox.xmax)
241-
ax.set_ylim(bbox.ymin, bbox.ymax)
242-
243-
return ax
244-
245192
def wms_get_map_as_image(self, bbox: QueryRectangle, colorizer_min_max: Tuple[float, float] = None) -> Image:
246193
'''Return the result of a WMS request as a PIL Image'''
247194

requirements.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
build
2-
filelock # for cartopy mpl tests
3-
flufl.lock # for cartopy mpl tests
2+
cartopy==0.19.0.post1 # for WMS example
3+
matplotlib
44
pycodestyle # formatter
55
pylint # code linter
66
pytest
77
requests_mock
8-
responses # mocking urllib3 requests
98
setuptools
9+
scipy # for WMS example
1010
twine # PyPI
1111
wheel

setup.cfg

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,12 @@ package_dir =
1818
packages = find:
1919
python_requires = >=3.7
2020
install_requires =
21-
cartopy==0.19.0.post1
2221
geopandas
23-
matplotlib
2422
numpy
2523
owslib
2624
pillow
27-
pyepsg # for cartopy
2825
rasterio
2926
requests
30-
scipy # for cartopy and xarray
3127
vega
3228
xarray
3329

tests/test_wms.py

Lines changed: 0 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
from PIL import Image
77

88
import requests_mock
9-
import cartopy.mpl.geoaxes
10-
from cartopy.tests.mpl import ImageTesting
11-
import responses
129

1310
from geoengine.types import QueryRectangle
1411
import geoengine as ge
@@ -20,93 +17,6 @@ class WmsTests(unittest.TestCase):
2017
def setUp(self) -> None:
2118
ge.reset()
2219

23-
@responses.activate
24-
@ImageTesting(['wms'], tolerance=0)
25-
def test_ndvi(self):
26-
with requests_mock.Mocker() as m, open("tests/responses/4326.gml", "rb") as epsg4326_gml:
27-
m.post('http://mock-instance/anonymous', json={
28-
"id": "c4983c3e-9b53-47ae-bda9-382223bd5081",
29-
"project": None,
30-
"view": None
31-
})
32-
33-
m.post('http://mock-instance/workflow',
34-
json={
35-
"id": "5b9508a8-bd34-5a1c-acd6-75bb832d2d38"
36-
},
37-
request_headers={'Authorization': 'Bearer c4983c3e-9b53-47ae-bda9-382223bd5081'})
38-
39-
m.get('http://mock-instance/workflow/5b9508a8-bd34-5a1c-acd6-75bb832d2d38/metadata',
40-
json={
41-
"type": "raster",
42-
"dataType": "U8",
43-
"spatialReference": "EPSG:4326",
44-
"measurement": {
45-
"type": "unitless"
46-
},
47-
"noDataValue": 0.0
48-
},
49-
request_headers={'Authorization': 'Bearer c4983c3e-9b53-47ae-bda9-382223bd5081'})
50-
51-
m.get('http://epsg.io/4326.gml?download', body=epsg4326_gml)
52-
53-
# Unfortunately, we need a separate library to catch the request from the WMS call
54-
with open("tests/responses/wms_capabilities.xml", "r", encoding="utf-8") as wms_capabilities:
55-
m.get(
56-
# pylint: disable=line-too-long
57-
'http://mock-instance/wms/5b9508a8-bd34-5a1c-acd6-75bb832d2d38?service=WMS&request=GetCapabilities&version=1.3.0',
58-
text=wms_capabilities.read())
59-
60-
with open("tests/responses/wms-ndvi.png", "rb") as wms_ndvi:
61-
responses.add(
62-
# pylint: disable=line-too-long
63-
'GET',
64-
'http://mock-instance/wms/5b9508a8-bd34-5a1c-acd6-75bb832d2d38?service=WMS&version=1.3.0&request=GetMap&layers=5b9508a8-bd34-5a1c-acd6-75bb832d2d38&styles=&width=620&height=310&crs=EPSG:4326&bbox=-90.0,-180.0,90.0,180.0&format=image/png&transparent=FALSE&bgcolor=0xFFFFFF&exceptions=XML&time=2014-04-01T12%3A00%3A00.000%2B00%3A00',
65-
match_querystring=True,
66-
body=wms_ndvi.read(),
67-
content_type='image/png'
68-
)
69-
70-
ge.initialize("http://mock-instance")
71-
72-
workflow_definition = {
73-
"type": "Raster",
74-
"operator": {
75-
"type": "GdalSource",
76-
"params": {
77-
"dataset": {
78-
"type": "internal",
79-
"datasetId": "36574dc3-560a-4b09-9d22-d5945f2b8093"
80-
}
81-
}
82-
}
83-
}
84-
85-
time = datetime.strptime(
86-
'2014-04-01T12:00:00.000Z', "%Y-%m-%dT%H:%M:%S.%f%z")
87-
88-
workflow = ge.register_workflow(workflow_definition)
89-
90-
ax = workflow.plot_image(
91-
QueryRectangle(
92-
[-180.0, -90.0, 180.0, 90.0],
93-
[time, time]
94-
)
95-
)
96-
97-
ax.plot()
98-
99-
self.assertEqual(type(ax), cartopy.mpl.geoaxes.GeoAxesSubplot)
100-
101-
# Check requests from the mocker
102-
self.assertEqual(len(m.request_history), 5)
103-
104-
workflow_request = m.request_history[1]
105-
self.assertEqual(workflow_request.method, "POST")
106-
self.assertEqual(workflow_request.url,
107-
"http://mock-instance/workflow")
108-
self.assertEqual(workflow_request.json(), workflow_definition)
109-
11020
def test_ndvi_image(self):
11121
with requests_mock.Mocker() as m,\
11222
open("tests/responses/wms-ndvi.png", "rb") as ndvi_png,\

0 commit comments

Comments
 (0)