IO
scan_spatial(path_or_buffer, layer=None, encoding=None, bbox=None, mask=None)
Scans a data source supported by pyogrio or a geoparquet file to produce a polars LazyFrame.
Note
Although geoparquet is supported, this implementation, in its current state, leaves a lot to be desired.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path_or_buffer
|
str | Path | BytesIO
|
A dataset path or URI, raw buffer, or file-like object with a read method. |
required |
layer
|
Optional[str | int]
|
If an integer is provided, it corresponds to the index of the layer with the data source. If a string is provided, it must match the name of the layer in the data source. Defaults to first layer in data source. |
None
|
encoding
|
Optional[str]
|
If present, will be used as the encoding for reading string values from the data source. By default will automatically try to detect the native encoding and decode to UTF-8. |
None
|
bbox
|
Optional[tuple[float, float, float, float]]
|
If present, will be used to filter records whose geometry intersects this box. This must be in the same CRS as the dataset. If GEOS is present and used by GDAL, only geometries that intersect this bbox will be returned; if GEOS is not available or not used by GDAL, all geometries with bounding boxes that intersect this bbox will be returned. Cannot be combined with mask keyword. Tuple should be in the format of (xmin, ymin, xmax, ymax). |
None
|
mask
|
Optional[Polygon]
|
If present, will be used to filter records whose geometry intersects this geometry. This must be in the same CRS as the dataset. If GEOS is present and used by GDAL, only geometries that intersect this geometry will be returned; if GEOS is not available or not used by GDAL, all geometries with bounding boxes that intersect the bounding box of this geometry will be returned. Requires Shapely >= 2.0. Cannot be combined with bbox keyword. |
None
|
Examples:
Scanning a layer from a geopackage:
>>> my_geopackage = r"c:\data\hiking_club.gpkg"
>>> lf = scan_spatial(my_geopackage, layer="hike")
>>> lf
naive plan: (run LazyFrame.explain(optimized=True) to see the optimized plan)
PYTHON SCAN []
PROJECT */4 COLUMNS
Scanning a shapefile:
>>> from spatial_polars import scan_spatial
>>> my_shapefile = r"c:\data\roads.shp"
>>> lf = scan_spatial(my_shapefile)
>>> lf
naive plan: (run LazyFrame.explain(optimized=True) to see the optimized plan)
PYTHON SCAN []
PROJECT */11 COLUMNS
Scanning a shapefile from within a zipped directory:
>>> zipped_shapefiles = r"C:\data\illinois-latest-free.shp.zip"
>>> lf = scan_spatial(zipped_shapefiles, layer="gis_osm_roads_free_1")
>>> lf
naive plan: (run LazyFrame.explain(optimized=True) to see the optimized plan)
PYTHON SCAN []
PROJECT */11 COLUMNS
Source code in src\spatial_polars\io.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
|
read_spatial(path_or_buffer, layer=None, encoding=None, bbox=None, mask=None)
Reads a spatial data source supported by pyogrio to produce a polars DataFrame.
Note
Although geoparquet is supported, this implementation, in its current state, leaves a lot to be desired.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path_or_buffer
|
str | Path | BytesIO
|
A dataset path or URI, raw buffer, or file-like object with a read method. |
required |
layer
|
Optional[str | int]
|
If an integer is provided, it corresponds to the index of the layer with the data source. If a string is provided, it must match the name of the layer in the data source. Defaults to first layer in data source. |
None
|
encoding
|
Optional[str]
|
If present, will be used as the encoding for reading string values from the data source. By default will automatically try to detect the native encoding and decode to UTF-8. |
None
|
bbox
|
Optional[tuple[float, float, float, float]]
|
If present, will be used to filter records whose geometry intersects this box. This must be in the same CRS as the dataset. If GEOS is present and used by GDAL, only geometries that intersect this bbox will be returned; if GEOS is not available or not used by GDAL, all geometries with bounding boxes that intersect this bbox will be returned. Cannot be combined with mask keyword. Tuple should be in the format of (xmin, ymin, xmax, ymax). |
None
|
mask
|
Optional[Polygon]
|
If present, will be used to filter records whose geometry intersects this geometry. This must be in the same CRS as the dataset. If GEOS is present and used by GDAL, only geometries that intersect this geometry will be returned; if GEOS is not available or not used by GDAL, all geometries with bounding boxes that intersect the bounding box of this geometry will be returned. Requires Shapely >= 2.0. Cannot be combined with bbox keyword. |
None
|
Examples:
Scanning a layer from a geopackage:
>>> from spatial_polars import read_spatial
>>> my_geopackage = r"c:\data\hiking_club.gpkg"
>>> df = read_spatial(my_geopackage, layer="hike")
>>> df
shape: (31, 4)
┌─────────────────────────────────┬────────────┬──────────┬─────────────────────────────────┐
│ LOCATION ┆ DATE ┆ DISTANCE ┆ geometry │
│ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ date ┆ f64 ┆ struct[2] │
╞═════════════════════════════════╪════════════╪══════════╪═════════════════════════════════╡
│ Watershed Nature Center ┆ 2023-01-14 ┆ 1.25 ┆ {b"\x01\x02\x00\x00\x00\xd8\x0… │
│ Ellis Island ┆ 2023-03-11 ┆ 2.25 ┆ {b"\x01\x02\x00\x00\x00\x82\x0… │
│ Cahokia Mounds State Historic … ┆ 2023-02-04 ┆ 1.75 ┆ {b"\x01\x02\x00\x00\x00\xb1\x0… │
│ Willoughby Heritage Farm ┆ 2022-12-03 ┆ 0.75 ┆ {b"\x01\x02\x00\x00\x00\xef\x0… │
│ Pere Marquette State Park ┆ 2022-10-15 ┆ 1.0 ┆ {b"\x01\x02\x00\x00\x002\x02\x… │
│ … ┆ … ┆ … ┆ … │
│ Haunted Glen Carbon ┆ 2024-10-19 ┆ 1.75 ┆ {b"\x01\x02\x00\x00\x00\x82\x0… │
│ Watershed Nature Center ┆ 2024-10-08 ┆ 1.0 ┆ {b"\x01\x02\x00\x00\x000\x00\x… │
│ Beaver Dam State Park ┆ 2024-10-26 ┆ 2.0 ┆ {b"\x01\x02\x00\x00\x00\xc4\x0… │
│ Willoughby Heritage Farm ┆ 2024-12-07 ┆ 1.5 ┆ {b"\x01\x02\x00\x00\x00>\x02\x… │
│ Cahokia Mounds State Historic … ┆ 2025-03-08 ┆ 1.75 ┆ {b"\x01\x02\x00\x00\x00\xeb\x0… │
└─────────────────────────────────┴────────────┴──────────┴─────────────────────────────────┘
Scanning a shapefile:
>>> my_shapefile = r"c:\data\roads.shp"
>>> df = read_spatial(my_shapefile)
>>> df
shape: (1_662_837, 11)
┌────────────┬──────┬─────────────┬─────────────┬───┬───────┬────────┬────────┬────────────────────┐
│ osm_id ┆ code ┆ fclass ┆ name ┆ … ┆ layer ┆ bridge ┆ tunnel ┆ geometry │
│ --- ┆ --- ┆ --- ┆ --- ┆ ┆ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ i32 ┆ str ┆ str ┆ ┆ i64 ┆ str ┆ str ┆ struct[2] │
╞════════════╪══════╪═════════════╪═════════════╪═══╪═══════╪════════╪════════╪════════════════════╡
│ 4265057 ┆ 5114 ┆ secondary ┆ 55th Street ┆ … ┆ 0 ┆ F ┆ F ┆ {b"\x01\x02\x00\x0 │
│ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ 0\x00\x03\x0… │
│ 4265058 ┆ 5114 ┆ secondary ┆ Fairview ┆ … ┆ 0 ┆ F ┆ F ┆ {b"\x01\x02\x00\x0 │
│ ┆ ┆ ┆ Avenue ┆ ┆ ┆ ┆ ┆ 0\x00\x0e\x0… │
│ 4267607 ┆ 5114 ┆ secondary ┆ 31st Street ┆ … ┆ 0 ┆ F ┆ F ┆ {b"\x01\x02\x00\x0 │
│ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ 0\x00\x02\x0… │
│ 4271616 ┆ 5115 ┆ tertiary ┆ 59th Street ┆ … ┆ 0 ┆ F ┆ F ┆ {b"\x01\x02\x00\x0 │
│ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ 0\x00\x15\x0… │
│ 4275365 ┆ 5122 ┆ residential ┆ 61st Street ┆ … ┆ 0 ┆ F ┆ F ┆ {b"\x01\x02\x00\x0 │
│ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ 0\x00"\x00\x… │
│ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … │
│ 1370383592 ┆ 5153 ┆ footway ┆ null ┆ … ┆ 0 ┆ F ┆ F ┆ {b"\x01\x02\x00\x0 │
│ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ 0\x00\x02\x0… │
│ 1370383593 ┆ 5153 ┆ footway ┆ null ┆ … ┆ 0 ┆ F ┆ F ┆ {b"\x01\x02\x00\x0 │
│ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ 0\x00\x07\x0… │
│ 1370383594 ┆ 5153 ┆ footway ┆ null ┆ … ┆ 0 ┆ F ┆ F ┆ {b"\x01\x02\x00\x0 │
│ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ 0\x00\x1c\x0… │
│ 1370383595 ┆ 5153 ┆ footway ┆ null ┆ … ┆ 0 ┆ F ┆ F ┆ {b"\x01\x02\x00\x0 │
│ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ 0\x00\x0b\x0… │
│ 1370398885 ┆ 5141 ┆ service ┆ null ┆ … ┆ 0 ┆ F ┆ F ┆ {b"\x01\x02\x00\x0 │
│ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ 0\x00\x02\x0… │
└────────────┴──────┴─────────────┴─────────────┴───┴───────┴────────┴────────┴────────────────────┘
Scanning a shapefile from within a zipped directory:
>>> zipped_shapefiles = r"C:\data\illinois-latest-free.shp.zip"
>>> df = read_spatial(zipped_shapefiles, layer="gis_osm_roads_free_1")
>>> df
shape: (1_662_837, 11)
┌────────────┬──────┬─────────────┬─────────────┬───┬───────┬────────┬────────┬────────────────────┐
│ osm_id ┆ code ┆ fclass ┆ name ┆ … ┆ layer ┆ bridge ┆ tunnel ┆ geometry │
│ --- ┆ --- ┆ --- ┆ --- ┆ ┆ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ i32 ┆ str ┆ str ┆ ┆ i64 ┆ str ┆ str ┆ struct[2] │
╞════════════╪══════╪═════════════╪═════════════╪═══╪═══════╪════════╪════════╪════════════════════╡
│ 4265057 ┆ 5114 ┆ secondary ┆ 55th Street ┆ … ┆ 0 ┆ F ┆ F ┆ {b"\x01\x02\x00\x0 │
│ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ 0\x00\x03\x0… │
│ 4265058 ┆ 5114 ┆ secondary ┆ Fairview ┆ … ┆ 0 ┆ F ┆ F ┆ {b"\x01\x02\x00\x0 │
│ ┆ ┆ ┆ Avenue ┆ ┆ ┆ ┆ ┆ 0\x00\x0e\x0… │
│ 4267607 ┆ 5114 ┆ secondary ┆ 31st Street ┆ … ┆ 0 ┆ F ┆ F ┆ {b"\x01\x02\x00\x0 │
│ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ 0\x00\x02\x0… │
│ 4271616 ┆ 5115 ┆ tertiary ┆ 59th Street ┆ … ┆ 0 ┆ F ┆ F ┆ {b"\x01\x02\x00\x0 │
│ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ 0\x00\x15\x0… │
│ 4275365 ┆ 5122 ┆ residential ┆ 61st Street ┆ … ┆ 0 ┆ F ┆ F ┆ {b"\x01\x02\x00\x0 │
│ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ 0\x00"\x00\x… │
│ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … │
│ 1370383592 ┆ 5153 ┆ footway ┆ null ┆ … ┆ 0 ┆ F ┆ F ┆ {b"\x01\x02\x00\x0 │
│ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ 0\x00\x02\x0… │
│ 1370383593 ┆ 5153 ┆ footway ┆ null ┆ … ┆ 0 ┆ F ┆ F ┆ {b"\x01\x02\x00\x0 │
│ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ 0\x00\x07\x0… │
│ 1370383594 ┆ 5153 ┆ footway ┆ null ┆ … ┆ 0 ┆ F ┆ F ┆ {b"\x01\x02\x00\x0 │
│ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ 0\x00\x1c\x0… │
│ 1370383595 ┆ 5153 ┆ footway ┆ null ┆ … ┆ 0 ┆ F ┆ F ┆ {b"\x01\x02\x00\x0 │
│ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ 0\x00\x0b\x0… │
│ 1370398885 ┆ 5141 ┆ service ┆ null ┆ … ┆ 0 ┆ F ┆ F ┆ {b"\x01\x02\x00\x0 │
│ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ 0\x00\x02\x0… │
└────────────┴──────┴─────────────┴─────────────┴───┴───────┴────────┴────────┴────────────────────┘
Source code in src\spatial_polars\io.py
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 |
|