Completed
Push — master ( 8a6b55...7194ea )
by
unknown
01:20 queued 50s
created

GeoUtilTests.test_check_in_flanders_tidal_zone()   A

Complexity

Conditions 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
1
# -*- coding: utf-8 -*-
2
import unittest
3
import responses
4
import json
5
6
try:
7
    from unittest.mock import Mock, patch, MagicMock
8
except:
9
    from mock import Mock, patch, MagicMock
10
11
from oe_geoutils.utils import (
12
    convert_geojson_to_wktelement,
13
    get_srid_from_geojson,
14
    convert_wktelement_to_geojson,
15
    get_centroid_xy,
16
    nearest_location,
17
    check_in_flanders,
18
    check_within_flanders,
19
    AdminGrenzenClient,
20
    remove_dupl_values,
21
    remove_dupl_coords,
22
    provincie_niscode, process_location_elements)
23
24
try:
25
    from __init__ import text_, mock_geozoekdiensten_response, mock_geozoekdiensten_get_gemeente_response, \
26
    get_gemeente_results, get_provincie_results, crab_gateway_mock
27
except:
28
    from tests import text_, mock_geozoekdiensten_response, mock_geozoekdiensten_get_gemeente_response, \
29
        get_gemeente_results, get_provincie_results, crab_gateway_mock
30
31
try:
32
    from tests import testdata
33
except:
34
    import testdata
35
import pytest
36
37
38
integration = pytest.mark.skipif(
39
    not pytest.config.getoption("--integration"),
40
    reason="need --integration option to run"
41
)
42
43
niscode_url = 'https://test-geo.onroerenderfgoed.be/zoekdiensten/administratievegrenzen'
44
45
46
class GeoUtilTests(unittest.TestCase):
47
    geojson_valid = {
48
        "type": "MultiPolygon",
49
        "coordinates": [[[[152184.01399999947, 212331.8648750011], [152185.94512499947, 212318.6137500011],
50
                          [152186.13837499946, 212318.6326250011], [152186.86699999947, 212313.9570000011],
51
                          [152186.91462499945, 212313.65187500112], [152192.45099999948, 212314.2943750011],
52
                          [152190.69212499948, 212319.2656250011], [152199.58799999946, 212319.5248750011],
53
                          [152197.85312499947, 212327.9388750011], [152197.57199999946, 212327.8978750011],
54
                          [152197.08099999945, 212333.2668750011], [152184.01399999947, 212331.8648750011]]]],
55
        "crs": {
56
            "type": "name",
57
            "properties": {
58
                "name": "urn:ogc:def:crs:EPSG::31370"
59
            }
60
        }
61
    }
62
63
    def test_geojson_none(self):
64
        self.assertEqual(None, get_srid_from_geojson(None))
65
66
    def test_geojson_value_error(self):
67
        geojson = {"type": "MultiPolygon",
68
                   "coordinates": [[]],
69
                   "crs": {
70
                       "type": "wrong value",
71
                       "properties": {
72
                           "name": "urn:ogc:def:crs:EPSG::31370"
73
                       }
74
                   }}
75
76
        self.assertRaises(ValueError, convert_geojson_to_wktelement, geojson)
77
78
    def test_conversions(self):
79
        self.assertIsNone(convert_wktelement_to_geojson(None))
80
        test_wktelement = convert_geojson_to_wktelement(testdata.test_geojson_valid)
81
        test_geojson_converted = convert_wktelement_to_geojson(test_wktelement)
82
        self.assertEqual(testdata.test_geojson_valid['type'], test_geojson_converted['type'])
83
        self.assertEqual(len(testdata.test_geojson_valid['coordinates']), len(test_geojson_converted['coordinates']))
84
        self.assertEqual(testdata.test_geojson_valid['crs']['type'], test_geojson_converted['crs']['type'])
85
        self.assertEqual(testdata.test_geojson_valid['crs']['properties']['name'],
86
                         test_geojson_converted['crs']['properties']['name'])
87
88
    def test_wktelement_attribute_error(self):
89
        wktelement = "string"
90
        self.assertRaises(AssertionError, convert_wktelement_to_geojson, wktelement)
91
92
    def test_get_centroid_polygon(self):
93
        self.assertEqual('172928.1839066983,174844.0219267663', get_centroid_xy(testdata.test_geojson_valid_polygon))
94
95
    def test_get_centroid(self):
96
        self.assertEqual('172928.1839066983,174844.0219267663', get_centroid_xy(testdata.test_geojson_valid))
97
98
    def test_get_centroid_2(self):
