Skip to content

Commit

Permalink
Add Fused notebook example
Browse files Browse the repository at this point in the history
  • Loading branch information
giswqs committed Dec 4, 2024
1 parent 33f1c4b commit 191fc2c
Show file tree
Hide file tree
Showing 2 changed files with 353 additions and 0 deletions.
351 changes: 351 additions & 0 deletions docs/notebooks/102_fused.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,351 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "d9e87e1d",
"metadata": {},
"source": [
"[![image](https://jupyterlite.rtfd.io/en/latest/_static/badge.svg)](https://demo.leafmap.org/lab/index.html?path=notebooks/102_fused.ipynb)\n",
"[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/opengeos/leafmap/blob/master/docs/notebooks/102_fused.ipynb)\n",
"[![image](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/opengeos/leafmap/HEAD)\n",
"\n",
"**Mapping Overture Buildings and Foursquare Places with Leafmap + Fused**\n",
"\n",
"Fetch data by calling [Fused UDFs](https://docs.fused.io/core-concepts/write/) and render it on a [Leafmap](https://leafmap.org/) map.\n",
"\n",
"This notebook shows how you can:\n",
"\n",
"1. Load [Overture Buildings](https://docs.overturemaps.org/getting-data/fused) for a drawn area\n",
"2. Load [Foursquare (FSQ) Place](https://docs.foursquare.com/data-products/docs/access-fsq-os-places) POIs for a drawn area"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d3148feb",
"metadata": {},
"outputs": [],
"source": [
"# %pip install fused>=1.11.1 \"leafmap[maplibre]\" -q"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fd0abbfd",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import pandas as pd\n",
"import geopandas as gpd\n",
"from shapely.geometry import shape\n",
"import fused\n",
"import leafmap\n",
"import leafmap.maplibregl as maplibre"
]
},
{
"cell_type": "markdown",
"id": "184a459c",
"metadata": {},
"source": [
"# 1. Load Overture Buildings for drawn area\n",
"\n",
"First draw a bounding box over an area of interest (AOI) and create a GeoDataFrame with it. Then, [run the Overture UDF](https://docs.fused.io/core-concepts/run/) to fetch data for that AOI.\n",
"\n",
"## 1.1 First, draw an AOI"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3739c910",
"metadata": {},
"outputs": [],
"source": [
"m = leafmap.Map(center=[40.713370, -73.996181], zoom=14)\n",
"m"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2eb9ff57",
"metadata": {},
"outputs": [],
"source": [
"# If no AOI is drawn, use the default AOI\n",
"\n",
"if m.user_roi is None:\n",
" m.user_roi = {'type': 'Feature',\n",
" 'properties': {},\n",
" 'geometry': {'type': 'Polygon',\n",
" 'coordinates': [[[-74.025621, 40.699967],\n",
" [-74.025621, 40.730283],\n",
" [-73.966055, 40.730283],\n",
" [-73.966055, 40.699967],\n",
" [-74.025621, 40.699967]]]}}"
]
},
{
"cell_type": "markdown",
"id": "40520eaf",
"metadata": {},
"source": [
"## 1.2 Then, convert it to a GeoDataFrame."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ef8f03fe",
"metadata": {},
"outputs": [],
"source": [
"import geopandas as gpd\n",
"\n",
"# Convert drawing to GeoDataFrame\n",
"gdf_drawing = gpd.GeoDataFrame([m.user_roi['properties']], geometry=[shape(m.user_roi['geometry'])])\n",
"\n",
"gdf_drawing"
]
},
{
"cell_type": "markdown",
"id": "d25258d6",
"metadata": {},
"source": [
"## 1.3 Fetch Overture Buildings for the AOI and render them on the map\n",
"\n",
"The data comes from calling the \"[Overture Maps Example](https://www.fused.io/workbench/catalog/Overture_Maps_Example-64071fb8-2c96-4015-adb9-596c3bac6787)\" public UDF with `fused.run`."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "96dba5c5",
"metadata": {},
"outputs": [],
"source": [
"# Fetch Overture Bulidings GDF with Fused UDF\n",
"gdf = fused.run(\"UDF_Overture_Maps_Example\", bbox=gdf_drawing)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6ac749c2",
"metadata": {},
"outputs": [],
"source": [
"gdf.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "64621eea",
"metadata": {},
"outputs": [],
"source": [
"print(f\"The number of buildings within the AOI: {len(gdf)}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "947b3e4b",
"metadata": {},
"outputs": [],
"source": [
"# Add GDF to map\n",
"m.add_gdf(gdf[['id', 'geometry', 'height', 'class']], layer_type=\"fill\", name=\"Buildings\", fill_colors=[\"red\"])"
]
},
{
"cell_type": "markdown",
"id": "3c4e67d8",
"metadata": {},
"source": [
"## 1.4 Visualize buildings in 3D."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c223690b",
"metadata": {},
"outputs": [],
"source": [
"m = maplibre.Map(pitch=60)\n",
"m.add_basemap(\"USGS.USImagery\")\n",
"gdf_filter = gdf[gdf.height > 0][['id', 'geometry', 'height', 'class']]\n",
"m.add_data(gdf_filter, column=\"height\", cmap=\"Blues\", extrude=True, name=\"Buildings\")\n",
"m"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a0acf10c",
"metadata": {},
"outputs": [],
"source": [
"m.layer_interact()"
]
},
{
"cell_type": "markdown",
"id": "09bc22dd",
"metadata": {},
"source": [
"# 2. Load FSQ Places POIs for drawn area\n",
"\n",
"## 2.1 Fetch FSQ Places for the AOI (defined above) and render them on the map\n",
"\n",
"The data comes from calling the \"[Foursquare Open Source Places](https://www.fused.io/workbench/catalog/Foursquare_Open_Source_Places-5cd75ead-e319-4279-8efc-04276de145bc)\" public UDF with `fused.run`."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a41a2213",
"metadata": {},
"outputs": [],
"source": [
"# Fetch FSQ GDF with Fused UDF\n",
"gdf = fused.run(\"UDF_Foursquare_Open_Source_Places\", bbox=gdf_drawing)\n",
"\n",
"# `add_points_from_xy` requires latitude and longitude columns\n",
"gdf['latitude'] = gdf.geometry.y\n",
"gdf['longitude'] = gdf.geometry.x\n",
"\n",
"# Show place categories\n",
"set(gdf.level1_category_name.values)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "985ac6ce",
"metadata": {},
"outputs": [],
"source": [
"# Subsample the gdf\n",
"gdf_sample = gdf[['name', 'latitude', 'longitude', 'geometry']].head(200).reset_index(drop=True)\n",
"# gdf_sample = gdf[gdf.level1_category_name == 'Dining and Drinking'][['name', 'latitude', 'longitude']].head(1000).reset_index(drop=True)\n",
"gdf_sample"
]
},
{
"cell_type": "markdown",
"id": "41986696",
"metadata": {},
"source": [
"## 2.2 Visualize the POIs on a 2d map"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "48aab2d9",
"metadata": {},
"outputs": [],
"source": [
"m = leafmap.Map(center=[40.713370, -73.996181], zoom=14)\n",
"# Add as marker cluster\n",
"m.add_points_from_xy(\n",
" gdf_sample,\n",
" x=\"longitude\",\n",
" y=\"latitude\",\n",
" spin=False,\n",
" add_legend=True,\n",
")\n",
"m"
]
},
{
"cell_type": "markdown",
"id": "c5343bb9",
"metadata": {},
"source": [
"## 2.3 Visualize POIs with OpenStreetMap vector tiles"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f0f793e4",
"metadata": {},
"outputs": [],
"source": [
"m = maplibre.Map(pitch=60, style=\"https://maps.gishub.org/styles/openstreetmap.json\")\n",
"m.add_gdf(gdf_sample, name=\"Places\")\n",
"m"
]
},
{
"cell_type": "markdown",
"id": "87aa9834",
"metadata": {},
"source": [
"## 2.4 Visualize the POIs on a 3D map with Overture buildings"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b76e9ee7",
"metadata": {},
"outputs": [],
"source": [
"m = maplibre.Map(zoom=18, pitch=60, style=\"positron\")\n",
"m.add_overture_3d_buildings()\n",
"m.add_gdf(gdf_sample, name=\"Places\")\n",
"m"
]
},
{
"cell_type": "markdown",
"id": "0290ec5f",
"metadata": {},
"source": [
"## 2.5 Visualize the POIs on a 3D map with [OpenFreeMap](https://openfreemap.org/) vector tiles"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "212752b7",
"metadata": {},
"outputs": [],
"source": [
"m = maplibre.Map(pitch=60, style=\"liberty\")\n",
"m.add_gdf(gdf_sample, name=\"Places\")\n",
"m"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
2 changes: 2 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ plugins:
"notebooks/94_mapbox.ipynb",
"notebooks/99_wetlands.ipynb",
"notebooks/101_nasa_opera.ipynb",
"notebooks/102_fused.ipynb",
]

markdown_extensions:
Expand Down Expand Up @@ -337,3 +338,4 @@ nav:
- notebooks/99_wetlands.ipynb
- notebooks/100_nlcd.ipynb
- notebooks/101_nasa_opera.ipynb
- notebooks/102_fused.ipynb

0 comments on commit 191fc2c

Please sign in to comment.