Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collections in geoparquet #437

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/src/geoarrow/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,7 @@ pub fn from_table(table: Table) -> Result<Vec<serde_json::Map<String, Value>>, c
"geometry".into(),
serde_json::to_value(geojson::Geometry::new(value))?,
);
let _ = row.insert("type".into(), crate::ITEM_TYPE.into());
items.push(unflatten(row));
}
}
Expand Down
7 changes: 6 additions & 1 deletion core/src/geoarrow/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub fn to_table(item_collection: impl Into<ItemCollection>) -> Result<Table> {
let value = value
.as_object_mut()
.expect("a flat item should serialize to an object");
let _ = value.remove("type");
let _ = value.remove("geometry");
if let Some(bbox) = value.remove("bbox") {
let bbox = bbox
Expand Down Expand Up @@ -143,13 +144,17 @@ pub fn from_table(table: Table) -> Result<ItemCollection> {
#[cfg(all(test, feature = "geoparquet"))]
mod tests {
use crate::{Item, ItemCollection};
use arrow_schema::DataType;
use geoarrow::io::parquet::GeoParquetRecordBatchReaderBuilder;
use std::fs::File;

#[test]
fn to_table() {
let item: Item = crate::read("examples/simple-item.json").unwrap();
let _ = super::to_table(vec![item]).unwrap();
let table = super::to_table(vec![item]).unwrap();
let (_, bbox_field) = table.schema().column_with_name("bbox").unwrap();
assert!(matches!(bbox_field.data_type(), DataType::Struct(_)));
assert!(table.schema().column_with_name("type").is_none());
}

#[test]
Expand Down
56 changes: 46 additions & 10 deletions scripts/validate-stac-geoparquet
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
#!/usr/bin/env python

import json
import sys
import shutil
import subprocess
import sys
import tempfile
from typing import Any
from deepdiff import DeepDiff
from pathlib import Path
from typing import Any

import pyarrow
import pyarrow.parquet
import stac_geoparquet.arrow
import pyarrow
from deepdiff import DeepDiff

root = Path(__file__).parents[1]
path = root / "spec-examples" / "v1.1.0" / "extended-item.json"
directory = tempfile.mkdtemp()
parquet_path = Path(directory) / "extended-item.parquet"


def clean_item(item: dict[str, Any]) -> None:
if item["geometry"]["type"] == "MultiPolygon" and len(item["geometry"]["coordinates"]) == 1:
if (
item["geometry"]["type"] == "MultiPolygon"
and len(item["geometry"]["coordinates"]) == 1
):
item["geometry"]["type"] = "Polygon"
item["geometry"]["coordinates"] = item["geometry"]["coordinates"][0]


def clean_report(report: dict[str, Any]) -> dict[str, Any]:
"""We expect datetime values to be changed in the report."""
if report.get("values_changed"):
Expand All @@ -32,12 +38,29 @@ def clean_report(report: dict[str, Any]) -> dict[str, Any]:
del report["values_changed"]["root['properties']['datetime']"]
if not report["values_changed"]:
del report["values_changed"]
if report.get("dictionary_item_removed"):
report["dictionary_item_removed"] = [
item for item in report["dictionary_item_removed"] if item != "root['type']"
]
if not report["dictionary_item_removed"]:
del report["dictionary_item_removed"]
return report


try:
# Writing
subprocess.check_call(
["cargo", "run", "--no-default-features", "-F", "geoparquet", "--", "translate", path, parquet_path]
[
"cargo",
"run",
"--no-default-features",
"-F",
"geoparquet",
"--",
"translate",
path,
parquet_path,
]
)
table = pyarrow.parquet.read_table(parquet_path)
after = next(stac_geoparquet.arrow.stac_table_to_items(table))
Expand All @@ -54,11 +77,24 @@ try:
# Reading
table = stac_geoparquet.arrow.parse_stac_items_to_arrow([before])
stac_geoparquet.arrow.to_parquet(table, parquet_path)
item_collection = json.loads(subprocess.check_output(
["cargo", "run", "--no-default-features", "-F", "geoparquet", "--", "translate", parquet_path]
))
item_collection = json.loads(
subprocess.check_output(
[
"cargo",
"run",
"--no-default-features",
"-F",
"geoparquet",
"--",
"translate",
parquet_path,
]
)
)
assert len(item_collection["features"]) == 1
clean_item(item_collection["features"][0]) # stac-geoparquet writes as a multi-polygon
clean_item(
item_collection["features"][0]
) # stac-geoparquet writes as a multi-polygon
report = DeepDiff(before, item_collection["features"][0]).to_dict()
report = clean_report(report)
if report:
Expand Down