99
        self.assertEqual('152191.3046633389,212324.6399979071', get_centroid_xy(testdata.test_geojson_mulipolygon))
100
101
    @integration
102
    def test_closed_crab_location(self):
103
        closed_adres = {'omschrijving_straat': u'Fonteinstraat, 75', 'huisnummer': u'75', 'straat': u'Fonteinstraat',
104
                        'postcode': u'3000', 'gemeente': u'Leuven', 'land': 'BE'}
105
        self.assertDictEqual(closed_adres, nearest_location(testdata.test_geojson_valid))
106
107
    @responses.activate
108
    def test_closed_crab_location_mock(self):
109
        responses.add(
110
            responses.GET,
111
            'https://loc.geopunt.be/geolocation/Location',
112
            body='{"LocationResult":[{"ID":201984,"FormattedAddress":"Fonteinstraat 75, 3000 Leuven","Location":{"Lat_WGS84":50.883485330273977,"Lon_WGS84":4.6941590167952487,"X_Lambert72":172899.0,"Y_Lambert72":174842.0},"LocationType":"crab_huisnummer_afgeleidVanGebouw","BoundingBox":{"LowerLeft":{"Lat_WGS84":50.883485330273977,"Lon_WGS84":4.6941590167952487,"X_Lambert72":172899.0,"Y_Lambert72":174842.0},"UpperRight":{"Lat_WGS84":50.883485330273977,"Lon_WGS84":4.6941590167952487,"X_Lambert72":172899.0,"Y_Lambert72":174842.0}}}]}',
113
            status=200)
114
        closed_adres = {'omschrijving_straat': u'Fonteinstraat, 75', 'huisnummer': u'75', 'straat': u'Fonteinstraat',
115
                        'postcode': u'3000', 'gemeente': u'Leuven', 'land': 'BE'}
116
        self.assertDictEqual(closed_adres, nearest_location(testdata.test_geojson_valid))
117
118
    @integration
119
    def test_closed_crab_location_2(self):
120
        closed_adres = {'straat': 'Krijgsbaan', 'land': 'BE', 'postcode': '2100',
121
                         'omschrijving_straat': 'Krijgsbaan, 150', 'huisnummer': '150', 'gemeente': 'Antwerpen'}
122
        self.assertDictEqual(closed_adres, nearest_location(
123
            {"type": "MultiPolygon",
124
             "coordinates": [[[[158788, 211982], [158789, 211982], [158789, 211983], [158788, 211982]]]],
125
             "crs": {"type": "name", "properties": {"name": "urn:ogc:def:crs:EPSG::31370"}}}))
126
127
    @responses.activate
128
    def test_closed_crab_location_2_mock(self):
129
        responses.add(
130
            responses.GET,
131
            'https://loc.geopunt.be/geolocation/Location',
132
            body='{"LocationResult":[{"ID":3884086,"FormattedAddress":"Krijgsbaan 150, 2100 Antwerpen (2100)","Location":{"Lat_WGS84":51.217719624709794,"Lon_WGS84":4.49453430920374,"X_Lambert72":158788.0,"Y_Lambert72":211982.0},"LocationType":"crab_huisnummer_afgeleidVanPerceelGrb","BoundingBox":{"LowerLeft":{"Lat_WGS84":51.217719624709794,"Lon_WGS84":4.49453430920374,"X_Lambert72":158788.0,"Y_Lambert72":211982.0},"UpperRight":{"Lat_WGS84":51.217719624709794,"Lon_WGS84":4.49453430920374,"X_Lambert72":158788.0,"Y_Lambert72":211982.0}}}]}',
133
            status=200)
134
        closed_adres = {'straat': 'Krijgsbaan', 'land': 'BE', 'postcode': '2100',
135
                         'omschrijving_straat': 'Krijgsbaan, 150', 'huisnummer': '150', 'gemeente': 'Antwerpen'}
136
        self.assertDictEqual(closed_adres, nearest_location(
137
            {"type": "MultiPolygon",
138
             "coordinates": [[[[158788, 211982], [158789, 211982], [158789, 211983], [158788, 211982]]]],
139
             "crs": {"type": "name", "properties": {"name": "urn:ogc:def:crs:EPSG::31370"}}}))
140
141
    @integration
142
    def test_closed_crab_location_none(self):
143
        self.assertIsNone(nearest_location(testdata.test_json_intersects_flanders))
144
145
    @responses.activate
146
    def test_closed_crab_location_none_mock(self):
