123456789101112131415161718192021222324252627282930313233343536 |
- import json
- from typing import Optional
- import cachetools
- @cachetools.cached(cache=cachetools.LRUCache(maxsize=10000))
- def latitude_of_zip_code_from_geojson(zip_code: str) -> Optional[float]:
- try:
- int(zip_code)
- except ValueError:
- return None
- for f in geojson_data['features']:
- if int(f['properties']['plz']) == int(zip_code):
- return f['geometry']['coordinates'][1]
- return None
- @cachetools.cached(cache=cachetools.LRUCache(maxsize=10000))
- def zip_code_exists(zip_code: str) -> bool:
- return latitude_of_zip_code_from_geojson(zip_code) is not None
- @cachetools.cached(cache=cachetools.LRUCache(maxsize=10000))
- def longitude_of_zip_code_from_geojson(zip_code: str) -> Optional[float]:
- try:
- int(zip_code)
- except ValueError:
- return None
- for f in geojson_data['features']:
- if int(f['properties']['plz']) == int(zip_code):
- return f['geometry']['coordinates'][0]
- return None
- geojson_data = json.loads(open('resources/plz-5stellig-centroid.geojson').read())
|