From 84a916ca733a612ed94c1c857f690f672da241c7 Mon Sep 17 00:00:00 2001 From: Matthias Mohr Date: Wed, 12 Jul 2023 17:47:08 +0200 Subject: [PATCH] Cater for optional fields #12 and fix some tests --- stac_geoparquet/pgstac_reader.py | 3 ++- stac_geoparquet/stac_geoparquet.py | 8 +++++++- tests/test_stac_geoparquet.py | 11 +++++++++-- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/stac_geoparquet/pgstac_reader.py b/stac_geoparquet/pgstac_reader.py index 6daacf3..a065ab8 100644 --- a/stac_geoparquet/pgstac_reader.py +++ b/stac_geoparquet/pgstac_reader.py @@ -321,7 +321,8 @@ def make_pgstac_items( item["bbox"] = list(geom.bounds) item["assets"] = content["assets"] - item["stac_extensions"] = content["stac_extensions"] + if "stac_extensions" in content: + item["stac_extensions"] = content["stac_extensions"] item["properties"] = content["properties"] pypgstac.hydration.hydrate(base_item, item) diff --git a/stac_geoparquet/stac_geoparquet.py b/stac_geoparquet/stac_geoparquet.py index 1c99c39..95f3a2c 100644 --- a/stac_geoparquet/stac_geoparquet.py +++ b/stac_geoparquet/stac_geoparquet.py @@ -76,9 +76,15 @@ def to_geodataframe(items: Sequence[dict[str, Any]]) -> geopandas.GeoDataFrame: "assets", "collection", ] + opt_columns = ["stac_extensions", "collection"] + for col in opt_columns: + if col not in gdf.columns: + columns.remove(col) + gdf = pd.concat([gdf[columns], gdf.drop(columns=columns)], axis="columns") for k in ["type", "stac_version", "id", "collection"]: - gdf[k] = gdf[k].astype("string") + if k in gdf: + gdf[k] = gdf[k].astype("string") return gdf diff --git a/tests/test_stac_geoparquet.py b/tests/test_stac_geoparquet.py index 5ba59ae..1b3256b 100644 --- a/tests/test_stac_geoparquet.py +++ b/tests/test_stac_geoparquet.py @@ -21,7 +21,7 @@ def test_assert_equal(): "https://planetarycomputer.microsoft.com/api/stac/v1/collections/sentinel-2-l2a/items/S2B_MSIL2A_20220612T182919_R027_T24XWR_20220613T123251" # noqa: E501 ) b = pystac.read_file( - "https://planetarycomputer.microsoft.com/api/stac/v1/collections/landsat-8-c2-l2/items/LC08_L2SP_202033_20220327_02_T1" # noqa: E501 + "https://planetarycomputer.microsoft.com/api/stac/v1/collections/landsat-c2-l2/items/LC08_L2SP_202033_20220327_02_T1" # noqa: E501 ) with pytest.raises(AssertionError): assert_equal(a, b) @@ -233,7 +233,8 @@ def test_to_geodataframe(): } ) for k in ["type", "stac_version", "id", "collection"]: - expected[k] = expected[k].astype("string") + if k in expected: + expected[k] = expected[k].astype("string") pandas.testing.assert_frame_equal(result, expected) @@ -247,6 +248,12 @@ def test_s1_grd(): item = requests.get( "https://planetarycomputer.microsoft.com/api/stac/v1/collections/sentinel-1-grd/items/S1A_EW_GRDM_1SSH_20150129T081916_20150129T081938_004383_005598" # noqa: E501 ).json() + + # pystac migrates EO extension to latest version, but PC is still on 1.0.0 + for i, ext in enumerate(item["stac_extensions"]): + if ext == "https://stac-extensions.github.io/eo/v1.0.0/schema.json": + item["stac_extensions"][i] = "https://stac-extensions.github.io/eo/v1.1.0/schema.json" + item["geometry"] = fix_empty_multipolygon(item["geometry"]).__geo_interface__ df = stac_geoparquet.to_geodataframe([item])