147
        responses.add(
148
            responses.GET,
149
            'https://loc.geopunt.be/geolocation/Location',
150
            body='{"LocationResult":[{"ID":1031530,"FormattedAddress":"Linkebeekstraat 35, 1180 Ukkel","Location":{"Lat_WGS84":50.779580783177835,"Lon_WGS84":4.327986356112306,"X_Lambert72":147125.0,"Y_Lambert72":163234.0},"LocationType":"urbis_huisnummer","BoundingBox":{"LowerLeft":{"Lat_WGS84":50.779580783177835,"Lon_WGS84":4.327986356112306,"X_Lambert72":147125.0,"Y_Lambert72":163234.0},"UpperRight":{"Lat_WGS84":50.779580783177835,"Lon_WGS84":4.327986356112306,"X_Lambert72":147125.0,"Y_Lambert72":163234.0}}}]}',
151
            status=200)
152
        self.assertIsNone(nearest_location(testdata.test_json_intersects_flanders))
153
154
    @integration
155
    def test_closed_crab_location_False(self):
156
        self.assertFalse(nearest_location(testdata.test_json_outside_flanders))
157
158
    @responses.activate
159
    def test_closed_crab_location_False_mock(self):
160
        responses.add(
161
            responses.GET,
162
            'https://loc.geopunt.be/geolocation/Location',
163
            body='{"LocationResult":[{"ID":1206833,"FormattedAddress":"Opperstraat 108, 1050 Elsene","Location":{"Lat_WGS84":50.830774347520865,"Lon_WGS84":4.362151535734923,"X_Lambert72":149535.0,"Y_Lambert72":168928.0},"LocationType":"urbis_huisnummer","BoundingBox":{"LowerLeft":{"Lat_WGS84":50.830774347520865,"Lon_WGS84":4.362151535734923,"X_Lambert72":149535.0,"Y_Lambert72":168928.0},"UpperRight":{"Lat_WGS84":50.830774347520865,"Lon_WGS84":4.362151535734923,"X_Lambert72":149535.0,"Y_Lambert72":168928.0}}}]}',
164
            status=200)
165
        self.assertFalse(nearest_location(testdata.test_json_outside_flanders))
166
167 View Code Duplication
    @integration
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
168
    def test_closesr_crab_location_crabpy_gateway(self):
169
        n75 = Mock()
170
        n75.id = 102
171
        n75.huisnummer = "75"
172
        fonteinstraat = Mock()
173
        fonteinstraat.id = 101
174
        fonteinstraat.label = "Fonteinstraat"
175
        fonteinstraat.huisnummers = [n75]
176
        leuven = Mock()
177
        leuven.id = 100
178
        leuven.naam = "Leuven"
179
        leuven.straten = [fonteinstraat]
180
        gemeenten = [leuven]
181
        crabpy_gateway = Mock()
182
        crabpy_gateway.list_gemeenten = Mock(return_value=gemeenten)
183
        closed_adres = {'omschrijving_straat': u'Fonteinstraat, 75', 'huisnummer': u'75', 'huisnummer_id': 102,
184
                        'straat': u'Fonteinstraat', 'straat_id': 101,
185
                        'postcode': u'3000', 'gemeente': u'Leuven', 'gemeente_id': 100, 'land': 'BE'}
186
        self.assertDictEqual(closed_adres, nearest_location(testdata.test_geojson_valid, crabpy_gateway))
187
188 View Code Duplication
    @responses.activate
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
189
    def test_closesr_crab_location_crabpy_gateway_mock(self):
190
        responses.add(
191
            responses.GET,
192
            'https://loc.geopunt.be/geolocation/Location',
193
            body='{"LocationResult":[{"ID":201984,"FormattedAddress":"Fonteinstraat 75, 3000 Leuven","Location":{"Lat_WGS84":50.883485330273977,"Lon_WGS84":4.6941590167952487,"X_Lambert72":172899.0,"Y_Lambert72":174842.0},"LocationType":"crab_huisnummer_afgeleidVanGebouw","BoundingBox":{"LowerLeft":{"Lat_WGS84":50.883485330273977,"Lon_WGS84":4.6941590167952487,"X_Lambert72":172899.0,"Y_Lambert72":174842.0},"UpperRight":{"Lat_WGS84":50.883485330273977,"Lon_WGS84":4.6941590167952487,"X_Lambert72":172899.0,"Y_Lambert72":174842.0}}}]}',
194
            status=200)
195
        n75 = Mock()
196
        n75.id = 102
197
        n75.huisnummer = "75"
198
        fonteinstraat = Mock()
199
        fonteinstraat.id = 101
200
        fonteinstraat.label = "Fonteinstraat"
