fetch_places.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import requests
  2. import json
  3. import uuid
  4. from google.cloud import firestore
  5. import os
  6. os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "physigo-firebase-adminsdk-uneji-b592d0d14b.json"
  7. db = firestore.Client()
  8. overpass_url = "http://overpass-api.de/api/interpreter"
  9. with open('map features.txt', 'r') as f:
  10. for line in f:
  11. if line[0] == "#":
  12. continue
  13. overpass_query = f"""
  14. [out:json];
  15. area["ISO3166-1"="PL"][admin_level=2];
  16. node[{line[:-1]}](area);
  17. out center;
  18. """
  19. response = requests.get(overpass_url, params={"data": overpass_query})
  20. try:
  21. elements = response.json()["elements"]
  22. except json.JSONDecodeError:
  23. print(f"Could not decode data for {line[:-1]}")
  24. continue
  25. object_type = line.split("=")[1].strip()
  26. data = {}
  27. for el in elements:
  28. try:
  29. data["name"] = el["tags"].get("name", object_type)
  30. data["location"] = firestore.GeoPoint(el["lat"], el["lon"])
  31. data["category"] = object_type
  32. db.collection("Places").document(str(el["id"])).set(data)
  33. except Exception as e:
  34. print(e)
  35. continue
  36. print(f"Finished adding {line[:-1]}")