diff --git a/python/generate_open_street_map/README.md b/python/generate_open_street_map/README.md new file mode 100644 index 00000000..981d4a5e --- /dev/null +++ b/python/generate_open_street_map/README.md @@ -0,0 +1,49 @@ +# 🗂 Generate Open Street Maps +A sample Dart Cloud Function that saves an open street maps tile for given latitude and longitude to Appwrite storage. + +## 📝 Environment Variables +Add the following environment variables in your Cloud Function settings. + +* **APPWRITE_API_KEY** - Create a key from the Appwrite console with the following scope (`files.write`) +* **APPWRITE_ENDPOINT** - Your Appwrite Endpoint + +## 🚀 Building and Packaging + +To package this example as a cloud function, follow these steps. + +```bash +$ cd demos-for-functions/python/backup-to-storage +$ PIP_TARGET=./.appwrite pip install -r ./requirements.txt --upgrade --ignore-installed +``` + +* Ensure that your folder structure looks like this +``` +. +├── .appwrite/ +├── main.py +├── requirments.txt +``` + +* Create a tarfile + +```bash +$ cd .. +$ tar -zcvf code.tar.gz generate_open_street_map +``` + +* Upload the tarfile to your Appwrite Console and use the following entrypoint command + +```bash +python main.py +``` + +## 🎯 Trigger + +Head over to your function in the Appwrite console and after clicking the `Execute Now` button, enter a JSON object in the form of +```json +{ + "latitude": 37.7822403, + "longitude": -122.3910414 +} +``` +with your respective coordinates. diff --git a/python/generate_open_street_map/main.py b/python/generate_open_street_map/main.py new file mode 100644 index 00000000..0a2d5b94 --- /dev/null +++ b/python/generate_open_street_map/main.py @@ -0,0 +1,54 @@ +from appwrite.client import Client +from appwrite.services.storage import Storage +import math +import requests +import os +import json + +client = Client() +client.set_endpoint(os.environ["APPWRITE_ENDPOINT"]) +client.set_project(os.environ["APPWRITE_FUNCTION_PROJECT_ID"]) # this is available by default +client.set_key(os.environ["APPWRITE_API_KEY"]) +client.set_self_signed() + +zoomLevel = 15 + + +def coordinatesToTilePoint(latitude, longitude): + x = math.floor((pow(2, zoomLevel) * ((longitude + 180) / 360))) + radLat = math.radians(latitude) + y = math.floor((pow(2, zoomLevel - 1) * (1 - math.log(math.tan(radLat)) + (1 / math.cos(radLat))) / math.pi)) + return (x,y) + +def FetchTile(latitude, longitude): + global point + point = coordinatesToTilePoint(latitude, longitude) + tileUrl = 'https://tile.openstreetmap.org/{}/{}/{}.png'.format(zoomLevel, point[0], point[1]) + print(tileUrl) + response = requests.get(tileUrl) + if (response.status_code != 200): + print('There was an error loading the tile ') + exit(1) + bytes = response.content + return bytes + +payload = json.loads(os.environ["APPWRITE_FUNCTION_DATA"]) +longitude = payload["longitude"] +latitude = payload["latitude"] + + +imageBytes = FetchTile(latitude, longitude) +with open("Location.txt", "wb") as binary_file: + binary_file.write(imageBytes) + +def send_image(): + storage = Storage(client) + result = storage.create_file(open('Location.txt','rb')) + +try: + print("Uploading your file....") + send_image() + print("Done!") +except Exception as e: + print(e) + diff --git a/python/generate_open_street_map/requirements.txt b/python/generate_open_street_map/requirements.txt new file mode 100644 index 00000000..7a6977ed --- /dev/null +++ b/python/generate_open_street_map/requirements.txt @@ -0,0 +1 @@ +appwrite \ No newline at end of file