| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- import requests
- import json
- import pygeohash as pgh
- from google.cloud import firestore
- import os
- os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "physigo-firebase-adminsdk-uneji-b592d0d14b.json"
- db = firestore.Client()
- batch = db.batch()
- count = 0
- overpass_url = "https://overpass-api.de/api/interpreter"
- with open('map features.txt', 'r') as f:
- for line in f:
- if line[0] == "#":
- continue
- category, category_name = line.split(":")
- category_name = category_name.replace("\"", "").strip()
- overpass_query = f"""
- [out:json];
- area["ISO3166-1"="PL"][admin_level=2];
- node[{category}](area);
- out center;
- """
- response = requests.get(overpass_url, params={"data": overpass_query})
- try:
- elements = response.json()["elements"]
- except json.JSONDecodeError:
- print(f"Could not decode data for {category}")
- continue
- object_type = category.split("=")[1].strip()
- data = {}
- for el in elements:
- try:
- data["name"] = el["tags"].get("name", category_name)
- data["location"] = {
- "geohash": pgh.encode(el["lat"], el["lon"], precision=9),
- "geopoint": firestore.GeoPoint(el["lat"], el["lon"])
- }
- data["category"] = object_type
- batch.set(db.collection("Places").document(str(el["id"])), data)
- count += 1
- if count == 500:
- count = 0
- batch.commit()
- except Exception as e:
- print(e)
- continue
- batch.commit()
- count = 0
- print(f"Finished adding {line[:-1]}")
|