102 fused
Mapping Overture Buildings and Foursquare Places with Leafmap + Fused
Fetch data by calling Fused UDFs and render it on a Leafmap map.
This notebook shows how you can:
- Load Overture Buildings for a drawn area
- Load Foursquare (FSQ) Place POIs for a drawn area
In [ ]:
Copied!
# %pip install fused>=1.11.1 "leafmap[maplibre]" -q
# %pip install fused>=1.11.1 "leafmap[maplibre]" -q
In [ ]:
Copied!
import os
import pandas as pd
import geopandas as gpd
from shapely.geometry import shape
import fused
import leafmap
import leafmap.maplibregl as maplibre
import os
import pandas as pd
import geopandas as gpd
from shapely.geometry import shape
import fused
import leafmap
import leafmap.maplibregl as maplibre
1. Load Overture Buildings for drawn area¶
First draw a bounding box over an area of interest (AOI) and create a GeoDataFrame with it. Then, run the Overture UDF to fetch data for that AOI.
1.1 First, draw an AOI¶
In [ ]:
Copied!
m = leafmap.Map(center=[40.713370, -73.996181], zoom=14)
m
m = leafmap.Map(center=[40.713370, -73.996181], zoom=14)
m
In [ ]:
Copied!
# If no AOI is drawn, use the default AOI
if m.user_roi is None:
m.user_roi = {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[-74.025621, 40.699967],
[-74.025621, 40.730283],
[-73.966055, 40.730283],
[-73.966055, 40.699967],
[-74.025621, 40.699967],
]
],
},
}
# If no AOI is drawn, use the default AOI
if m.user_roi is None:
m.user_roi = {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[-74.025621, 40.699967],
[-74.025621, 40.730283],
[-73.966055, 40.730283],
[-73.966055, 40.699967],
[-74.025621, 40.699967],
]
],
},
}
1.2 Then, convert it to a GeoDataFrame.¶
In [ ]:
Copied!
import geopandas as gpd
# Convert drawing to GeoDataFrame
gdf_drawing = gpd.GeoDataFrame(
[m.user_roi["properties"]], geometry=[shape(m.user_roi["geometry"])]
)
gdf_drawing
import geopandas as gpd
# Convert drawing to GeoDataFrame
gdf_drawing = gpd.GeoDataFrame(
[m.user_roi["properties"]], geometry=[shape(m.user_roi["geometry"])]
)
gdf_drawing
1.3 Fetch Overture Buildings for the AOI and render them on the map¶
The data comes from calling the "Overture Maps Example" public UDF with fused.run
.
In [ ]:
Copied!
# Fetch Overture Buildings GDF with Fused UDF
gdf = fused.run("UDF_Overture_Maps_Example", bbox=gdf_drawing)
# Fetch Overture Buildings GDF with Fused UDF
gdf = fused.run("UDF_Overture_Maps_Example", bbox=gdf_drawing)
In [ ]:
Copied!
gdf.head()
gdf.head()
In [ ]:
Copied!
print(f"The number of buildings within the AOI: {len(gdf)}")
print(f"The number of buildings within the AOI: {len(gdf)}")
In [ ]:
Copied!
# Add GDF to map
m.add_gdf(
gdf[["id", "geometry", "height", "class"]],
layer_type="fill",
name="Buildings",
fill_colors=["red"],
)
# Add GDF to map
m.add_gdf(
gdf[["id", "geometry", "height", "class"]],
layer_type="fill",
name="Buildings",
fill_colors=["red"],
)
1.4 Visualize buildings in 3D.¶
In [ ]:
Copied!
m = maplibre.Map(pitch=60)
m.add_basemap("USGS.USImagery")
gdf_filter = gdf[gdf.height > 0][["id", "geometry", "height", "class"]]
m.add_data(gdf_filter, column="height", cmap="Blues", extrude=True, name="Buildings")
m
m = maplibre.Map(pitch=60)
m.add_basemap("USGS.USImagery")
gdf_filter = gdf[gdf.height > 0][["id", "geometry", "height", "class"]]
m.add_data(gdf_filter, column="height", cmap="Blues", extrude=True, name="Buildings")
m
In [ ]:
Copied!
m.layer_interact()
m.layer_interact()
2. Load FSQ Places POIs for drawn area¶
2.1 Fetch FSQ Places for the AOI (defined above) and render them on the map¶
The data comes from calling the "Foursquare Open Source Places" public UDF with fused.run
.
In [ ]:
Copied!
# Fetch FSQ GDF with Fused UDF
gdf = fused.run("UDF_Foursquare_Open_Source_Places", bbox=gdf_drawing)
# `add_points_from_xy` requires latitude and longitude columns
gdf["latitude"] = gdf.geometry.y
gdf["longitude"] = gdf.geometry.x
# Show place categories
set(gdf.level1_category_name.values)
# Fetch FSQ GDF with Fused UDF
gdf = fused.run("UDF_Foursquare_Open_Source_Places", bbox=gdf_drawing)
# `add_points_from_xy` requires latitude and longitude columns
gdf["latitude"] = gdf.geometry.y
gdf["longitude"] = gdf.geometry.x
# Show place categories
set(gdf.level1_category_name.values)
In [ ]:
Copied!
# Subsample the gdf
gdf_sample = (
gdf[["name", "latitude", "longitude", "geometry"]].head(200).reset_index(drop=True)
)
# gdf_sample = gdf[gdf.level1_category_name == 'Dining and Drinking'][['name', 'latitude', 'longitude']].head(1000).reset_index(drop=True)
gdf_sample
# Subsample the gdf
gdf_sample = (
gdf[["name", "latitude", "longitude", "geometry"]].head(200).reset_index(drop=True)
)
# gdf_sample = gdf[gdf.level1_category_name == 'Dining and Drinking'][['name', 'latitude', 'longitude']].head(1000).reset_index(drop=True)
gdf_sample
2.2 Visualize the POIs on a 2d map¶
In [ ]:
Copied!
m = leafmap.Map(center=[40.713370, -73.996181], zoom=14)
# Add as marker cluster
m.add_points_from_xy(
gdf_sample,
x="longitude",
y="latitude",
spin=False,
add_legend=True,
)
m
m = leafmap.Map(center=[40.713370, -73.996181], zoom=14)
# Add as marker cluster
m.add_points_from_xy(
gdf_sample,
x="longitude",
y="latitude",
spin=False,
add_legend=True,
)
m
2.3 Visualize POIs with OpenStreetMap vector tiles¶
In [ ]:
Copied!
m = maplibre.Map(pitch=60, style="https://maps.gishub.org/styles/openstreetmap.json")
m.add_gdf(gdf_sample, name="Places")
m
m = maplibre.Map(pitch=60, style="https://maps.gishub.org/styles/openstreetmap.json")
m.add_gdf(gdf_sample, name="Places")
m
2.4 Visualize the POIs on a 3D map with Overture buildings¶
In [ ]:
Copied!
m = maplibre.Map(zoom=18, pitch=60, style="positron")
m.add_overture_3d_buildings()
m.add_gdf(gdf_sample, name="Places")
m
m = maplibre.Map(zoom=18, pitch=60, style="positron")
m.add_overture_3d_buildings()
m.add_gdf(gdf_sample, name="Places")
m
2.5 Visualize the POIs on a 3D map with OpenFreeMap vector tiles¶
In [ ]:
Copied!
m = maplibre.Map(pitch=60, style="liberty")
m.add_gdf(gdf_sample, name="Places")
m
m = maplibre.Map(pitch=60, style="liberty")
m.add_gdf(gdf_sample, name="Places")
m