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

Addings types, and precision arg

parent a6a1b4d0
......@@ -5,20 +5,22 @@ e.g. for RegionChooser or download_files_from_LGL_BW.py.
Also accepts multiple Zipcodes, or Zipcode prefix.
python get_coordinates_by_zipcode.py --help
usage: get_coordinates_by_zipcode.py [-h] PLZ [PLZ ...]
usage: get_coordinates_by_zipcode.py [-h] [-p PRECISION] PLZ [PLZ ...]
Get WKT geometry for desired PLZs
positional arguments:
PLZ desired PLZs
PLZ desired PLZs
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 70567 70569
> python get_coordinates_by_zipcode.py 70
> python get_coordinates_by_zipcode.py -p1000 70
"""
# TODO: Write tests
......@@ -32,15 +34,16 @@ from shapely.geometry import shape
from shapely.ops import unary_union
INPUT_FOLDER = Path('plz')
PLZ_FILENAME = 'plz-5stellig.geojson'
PLZ_FILENAME: str = 'plz-5stellig.geojson'
PLZ_SHAPE_FILE = INPUT_FOLDER / PLZ_FILENAME
PRECISION = 10 # [m]
ONE_DEGREE = 40e6 / 360 # [m]
PRECISION: float = 10 # [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():
from tqdm import tqdm
import requests
......@@ -54,9 +57,9 @@ def _download_plz_shapes_if_needed():
print(' Done')
def _get_plz_shapes():
global PLZ_SHAPES
if PLZ_SHAPES:
def _get_plz_shapes() -> dict:
global PLZ_SHAPES, CACHED
if CACHED:
return PLZ_SHAPES
_download_plz_shapes_if_needed()
......@@ -65,13 +68,14 @@ def _get_plz_shapes():
with open(PLZ_SHAPE_FILE) as f:
print(' Done')
PLZ_SHAPES = json.load(f)
CACHED = True
return PLZ_SHAPES
except json.decoder.JSONDecodeError:
PLZ_SHAPE_FILE.unlink()
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()
geometries = []
for plz_pattern in plz_patterns:
......@@ -114,5 +118,7 @@ if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Get WKT geometry for desired PLZs')
parser.add_argument('plzs', metavar='PLZ', type=str, nargs='+',
help='desired PLZs')
parser.add_argument('-p', '--precision', default=PRECISION, type=int,
help='precision of returned polygon [m]')
args = parser.parse_args()
get_coordinates_by_zipcode(args.plzs)
get_coordinates_by_zipcode(args.plzs, args.precision)
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):
self.assertEqual('foo'.upper(), 'FOO')
get_coordinates_by_zipcode.get_coordinates_by_zipcode(['70567'])
get_coordinates_by_zipcode.get_coordinates_by_zipcode(['70567'], 1_000)
def test_get_mohringen(self):
coords = get_coordinates_by_zipcode(['70567'])
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