mirror of
https://github.com/protomaps/PMTiles.git
synced 2026-02-04 10:51:07 +00:00
aws lambda has configurable tile_path and pmtiles_path
This commit is contained in:
@@ -5,7 +5,11 @@ import gzip
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
|
||||
# Exists inside all lambda functions
|
||||
import boto3
|
||||
|
||||
# create_lambda_function.py will vendor the relevant file
|
||||
import pmtiles
|
||||
|
||||
Zxy = collections.namedtuple("Zxy", ["z", "x", "y"])
|
||||
@@ -13,7 +17,9 @@ Zxy = collections.namedtuple("Zxy", ["z", "x", "y"])
|
||||
s3 = boto3.client("s3")
|
||||
|
||||
|
||||
@lru_cache
|
||||
# Given a 512MB lambda function, use half of the memory for the cache,
|
||||
# assuming the average root/leaf/tile size is 512 KB
|
||||
@lru_cache(maxsize=500)
|
||||
def get_object_bytes(key, offset, length):
|
||||
end = offset + length - 1
|
||||
return (
|
||||
@@ -27,29 +33,53 @@ def get_object_bytes(key, offset, length):
|
||||
)
|
||||
|
||||
|
||||
def parse_tile_uri(str):
|
||||
m = re.match("^(?:/([0-9a-zA-Z/!\-_\.\*'\(\)]+))?/(\d+)/(\d+)/(\d+).pbf$", str)
|
||||
def pmtiles_path(p, tileset):
|
||||
if not p:
|
||||
p = "{tileset}.pmtiles"
|
||||
return p.replace("{tileset}", tileset)
|
||||
|
||||
|
||||
def parse_tile_path(p, str):
|
||||
if not p:
|
||||
p = "/{tileset}/{z}/{x}/{y}.pbf"
|
||||
p = re.escape(p)
|
||||
p = p.replace(r"\{tileset\}", r"(?P<tileset>[0-9a-zA-Z/!\-_\.\*'\(\)]+)")
|
||||
p = p.replace(r"\{z\}", r"(?P<z>\d+)")
|
||||
p = p.replace(r"\{x\}", r"(?P<x>\d+)")
|
||||
p = p.replace(r"\{y\}", r"(?P<y>\d+)")
|
||||
m = re.match(f"^{p}$", str)
|
||||
if not m:
|
||||
return None, None
|
||||
return (m.group(1), Zxy(int(m.group(2)), int(m.group(3)), int(m.group(4))))
|
||||
return (
|
||||
m.group("tileset"),
|
||||
Zxy(int(m.group("z")), int(m.group("x")), int(m.group("y"))),
|
||||
)
|
||||
|
||||
|
||||
# Assumes event is a API Gateway V2 or Lambda Function URL formatted dict
|
||||
# and returns API Gateway V2 / Lambda Function dict responses
|
||||
# Does not work with CloudFront events/Lambda@Edge; see README
|
||||
def lambda_handler(event, context):
|
||||
start = datetime.now()
|
||||
uri = event["rawPath"] # API Gateway and Lambda Function URLs
|
||||
tileset, tile = parse_tile_uri(uri)
|
||||
uri = event["rawPath"]
|
||||
tileset, tile = parse_tile_uri(os.environ.get("TILE_PATH"), uri)
|
||||
|
||||
if not tile:
|
||||
return {"statusCode": 400, "body": "Invalid Tile URL"}
|
||||
|
||||
def get_bytes(offset, length):
|
||||
return get_object_bytes(tileset + ".pmtiles", offset, length)
|
||||
return get_object_bytes(
|
||||
pmtiles_path(os.environ.get("PMTILES_PATH"), tileset), offset, length
|
||||
)
|
||||
|
||||
reader = pmtiles.Reader(get_bytes)
|
||||
tile_data = reader.get(tile.z, tile.x, tile.y)
|
||||
if not tile_data:
|
||||
return {"statusCode": 404, "body": "Tile not found"}
|
||||
|
||||
# CloudFront requires decompressed responses from lambda
|
||||
# in order to implement the Compressed CacheOptimized policy correctly
|
||||
# as well as Brotli support
|
||||
if reader.header().metadata.get("compression") == "gzip":
|
||||
tile_data = gzip.decompress(tile_data)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user