Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set map boundary and stop endless panning in a loop of Folium map

This is my folium code:

import folium
mp = folium.Map(location=[37, -102], 
                zoom_start=1, 
                tiles="Stamen Terrain", 
                )
display(mp)

This is the output I get: enter image description here

There are two problems with leaflet map:

  1. The continents are displayed 2 times or more in a loop.
  2. The map can be panned endlessly from left to right or vice-versa, in a loop.

Both of these are nuisance. The first issue can be addressed temporarily by setting the zoom_start to something else than 1. But even then, zooming out of the map bring this issue back again. The less said about the second one the better.

Now what I want is to limit the boundary of my map to, say, [-150, 150, -70, 70] or smaller. And I don't want to display beyond this bound, either by panning or zooming. Neither do I want my map to pan infinitely in a loop.

Is it possible to do that in Folium?

like image 592
SereneWizard Avatar asked Jan 02 '23 19:01

SereneWizard


2 Answers

It's possible! Just use the min_zoom (and max_zoom for the opposite problem) attribute!

f = folium.Figure(width=1000, height=500)
m = folium.Map(location= initial_location, tiles="openstreetmap",
zoom_start=zoom_start_defined, min_zoom = min_zoom_defined).add_to(f)

I think a min_zoom of 2 should do the work

like image 138
davidvarenne Avatar answered Jan 05 '23 18:01

davidvarenne


One easy way is to use the max_bounds parameter in the Map() function and set it to True. Using this parameter restricts the map to one view of the continents.

Here's an example :

m = folium.Map(location=loc,max_bounds=True)
like image 40
Baumga Avatar answered Jan 05 '23 19:01

Baumga