|
| 1 | +import json |
| 2 | +from typing import Any |
| 3 | + |
| 4 | +import pyarrow as pa |
| 5 | +import pyarrow.parquet as pq |
| 6 | +from pyproj import CRS |
| 7 | + |
| 8 | +WGS84_CRS_JSON = CRS.from_epsg(4326).to_json_dict() |
| 9 | + |
| 10 | + |
| 11 | +def to_parquet(table: pa.Table, where: Any, **kwargs: Any) -> None: |
| 12 | + """Write an Arrow table with STAC data to GeoParquet |
| 13 | +
|
| 14 | + This writes metadata compliant with GeoParquet 1.1. |
| 15 | +
|
| 16 | + Args: |
| 17 | + table: The table to write to Parquet |
| 18 | + where: The destination for saving. |
| 19 | + """ |
| 20 | + # TODO: include bbox of geometries |
| 21 | + column_meta = { |
| 22 | + "encoding": "WKB", |
| 23 | + # TODO: specify known geometry types |
| 24 | + "geometry_types": [], |
| 25 | + "crs": WGS84_CRS_JSON, |
| 26 | + "edges": "planar", |
| 27 | + "covering": { |
| 28 | + "bbox": { |
| 29 | + "xmin": ["bbox", "xmin"], |
| 30 | + "ymin": ["bbox", "ymin"], |
| 31 | + "xmax": ["bbox", "xmax"], |
| 32 | + "ymax": ["bbox", "ymax"], |
| 33 | + } |
| 34 | + }, |
| 35 | + } |
| 36 | + geo_meta = { |
| 37 | + "version": "1.1.0-dev", |
| 38 | + "columns": {"geometry": column_meta}, |
| 39 | + "primary_column": "geometry", |
| 40 | + } |
| 41 | + |
| 42 | + metadata = table.schema.metadata or {} |
| 43 | + metadata.update({b"geo": json.dumps(geo_meta).encode("utf-8")}) |
| 44 | + table = table.replace_schema_metadata(metadata) |
| 45 | + |
| 46 | + pq.write_table(table, where, **kwargs) |
0 commit comments