0

I am using folium to generate a map.

m = folium.Map(
    location=[47.842167, -120.101655],
    zoom_start=8,
    tiles='Stamen Toner'
)
points = (47.842167, -120.101655), (46.835627, -118.26239)
folium.Rectangle(bounds=points, color='#ff7800', fill=True, fill_color='#ffff00', fill_opacity=0.2).add_to(m)

m

I would like to save just the part that is in the rectangle..

The part of the map I would like to save

Is it possible to do that with python ? Thank you in advance.

Kyv
  • 615
  • 6
  • 26

1 Answers1

0

Using a combination of disabling pan and zoom features and also using folium's fit_bounds() you could do something like this. I think this comes close to your goal

Note: if you don't want to "lock down" the exported file, you can omit the last 3 params in Map() i.e. zoom_control, scrollWheelZoom and dragging

m = folium.Map(
                location=[47.842167, -120.101655],
                zoom_start=8,
                tiles='Stamen Toner',
                zoom_control=False,
                scrollWheelZoom=False,
                dragging=False
              )

sw = [46.835627, -120.101655]
ne = [47.842167, -118.26239]

m.fit_bounds([sw, ne]) 

m.save('mymap.html')

enter image description here

Bob Haffner
  • 8,235
  • 1
  • 36
  • 43
  • Thank you @BobHaffner for you answer. I needed something like that. But it looks like it does not well fit the map. ```m=folium.Map(width=700,height=700,zoom_control=False,scrollWheelZoom=False,dragging=False) points = (46.835627, -120.101655), (47.842167, -118.26239) folium.Rectangle(bounds=points, color='#ff7800', fill=True, fill_color='#ffff00').add_to(m) sw = [46.835627, -120.101655] ne = [47.842167, -118.26239] m.fit_bounds([sw, ne]) m.save('mymap.html') m``` It still adds some area around the rectangle. I have displayed the rectangle on the map for better visualization. – Kyv Feb 12 '21 at 11:31
  • Yeah, i see what you mean. Maybe fit_bounds() builds in some padding? Not sure on that. – Bob Haffner Feb 12 '21 at 14:47