Skip to content
This repository was archived by the owner on Jul 16, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions python/generate_open_street_map/README.md
Original file line number Diff line number Diff line change
@@ -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.
54 changes: 54 additions & 0 deletions python/generate_open_street_map/main.py
Original file line number Diff line number Diff line change
@@ -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)

1 change: 1 addition & 0 deletions python/generate_open_street_map/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
appwrite