Python folium github

Tonya 183 Published: 06/30/2024

Python folium github

The fascinating world of geospatial visualization!

Python's folium library is a fantastic tool for creating interactive maps with various markers, pop-ups, and overlays. Developed by the brilliant folks at Mapbox, folium leverages the power of both Python and the popular Leaflet.js library to create stunning, web-based maps.

Getting Started

To get started with folium, you'll need to install it via pip:

pip install folium

Once installed, you can import the necessary modules and start creating your map:

import folium
Create a Map Object

m = folium.Map(location=[45.5236, -122.6750], zoom_start=13)

In this example, we're creating a basic map centered at Portland, Oregon (USA).

Markers & Pop-ups

One of the most powerful features of folium is its ability to add markers and pop-ups to your map. Here's an example:

# Add Marker 1

marker1 = folium.Marker([45.5236, -122.6750], popup='Hello from Portland!')

marker1.add_to(m)

Add Marker 2 with HTML Content

popup_content = '

This is a test'

marker2 = folium.Marker([45.5379, -122.6437], popup=folium.Popup(popup_content))

marker2.add_to(m)

In this example, we're adding two markers to our map: one with a simple text popup and another with an HTML-based popup.

Overlays & Heatmaps

Folium also allows you to add overlays, such as heatmaps, polygons, and even custom shapes!

Here's an example of creating a heatmap:

import numpy as np
Create a random dataset

data = np.random.rand(1000)

Create the heatmap overlay

heatmap_overlay = folium.plugins.HeatMap([list(x) for x in zip(np.random.uniform(45.5, 45.6, 100), np.random.uniform(-122.65, -122.68, 100))], radius=10)

heatmap_overlay.add_to(m)

Customizing Your Map

The fun doesn't stop there! Folium provides various options to customize your map, such as:

Setting the background color or image Adding labels and legends Customizing marker shapes and styles Using different projection types (e.g., Mercator, WebMercator)

These are just a few examples of what you can do with folium. With its flexibility, ease-of-use, and impressive feature set, it's an excellent choice for creating stunning, web-based maps.

GitHub & Community

The folium project is hosted on GitHub, where you can find the codebase, issue tracker, and community forum. The community is active and welcoming, with many users sharing their experiences, asking questions, and contributing new features!

If you're interested in learning more about folium or want to get started with geospatial visualization in Python, I encourage you to explore the GitHub repository and join the conversation.

Happy mapping!

Python folium tutorial

I'd be happy to provide a Folium tutorial in English! Here it is:

Introduction

Folium is an open-source library for Python that allows you to create interactive maps and plot data on top of them. It's built on top of Leaflet, which is a popular JavaScript library for creating interactive maps. With Folium, you can easily add markers, polygons, and other elements to your map, as well as style it with various themes and overlays.

Basic Usage

To use Folium, you'll need to install it first using pip:

pip install folium

Once installed, here's an example of how you might create a basic map with a marker:

import folium
Create a map centered on the city of Berlin

m = folium.Map(location=[52.5200, 13.4050], zoom_start=12)

Add a marker to the map at a specific location

marker = folium.Marker([52.5309, 13.4043], popup='Hello, Berlin!')

marker.add_to(m)

Save the map as an HTML file

m.save('berlin_map.html')

This code creates a map centered on Berlin, adds a marker with a popup text "Hello, Berlin!", and then saves it to an HTML file.

Adding Popups

Popups are a great way to add additional information to your markers. Here's how you can create a simple popup:

import folium
Create a map centered on the city of Paris

m = folium.Map(location=[48.8566, 2.3522], zoom_start=12)

Add a marker to the map at a specific location with a popup

marker = folium.Marker([48.8514, 2.2943],

popup=f'

{marker_name}

This is some text!

')

marker.add_to(m)

Save the map as an HTML file

m.save('paris_map.html')

In this example, we create a popup with a title and some text inside it.

Adding Polygons

You can also add polygons to your map. Here's how:

import folium
Create a map centered on the city of Tokyo

m = folium.Map(location=[35.6895, 139.7670], zoom_start=12)

Define the polygon coordinates

poly_coords = [(35.6863, 139.7446), (35.6998, 139.7421),

(35.7014, 139.7559), (35.6947, 139.7545)]

Add a polygon to the map

folium.RegularPolygonMarker(

location=[35.6984, 139.7513],

size=10000,

radius=None,

color='blue',

fill=True,

).add_to(m)

Save the map as an HTML file

m.save('tokyo_map.html')

In this example, we create a simple polygon around Tokyo.

Adding Overlays

You can add various overlays to your map, such as OpenStreetMap (OSM) tiles or satellite imagery. Here's how:

import folium
Create a map centered on the city of New York

m = folium.Map(location=[40.7128, 74.0060], zoom_start=12)

Add OSM tiles to the map

folium.TileLayer('OpenStreetMap').add_to(m)

Save the map as an HTML file

m.save('new_york_map.html')

This code adds the OSM tile overlay to our map.

Conclusion

Folium is a powerful and easy-to-use library for creating interactive maps in Python. With its many features, you can add markers, polygons, popups, and more to your maps. Whether you're working on a project or just want to create some cool visualizations, Folium has got you covered!