forked from open-runtimes/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request open-runtimes#65 from partik03/python
Added generate map function
- Loading branch information
Showing
3 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# 🗺️ Generate Map Image | ||
A Python Cloud Function that generate static map image from longitude and latitude. | ||
_Example input:_ | ||
```json | ||
{ | ||
"lng": 12.561767, | ||
"lat": 41.857177 | ||
} | ||
``` | ||
_Example output:_ | ||
|
||
```json | ||
{ | ||
"success":true, | ||
"image":"iVBORw0KGgoAAAANSUhEUgAAAaQAAALiCAY...QoH9hbkTPQAAAABJRU5ErkJggg==" | ||
} | ||
``` | ||
## 📝 Environment Variables | ||
|
||
List of environment variables used by this cloud function: | ||
|
||
**MAPBOX_ACCESS_TOKEN** - Your Mapbox API key. | ||
|
||
## 🚀 Deployment | ||
|
||
1. Clone this repository, and enter this function folder: | ||
``` | ||
$ git clone https://github.com/open-runtimes/examples.git && cd examples | ||
$ cd python/generate-map | ||
``` | ||
|
||
2. Enter this function folder and build the code: | ||
``` | ||
docker run --rm --interactive --tty --volume $PWD:/usr/code openruntimes/python:v2-3.10 sh /usr/local/src/build.sh | ||
``` | ||
As a result, a `code.tar.gz` file will be generated. | ||
|
||
3. Start the Open Runtime: | ||
``` | ||
docker run -p 3000:3000 -e INTERNAL_RUNTIME_KEY=secret-key -e INTERNAL_RUNTIME_ENTRYPOINT=main.py --rm --interactive --tty --volume $PWD/code.tar.gz:/tmp/code.tar.gz:ro openruntimes/python:v2-3.10 sh /usr/local/src/start.sh | ||
``` | ||
|
||
Your function is now listening on port `3000`, and you can execute it by sending `POST` request with appropriate authorization headers. To learn more about runtime, you can visit Python runtime [README](https://github.com/open-runtimes/open-runtimes/tree/main/runtimes/python-3.10). | ||
|
||
4. Execute function: | ||
|
||
``` | ||
curl http:https://localhost:3000/ -d '{"variables":{"MAPBOX_ACCESS_TOKEN":"YOUR_API_KEY"},"payload": "{\"lng\":50,\"lat\":60}"}' -H "X-Internal-Challenge: secret-key" -H "Content-Type: application/json" | ||
``` | ||
|
||
## 📝 Notes | ||
- This function is designed for use with Appwrite Cloud Functions. You can learn more about it in [Appwrite docs](https://appwrite.io/docs/functions). | ||
- This example is compatible with Python 3.10. Other versions may work but are not guaranteed to work as they haven't been tested. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import json | ||
import requests | ||
import base64 | ||
|
||
def error(res,message): | ||
return res.json({"success":False,"message":message}) | ||
|
||
def main(req,res): | ||
try: | ||
payload = json.loads('{}' if not req.payload else req.payload) | ||
lng = payload.get('lng', None) | ||
lat = payload.get('lat', None) | ||
access_token = req.variables["MAPBOX_ACCESS_TOKEN"] | ||
except Exception: | ||
return error(res,"The payload must contain lng and lat or access token is not present") | ||
if lng == None or lat == None: | ||
return error(res,"The payload must contain lng and lat") | ||
elif((type(lng)!=float or type(lat)!=float) and (type(lng)!=int or type(lat)!=int)): | ||
return error(res,"The longitude and latitude must be a number") | ||
elif lng >180 or lng < -180: | ||
return error(res,"The longitude must be between -180 and 180") | ||
elif lat >90 or lat < -90: | ||
return error(res,"The latitude must be between -90 and 90") | ||
|
||
url ="https://api.mapbox.com/styles/v1/mapbox/streets-v11/static/{0},{1},15,0,0/1000x1000?access_token={2}".format(lng,lat,access_token) | ||
response = requests.get(url,timeout=30) | ||
|
||
if response.status_code ==requests.codes.ok: | ||
image = base64.b64encode(response.content) | ||
return res.json({"success":True,"image":image.decode("utf-8")}) | ||
else: | ||
return error(res,"Error " + str(response.status_code) + ": " + str(response.content)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
requests==2.27.1 |