Folium Mapping – Techniques for Mapping Multiple Polygons

foliumgeopandaspandaspolygonpython

I have a list of of coordinates that have areas mapped out on a map

Zone    zone_name   users   distinct_users  geometry
1          A         635          51        POLYGON ((13.05584728590942 77.47638480859382, 13.04673679588868 77.50519132714851, 13.03294330911764 77.53331120019539, 12.98436754600364 77.51502097802745, 12.98663777798433 77.47269816308585, 13.05584728590942 77.47638480859382))
2          B         750          46        POLYGON ((12.98643960066976 77.47192161279304, 12.98451936478889 77.51411771875007, 12.95353196452923 77.51547471337904, 12.93743118939856 77.5343411684571, 12.90406392555974 77.52909721093761, 12.87942997600877 77.49609116845704, 12.88920142757589 77.48010313134762, 12.89796851858703 77.4673766604003, 12.9412878289885 77.46896249108886, 12.98643960066976 77.47192161279304))
3          C         2171         106       POLYGON ((13.06797072753133 77.59028213220222, 13.04330855400157 77.58956289392097, 13.04874693493876 77.57270748620613, 13.04533078511338 77.55819381433116, 13.03927461556928 77.54338387780763, 13.03321829777183 77.53252215295402, 13.04574547711091 77.50741463952636, 13.05556623048057 77.4768047609864, 13.06672433013411 77.50593304162601, 13.08557342240114 77.5560040102539, 13.10051488229898 77.59872061639408, 13.06797072753133 77.59028213220222))
4          D         146          12        POLYGON ((13.09932169348356 77.59929435449226, 13.10124780755884 77.63565397363288, 13.05372024880592 77.63357774072279, 13.03896385704673 77.6273816347657, 13.03948969171691 77.61483378320304, 13.04023452334728 77.6059707460937, 13.04432401103174 77.58955460839843, 13.06981781828728 77.59099125390628, 13.09932169348356 77.59929435449226))
5          E         481          26        POLYGON ((13.07858870108154 77.6346565981446, 13.07486997454336 77.67549570947267, 13.05777373953371 77.75307035546882, 13.05204798886958 77.75889054589857, 13.05434903600938 77.75990421777351, 13.00577746936363 77.68599570947276, 13.00356732656461 77.68131379418946, 13.00269524680756 77.67628855615226, 12.99808413747378 77.66580367379754, 13.00116706406973 77.66355853753657, 13.00998421114968 77.66303678422537, 13.01712854882608 77.66148506265256, 13.02138197244171 77.64876858239745, 13.02938687545724 77.63277699761966, 13.03806046465725 77.62708509545894, 13.05067873716547 77.63246995263671, 13.07858870108154 77.6346565981446))

I am tying to map this on to a Folium map as such

gdf.crs = "epsg:4326"
gdf.to_crs = {'init' :'epsg:4326'}


map_test = folium.Map(location=[12.9716, 77.5946],tiles="cartodbpositron", zoom_start=11)
folium.GeoJson(data=gdf["geometry"]).add_to(map_test)

This does on add on these polygons to the map.

What I am doing wrong here?

Best Answer

If you use the data of Converting a column with coordinates in Pandas to GeoPandas, there is a problem with your location value or with your data

The data in the GeoDataFrame

x1,y1,x2,y2 = gdf['geometry'].total_bounds
print(x1,y1,x2,y2)
12.879429976008769 77.4673766604003 13.101247807558844 77.75990421777351
m = folium.Map(tiles='openstreetmap')
m.fit_bounds([[y1, x1], [y2, x2]])
folium.GeoJson(gdf["geometry"]).add_to(m)

Result

enter image description here

With your location value

map_test = folium.Map(location=[12.9716, 77.5946],tiles="openstreetmap", zoom_start=11)
folium.GeoJson(data=gdf["geometry"]).add_to(map_test)

enter image description here

But

map_test = folium.Map(location=[77.5946,12.9716],tiles="openstreetmap",   zoom_start=11)
folium.GeoJson(data=gdf["geometry"]).add_to(map_test)
map_test

enter image description here

same problem with (inverse x,y values of the bounds)

m = folium.Map(tiles='openstreetmap')
m.fit_bounds([[x1, y1], [x2, y2]])
folium.GeoJson(gdf["geometry"]).add_to(m)

enter image description here

Therefore your data list are in lat,long format and not long,lat format

New

Change the order of xy coordinates

gdf["coordinates"]= gdf.apply(lambda row:eval(row['coordinates']), axis=1)
gdf["coordinates"]= df.apply(lambda row:[(i[1],i[0]) for i in row['coordinates']], axis=1)
gdf['geometry'] = gdf.apply(lambda row:Polygon(row['coordinates']), axis=1)    
gdf.head(2)
  zones                 coordinates                                    geometry
0   1   [(77.47638480859382, 13.055847285909415), (77....   POLYGON ((77.47638 13.05585, 77.50519 13.04674...
1   2   [(77.47192161279304, 12.986439600669756), (77....   POLYGON ((77.47192 12.98644, 77.51412 12.98452...

map_test = folium.Map(location=[12.9716, 77.5946],tiles="openstreetmap",   zoom_start=11)
folium.GeoJson(data=df["geometry"]).add_to(map_test)

enter image description here