Skip to content

Commit

Permalink
Response format for center and bounding box #73
Browse files Browse the repository at this point in the history
  • Loading branch information
cahytinne committed Apr 20, 2017
1 parent 4a29118 commit 832b0b4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
33 changes: 23 additions & 10 deletions crabpy/gateway/capakey.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from __future__ import unicode_literals
import six
import json

import logging
log = logging.getLogger(__name__)
Expand Down Expand Up @@ -503,6 +504,18 @@ def __init__(self, **kwargs):
'%s.' % cr
)

@staticmethod
def _parse_centroid(center):
coordinates = json.loads(center)["coordinates"]
return coordinates[0], coordinates[1]

@staticmethod
def _parse_bounding_box(bounding_box):
coordinates = json.loads(bounding_box)["coordinates"]
x_coords = [x for x, y in coordinates[0]]
y_coords = [y for x, y in coordinates[0]]
return min(x_coords), min(y_coords), max(x_coords), max(y_coords)

def list_gemeenten(self, sort=1):
'''
List all `gemeenten` in Vlaanderen.
Expand Down Expand Up @@ -547,8 +560,8 @@ def creator():
return Gemeente(
res['municipalityCode'],
res['municipalityName'],
res['geometry']['center'],
res['geometry']['boundingBox']
self._parse_centroid(res['geometry']['center']),
self._parse_bounding_box(res['geometry']['boundingBox'])
)
if self.caches['long'].is_configured:
key = 'get_gemeente_by_id_rest#%s' % id
Expand Down Expand Up @@ -635,8 +648,8 @@ def creator():
id=res['departmentCode'],
naam=res['departmentName'],
gemeente=Gemeente(res['municipalityCode'], res['municipalityName']),
centroid=res['geometry']['center'],
bounding_box=res['geometry']['boundingBox']
centroid=self._parse_centroid(res['geometry']['center']),
bounding_box=self._parse_bounding_box(res['geometry']['boundingBox'])
)
if self.caches['long'].is_configured:
key = 'get_kadastrale_afdeling_by_id_rest#%s' % aid
Expand Down Expand Up @@ -709,8 +722,8 @@ def creator():
return Sectie(
res['sectionCode'],
afdeling,
res['geometry']['center'],
res['geometry']['boundingBox'],
self._parse_centroid(res['geometry']['center']),
self._parse_bounding_box(res['geometry']['boundingBox'])
)
if self.caches['long'].is_configured:
key = 'get_sectie_by_id_and_afdeling_rest#%s#%s' % (id, aid)
Expand Down Expand Up @@ -816,8 +829,8 @@ def creator():
Perceel.get_percid_from_capakey(res['capakey']),
None,
None,
res['geometry']['center'],
res['geometry']['boundingBox']
self._parse_centroid(res['geometry']['center']),
self._parse_bounding_box(res['geometry']['boundingBox'])
)
if self.caches['short'].is_configured:
key = 'get_perceel_by_id_and_sectie_rest#%s#%s#%s' % (id, sectie.id, sectie.afdeling.id)
Expand Down Expand Up @@ -857,8 +870,8 @@ def creator():
Perceel.get_percid_from_capakey(res['capakey']),
None,
None,
res['geometry']['center'],
res['geometry']['boundingBox']
self._parse_centroid(res['geometry']['center']),
self._parse_bounding_box(res['geometry']['boundingBox'])
)
if self.caches['short'].is_configured:
key = 'get_perceel_by_capakey_rest#%s' % capakey
Expand Down
4 changes: 4 additions & 0 deletions tests/gateway/test_capakey.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ def test_get_perceel_by_capakey(self, capakey_gateway):
assert isinstance(res, Perceel)
assert res.sectie.id == 'A'
assert res.sectie.afdeling.id == 44021
assert res.centroid == (104033.43150000274, 194675.36899999902)
assert res.bounding_box == (104026.09700000286, 194663.61899999902, 104040.76600000262, 194687.11899999902)

def test_get_perceel_by_unexisting_capakey(self, capakey_gateway):
with pytest.raises(GatewayResourceNotFoundException):
Expand Down Expand Up @@ -281,6 +283,8 @@ def test_get_perceel_by_capakey(self, capakey_rest_gateway):
assert isinstance(res, Perceel)
assert res.sectie.id == 'A'
assert res.sectie.afdeling.id == 44021
assert res.centroid == (104033.43150000274, 194675.36899999902)
assert res.bounding_box == (104026.09700000286, 194663.61899999902, 104040.76600000262, 194687.11899999902)

def test_get_perceel_by_percid(self, capakey_rest_gateway):
s = capakey_rest_gateway.get_sectie_by_id_and_afdeling('A', 44021)
Expand Down

0 comments on commit 832b0b4

Please sign in to comment.