-
Notifications
You must be signed in to change notification settings - Fork 21
Docs add lonboard tutorial #529
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aboydnw
wants to merge
4
commits into
microsoft:develop
Choose a base branch
from
aboydnw:docs-add-lonboard-tutorial
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+125
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bf0753a
docs: add Lonboard tutorial under overview
aboydnw df86c30
feat: pivot Lonboard tutorial to vector (building footprints)
aboydnw 2bc2377
style: remove em dashes and ellipsis header from Lonboard tutorial
aboydnw 5c1d041
docs: address Lonboard tutorial review
aboydnw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| # Visualizing Planetary Computer data with Lonboard | ||
|
|
||
| [Lonboard](https://developmentseed.org/lonboard/) is a Python library for interactive geospatial visualization in Jupyter. It renders large vector datasets on a GPU-accelerated WebGL map directly in the notebook, with no tile server in the loop. Geometries stream to the browser as [Apache Arrow](https://arrow.apache.org/), so hundreds of thousands of features stay interactive. Layers compose: stack footprints, points, and analysis results in a single map. | ||
|
|
||
| A companion notebook walks through every step end-to-end with live maps. [Open in Planetary Computer Hub](https://pccompute.westeurope.cloudapp.azure.com/compute/hub/user-redirect/git-pull?repo=https://github.com/microsoft/PlanetaryComputerExamples&urlpath=lab/tree/PlanetaryComputerExamples/quickstarts/lonboard.ipynb&branch=main) | ||
|
|
||
| ## Install Lonboard | ||
|
|
||
| ```bash | ||
| uv add lonboard pystac-client planetary-computer geopandas deltalake adlfs mercantile | ||
| ``` | ||
|
|
||
| `pystac-client` queries the Planetary Computer STAC API; `planetary-computer` signs asset URLs; `deltalake` opens the Delta Table partition for the dataset; `geopandas` + `adlfs` read the parquet files straight off Azure Blob; `mercantile` converts a longitude/latitude to a quadkey for the search query. | ||
|
|
||
| ## Connect to the Planetary Computer STAC catalog | ||
|
|
||
| Set up the catalog client with PC's signer so every search result has a signed asset href: | ||
|
|
||
| ```python | ||
| import pystac_client | ||
| import planetary_computer | ||
|
|
||
| catalog = pystac_client.Client.open( | ||
| "https://planetarycomputer.microsoft.com/api/stac/v1", | ||
| modifier=planetary_computer.sign_inplace, | ||
| ) | ||
| ``` | ||
|
|
||
| `modifier=planetary_computer.sign_inplace` signs every asset as the search returns. | ||
|
|
||
| ## Find the building-footprints partition for Portland | ||
|
|
||
| We'll render [Microsoft Building Footprints](https://planetarycomputer.microsoft.com/dataset/ms-buildings), a dataset partitioned by [quadkey](https://learn.microsoft.com/en-us/bingmaps/articles/bing-maps-tile-system). Use `mercantile` to convert a Portland coordinate to a zoom-9 quadkey, then fetch the STAC item whose partition covers it: | ||
|
|
||
| ```python | ||
| import mercantile | ||
|
|
||
| tile = mercantile.tile(-122.66, 45.52, 9) | ||
| quadkey = mercantile.quadkey(*tile) | ||
|
|
||
| item = next(catalog.search( | ||
| collections=["ms-buildings"], | ||
| query={ | ||
| "msbuildings:region": {"eq": "UnitedStates"}, | ||
| "msbuildings:quadkey": {"eq": quadkey}, | ||
| }, | ||
| ).items()) | ||
| asset = item.assets["data"] | ||
| ``` | ||
|
|
||
| ## Load the footprints into a GeoDataFrame | ||
|
|
||
| The asset is a Delta Table partition on Azure Blob. Open it with `deltalake`, enumerate the parquet files in the partition, then read each one with `geopandas`. Clip to the Portland metro for a focused view: | ||
|
|
||
| ```python | ||
| import geopandas as gpd | ||
| import pandas as pd | ||
| from deltalake import DeltaTable | ||
|
|
||
| storage_options = { | ||
| "account_name": asset.extra_fields["table:storage_options"]["account_name"], | ||
| "sas_token": asset.extra_fields["table:storage_options"]["credential"], | ||
| } | ||
| table = DeltaTable(asset.href, storage_options=storage_options) | ||
| gdf = pd.concat([ | ||
| gpd.read_parquet(uri, storage_options=storage_options) | ||
| for uri in table.file_uris() | ||
| ]) | ||
| gdf = gdf.cx[-122.85:-122.45, 45.42:45.62] | ||
|
|
||
| len(gdf) # a few hundred thousand buildings | ||
| ``` | ||
|
Comment on lines
+51
to
+72
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd recommend following the behavior of the existing notebook to load this data. We don't need |
||
|
|
||
| ## Render the footprints | ||
|
|
||
| [`PolygonLayer.from_geopandas()`](https://developmentseed.org/lonboard/latest/api/layers/polygon-layer/#lonboard.PolygonLayer.from_geopandas) uploads the geometry to the GPU as Arrow. Drawing the footprints as outlines keeps every building legible at city scale, and the map stays fully interactive with no tile server in the loop: | ||
|
|
||
| ```python | ||
| from lonboard import Map, PolygonLayer | ||
|
|
||
| layer = PolygonLayer.from_geopandas( | ||
| gdf, | ||
| get_line_color=[230, 100, 0], | ||
| filled=False, | ||
| line_width_min_pixels=0.5, | ||
| ) | ||
| Map(layer, view_state={"longitude": -122.66, "latitude": 45.52, "zoom": 12}) | ||
| ``` | ||
|
|
||
| ```{image} images/lonboard-buildings-portland.png | ||
| :height: 500 | ||
| :name: Lonboard building footprints over Portland | ||
| :class: no-scaled-link | ||
| ``` | ||
|
|
||
| ## Color by building height | ||
|
|
||
| Each footprint carries a `meanHeight`. Map it through a continuous colormap and recolor the layer in place: data-driven styling across the whole dataset, evaluated on the GPU: | ||
|
|
||
| ```python | ||
| import matplotlib as mpl | ||
| from lonboard.colormap import apply_continuous_cmap | ||
|
|
||
| heights = gdf["meanHeight"].clip(0, 30) | ||
| normalized = (heights - heights.min()) / (heights.max() - heights.min()) | ||
|
|
||
| layer.get_line_color = apply_continuous_cmap( | ||
| normalized.to_numpy(), mpl.colormaps["plasma"] | ||
| ) | ||
| layer.line_width_min_pixels = 1.5 | ||
| ``` | ||
|
|
||
| `plasma` shades low buildings purple and tall ones yellow. Reassigning the property mutates the existing map without re-uploading geometry. | ||
|
|
||
| ```{image} images/lonboard-buildings-by-height.png | ||
| :height: 420 | ||
| :name: Lonboard building footprints colored by height | ||
| :class: no-scaled-link | ||
| ``` | ||
|
|
||
| ## When to use something else | ||
|
|
||
| Lonboard's surface is the notebook. For pixel-level *raster* analysis in Python (window reads, overview traversal), use [async-geotiff](https://github.com/developmentseed/async-geotiff). For a standalone web app instead of a notebook, the [deck.gl-raster](https://github.com/developmentseed/deck.gl-raster) renderer is available in TypeScript. For shareable tile endpoints consumed by third-party frontends, see [titiler](https://developmentseed.org/titiler/). | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: I think
Azure Blob storagereads better than justAzure Blob