Manuel Martinez
Manuel Martinez

Reputation: 828

Adding choropleth map on top of map tile using Bokeh

I have custom shapefiles for school zones in a school district, which I'm able to plot on top of a map tile e.g. cartodbpositron using Folium. However, I'd like to add widgets so that when selecting different widget options, the map rendering is updated. To do this, I'm using Bokeh. However, on Bokeh, the map plot is done on an empty canvas, instead of a map tile and I'm having trouble overlaying the shapefile boundaries on top of a map tile in Bokeh.

Apologies if this question is not complete with code sample, but the question is not necessarily a programmatic one, but one of packet capability.

Thanks in advance.

Upvotes: 1

Views: 614

Answers (1)

Manuel Martinez
Manuel Martinez

Reputation: 828

The issue was that Bokeh map tiles expect Web Mercator coordinates. The fact that my custom shapefiles had lat / lon coordinate pairs, made it incompatible with Bokeh map tile renderings.

I transformed polygon coordinates from lat / lon pairs to Web Mercator coordinates using:

def latlontomercator_math(row):
    x_lon = row['x']
    y_lat = row['y']

    # longitudes are a one step transformation
    x_mer = list(map(lambda x: x * 20037508.34 / 180, x_lon))

    # latitudes are a two step transformation
    y_mer_aux = list(map(lambda y: math.log(math.tan((90 + y) * math.pi / 360))
                                    / (math.pi / 180), y_lat))
    y_mer = list(map(lambda y: y * 20037508.34 / 180, y_mer_aux))

    return(x_mer, y_mer)

data[['x_mer', 'y_mer']] = data.apply(latlontomercator_math, axis=1).apply(pd.Series)

The function was written to be applied row wise using a Pandas dataframe.

Upvotes: 2

Related Questions