Commit 47f08468 authored by Eric Duminil's avatar Eric Duminil
Browse files

Addings types, and precision arg

parent a6a1b4d0
...@@ -5,8 +5,7 @@ e.g. for RegionChooser or download_files_from_LGL_BW.py. ...@@ -5,8 +5,7 @@ e.g. for RegionChooser or download_files_from_LGL_BW.py.
Also accepts multiple Zipcodes, or Zipcode prefix. Also accepts multiple Zipcodes, or Zipcode prefix.
python get_coordinates_by_zipcode.py --help usage: get_coordinates_by_zipcode.py [-h] [-p PRECISION] PLZ [PLZ ...]
usage: get_coordinates_by_zipcode.py [-h] PLZ [PLZ ...]
Get WKT geometry for desired PLZs Get WKT geometry for desired PLZs
...@@ -15,10 +14,13 @@ Also accepts multiple Zipcodes, or Zipcode prefix. ...@@ -15,10 +14,13 @@ Also accepts multiple Zipcodes, or Zipcode prefix.
options: options:
-h, --help show this help message and exit -h, --help show this help message and exit
-p PRECISION, --precision PRECISION
precision of returned polygon [m]
> python get_coordinates_by_zipcode.py 70174 > python get_coordinates_by_zipcode.py 70174
> python get_coordinates_by_zipcode.py 70567 70569 > python get_coordinates_by_zipcode.py 70567 70569
> python get_coordinates_by_zipcode.py 70 > python get_coordinates_by_zipcode.py -p1000 70
""" """
# TODO: Write tests # TODO: Write tests
...@@ -32,15 +34,16 @@ from shapely.geometry import shape ...@@ -32,15 +34,16 @@ from shapely.geometry import shape
from shapely.ops import unary_union from shapely.ops import unary_union
INPUT_FOLDER = Path('plz') INPUT_FOLDER = Path('plz')
PLZ_FILENAME = 'plz-5stellig.geojson' PLZ_FILENAME: str = 'plz-5stellig.geojson'
PLZ_SHAPE_FILE = INPUT_FOLDER / PLZ_FILENAME PLZ_SHAPE_FILE = INPUT_FOLDER / PLZ_FILENAME
PRECISION = 10 # [m] PRECISION: float = 10 # [m]
ONE_DEGREE = 40e6 / 360 # [m] ONE_DEGREE: float = 40e6 / 360 # [m]
PLZ_SHAPES = None PLZ_SHAPES: dict
CACHED: bool = False
def _download_plz_shapes_if_needed(): def _download_plz_shapes_if_needed() -> None:
if not PLZ_SHAPE_FILE.exists(): if not PLZ_SHAPE_FILE.exists():
from tqdm import tqdm from tqdm import tqdm
import requests import requests
...@@ -54,9 +57,9 @@ def _download_plz_shapes_if_needed(): ...@@ -54,9 +57,9 @@ def _download_plz_shapes_if_needed():
print(' Done') print(' Done')
def _get_plz_shapes(): def _get_plz_shapes() -> dict:
global PLZ_SHAPES global PLZ_SHAPES, CACHED
if PLZ_SHAPES: if CACHED:
return PLZ_SHAPES return PLZ_SHAPES
_download_plz_shapes_if_needed() _download_plz_shapes_if_needed()
...@@ -65,13 +68,14 @@ def _get_plz_shapes(): ...@@ -65,13 +68,14 @@ def _get_plz_shapes():
with open(PLZ_SHAPE_FILE) as f: with open(PLZ_SHAPE_FILE) as f:
print(' Done') print(' Done')
PLZ_SHAPES = json.load(f) PLZ_SHAPES = json.load(f)
CACHED = True
return PLZ_SHAPES return PLZ_SHAPES
except json.decoder.JSONDecodeError: except json.decoder.JSONDecodeError:
PLZ_SHAPE_FILE.unlink() PLZ_SHAPE_FILE.unlink()
raise AttributeError(f"{PLZ_FILENAME} seems to be damaged. Removing it. Please try again!") raise AttributeError(f"{PLZ_FILENAME} seems to be damaged. Removing it. Please try again!")
def get_coordinates_by_zipcode(plz_patterns, precision=PRECISION): def get_coordinates_by_zipcode(plz_patterns: list[str], precision: float = PRECISION) -> str:
plz_shapes = _get_plz_shapes() plz_shapes = _get_plz_shapes()
geometries = [] geometries = []
for plz_pattern in plz_patterns: for plz_pattern in plz_patterns:
...@@ -114,5 +118,7 @@ if __name__ == '__main__': ...@@ -114,5 +118,7 @@ if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Get WKT geometry for desired PLZs') parser = argparse.ArgumentParser(description='Get WKT geometry for desired PLZs')
parser.add_argument('plzs', metavar='PLZ', type=str, nargs='+', parser.add_argument('plzs', metavar='PLZ', type=str, nargs='+',
help='desired PLZs') help='desired PLZs')
parser.add_argument('-p', '--precision', default=PRECISION, type=int,
help='precision of returned polygon [m]')
args = parser.parse_args() args = parser.parse_args()
get_coordinates_by_zipcode(args.plzs) get_coordinates_by_zipcode(args.plzs, args.precision)
import unittest import unittest
import get_coordinates_by_zipcode from get_coordinates_by_zipcode import get_coordinates_by_zipcode
class TestClass(unittest.TestCase): class TestGetCoordinates(unittest.TestCase):
def test_method(self): def test_get_mohringen(self):
self.assertEqual('foo'.upper(), 'FOO') coords = get_coordinates_by_zipcode(['70567'])
get_coordinates_by_zipcode.get_coordinates_by_zipcode(['70567'])
get_coordinates_by_zipcode.get_coordinates_by_zipcode(['70567'], 1_000)
if __name__ == '__main__': if __name__ == '__main__':
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment