1

I am serving a simple folium map using the following flask app running as an AWS Lambda function:

from flask import Flask
import serverless_wsgi

from Map import fullscreen_map

import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)

app = Flask(__name__,
            static_folder="maps", 
            static_url_path="/maps")

@app.route("/")
def homepage():
    return fullscreen_map().get_root().render()

def handler(event, context):
    return serverless_wsgi.handle_request(app, event, context)

The folium app (imported into the above):

from branca.element import Template, MacroElement
import folium
from flask import request

def fullscreen_map():

    m = folium.Map(
        location=(40.746759, -74.042197), zoom_start=16, tiles="cartodb positron"
    )

    heights_building_footprints_url = f"{request.base_url}/maps/heights-building-footprints.geojson"
    logging.info(f"heights_building_footprints_url: {heights_building_footprints_url}")
    folium.GeoJson(
        heights_building_footprints_url,
        name="Building Footprints",
        style_function=lambda feature: {
            'fillColor': 'grey',
            'color': 'black',
            'weight': 0.5,
            'dashArray': '3, 3'
        },
    ).add_to(m)

return m

The problem I am having is that for some reason either flask or something in the Lambda handler seem to be encoding the geojson files as binary. I don't know why because it serves HTML files in the same static folder properly.

You can see this for yourself in a test deployment:

https://rna.chilltownlabs.com/maps/test.html — flask serves this fine, so the static directory is working.

https://rna.chilltownlabs.com/maps/boundaries-rna.geojson - this appears to have some kind of binary encoding.

Obviously the app dies, because the folium.GeoJson constructor in fullscreen_map tries to load this file via the web throws a JSON error.

Assuming this has something to do with http content-type header but not sure what the right approach is to fix. A new route handler only the geojson files?

This app is deployed as part of an AWS CDK v2 stack (python) but I don't think that's relevant.

This is a very low-traffic app and would prefer not to go through the hassle of setting up an nginx server just to serve 5 static files.

How can I fix this?

I expected a geojson file with the proper encoding to be served up by flask. So far I have not tried to solve this, but would likely create a new route handler with a Response set for just for the geojson.

davidism
  • 121,510
  • 29
  • 395
  • 339
  • 2
    Presumably the content type should be application/vnd.geo+json or application/json but it's application/octet-stream. Related: [Python Flask, how to set content type](https://stackoverflow.com/questions/11773348/python-flask-how-to-set-content-type). Also, are you using API Gateway in front of the Lambda function? – jarmod Mar 11 '23 at 16:37
  • yes API gateway. made some changes to read the geojson file and serve it in a response with the right mimetype. working now, but fails on files more than about 1mb in size. – Anthony Townsend Mar 12 '23 at 02:36
  • 1
    pretty sure this is the 10Mb API Gateway response limit. going to have to figure out a way around this, maybe put the static stuff in S3 https://zaccharles.medium.com/deep-dive-lambdas-response-payload-size-limit-8aedba9530ed – Anthony Townsend Mar 12 '23 at 07:15
  • verified it was the API gateway response limit. working around it by putting the geojson files in S3 with a CDK S3 deployment construct. the URL is access them is passed into flask via an environment variable in a Lambda function construct and those are used by Folium to get the data from S3 via http directly. – Anthony Townsend Mar 12 '23 at 15:44
  • 1
    Also take at look at API Gateway [compression options](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-receive-response-with-compressed-payload.html), in case it's helpful. – jarmod Mar 12 '23 at 17:26
  • thx. i think moving the static content to S3 was inevitable due to Lambda container size constraints. – Anthony Townsend Mar 13 '23 at 18:43

0 Answers0