-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
46 lines (38 loc) · 1.15 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import sys
from pymgl import Map
import base64
import json
from io import BytesIO
from PIL import Image
style = """{
"version": 8,
"sources": {
"basemap": {
"type": "raster",
"tiles": ["https://services.arcgisonline.com/arcgis/rest/services/Ocean/World_Ocean_Base/MapServer/tile/{z}/{y}/{x}"],
"tileSize": 256
}
},
"layers": [
{ "id": "basemap", "source": "basemap", "type": "raster" }
]
}"""
def handler(event, context):
img_shape = (512, 512)
basemap = Map(style, width=img_shape[0], height=img_shape[1], longitude=0.1276, latitude=51.6072,
zoom=11)# <token=None>, <provider=None>)
buf = basemap.renderBuffer()
img = Image.frombytes("RGBA", img_shape, buf)
img_bytes = BytesIO()
img.save(img_bytes, format="PNG")
img_bytes.seek(0)
base64_image = base64.b64encode(img_bytes.getvalue()).decode()
return {
'statusCode': 200,
'headers': {
'Content-Type': 'image/png',
'Content-Disposition': f'attachment; filename="map.png"'
},
'isBase64Encoded': True,
'body': base64_image
}