Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pre-determine optimal level of zoom in folium

I am plotting some maps using folium. Works pretty smoothly. However, I could not figure out how to pre-calculate the right level of zoom

I can set it automatically

import folium
m = folium.Map([point_of_interest.iloc[0].Lat, point_of_interest.iloc[0].Long])

but in my use case I would need to pre-calculate zoom_start such that:

  • all couples (Lat,Long) from my pandas dataframe of point_of_interest are within the map
  • the zoom level is the mnimum possilbe
like image 489
00__00__00 Avatar asked Sep 30 '19 05:09

00__00__00


People also ask

What is Zoom start in Folium?

zoom_start (int, default 10) – Initial zoom level for the map. attr (string, default None) – Map tile attribution; only required if passing custom tile URL.

How do I change the pop up size in Folium?

As a general suggestion, you could use folium. Popup() with the combo of min_width and max_width parameters to force the width of a popup. Show activity on this post. It worked for me, you need to initiate marker with custom icon in each iteration as shown in this code, It will work perfectly...

What are tile styles of Folium maps?

Folium allows us to create maps with different tiles like Stamen Terrain, Stamen Toner, Stamen Water Color, CartoDB Positron, and many more. By default, the tiles are set to OpenStreetMap. Each tileset shows different features of a map and is suitable for different purposes.


1 Answers

folium's fit_bounds method should work for you

Some random sample data

import folium
import numpy as np
import pandas as pd

center_point = [40, -90]

data = (
    np.random.normal(size=(100, 2)) *
    np.array([[.5, .5]]) +
    np.array([center_point])
)

df = pd.DataFrame(data, columns=['Lat', 'Long'])

Creating a map with some markers

m = folium.Map(df[['Lat', 'Long']].mean().values.tolist())

for lat, lon in zip(df['Lat'], df['Long']):
    folium.Marker([lat, lon]).add_to(m)

fit_bounds requires the 'bounds' of our data in the form of the southwest and northeast corners. There are some padding parameters you can use as well

sw = df[['Lat', 'Long']].min().values.tolist()
ne = df[['Lat', 'Long']].max().values.tolist()

m.fit_bounds([sw, ne]) 
m

enter image description here

like image 114
Bob Haffner Avatar answered Sep 22 '22 16:09

Bob Haffner