3
3
from datetime import datetime
4
4
import unittest
5
5
import textwrap
6
+ from PIL import Image
6
7
7
8
import requests_mock
8
9
import cartopy .mpl .geoaxes
@@ -22,7 +23,7 @@ def setUp(self) -> None:
22
23
@responses .activate
23
24
@ImageTesting (['wms' ], tolerance = 0 )
24
25
def test_ndvi (self ):
25
- with requests_mock .Mocker () as m :
26
+ with requests_mock .Mocker () as m , open ( "tests/responses/4326.gml" , "rb" ) as epsg4326_gml :
26
27
m .post ('http://mock-instance/anonymous' , json = {
27
28
"id" : "c4983c3e-9b53-47ae-bda9-382223bd5081" ,
28
29
"project" : None ,
@@ -47,6 +48,8 @@ def test_ndvi(self):
47
48
},
48
49
request_headers = {'Authorization' : 'Bearer c4983c3e-9b53-47ae-bda9-382223bd5081' })
49
50
51
+ m .get ('http://epsg.io/4326.gml?download' , body = epsg4326_gml )
52
+
50
53
# Unfortunately, we need a separate library to catch the request from the WMS call
51
54
with open ("tests/responses/wms-ndvi.png" , "rb" ) as wms_ndvi :
52
55
responses .add (
@@ -90,16 +93,84 @@ def test_ndvi(self):
90
93
self .assertEqual (type (ax ), cartopy .mpl .geoaxes .GeoAxesSubplot )
91
94
92
95
# Check requests from the mocker
93
- self .assertEqual (len (m .request_history ), 3 )
96
+ self .assertEqual (len (m .request_history ), 4 )
94
97
95
98
workflow_request = m .request_history [1 ]
96
99
self .assertEqual (workflow_request .method , "POST" )
97
100
self .assertEqual (workflow_request .url ,
98
101
"http://mock-instance/workflow" )
99
102
self .assertEqual (workflow_request .json (), workflow_definition )
100
103
104
+ def test_ndvi_image (self ):
105
+ with requests_mock .Mocker () as m ,\
106
+ open ("tests/responses/wms-ndvi.png" , "rb" ) as ndvi_png ,\
107
+ open ("tests/responses/4326.gml" , "rb" ) as epsg4326_gml :
108
+ m .post ('http://mock-instance/anonymous' , json = {
109
+ "id" : "c4983c3e-9b53-47ae-bda9-382223bd5081" ,
110
+ "project" : None ,
111
+ "view" : None
112
+ })
113
+
114
+ m .post ('http://mock-instance/workflow' ,
115
+ json = {
116
+ "id" : "5b9508a8-bd34-5a1c-acd6-75bb832d2d38"
117
+ },
118
+ request_headers = {'Authorization' : 'Bearer c4983c3e-9b53-47ae-bda9-382223bd5081' })
119
+
120
+ m .get ('http://mock-instance/workflow/5b9508a8-bd34-5a1c-acd6-75bb832d2d38/metadata' ,
121
+ json = {
122
+ "type" : "raster" ,
123
+ "dataType" : "U8" ,
124
+ "spatialReference" : "EPSG:4326" ,
125
+ "measurement" : {
126
+ "type" : "unitless"
127
+ },
128
+ "noDataValue" : 0.0
129
+ },
130
+ request_headers = {'Authorization' : 'Bearer c4983c3e-9b53-47ae-bda9-382223bd5081' })
131
+
132
+ m .get ('http://epsg.io/4326.gml?download' , body = epsg4326_gml )
133
+
134
+ # Unfortunately, we need a separate library to catch the request from the WMS call
135
+ m .get (
136
+ # pylint: disable=line-too-long
137
+ 'http://mock-instance/wms?service=WMS&version=1.3.0&request=GetMap&layers=5b9508a8-bd34-5a1c-acd6-75bb832d2d38&time=2014-04-01T12%3A00%3A00.000%2B00%3A00&crs=EPSG%3A4326&bbox=-90.0%2C-180.0%2C90.0%2C180.0&width=200&height=100&format=image%2Fpng&styles=custom%3A%7B%22type%22%3A+%22linearGradient%22%2C+%22breakpoints%22%3A+%5B%7B%22value%22%3A+0%2C+%22color%22%3A+%5B0%2C+0%2C+0%2C+255%5D%7D%2C+%7B%22value%22%3A+255%2C+%22color%22%3A+%5B255%2C+255%2C+255%2C+255%5D%7D%5D%2C+%22noDataColor%22%3A+%5B0%2C+0%2C+0%2C+0%5D%2C+%22defaultColor%22%3A+%5B0%2C+0%2C+0%2C+0%5D%7D' ,
138
+ body = ndvi_png ,
139
+ )
140
+
141
+ ge .initialize ("http://mock-instance" )
142
+
143
+ workflow_definition = {
144
+ "type" : "Raster" ,
145
+ "operator" : {
146
+ "type" : "GdalSource" ,
147
+ "params" : {
148
+ "dataset" : {
149
+ "type" : "internal" ,
150
+ "datasetId" : "36574dc3-560a-4b09-9d22-d5945f2b8093"
151
+ }
152
+ }
153
+ }
154
+ }
155
+
156
+ time = datetime .strptime (
157
+ '2014-04-01T12:00:00.000Z' , "%Y-%m-%dT%H:%M:%S.%f%z" )
158
+
159
+ workflow = ge .register_workflow (workflow_definition )
160
+
161
+ img = workflow .wms_get_map_as_image (
162
+ QueryRectangle (
163
+ [- 180.0 , - 90.0 , 180.0 , 90.0 ],
164
+ [time , time ],
165
+ resolution = (1.8 , 1.8 )
166
+ ),
167
+ colorizer_min_max = (0 , 255 )
168
+ )
169
+
170
+ self .assertEqual (img , Image .open ("tests/responses/wms-ndvi.png" ))
171
+
101
172
def test_wms_url (self ):
102
- with requests_mock .Mocker () as m :
173
+ with requests_mock .Mocker () as m , open ( "tests/responses/4326.gml" , "rb" ) as epsg4326_gml :
103
174
m .post ('http://mock-instance/anonymous' , json = {
104
175
"id" : "c4983c3e-9b53-47ae-bda9-382223bd5081" ,
105
176
"project" : None ,
@@ -124,6 +195,8 @@ def test_wms_url(self):
124
195
},
125
196
request_headers = {'Authorization' : 'Bearer c4983c3e-9b53-47ae-bda9-382223bd5081' })
126
197
198
+ m .get ('http://epsg.io/4326.gml?download' , body = epsg4326_gml )
199
+
127
200
ge .initialize ("http://mock-instance" )
128
201
129
202
workflow_definition = {
@@ -153,7 +226,7 @@ def test_wms_url(self):
153
226
self .assertEqual (
154
227
# pylint: disable=line-too-long
155
228
wms_curl ,
156
- """curl -X GET -H "Authorization: Bearer c4983c3e-9b53-47ae-bda9-382223bd5081" 'http://mock-instance/wms?service=WMS&version=1.3.0&request=GetMap&layers=5b9508a8-bd34-5a1c-acd6-75bb832d2d38&time=2014-04-01T12%3A00%3A00.000%2B00%3A00&crs=EPSG%3A4326&bbox=-180 .0%2C-90 .0%2C180 .0%2C90 .0&width=360&height=180&format=image%2Fpng&styles='"""
229
+ """curl -X GET -H "Authorization: Bearer c4983c3e-9b53-47ae-bda9-382223bd5081" 'http://mock-instance/wms?service=WMS&version=1.3.0&request=GetMap&layers=5b9508a8-bd34-5a1c-acd6-75bb832d2d38&time=2014-04-01T12%3A00%3A00.000%2B00%3A00&crs=EPSG%3A4326&bbox=-90 .0%2C-180 .0%2C90 .0%2C180 .0&width=360&height=180&format=image%2Fpng&styles='"""
157
230
)
158
231
159
232
def test_result_descriptor (self ):
0 commit comments