From d6faaea11953593fd3fdeb4bde217f0811be9c6b Mon Sep 17 00:00:00 2001 From: achintya Date: Tue, 28 Dec 2021 21:54:51 +0530 Subject: [PATCH 1/3] Create README.md --- python/generate_open_street_map/README.md | 43 +++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 python/generate_open_street_map/README.md diff --git a/python/generate_open_street_map/README.md b/python/generate_open_street_map/README.md new file mode 100644 index 00000000..29c7941e --- /dev/null +++ b/python/generate_open_street_map/README.md @@ -0,0 +1,43 @@ +# 🗂 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 under the Settings Tab, enable the `storage.files.create` event. +Or, Just press Excecute From c91a9f23f72bafc83b8b48c60e4b7785643f5139 Mon Sep 17 00:00:00 2001 From: achintya Date: Tue, 28 Dec 2021 21:55:28 +0530 Subject: [PATCH 2/3] Add files via upload --- python/generate_open_street_map/main.py | 54 +++++++++++++++++++ .../generate_open_street_map/requirements.txt | 1 + 2 files changed, 55 insertions(+) create mode 100644 python/generate_open_street_map/main.py create mode 100644 python/generate_open_street_map/requirements.txt 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 From 48a343b136462ace4b2668f4573764bffe08cb39 Mon Sep 17 00:00:00 2001 From: achintya Date: Tue, 28 Dec 2021 22:06:49 +0530 Subject: [PATCH 3/3] Update README.md --- python/generate_open_street_map/README.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/python/generate_open_street_map/README.md b/python/generate_open_street_map/README.md index 29c7941e..981d4a5e 100644 --- a/python/generate_open_street_map/README.md +++ b/python/generate_open_street_map/README.md @@ -39,5 +39,11 @@ python main.py ## 🎯 Trigger -Head over to your function in the Appwrite console and under the Settings Tab, enable the `storage.files.create` event. -Or, Just press Excecute +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.