fetch_places.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import requests
  2. import json
  3. from google.cloud import firestore
  4. import os
  5. os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "physigo-firebase-adminsdk-uneji-b592d0d14b.json"
  6. db = firestore.Client()
  7. batch = db.batch()
  8. overpass_url = "https://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. batch.set(db.collection("Places").document(str(el["id"])), data)
  33. except Exception as e:
  34. print(e)
  35. continue
  36. batch.commit()
  37. print(f"Finished adding {line[:-1]}")