fetch_places.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. count = 0
  9. overpass_url = "https://overpass-api.de/api/interpreter"
  10. with open('map features.txt', 'r') as f:
  11. for line in f:
  12. if line[0] == "#":
  13. continue
  14. overpass_query = f"""
  15. [out:json];
  16. area["ISO3166-1"="PL"][admin_level=2];
  17. node[{line[:-1]}](area);
  18. out center;
  19. """
  20. response = requests.get(overpass_url, params={"data": overpass_query})
  21. try:
  22. elements = response.json()["elements"]
  23. except json.JSONDecodeError:
  24. print(f"Could not decode data for {line[:-1]}")
  25. continue
  26. object_type = line.split("=")[1].strip()
  27. data = {}
  28. for el in elements:
  29. try:
  30. data["name"] = el["tags"].get("name", object_type)
  31. data["location"] = firestore.GeoPoint(el["lat"], el["lon"])
  32. data["category"] = object_type
  33. batch.set(db.collection("Places").document(str(el["id"])), data)
  34. count += 1
  35. if count == 500:
  36. count = 0
  37. batch.commit()
  38. except Exception as e:
  39. print(e)
  40. continue
  41. batch.commit()
  42. count = 0
  43. print(f"Finished adding {line[:-1]}")