Storage — geocatalog.storage¶
Save, share and reopen catalogs: the schema-versioned GeoParquet format,
streaming writes, STAC export, and the provenance-recording
CatalogBundle.
GeoParquet round-trip¶
geocatalog.storage.to_geoparquet(catalog, out_path, *, schema_version=SCHEMA_VERSION_CURRENT, write_covering_bbox=True, partition_by=None)
¶
Persist catalog as a GeoParquet file on disk.
The result is a single Parquet file readable by any GeoParquet-aware
tool (DuckDB, GDAL, pandas, geopandas) — not a pickle, so it
survives version bumps and crosses Python / language boundaries.
DuckDBGeoCatalog reads the same artifact, so this
is the canonical interchange format for both backends.
Two columns are added on write and stripped on load:
_backend: round-trips the catalogkindsofrom_geoparquetrestores the right loader dispatch._schema_version: reserved for forward-compat (§10.4 of the design plan); bump on first substantive schema change.
The row-level pd.IntervalIndex is unpacked into start_time /
end_time columns (Parquet has no native IntervalIndex type);
from_geoparquet rebuilds the index from those columns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
catalog
|
InMemoryGeoCatalog
|
An |
required |
out_path
|
str | Path
|
Destination path. The |
required |
schema_version
|
int
|
Value written to the reserved
|
SCHEMA_VERSION_CURRENT
|
write_covering_bbox
|
bool
|
Emit the per-row |
True
|
partition_by
|
tuple[str, ...] | None
|
Optional Hive partition columns for directory
output. Built-in |
None
|
Source code in packages/geotoolz-catalog/src/geocatalog/_src/storage/parquet.py
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 | |
geocatalog.storage.from_geoparquet(source, *, kind=None, strict=False, retries=3, storage_options=None)
¶
Load a GeoParquet file into an InMemoryGeoCatalog.
Inverse of to_geoparquet: rebuilds the IntervalIndex from
start_time / end_time columns and recovers the kind from
the reserved _backend column. Externally produced files (no
_backend column) default to kind "raster" — adjust
on the returned catalog if that's wrong.
The reserved _schema_version column drives forward migration
(see SCHEMA_VERSION_CURRENT and _MIGRATIONS):
v_artifact == v_current: load directly.v_artifact < v_current: chain forward migrations transparently.v_artifact > v_current: raiseCatalogSchemaError— the reader is older than the writer and needs upgrading.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str | Path
|
Path to a GeoParquet file produced by |
required |
kind
|
CatalogKind | None
|
Kind override (loader dispatch). |
None
|
strict
|
bool
|
If |
False
|
retries
|
int
|
Number of retries for transient remote I/O failures.
|
3
|
storage_options
|
dict[str, Any] | None
|
obstore store options for cloud/HTTP URIs
(e.g. |
None
|
Returns:
| Type | Description |
|---|---|
InMemoryGeoCatalog
|
An |
InMemoryGeoCatalog
|
recoverable) kind as the source. |
Raises:
| Type | Description |
|---|---|
CatalogSchemaError
|
If the artifact's |
CatalogMetadataError
|
|
Source code in packages/geotoolz-catalog/src/geocatalog/_src/storage/parquet.py
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 306 307 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 | |
Schema migration¶
geocatalog.storage.SCHEMA_VERSION_CURRENT = 1
module-attribute
¶
The reader's schema version. Bump on every substantive schema change
and register the previous → this migration in parquet._MIGRATIONS.
v1 reserves STATE_COLUMNS.
geocatalog.storage.migrate_geoparquet(source, *, to_version)
¶
Read source, migrate it to to_version, write back in-place.
A thin file-level wrapper over from_geoparquet + to_geoparquet
used by the geocatalog migrate CLI. The new version is written
to a sibling temp file and renamed over source, so a crash
mid-migration leaves the original intact. The artifact is rewritten
only if the migration actually changed the version, so calling
twice is idempotent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str | Path
|
GeoParquet file to migrate. Rewritten in-place. |
required |
to_version
|
int
|
Target version. Must equal |
required |
Returns:
| Type | Description |
|---|---|
int
|
The artifact's |
int
|
to |
Raises:
| Type | Description |
|---|---|
CatalogSchemaError
|
If |
Source code in packages/geotoolz-catalog/src/geocatalog/_src/storage/parquet.py
Streaming writes¶
geocatalog.storage.StreamingParquetWriter
¶
Append-only GeoParquet 1.1 writer over a row iterator.
Buffers rows into Arrow record batches of batch_size, writes each
batch via pyarrow.parquet.ParquetWriter, and on close() injects the
aggregate GeoParquet geo key-value metadata (PROJJSON CRS, observed
geometry types, total bbox, and the bbox-covering struct pointer).
Geometries are encoded as WKB on the fly; the per-row bbox column
(xmin/ymin/xmax/ymax struct) is computed per batch when
write_bbox=True.
Rows go to a hidden sibling temp file that close renames onto
path; abort (or leaving the with block on an exception)
discards it. path therefore only ever holds a complete artifact —
a failed write leaves any existing file there untouched.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Destination GeoParquet file. Parent directory must exist. |
required |
crs
|
Any
|
CRS of every input geometry. Written into |
required |
kind
|
CatalogKind
|
The catalog kind, copied to the reserved |
required |
schema_version
|
int
|
Value for the reserved |
SCHEMA_VERSION_CURRENT
|
write_bbox
|
bool
|
Emit the GeoParquet 1.1 |
True
|
batch_size
|
int
|
Rows per Arrow record batch. Default 10 000 → peak memory ≈ 10 MB for ~1 KB-per-row catalogs (design §4.6). |
10000
|
schema
|
Schema | None
|
Arrow types for the extras columns. Fields named like a
reserved or internal column are ignored (the writer owns
those). |
None
|
start_time / end_time are stored as timestamp("ns") in
naive UTC — the catalog's time-axis contract (tz-aware inputs are
converted, naive inputs are taken as UTC).
Usage::
with StreamingParquetWriter(path, crs=crs, kind="raster") as w:
for row in row_iter:
w.write_row(row)
Source code in packages/geotoolz-catalog/src/geocatalog/_src/storage/streaming.py
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 306 307 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 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 | |
abort()
¶
Discard everything written so far; path is left untouched.
close()
¶
Flush remaining rows, finalise the metadata, move the file into place.
Source code in packages/geotoolz-catalog/src/geocatalog/_src/storage/streaming.py
write_row(row)
¶
Buffer one row; flush when the batch fills.
Source code in packages/geotoolz-catalog/src/geocatalog/_src/storage/streaming.py
geocatalog.storage.sort_geoparquet(source, out_path, *, sort_by, crs, kind, schema_version=_SCHEMA_VERSION, write_bbox=True, batch_size=10000)
¶
Rewrite a streamed GeoParquet file in sort_by order via DuckDB.
Opens source as a DuckDB relation, sorts by the given keys (the
literal token "geometry_hilbert" expands to
ST_Hilbert(ST_Centroid(geometry))), and streams the sorted
relation back through pyarrow.parquet.ParquetWriter so the final
file carries the same GeoParquet 1.1 geo metadata + bbox covering
struct as the streamed input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str | Path
|
Path to the unsorted GeoParquet (written by
|
required |
out_path
|
str | Path
|
Path to write the sorted result. Overwrites if it exists. |
required |
sort_by
|
Sequence[str]
|
Sort keys. Plain column names pass through; the literal
token |
required |
crs
|
Any
|
CRS to encode in the destination's GeoParquet metadata. |
required |
kind
|
CatalogKind
|
Catalog kind for the destination's metadata. |
required |
schema_version
|
int
|
Schema version for the destination. |
SCHEMA_VERSION_CURRENT
|
write_bbox
|
bool
|
Emit the GeoParquet 1.1 covering bbox struct. |
True
|
batch_size
|
int
|
Pyarrow batch size for the streamed rewrite. |
10000
|
Raises:
| Type | Description |
|---|---|
ImportError
|
|
Source code in packages/geotoolz-catalog/src/geocatalog/_src/storage/streaming.py
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 | |
STAC export (extras: [stac])¶
geocatalog.storage.to_stac_collection(catalog, *, collection_id, description='', asset_key='data')
¶
Convert a catalog into a STAC collection.
Rows are grouped into one item per scene: rows sharing a source
stac_collection and a stac_item_id (or, failing that, an
id — the CatalogBundle primary key) become one item whose
assets are keyed by each row's asset_key. An id that occurs in
more than one source collection is exported as
"<source collection>:<id>" so the output ids stay unique. A row
with neither id gets its own item, "<collection_id>-<row>". The
item's geometry, time and properties come from its first row.
Properties are made JSON-safe: numpy scalars become Python numbers,
timestamps RFC 3339 strings, and missing values (None, NaN,
NaT, non-finite floats) are omitted, so
json.dumps(collection.to_dict()) always works.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
catalog
|
GeoCatalog
|
Catalog to export. |
required |
collection_id
|
str
|
The collection id; also stamped on every item. |
required |
description
|
str
|
Collection description. |
''
|
asset_key
|
str
|
Asset key for rows without an |
'data'
|
Returns:
| Type | Description |
|---|---|
Collection
|
A |
Raises:
| Type | Description |
|---|---|
ValueError
|
Two rows of the same item carry the same asset key. |
Source code in packages/geotoolz-catalog/src/geocatalog/_src/storage/stac.py
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 | |
Provenance bundles¶
geocatalog.storage.CatalogBundle
¶
Wraps an InMemoryGeoCatalog + queries + matchups + persistence.
See module docstring for the directory layout and lifecycle.
Attributes:
| Name | Type | Description |
|---|---|---|
catalog |
The items table as an |
|
crs |
Authoritative CRS for the items table's geometry column.
Set at construction; reprojection happens on ingest, never
on lookup. ( |
|
kind |
The catalog kind forwarded to the items table.
( |
|
queries |
list[QueryRecord]
|
In-memory list of |
matchups |
list[MatchupRow]
|
In-memory list of |
Source code in packages/geotoolz-catalog/src/geocatalog/_src/storage/bundle.py
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 306 307 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 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 | |
n_items
property
¶
Number of rows in the items table.
empty(*, crs='EPSG:4326', kind='raster')
classmethod
¶
Create a fresh bundle with an empty items table.
Use this as the starting point when ingesting from a Source:
>>> bundle = CatalogBundle.empty(crs="EPSG:4326")
>>> bundle.ingest(STACSource.planetary_computer(), ...)
>>> bundle.to_directory("my_catalog/")
Source code in packages/geotoolz-catalog/src/geocatalog/_src/storage/bundle.py
from_catalog(catalog, *, kind=None)
classmethod
¶
Wrap an already-built InMemoryGeoCatalog.
Useful when the user constructed a catalog via
build_raster_catalog / build_vector_catalog and now
wants to add queries/matchups state.
Source code in packages/geotoolz-catalog/src/geocatalog/_src/storage/bundle.py
from_directory(path)
classmethod
¶
Load a bundle from disk. Inverse of to_directory.
Source code in packages/geotoolz-catalog/src/geocatalog/_src/storage/bundle.py
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 | |
ingest(source, *, bounds, interval=None, collection=None, filters=None, limit=None, primary_asset=None, tag=None, notes=None, on_duplicate='error')
¶
Query a Source and append matching rows to the items table.
Records the call in self.queries with a fresh UUID so
every row can be traced back to the call that produced it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Source
|
Any |
required |
bounds
|
tuple[float, float, float, float]
|
|
required |
interval
|
Interval | None
|
Optional time window. |
None
|
collection
|
str | None
|
Upstream collection id (passed through to
|
None
|
filters
|
Mapping[str, Any] | None
|
Adapter-specific filter dict. |
None
|
limit
|
int | None
|
Cap on the number of rows returned. |
None
|
primary_asset
|
str | None
|
Asset key to promote to the catalog's
|
None
|
tag
|
str | None
|
User label persisted in |
None
|
notes
|
str | None
|
Free-form notes recorded in |
None
|
on_duplicate
|
Literal['error', 'skip', 'replace']
|
What to do with a row whose
|
'error'
|
Returns:
| Type | Description |
|---|---|
str
|
The query_id (uuid4 hex) of this ingest call. |
Raises:
| Type | Description |
|---|---|
ValueError
|
A duplicate |
KeyError
|
|
Source code in packages/geotoolz-catalog/src/geocatalog/_src/storage/bundle.py
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 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 | |
matchups_df()
¶
Return the matchups table as a pd.DataFrame.
Source code in packages/geotoolz-catalog/src/geocatalog/_src/storage/bundle.py
queries_df()
¶
Return the queries table as a pd.DataFrame for ad-hoc analysis.
Source code in packages/geotoolz-catalog/src/geocatalog/_src/storage/bundle.py
to_directory(path)
¶
Persist the bundle as a directory of Parquet files + metadata.
Layout::
<path>/
items.parquet
queries.parquet (omitted when empty)
matchups.parquet (omitted when empty)
_meta.json
Existing files inside path are overwritten. Stale
queries.parquet / matchups.parquet from a previous
write are removed when the corresponding in-memory list is
empty — otherwise from_directory() would silently
resurrect rows that should have been dropped.
Every component is first written to a hidden staging directory
next to path; only once all of them exist is the bundle
published, by swapping that directory in for path (files the
bundle does not own are moved across). A reader therefore sees
either the previous generation or the new one — never new items
beside old queries or metadata — and a failure while writing
leaves the previous bundle untouched.
created_at records the first save and survives later ones;
updated_at is the latest.
Source code in packages/geotoolz-catalog/src/geocatalog/_src/storage/bundle.py
write_matchups(rows, *, tag=None)
¶
Persist a stream of MatchupRows into the bundle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rows
|
Iterable[MatchupRow]
|
Iterable of |
required |
tag
|
str | None
|
Optional |
None
|
matchup_id is the table's key: a row whose id is already in
the bundle replaces the stored one in place (so re-running a
deterministic matchup does not duplicate it, and a re-run under
another tag moves it to that query_set).
Returns:
| Type | Description |
|---|---|
int
|
How many rows were written (added or replaced). |
Source code in packages/geotoolz-catalog/src/geocatalog/_src/storage/bundle.py
geocatalog.storage.QueryRecord
dataclass
¶
One row of queries.parquet.
Persisted alongside the items so a user can answer "which call produced these rows?" without storing per-row provenance copies.
Attributes:
| Name | Type | Description |
|---|---|---|
query_id |
str
|
Stable identifier (uuid4 hex). Matches the
|
source |
str
|
|
collection |
str | None
|
Upstream collection identifier searched. |
bounds_wkt |
str
|
WKT representation of the search bbox (WGS84). |
time_start, |
time_end
|
Time-window endpoints; |
filters_json |
str
|
JSON-encoded filters dict; |
created_at |
datetime
|
When the call completed (UTC). |
n_returned |
int
|
How many rows the call yielded. |
tag |
str | None
|
User-supplied label (e.g. |
notes |
str | None
|
Free-form text. |