geojson_map.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import json
  2. from typing import Optional
  3. import cachetools
  4. @cachetools.cached(cache=cachetools.LRUCache(maxsize=10000))
  5. def latitude_of_zip_code_from_geojson(zip_code: str) -> Optional[float]:
  6. try:
  7. int(zip_code)
  8. except ValueError:
  9. return None
  10. for f in geojson_data['features']:
  11. if int(f['properties']['plz']) == int(zip_code):
  12. return f['geometry']['coordinates'][1]
  13. return None
  14. @cachetools.cached(cache=cachetools.LRUCache(maxsize=10000))
  15. def zip_code_exists(zip_code: str) -> bool:
  16. return latitude_of_zip_code_from_geojson(zip_code) is not None
  17. @cachetools.cached(cache=cachetools.LRUCache(maxsize=10000))
  18. def longitude_of_zip_code_from_geojson(zip_code: str) -> Optional[float]:
  19. try:
  20. int(zip_code)
  21. except ValueError:
  22. return None
  23. for f in geojson_data['features']:
  24. if int(f['properties']['plz']) == int(zip_code):
  25. return f['geometry']['coordinates'][0]
  26. return None
  27. geojson_data = json.loads(open('resources/plz-5stellig-centroid.geojson').read())