201
        fonteinstraat.huisnummers = [n75]
202
        leuven = Mock()
203
        leuven.id = 100
204
        leuven.naam = "Leuven"
205
        leuven.straten = [fonteinstraat]
206
        gemeenten = [leuven]
207
        crabpy_gateway = Mock()
208
        crabpy_gateway.list_gemeenten = Mock(return_value=gemeenten)
209
        closed_adres = {'omschrijving_straat': u'Fonteinstraat, 75', 'huisnummer': u'75', 'huisnummer_id': 102,
210
                        'straat': u'Fonteinstraat', 'straat_id': 101,
211
                        'postcode': u'3000', 'gemeente': u'Leuven', 'gemeente_id': 100, 'land': 'BE'}
212
        self.assertDictEqual(closed_adres, nearest_location(testdata.test_geojson_valid, crabpy_gateway))
213
214
    def test_check_in_flanders(self):
215
        self.assertTrue(check_in_flanders(testdata.test_geojson_valid))
216
        self.assertTrue(check_in_flanders(testdata.test_json_intersects_flanders))
217
        self.assertFalse(check_in_flanders(testdata.test_json_outside_flanders))
218
219
    def test_check_in_flanders_tidal_zone(self):
220
        self.assertTrue(check_in_flanders(testdata.test_geojson_valid, 1))
221
        self.assertTrue(check_in_flanders(testdata.test_json_intersects_flanders, 1))
222
        self.assertFalse(check_in_flanders(testdata.test_json_outside_flanders, 1))
223
        self.assertTrue(check_in_flanders(testdata.test_json_tidal_zone_flanders, 1))
224
        self.assertFalse(check_in_flanders(testdata.test_json_tidal_zone_flanders, 0))
225
        self.assertFalse(check_in_flanders(testdata.test_json_tidal_zone_flanders))
226
227
    def test_check_within_flanders(self):
228
        self.assertTrue(check_within_flanders(testdata.test_geojson_valid))
229
        self.assertFalse(check_within_flanders(testdata.test_json_intersects_flanders))
230
        self.assertFalse(check_within_flanders(testdata.test_json_outside_flanders))
231
232
    def test_convert_geojson_to_wktelement_none(self):
233
        self.assertIsNone(convert_geojson_to_wktelement(None))
234
235
    @responses.activate
236
    def test_admin_grenzen_client(self):
237
        base_url = mock_geozoekdiensten_response()
238
        gemeenten = AdminGrenzenClient(base_url).get_gemeenten(self.geojson_valid)
239
        self.assertIsInstance(gemeenten, list)
240
        self.assertGreater(len(gemeenten), 0)
241
242
    @responses.activate
243
    def test_admin_grenzen_client_outside_flanders(self):
244
        base_url = mock_geozoekdiensten_response()
245
        gemeenten = AdminGrenzenClient(base_url).get_gemeenten(testdata.test_json_outside_flanders)
246
        self.assertIsInstance(gemeenten, list)
247
        self.assertEqual(len(gemeenten), 0)
248
249
    @responses.activate
250
    def test_admin_grenzen_client_raise_service_error(self):
251
        base_url = mock_geozoekdiensten_response(response_status=500)
252
        with self.assertRaises(Exception) as ex:
253
            AdminGrenzenClient(base_url).get_gemeenten(self.geojson_valid)
254
255
    @responses.activate
256
    def test_get_gemeente_2(self):
257
        base_url = mock_geozoekdiensten_get_gemeente_response(len_results=2)
258
        gemeente = AdminGrenzenClient(base_url).get_gemeente(testdata.test_geojson_mulipolygon)
259
        self.assertDictEqual({'naam': 'Antwerpen', 'niscode': '11002'}, gemeente)
260
261
    @responses.activate
262
    def test_get_gemeente_1(self):
263
        base_url = mock_geozoekdiensten_get_gemeente_response(len_results=1)
264
        gemeente = AdminGrenzenClient(base_url).get_gemeente(testdata.test_geojson_mulipolygon)
265
        self.assertDictEqual({'naam': 'gemeente', 'niscode': 'niscode'}, gemeente)
266
267
    @responses.activate
268
    def test_get_gemeente_0(self):
269
        base_url = mock_geozoekdiensten_get_gemeente_response(len_results=0)
270
        gemeente = AdminGrenzenClient(base_url).get_gemeente(testdata.test_geojson_mulipolygon)
271
        self.assertIsNone(gemeente)
272
273
    @responses.activate
274
    def test_get_provincie(self):
