Key Features¶
You can try out leafmap by using the cloud-computing platforms below without having to install anything on your computer:
Install leafmap¶
In [1]:
Copied!
# !pip install leafmap geopandas pycrs osmnx
# !pip install leafmap geopandas pycrs osmnx
Use ipyleaflet plotting backend¶
In [2]:
Copied!
import os
import leafmap
import os
import leafmap
Create an interactive map¶
In [3]:
Copied!
m = leafmap.Map(center=(40, -100), zoom=4)
m
m = leafmap.Map(center=(40, -100), zoom=4)
m
Out[3]:
Customize map height¶
In [4]:
Copied!
m = leafmap.Map(height="400px", width="800px")
m
m = leafmap.Map(height="400px", width="800px")
m
Out[4]:
Set control visibility¶
In [5]:
Copied!
m = leafmap.Map(
draw_control=False,
measure_control=False,
fullscreen_control=False,
attribution_control=True,
)
m
m = leafmap.Map(
draw_control=False,
measure_control=False,
fullscreen_control=False,
attribution_control=True,
)
m
Out[5]:
Change basemaps¶
In [6]:
Copied!
m = leafmap.Map()
m.add_basemap("OpenTopoMap")
m
m = leafmap.Map()
m.add_basemap("OpenTopoMap")
m
Out[6]:
Add XYZ tile layer¶
In [7]:
Copied!
m = leafmap.Map()
m.add_tile_layer(
url="https://mt1.google.com/vt/lyrs=y&x={x}&y={y}&z={z}",
name="Google Satellite",
attribution="Google",
)
m
m = leafmap.Map()
m.add_tile_layer(
url="https://mt1.google.com/vt/lyrs=y&x={x}&y={y}&z={z}",
name="Google Satellite",
attribution="Google",
)
m
Out[7]:
Add WMS tile layer¶
In [8]:
Copied!
m = leafmap.Map(center=[40, -100], zoom=4)
naip_url = "https://www.mrlc.gov/geoserver/mrlc_display/NLCD_2019_Land_Cover_L48/wms?"
m.add_wms_layer(
url=naip_url,
layers="NLCD_2019_Land_Cover_L48",
name="NLCD 2019",
attribution="MRLC",
format="image/png",
shown=True,
)
m.add_legend(title="NLCD Land Cover Type", builtin_legend="NLCD")
m
m = leafmap.Map(center=[40, -100], zoom=4)
naip_url = "https://www.mrlc.gov/geoserver/mrlc_display/NLCD_2019_Land_Cover_L48/wms?"
m.add_wms_layer(
url=naip_url,
layers="NLCD_2019_Land_Cover_L48",
name="NLCD 2019",
attribution="MRLC",
format="image/png",
shown=True,
)
m.add_legend(title="NLCD Land Cover Type", builtin_legend="NLCD")
m
Out[8]:
Add COG layer¶
In [9]:
Copied!
m = leafmap.Map()
url = "https://github.com/opengeos/data/releases/download/raster/Libya-2023-07-01.tif"
m.add_cog_layer(url, name="Fire (pre-event)")
m
m = leafmap.Map()
url = "https://github.com/opengeos/data/releases/download/raster/Libya-2023-07-01.tif"
m.add_cog_layer(url, name="Fire (pre-event)")
m
Out[9]:
Add STAC layer¶
In [10]:
Copied!
m = leafmap.Map()
url = "https://canada-spot-ortho.s3.amazonaws.com/canada_spot_orthoimages/canada_spot5_orthoimages/S5_2007/S5_11055_6057_20070622/S5_11055_6057_20070622.json"
m.add_stac_layer(url, bands=["B3", "B2", "B1"], name="False color")
m
m = leafmap.Map()
url = "https://canada-spot-ortho.s3.amazonaws.com/canada_spot_orthoimages/canada_spot5_orthoimages/S5_2007/S5_11055_6057_20070622/S5_11055_6057_20070622.json"
m.add_stac_layer(url, bands=["B3", "B2", "B1"], name="False color")
m
Out[10]:
Add legend¶
In [11]:
Copied!
m = leafmap.Map()
url = "https://www.mrlc.gov/geoserver/mrlc_display/NLCD_2016_Land_Cover_L48/wms?"
m.add_wms_layer(
url,
layers="NLCD_2016_Land_Cover_L48",
name="NLCD 2016 CONUS Land Cover",
format="image/png",
transparent=True,
)
m.add_legend(builtin_legend="NLCD")
m
m = leafmap.Map()
url = "https://www.mrlc.gov/geoserver/mrlc_display/NLCD_2016_Land_Cover_L48/wms?"
m.add_wms_layer(
url,
layers="NLCD_2016_Land_Cover_L48",
name="NLCD 2016 CONUS Land Cover",
format="image/png",
transparent=True,
)
m.add_legend(builtin_legend="NLCD")
m
Out[11]:
Add colorbar¶
In [12]:
Copied!
m = leafmap.Map()
m.add_basemap("USGS 3DEP Elevation")
colors = ["006633", "E5FFCC", "662A00", "D8D8D8", "F5F5F5"]
vmin = 0
vmax = 4000
m.add_colorbar(colors=colors, vmin=vmin, vmax=vmax)
m
m = leafmap.Map()
m.add_basemap("USGS 3DEP Elevation")
colors = ["006633", "E5FFCC", "662A00", "D8D8D8", "F5F5F5"]
vmin = 0
vmax = 4000
m.add_colorbar(colors=colors, vmin=vmin, vmax=vmax)
m
Out[12]:
Add GeoJSON¶
In [13]:
Copied!
m = leafmap.Map(center=[0, 0], zoom=2)
in_geojson = "https://raw.githubusercontent.com/opengeos/leafmap/master/examples/data/cable_geo.geojson"
m.add_geojson(in_geojson, layer_name="Cable lines")
m
m = leafmap.Map(center=[0, 0], zoom=2)
in_geojson = "https://raw.githubusercontent.com/opengeos/leafmap/master/examples/data/cable_geo.geojson"
m.add_geojson(in_geojson, layer_name="Cable lines")
m
Skipping field coordinates: unsupported OGR type: 3
Out[13]:
In [14]:
Copied!
# Add a GeoJSON with random filled color to the map.
m = leafmap.Map(center=[0, 0], zoom=2)
url = "https://raw.githubusercontent.com/opengeos/leafmap/master/examples/data/countries.geojson"
style = {"fillOpacity": 0.5}
m.add_geojson(
url,
layer_name="Countries",
style=style,
fill_colors=["red", "yellow", "green", "orange"],
)
m
# Add a GeoJSON with random filled color to the map.
m = leafmap.Map(center=[0, 0], zoom=2)
url = "https://raw.githubusercontent.com/opengeos/leafmap/master/examples/data/countries.geojson"
style = {"fillOpacity": 0.5}
m.add_geojson(
url,
layer_name="Countries",
style=style,
fill_colors=["red", "yellow", "green", "orange"],
)
m
Out[14]:
In [15]:
Copied!
# Use custom style and hover_style functions.
m = leafmap.Map(center=[0, 0], zoom=2)
url = "https://raw.githubusercontent.com/opengeos/leafmap/master/examples/data/countries.geojson"
style = {
"stroke": True,
"color": "#0000ff",
"weight": 2,
"opacity": 1,
"fill": True,
"fillColor": "#0000ff",
"fillOpacity": 0.1,
}
hover_style = {"fillOpacity": 0.7}
m.add_geojson(url, layer_name="Countries", style=style, hover_style=hover_style)
m
# Use custom style and hover_style functions.
m = leafmap.Map(center=[0, 0], zoom=2)
url = "https://raw.githubusercontent.com/opengeos/leafmap/master/examples/data/countries.geojson"
style = {
"stroke": True,
"color": "#0000ff",
"weight": 2,
"opacity": 1,
"fill": True,
"fillColor": "#0000ff",
"fillOpacity": 0.1,
}
hover_style = {"fillOpacity": 0.7}
m.add_geojson(url, layer_name="Countries", style=style, hover_style=hover_style)
m
Out[15]:
Add shapefile¶
In [16]:
Copied!
m = leafmap.Map(center=[0, 0], zoom=2)
in_shp = "https://github.com/opengeos/leafmap/raw/master/examples/data/countries.zip"
m.add_shp(in_shp, layer_name="Countries")
m
m = leafmap.Map(center=[0, 0], zoom=2)
in_shp = "https://github.com/opengeos/leafmap/raw/master/examples/data/countries.zip"
m.add_shp(in_shp, layer_name="Countries")
m
Out[16]:
Add KML¶
In [17]:
Copied!
try:
import geopandas
except ImportError:
print("Installing geopandas ...")
subprocess.check_call(["python", "-m", "pip", "install", "geopandas"])
try:
import geopandas
except ImportError:
print("Installing geopandas ...")
subprocess.check_call(["python", "-m", "pip", "install", "geopandas"])
In [18]:
Copied!
m = leafmap.Map()
in_kml = "https://raw.githubusercontent.com/opengeos/leafmap/master/examples/data/us_states.kml"
m.add_kml(in_kml, layer_name="US States KML")
m
m = leafmap.Map()
in_kml = "https://raw.githubusercontent.com/opengeos/leafmap/master/examples/data/us_states.kml"
m.add_kml(in_kml, layer_name="US States KML")
m
Downloading... From: https://raw.githubusercontent.com/opengeos/leafmap/master/examples/data/us_states.kml To: /home/runner/work/leafmap/leafmap/docs/notebooks/us_states.kml
0%| | 0.00/29.8k [00:00<?, ?B/s]
95.4kB [00:00, 58.9MB/s]
Out[18]:
Add GeoDataFrame¶
In [19]:
Copied!
import geopandas as gpd
m = leafmap.Map()
gdf = gpd.read_file(
"https://github.com/opengeos/leafmap/raw/master/examples/data/cable_geo.geojson"
)
m.add_gdf(gdf, layer_name="Cable lines")
m
import geopandas as gpd
m = leafmap.Map()
gdf = gpd.read_file(
"https://github.com/opengeos/leafmap/raw/master/examples/data/cable_geo.geojson"
)
m.add_gdf(gdf, layer_name="Cable lines")
m
Skipping field coordinates: unsupported OGR type: 3
Out[19]:
Create heat map¶
In [20]:
Copied!
m = leafmap.Map()
in_csv = "https://raw.githubusercontent.com/opengeos/leafmap/master/examples/data/world_cities.csv"
m.add_heatmap(
in_csv,
latitude="latitude",
longitude="longitude",
value="pop_max",
name="Heat map",
radius=20,
)
m = leafmap.Map()
in_csv = "https://raw.githubusercontent.com/opengeos/leafmap/master/examples/data/world_cities.csv"
m.add_heatmap(
in_csv,
latitude="latitude",
longitude="longitude",
value="pop_max",
name="Heat map",
radius=20,
)
In [21]:
Copied!
colors = ["blue", "lime", "red"]
vmin = 0
vmax = 10000
m.add_colorbar(colors=colors, vmin=vmin, vmax=vmax)
m.add_title("World Population Heat Map", font_size="20px", align="center")
m
colors = ["blue", "lime", "red"]
vmin = 0
vmax = 10000
m.add_colorbar(colors=colors, vmin=vmin, vmax=vmax)
m.add_title("World Population Heat Map", font_size="20px", align="center")
m
Out[21]:
Save map to HTML¶
In [22]:
Copied!
m = leafmap.Map()
m.add_basemap("Esri.NatGeoWorldMap")
m
m = leafmap.Map()
m.add_basemap("Esri.NatGeoWorldMap")
m
Out[22]:
In [23]:
Copied!
m.to_html("mymap.html")
m.to_html("mymap.html")
Add Planet imagery¶
In [24]:
Copied!
os.environ["PLANET_API_KEY"] = "your-api-key"
os.environ["PLANET_API_KEY"] = "your-api-key"
In [25]:
Copied!
m = leafmap.Map()
m.add_planet_by_month(year=2020, month=8)
m.add_planet_by_quarter(year=2019, quarter=2)
m
m = leafmap.Map()
m.add_planet_by_month(year=2020, month=8)
m.add_planet_by_quarter(year=2019, quarter=2)
m
Out[25]: