From 832b0b4e2bfd72940b3727b1b930d658a4ed3487 Mon Sep 17 00:00:00 2001 From: cahytinne Date: Thu, 20 Apr 2017 13:44:56 +0200 Subject: [PATCH 1/3] Response format for center and bounding box #73 --- crabpy/gateway/capakey.py | 33 +++++++++++++++++++++++---------- tests/gateway/test_capakey.py | 4 ++++ 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/crabpy/gateway/capakey.py b/crabpy/gateway/capakey.py index 3aebaa2..54a89b1 100644 --- a/crabpy/gateway/capakey.py +++ b/crabpy/gateway/capakey.py @@ -7,6 +7,7 @@ from __future__ import unicode_literals import six +import json import logging log = logging.getLogger(__name__) @@ -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. @@ -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 @@ -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 @@ -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) @@ -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) @@ -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 diff --git a/tests/gateway/test_capakey.py b/tests/gateway/test_capakey.py index 93f285f..7628371 100644 --- a/tests/gateway/test_capakey.py +++ b/tests/gateway/test_capakey.py @@ -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): @@ -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) From 6b8306e0fed6b8238be8f779df0d2568722922aa Mon Sep 17 00:00:00 2001 From: cahytinne Date: Thu, 20 Apr 2017 15:47:06 +0200 Subject: [PATCH 2/3] add doc strings to new functions #73 --- crabpy/gateway/capakey.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/crabpy/gateway/capakey.py b/crabpy/gateway/capakey.py index 54a89b1..678590e 100644 --- a/crabpy/gateway/capakey.py +++ b/crabpy/gateway/capakey.py @@ -506,11 +506,23 @@ def __init__(self, **kwargs): @staticmethod def _parse_centroid(center): + ''' + Parse response center from the CapakeyRestGateway to (CenterX, CenterY) + + :param center: response center from the CapakeyRestGateway + :return: (CenterX, CenterY) + ''' coordinates = json.loads(center)["coordinates"] return coordinates[0], coordinates[1] @staticmethod def _parse_bounding_box(bounding_box): + ''' + Parse response bounding box from the CapakeyRestGateway to (MinimumX, MinimumY, MaximumX, MaximumY) + + :param bounding_box: response bounding box from the CapakeyRestGateway + :return: (MinimumX, MinimumY, MaximumX, MaximumY) + ''' coordinates = json.loads(bounding_box)["coordinates"] x_coords = [x for x, y in coordinates[0]] y_coords = [y for x, y in coordinates[0]] From d27e4f8cea7f901ac67ed976dcb386720653bc1b Mon Sep 17 00:00:00 2001 From: cahytinne Date: Thu, 20 Apr 2017 15:59:47 +0200 Subject: [PATCH 3/3] update changes and setup.py --- CHANGES.rst | 6 ++++++ setup.py | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index 43ff0d8..610459c 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,3 +1,9 @@ +0.8.1 (??-04-2017) +------------------ + +- Updated center and bounding box format in responses of the CapakeyRestGateway + in accordance with the CapakeyGateway (#73). + 0.8.0 (19-04-2017) ------------------ diff --git a/setup.py b/setup.py index 9ec86d2..ffeefe6 100755 --- a/setup.py +++ b/setup.py @@ -20,7 +20,7 @@ setup( name='crabpy', - version='0.8.0', + version='0.8.1', description='Interact with AGIV webservices.', long_description=open('README.rst').read() + '\n\n' + open('CHANGES.rst').read(),