Lesson 4. Create interactive leaflet maps using folium in jupyter notebooks: GIS in Python


Learning Objectives

After completing this tutorial, you will be able to:

  • Create interactive plots using folium in Python Jupyter notebook

What You Need

You will need a computer with internet access to complete this lesson and the data for week 4 of the course.

Download Spatial Lidar Teaching Data Subset data

or using the earthpy package:

et.data.get_data("spatial-vector-lidar")

import geopandas as gpd
import matplotlib.pyplot as plt
import os
# plot data inline
plt.ion()
os.chdir("/Users/lewa8222/Documents/earth-analytics")

# conda install -c ioos folium=0.2.1
import folium
# create interactive map
map_osm = folium.Map(location=[45.5236, -122.6750])
map_osm

# https://blog.dominodatalab.com/creating-interactive-crime-maps-with-folium/
sjer_crop_extent = gpd.read_file("data/week_04/california/SJER/vector_data/SJER_crop.shp")
sjer_crop_extent = sjer_crop_extent.to_crs(epsg='4326')
mapcen = sjer_crop_extent["geometry"].centroid
xmin, ymin = mapcen.bounds.minx, mapcen.bounds.minx
ymin
print(xmin)
0   -119.73765
Name: minx, dtype: float64
# https://ocefpaf.github.io/python4oceanographers/blog/2015/12/14/geopandas_folium/
# import aoi region

# import shapefile using geopandas
sjer_plot_locations = gpd.read_file(filename="data/week_04/california/SJER/vector_data/SJER_plot_centroids.shp")
SJER_plot_center = sjer_plot_locations.to_crs(epsg='4326')

SJER_plot_center['geometry'].centroid
#sjer_plot_locations_json = sjer_plot_locations.to_crs(epsg='4326').to_json()


# get centroid

0      POINT (-119.747956417154 37.11863532058757)
1     POINT (-119.7303901869959 37.11661877395714)
2     POINT (-119.7366233653719 37.11215762190971)
3     POINT (-119.7433918577241 37.09336678716178)
4     POINT (-119.7462969208354 37.10883568470847)
5     POINT (-119.7341081624058 37.11734044513655)
6     POINT (-119.7384511531834 37.11437916082954)
7     POINT (-119.7384244291388 37.12426253075144)
8      POINT (-119.745898893763 37.11837718958751)
9     POINT (-119.7176689594373 37.10391682267985)
10    POINT (-119.7202321187217 37.10660372342947)
11    POINT (-119.7341737537721 37.08254121557189)
12    POINT (-119.7433339680527 37.08279602375001)
13     POINT (-119.7319028042519 37.1027812234229)
14    POINT (-119.7600476384893 37.10647224370553)
15    POINT (-119.7437157261749 37.10503415123851)
16      POINT (-119.729244441449 37.1014852682469)
17    POINT (-119.7474815085839 37.11135809245318)
dtype: object
sjer_plot_locations_json = sjer_plot_locations.to_crs(epsg='4326').to_json()
#-119.747956417154 37.11863532058757
mapa = folium.Map([37.12, -119.737],
                  zoom_start=14,
                  tiles='cartodbpositron')

points = folium.features.GeoJson(sjer_plot_locations_json)

mapa.add_child(points)
mapa

Additional resources:

*

Leave a Comment