275
        base_url = 'http://geozoekdienst.en/provincies'
276
        responses.add(responses.POST, base_url, body=json.dumps(get_provincie_results))
277
        provincie = AdminGrenzenClient(base_url).get_provincie(testdata.test_geojson_mulipolygon)
278
        self.assertDictEqual({'naam': 'Antwerpen', 'niscode': '10000'}, provincie)
279
280
    @responses.activate
281
    def test_get_provincies(self):
282
        base_url = 'http://geozoekdienst.en/provincies'
283
        responses.add(responses.POST, base_url, body=json.dumps(get_provincie_results))
284
        provincies = AdminGrenzenClient(base_url).get_provincies(testdata.test_geojson_mulipolygon)
285
        self.assertListEqual(['Antwerpen', 'Vlaams Brabant'], provincies)
286
287
    def test_remove_dupl_values_simple(self):
288
        a = [1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 5, 1]
289
        remove_dupl_values(a)
290
        self.assertEqual([1, 2, 3, 4, 5, 1], a)
291
292
    def test_remove_dupl_values(self):
293
        a = [[94826.49908124168, 193977.986615351],
294
             [94826.49908124168, 193976.986615351],
295
             [94826.49908124168, 193976.986615351],
296
             [94820.99908124168, 193974.486615351],
297
             [94824.99908124168, 193967.986615351],
298
             [94830.99908124168, 193972.986615351],
299
             [94826.49908124168, 193977.986615351]]
300
        remove_dupl_values(a)
301
        self.assertEqual(
302
            [[94826.49908124168, 193977.986615351],
303
             [94826.49908124168, 193976.986615351],
304
             [94820.99908124168, 193974.486615351],
305
             [94824.99908124168, 193967.986615351],
306
             [94830.99908124168, 193972.986615351],
307
             [94826.49908124168, 193977.986615351]], a)
308
309
    def test_remove_dupl_coords(self):
310
        a = {
311
            "crs": {"type": "name", "properties": {"name": "urn:ogc:def:crs:EPSG::31370"}},
312
            "type": "MultiPolygon",
313
            "coordinates": [
314
                [
315
                    [
316
                        [94826.49908124168, 193977.986615351],
317
                        [94826.49908124168, 193976.986615351],
318
                        [94826.49908124168, 193976.986615351],
319
                        [94820.99908124168, 193974.486615351],
320
                        [94824.99908124168, 193967.986615351],
321
                        [94830.99908124168, 193972.986615351],
322
                        [94826.49908124168, 193977.986615351]
323
                    ]
324
                ]
325
            ]
326
        }
327
        remove_dupl_coords(a["coordinates"])
328
        self.assertEqual(
329
            [[[[94826.49908124168, 193977.986615351],
330
               [94826.49908124168, 193976.986615351],
331
               [94820.99908124168, 193974.486615351],
332
               [94824.99908124168, 193967.986615351],
333
               [94830.99908124168, 193972.986615351],
334
               [94826.49908124168, 193977.986615351]]]], a["coordinates"])
335
336
    def test_provincie_niscode(self):
337
        pniscode = provincie_niscode(12021)
338
        self.assertEqual(10000, pniscode)
339
340
    def test_provincie_niscode_vlb(self):
341
        pniscode = provincie_niscode(24062)
342
        self.assertEqual(20001, pniscode)
343
344
    @responses.activate
345
    def test_process_location_elements(self):
346
        base_url = 'http://geozoekdienst.en/gemeenten'
347
        responses.add(responses.POST, base_url, body=json.dumps(get_gemeente_results))
348
        location_elements = process_location_elements(
349
            crab_gateway=crab_gateway_mock,
350
            admin_grenzen_client=AdminGrenzenClient(base_url),
351
            contour=self.geojson_valid
352
        )
353
        self.assertEqual([11002, 24062], [le.gemeente_niscode for le in location_elements])
354
        self.assertEqual(2, len(location_elements))
355
356
    @responses.activate
357
    def test_process_location_elements_ignore_niscode(self):
358
        base_url = 'http://geozoekdienst.en/gemeenten'
359
        responses.add(responses.POST, base_url, body=json.dumps(get_gemeente_results))
360
        location_elements = process_location_elements(
361
            crab_gateway=crab_gateway_mock,
362
            admin_grenzen_client=AdminGrenzenClient(base_url),
363
            contour=self.geojson_valid,
364
            municipalities_niscodes=[11002]
365
        )
366
        self.assertEqual([24062], [le.gemeente_niscode for le in location_elements])
367
        self.assertEqual(1, len(location_elements))
368
369