This repository has been archived by the owner on Sep 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_stac_table.py
171 lines (149 loc) · 6.41 KB
/
test_stac_table.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import datetime
import os
import shutil
from pathlib import Path
import pandas as pd
import dask_geopandas
import geopandas
import pystac
import pytest
import shapely.geometry
import stac_table
@pytest.fixture
def ensure_clean():
yield
shutil.rmtree("data.parquet", ignore_errors=True)
try:
os.remove("data.parquet")
except FileNotFoundError:
pass
@pytest.mark.usefixtures("ensure_clean")
@pytest.mark.filterwarnings("ignore:An exception was ignored")
@pytest.mark.filterwarnings("ignore:.*initial implementation.*")
@pytest.mark.filterwarnings("ignore:.ParquetDataset.p.*")
class TestItem:
@pytest.mark.parametrize("partition", [True, False])
def test_generate_item(self, partition):
gdf = geopandas.read_file(geopandas.datasets.get_path("naturalearth_lowres"))
union = gdf["geometry"].unary_union
geometry = shapely.geometry.mapping(union)
bbox = union.bounds
if partition:
gdf = dask_geopandas.from_geopandas(gdf, npartitions=2)
gdf.to_parquet("data.parquet")
item = pystac.Item(
"naturalearth_lowres", geometry, bbox, datetime.datetime(2021, 1, 1), {}
)
ds = str(Path("data.parquet").absolute())
result = stac_table.generate(
ds,
item,
asset_extra_fields={"table:storage_options": {"storage_account": "foo"}},
)
expected_columns = [
{"name": "pop_est", "type": "int64"},
{"name": "continent", "type": "byte_array"},
{"name": "name", "type": "byte_array"},
{"name": "iso_a3", "type": "byte_array"},
{"name": "gdp_md_est", "type": "double"},
{"name": "geometry", "type": "byte_array"},
]
assert result.properties["table:columns"] == expected_columns
asset = result.assets["data"]
assert asset.href == ds
assert asset.media_type == "application/x-parquet"
assert asset.roles == ["data"]
assert asset.extra_fields["table:storage_options"] == {"storage_account": "foo"}
assert pystac.extensions.projection.SCHEMA_URI in result.stac_extensions
assert result.properties["proj:epsg"] == 4326
def test_infer_bbox(self):
df = geopandas.GeoDataFrame(
{"A": [1, 2]},
geometry=[shapely.geometry.Point(1, 2), shapely.geometry.Point(2, 3)],
crs=4326,
)
df.to_parquet("data.parquet")
item = pystac.Item(
"naturalearth_lowres", None, None, datetime.datetime(2021, 1, 1), {}
)
result = stac_table.generate("data.parquet", item, infer_bbox=True)
assert result.bbox == (1.0, 2.0, 2.0, 3.0)
assert result.properties["proj:epsg"] == 4326
assert result.properties["proj:bbox"] == (1.0, 2.0, 2.0, 3.0)
def test_infer_geometry(self):
df = geopandas.GeoDataFrame(
{"A": [1, 2]},
geometry=[shapely.geometry.Point(1, 2), shapely.geometry.Point(2, 3)],
crs=4326, # TODO: other epsg
)
df.to_parquet("data.parquet")
item = pystac.Item(
"naturalearth_lowres", None, None, datetime.datetime(2021, 1, 1), {}
)
result = stac_table.generate("data.parquet", item, infer_geometry=True)
expected = {"type": "MultiPoint", "coordinates": ((1.0, 2.0), (2.0, 3.0))}
assert result.geometry == expected
assert result.properties["proj:epsg"] == 4326
assert result.properties["proj:geometry"] == expected
def test_infer_datetime_midpoint(self):
df = geopandas.GeoDataFrame(
{"A": [pd.Timestamp("2000-01-01"), pd.Timestamp("2000-01-03")]},
geometry=[shapely.geometry.Point(1, 2), shapely.geometry.Point(2, 3)],
)
df.to_parquet("data.parquet")
item = pystac.Item(
"naturalearth_lowres", None, None, datetime.datetime(2021, 1, 1), {}
)
result = stac_table.generate(
"data.parquet", item, datetime_column="A", infer_datetime="midpoint"
)
assert result.properties["datetime"] == "2021-01-01T00:00:00Z"
def test_infer_datetime_unique(self):
df = geopandas.GeoDataFrame(
{"A": [pd.Timestamp("2000-01-01"), pd.Timestamp("2000-01-01")]},
geometry=[shapely.geometry.Point(1, 2), shapely.geometry.Point(2, 3)],
)
df.to_parquet("data.parquet")
item = pystac.Item(
"naturalearth_lowres", None, None, datetime.datetime(2021, 1, 1), {}
)
result = stac_table.generate(
"data.parquet", item, datetime_column="A", infer_datetime="unique"
)
assert result.properties["datetime"] == "2021-01-01T00:00:00Z"
def test_infer_datetime_range(self):
df = geopandas.GeoDataFrame(
{"A": [pd.Timestamp("2000-01-01"), pd.Timestamp("2000-01-03")]},
geometry=[shapely.geometry.Point(1, 2), shapely.geometry.Point(2, 3)],
)
df.to_parquet("data.parquet")
item = pystac.Item(
"naturalearth_lowres", None, None, datetime.datetime(2021, 1, 1), {}
)
result = stac_table.generate(
"data.parquet", item, datetime_column="A", infer_datetime="range"
)
assert result.properties["start_datetime"] == "2000-01-01T00:00:00Z"
assert result.properties["end_datetime"] == "2000-01-03T00:00:00Z"
def test_metadata(self):
df = geopandas.GeoDataFrame(
{"A": [pd.Timestamp("2000-01-01"), pd.Timestamp("2000-01-03")]},
geometry=[shapely.geometry.Point(1, 2), shapely.geometry.Point(2, 3)],
)
df.to_parquet("data.parquet")
item = pystac.Item(
"naturalearth_lowres", None, None, datetime.datetime(2021, 1, 1), {}
)
result = stac_table.generate(
"data.parquet", item, datetime_column="A", infer_datetime="range"
)
assert "metadata" not in result.properties["table:columns"][0]
# Bug in pandas? We apparently aren't writing the metadata...
# df["A"].attrs = {"key": "value!"}
# df.to_parquet("data.parquet")
# item = pystac.Item("naturalearth_lowres", None, None,
# datetime.datetime(2021, 1, 1), {})
# result = stac_table.generate(
# "data.parquet", item, datetime_column="A", infer_datetime="range"
# )
# assert result.properties["table:columns"][0]["metadata"] == {"key": "value!"}