PolygonLayer (polygons)
In this notebook we will demonstrate using spatial polars to visualize spatial data on a Lonboard map using a polygon layer to visualize the polygons.
Note
This example makes use the geodatasets python package to access some spatial data easily.
Calling geodatasets.get_path() will download data the specified data to the machine and return the path to the downloaded file. If the file has already been downloaded it will simply return the path to the file.
See downloading and caching for further details.
Creating a PolygonLayer
The following cell will read the nyc earnings dataset into a dataframe and create a Lonboard PolygonLayer
I don't know much about the dataset but my understanding is that the CE03_* columns contain the number of jobs in within the polygon that earn more than $3333/month and all the polygons are in the New York City area, and it looks like "COUNTYFP10"==61 just gives us the polygons in Manhattan. The content of the data isn't super critical, we're here just to show how we can use spatial polars to make a lonboard polygon layer that looks really nice.
import geodatasets
from lonboard import Map
from palettable.colorbrewer.diverging import RdYlGn_11
import polars as pl
from spatial_polars import scan_spatial
nyc_earnings_df = (
scan_spatial(geodatasets.get_path("geoda.nyc_earnings")) # (1)!
.filter(pl.col("COUNTYFP10") == 61) # (2)!
.collect(engine="streaming")
)
polygon_layer = nyc_earnings_df.spatial.to_polygonlayer( # (3)!
fill_cmap_col="CE03_14", # (4)!
fill_cmap_type="continuous", # (5)!
fill_cmap=RdYlGn_11, # (6)!
fill_normalize_cmap_col=True, # (7)!
elevation="CE03_14", # (8)!
wireframe=True, # (9)!
)
polygonlayer_map = Map(layers=[polygon_layer]) # (10)!
polygonlayer_map
- Scan the geoda.nyc_earnings geodataset
- Filter to just the rows in manhattan
- Create a polygonlayer
- Use the values from the CE03_14 column to base our fill colors
- The values in the column are continuous, not discreete categories, so we will use
fill_cmap_type="continuous" - Palettable's RdYlGn_11 color map will be used for the colors of the polygons
- The values in the column are not all between 0-1 so we will use
fill_normalize_cmap_col=True(True is the default, so we don't really need to specify it, but it's important so I wanted to call attention to it) which will scale a copy of the numbers in the column to be between 0-1 before using Lonboard's apply_continuous_cmap function to set the color of each polygon. If our column contained values that were all between 0-1 we could have usedfill_normalize_cmap_col=False. - Use the "CE03_14" column to base our elevations of our polygons, so the different polygons will stick out of the map corresponding to the values in that column of our dataframe, Specifying the column name for the elevation will set the
extrudedproperty on the layer to be True. - To make the edges of extruded polygons look really sharp, we'll set
wireframe=True. - display the polygonlayer on a map
Because the data uses a coordinate reference system other than EPSG:4326, Lonboard will emit a warning that our input data is going to be re-projected to EPSG:4326. To avoid that warning we could have reprojected the data in the dataframe with the spatial.reproject() expression on the geometry column before creating the PolygonLayer.
Tip
Right clicking, and moving your mouse on the lonboard map will allow you to tilt the view angle and really make those buildings look neat!
Note
If you're interested in using a categorical colormap instead of a continuous, head over to the scatterplotlayer example to check out how we can do it.