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.
- Loading branch information
Showing
2 changed files
with
30 additions
and
20 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
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 |
---|---|---|
@@ -1,32 +1,34 @@ | ||
import json | ||
from PIL import Image | ||
import requests | ||
from io import BytesIO | ||
from base64 import b64encode | ||
import base64 | ||
|
||
def error(res,message): | ||
return res.json({"success":False,"message":message}) | ||
|
||
def main(req,res): | ||
|
||
try: | ||
payload = req.payload | ||
lng = payload["lng"] | ||
lat = payload["lat"] | ||
access_token = req.variables["token"] | ||
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 : | ||
elif lng >180 or lng < -180: | ||
return error(res,"The longitude must be between -180 and 180") | ||
elif lat >90 or lat < -90 : | ||
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: | ||
with Image.open(BytesIO(response.content)) as img: | ||
byt = b64encode(img.tobytes()) | ||
str1 = byt.decode() | ||
return res.json({"success":True,"image":str1}) | ||
image = base64.b64encode(response.content) | ||
return res.json({"success":True,"image":image.decode("utf-8")}) | ||
else: | ||
return error(res,"Nothing to retrieve please check the url") | ||
return error(res,"Error " + str(response.status_code) + ": " + str(response.content)) |