-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* exhaustive schema inference * Update docstrings * fix dict accessor * fix setter * Fix Arrow -> STAC conversion for nested proj:geometry * fix types * fix circular import
- Loading branch information
1 parent
61282d1
commit 2eaf22d
Showing
6 changed files
with
345 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from pyproj import CRS | ||
|
||
WGS84_CRS_JSON = CRS.from_epsg(4326).to_json_dict() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import json | ||
from pathlib import Path | ||
from typing import Any, Dict, Iterable, Sequence, Union | ||
|
||
import pyarrow as pa | ||
|
||
from stac_geoparquet.arrow._util import stac_items_to_arrow | ||
|
||
|
||
class InferredSchema: | ||
""" | ||
A schema representing the original STAC JSON with absolutely minimal modifications. | ||
The only modification from the data is converting any geometry fields from GeoJSON | ||
to WKB. | ||
""" | ||
|
||
inner: pa.Schema | ||
"""The underlying Arrow schema.""" | ||
|
||
count: int | ||
"""The total number of items scanned.""" | ||
|
||
def __init__(self) -> None: | ||
self.inner = pa.schema([]) | ||
self.count = 0 | ||
|
||
def update_from_ndjson( | ||
self, | ||
path: Union[Union[str, Path], Iterable[Union[str, Path]]], | ||
*, | ||
chunk_size: int = 65536, | ||
) -> None: | ||
""" | ||
Update this inferred schema from one or more newline-delimited JSON STAC files. | ||
Args: | ||
path: One or more paths to files with STAC items. | ||
chunk_size: The chunk size to load into memory at a time. Defaults to 65536. | ||
""" | ||
# Handle multi-path input | ||
if not isinstance(path, (str, Path)): | ||
for p in path: | ||
self.update_from_ndjson(p) | ||
|
||
return | ||
|
||
# Handle single-path input | ||
with open(path) as f: | ||
items = [] | ||
for line in f: | ||
item = json.loads(line) | ||
items.append(item) | ||
|
||
if len(items) >= chunk_size: | ||
self.update_from_items(items) | ||
items = [] | ||
|
||
# Handle remainder | ||
if len(items) > 0: | ||
self.update_from_items(items) | ||
|
||
def update_from_items(self, items: Sequence[Dict[str, Any]]) -> None: | ||
"""Update this inferred schema from a sequence of STAC Items.""" | ||
self.count += len(items) | ||
current_schema = stac_items_to_arrow(items, schema=None).schema | ||
new_schema = pa.unify_schemas( | ||
[self.inner, current_schema], promote_options="permissive" | ||
) | ||
self.inner = new_schema |
Oops, something went wrong.