Reprojection
Spatial polars is happy to reproject your data using pyproj in the exact same manner as geopandas using the .reproject 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.
To reproject data into a new CRS you can use the .spatial.reproject()
expression
Reprojecting data to a new CRS
import geodatasets
import polars as pl
from spatial_polars import scan_spatial
geom = (
scan_spatial(
geodatasets.get_path("geoda.nyc_earnings") # (1)!
)
.select(
pl.col("geometry").spatial.to_shapely_array() # (2)!
)
.head(1)
.collect()
.item()
) # (3)!
print(f"Without reprojecting centroid of first geometry: {geom.centroid.coords[0]}")
geom = (
scan_spatial(
geodatasets.get_path("geoda.nyc_earnings") # (4)!
)
.select(
pl.col("geometry").spatial.reproject(4326).spatial.to_shapely_array() # (5)!
)
.head(1)
.collect()
.item()
) # (6)!
print(f"With reprojecting centroid of first geometry: {geom.centroid.coords[0]}")
-
Scan the nyc_earnings geodataset
Note
This data is in the USA_Contiguous_Albers_Equal_Area_Conic projection
-
Select the geometry column and convert it to shapely geometry objects
- Collect the first row of the dataframe and grab the item from the dataframe since it only has one column and one row
- Scan the same dataset as above
- Select the geometry column reproject it to WGS84 and convert it to shapely geometry objects
- Collect the first row of the dataframe and grab the item from the dataframe since it only has one column and one row