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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
import csv
import os
from datetime import datetime, timezone
from typing import Any
import pystac
from pystac import (
Asset,
Catalog,
Collection,
Extent,
Item,
MediaType,
SpatialExtent,
TemporalExtent,
)
TEST_LABEL_CATALOG = {
"country-1": {
"area-1-1": {
"dsm": "area-1-1_dsm.tif",
"ortho": "area-1-1_ortho.tif",
"labels": "area-1-1_labels.geojson",
},
"area-1-2": {
"dsm": "area-1-2_dsm.tif",
"ortho": "area-1-2_ortho.tif",
"labels": "area-1-2_labels.geojson",
},
},
"country-2": {
"area-2-1": {
"dsm": "area-2-1_dsm.tif",
"ortho": "area-2-1_ortho.tif",
"labels": "area-2-1_labels.geojson",
},
"area-2-2": {
"dsm": "area-2-2_dsm.tif",
"ortho": "area-2-2_ortho.tif",
"labels": "area-2-2_labels.geojson",
},
},
}
ARBITRARY_GEOM: dict[str, Any] = {
"type": "Polygon",
"coordinates": [
[
[-2.5048828125, 3.8916575492899987],
[-1.9610595703125, 3.8916575492899987],
[-1.9610595703125, 4.275202171119132],
[-2.5048828125, 4.275202171119132],
[-2.5048828125, 3.8916575492899987],
]
],
}
ARBITRARY_BBOX: list[float] = [
ARBITRARY_GEOM["coordinates"][0][0][0],
ARBITRARY_GEOM["coordinates"][0][0][1],
ARBITRARY_GEOM["coordinates"][0][1][0],
ARBITRARY_GEOM["coordinates"][0][1][1],
]
ARBITRARY_EXTENT = Extent(
spatial=SpatialExtent.from_coordinates(ARBITRARY_GEOM["coordinates"]),
temporal=TemporalExtent.from_now(),
)
class ExampleInfo:
def __init__(
self,
path: str,
object_type: pystac.STACObjectType,
stac_version: str,
extensions: list[str],
valid: bool,
) -> None:
self.path = path
self.object_type = object_type
self.stac_version = stac_version
self.extensions = extensions
self.valid = valid
class TestCases:
bad_catalog_case = "data-files/catalogs/invalid-catalog/catalog.json"
@staticmethod
def get_path(rel_path: str) -> str:
return os.path.abspath(os.path.join(os.path.dirname(__file__), "..", rel_path))
@staticmethod
def get_examples_info() -> list[ExampleInfo]:
examples: list[ExampleInfo] = []
info_path = TestCases.get_path("data-files/examples/example-info.csv")
with open(TestCases.get_path("data-files/examples/example-info.csv")) as f:
for row in csv.reader(f):
path = os.path.abspath(os.path.join(os.path.dirname(info_path), row[0]))
object_type = row[1]
stac_version = row[2]
extensions: list[str] = []
if row[3]:
extensions = row[3].split("|")
valid = True
if len(row) > 4:
# The 5th column will be "INVALID" if the example
# shouldn't pass validation
valid = row[4] != "INVALID"
examples.append(
ExampleInfo(
path=path,
object_type=pystac.STACObjectType(object_type),
stac_version=stac_version,
extensions=extensions,
valid=valid,
)
)
return examples
@staticmethod
def all_test_catalogs() -> list[Catalog]:
return [
TestCases.case_1(),
TestCases.case_2(),
TestCases.case_3(),
TestCases.case_4(),
TestCases.case_5(),
TestCases.case_7(),
TestCases.case_8(),
]
@staticmethod
def case_1() -> Catalog:
return Catalog.from_file(
TestCases.get_path("data-files/catalogs/test-case-1/catalog.json")
)
@staticmethod
def case_2() -> Catalog:
return Catalog.from_file(
TestCases.get_path("data-files/catalogs/test-case-2/catalog.json")
)
@staticmethod
def case_3() -> Catalog:
root_cat = Catalog(
id="test3", description="test case 3 catalog", title="test case 3 title"
)
image_item = Item(
id="imagery-item",
geometry=ARBITRARY_GEOM,
bbox=ARBITRARY_BBOX,
datetime=datetime.now(timezone.utc),
properties={},
)
image_item.add_asset(
"ortho", Asset(href="some/geotiff.tiff", media_type=MediaType.GEOTIFF)
)
label_item = Item(
id="label-items",
geometry=ARBITRARY_GEOM,
bbox=ARBITRARY_BBOX,
datetime=datetime.now(timezone.utc),
properties={},
)
root_cat.add_item(image_item)
root_cat.add_item(label_item)
return root_cat
@staticmethod
def case_4() -> Catalog:
"""Test case that is based on a local copy of the Tier 1 dataset from
DrivenData's OpenCities AI Challenge.
See: https://www.drivendata.org/competitions/60/building-segmentation-disaster\
-resilience
"""
return Catalog.from_file(
TestCases.get_path("data-files/catalogs/test-case-4/catalog.json")
)
@staticmethod
def case_5() -> Catalog:
"""Based on a subset of https://cbers.stac.cloud/"""
return Catalog.from_file(
TestCases.get_path("data-files/catalogs/test-case-5/catalog.json")
)
@staticmethod
def case_6() -> Catalog:
"""Based on a subset of CBERS, contains a root and 4 empty children"""
return Catalog.from_file(
TestCases.get_path("data-files/catalogs/cbers-partial/catalog.json")
)
@staticmethod
def case_7() -> Catalog:
"""Test case 4 as STAC version 0.8.1"""
return Catalog.from_file(
TestCases.get_path("data-files/catalogs/label_catalog-v0.8.1/catalog.json")
)
@staticmethod
def case_8() -> Collection:
"""Planet disaster data example catalog, 1.0.0-beta.2"""
return Collection.from_file(
TestCases.get_path(
"data-files/catalogs/planet-example-v1.0.0-beta.2/collection.json"
)
)
|