Single geometry column input expressions
Expressions involving a single geometry column
Spatial polars has many expressions which perform one of shapely's operations or return a property from each geometry in the series.
To use these expressions which are called from a single geometry column, simply call the expression from the geometry column of the dataframe.
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.
import geodatasets
import polars as pl
from spatial_polars import scan_spatial
df = (
scan_spatial(
geodatasets.get_path("geoda.guerry") # (1)!
)
.select(
pl.col("geometry").spatial.area().alias("polyogn_area"), # (2)!
pl.col("geometry").spatial.length().alias("polyogn_perimeter"), # (3)!
pl.col("geometry").spatial.bounds().alias("bounds"), # (4)!
pl.col("geometry").spatial.centroid().alias("centroid"), # (5)!
pl.col("geometry"), # (6)!
)
.collect(engine="streaming")
) # (7)!
print(df)
- Scan some polygon data
- Compute .area() for each polygon aliased as 'polygon_area'.
- Compute polygon perimeter .length() for each polygon aliased as 'polygon_perimeter'
- Compute polygon extent/bounds .bounds() for each polygon aliased as 'bounds'
- Compute polygon .centroid() for each polygon aliased as 'centroid'
- Select the geometry column for our results
- Collect the query to return a dataframe
As you can see in the results below, all these expressions (and many others in the API) are executed on a geometry column but depending on the expression different datatypes are returned
shape: (85, 5)
┌──────────────┬───────────────────┬───────────────────────────┬───────────────────────────┬───────────────────────────┐
│ polyogn_area ┆ polyogn_perimeter ┆ bounds ┆ centroid ┆ geometry │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ f64 ┆ f64 ┆ array[f64, 4] ┆ struct[2] ┆ struct[2] │
╞══════════════╪═══════════════════╪═══════════════════════════╪═══════════════════════════╪═══════════════════════════╡
│ 5.7843e9 ┆ 478204.340576 ┆ [785562.0, 2.073221e6, … ┆ {b"\x01\x01\x00\x00\x00lA ┆ {b"\x01\x03\x00\x00\x00\x │
│ ┆ ┆ 2.172… ┆ \xbc\… ┆ 01\x0… │
│ 7.4359e9 ┆ 597865.43152 ┆ [645357.0, 2.42727e6, … ┆ {b"\x01\x01\x00\x00\x00i\ ┆ {b"\x01\x03\x00\x00\x00\x │
│ ┆ ┆ 2.5645… ┆ xa90A… ┆ 02\x0… │
│ 7.3786e9 ┆ 539122.876967 ┆ [595532.0, 2.10432e6, … ┆ {b"\x01\x01\x00\x00\x00\x ┆ {b"\x01\x03\x00\x00\x00\x │
│ ┆ ┆ 2.2006… ┆ 0eE\x… ┆ 01\x0… │
│ 7.0098e9 ┆ 573525.139332 ┆ [853311.0, 1.858801e6, … ┆ {b"\x01\x01\x00\x00\x00$y ┆ {b"\x01\x03\x00\x00\x00\x │
│ ┆ ┆ 1.972… ┆ G\x80… ┆ 01\x0… │
│ 5.6966e9 ┆ 572041.370424 ┆ [845528.0, 1.915176e6, … ┆ {b"\x01\x01\x00\x00\x00\x ┆ {b"\x01\x03\x00\x00\x00\x │
│ ┆ ┆ 2.022… ┆ d3\xd… ┆ 01\x0… │
│ … ┆ … ┆ … ┆ … ┆ … │
│ 6.7707e9 ┆ 570165.566743 ┆ [238257.0, 2.147245e6, … ┆ {b"\x01\x01\x00\x00\x00N\ ┆ {b"\x01\x06\x00\x00\x00\x │
│ ┆ ┆ 2.237… ┆ xe6S\… ┆ 03\x0… │
│ 7.0338e9 ┆ 558978.039694 ┆ [414673.0, 2.118365e6, … ┆ {b"\x01\x01\x00\x00\x00\x ┆ {b"\x01\x03\x00\x00\x00\x │
│ ┆ ┆ 2.244… ┆ ae\xd… ┆ 01\x0… │
│ 5.5602e9 ┆ 492101.970229 ┆ [467110.0, 2.049172e6, … ┆ {b"\x01\x01\x00\x00\x00\x ┆ {b"\x01\x03\x00\x00\x00\x │
│ ┆ ┆ 2.156… ┆ a4@\x… ┆ 01\x0… │
│ 5.9006e9 ┆ 553742.211363 ┆ [826426.0, 2.322215e6, … ┆ {b"\x01\x01\x00\x00\x00Gt ┆ {b"\x01\x03\x00\x00\x00\x │
│ ┆ ┆ 2.401… ┆ 1\xaa… ┆ 01\x0… │
│ 7.4605e9 ┆ 582958.399801 ┆ [638420.0, 2.258284e6, … ┆ {b"\x01\x01\x00\x00\x00\x ┆ {b"\x01\x03\x00\x00\x00\x │
│ ┆ ┆ 2.378… ┆ fd\xf… ┆ 01\x0… │
└──────────────┴───────────────────┴───────────────────────────┴───────────────────────────┴───────────────────────────┘