Geometry column and scalar geometry input expressions
Expressions involving a geometry column and a scalar geometry
Spatial polars has many expressions which can be called from a geometry column but need another geometry to perform the operation.
If the desired output is to compute the operation on all the geometries in the series along with another single geometry, we can call the expression from the geometry column in our dataframe and provide the scalar geometry to the expression
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.
In the example below we'll use the distance expression to compute the distance of the counties in thelower 48 states of the USA to the gateway Arch in St. Louis Mo. Then to give some context to the numbers, we'll display the data on a Lonboard map symbolized by the distance we calculated.
import geodatasets
import polars as pl
import shapely
from lonboard import Map
from palettable.colorbrewer.diverging import RdYlGn_11
from spatial_polars import scan_spatial
arch_pt = shapely.Point(-90.18497, 38.62456) # (1)!
df = (
scan_spatial(geodatasets.get_path("geoda.health")) # (2)!
.select(
"countyname", # (3)!
"geometry", # (4)!
pl.col("geometry").spatial.distance(arch_pt).alias("dist_to_arch"), # (5)!
)
.collect(engine="streaming") # (6)!
)
polygon_layer = df.spatial.to_polygonlayer( # (7)!
fill_cmap_col="dist_to_arch",
fill_cmap_type="continuous",
fill_cmap=RdYlGn_11,
fill_normalize_cmap_col=True,
line_width_min_pixels=0.5,
)
arch_map = Map(layers=[polygon_layer]) # (8)!
arch_map
- The point we want to compute the distance to for each of the polygons in our dataframe
- Scan some polygon data
- Select the county name
- Select the geometry column (so we can show the data on a map)
- Compute the distance of each polygon in the dataframe to our point
- Collect the query to create a dataframe
- Create a lonboard polygon layer that's symbolized by the distance we computed, making the smaller numbers(closer to the arch) red, and larger ones (further from the arch) green
- Add the layer to a map and show the results