views-datafactory 1.9.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- datafactory_adapters/ARCHITECTURE.md +54 -0
- datafactory_adapters/__init__.py +38 -0
- datafactory_adapters/_conservation.py +95 -0
- datafactory_adapters/_reconciliation.py +92 -0
- datafactory_adapters/_validation.py +39 -0
- datafactory_adapters/feature_frame.py +25 -0
- datafactory_adapters/grid_from_feature_frame.py +89 -0
- datafactory_adapters/grid_to_country_month.py +169 -0
- datafactory_adapters/grid_to_dataframe.py +176 -0
- datafactory_compilation/ARCHITECTURE.md +63 -0
- datafactory_compilation/__init__.py +39 -0
- datafactory_compilation/aggregation.py +64 -0
- datafactory_compilation/compilation_config.py +97 -0
- datafactory_compilation/conservation.py +36 -0
- datafactory_compilation/grid_compilation.py +352 -0
- datafactory_compilation/output.py +85 -0
- datafactory_compilation/preflight.py +61 -0
- datafactory_compilation/pregridded_compilation.py +291 -0
- datafactory_consolidation/ARCHITECTURE.md +58 -0
- datafactory_consolidation/__init__.py +28 -0
- datafactory_consolidation/consolidation_result.py +27 -0
- datafactory_consolidation/consolidators/__init__.py +32 -0
- datafactory_consolidation/consolidators/acled.py +462 -0
- datafactory_consolidation/consolidators/ucdp.py +524 -0
- datafactory_consolidation/event_store.py +67 -0
- datafactory_consolidation/tagging.py +46 -0
- datafactory_harvester/ARCHITECTURE.md +103 -0
- datafactory_harvester/__init__.py +37 -0
- datafactory_harvester/event_validation.py +372 -0
- datafactory_harvester/harvest_runner.py +72 -0
- datafactory_harvester/pipeline_runner.py +125 -0
- datafactory_harvester/snapshot_storage.py +117 -0
- datafactory_harvester/sources/__init__.py +36 -0
- datafactory_harvester/sources/_ucdp_common.py +34 -0
- datafactory_harvester/sources/acled.py +743 -0
- datafactory_harvester/sources/gaul_admin.py +567 -0
- datafactory_harvester/sources/ghsbuilts.py +271 -0
- datafactory_harvester/sources/ghspop.py +267 -0
- datafactory_harvester/sources/priogrid_static.py +359 -0
- datafactory_harvester/sources/shdi.py +625 -0
- datafactory_harvester/sources/ucdp_annual.py +616 -0
- datafactory_harvester/sources/ucdp_candidate.py +419 -0
- datafactory_harvester/sources/ucdp_dot9.py +407 -0
- datafactory_harvester/sources/vdem.py +350 -0
- datafactory_harvester/validation.py +74 -0
- datafactory_http/__init__.py +10 -0
- datafactory_http/retry.py +102 -0
- datafactory_priogrid/ARCHITECTURE.md +62 -0
- datafactory_priogrid/__init__.py +51 -0
- datafactory_priogrid/cell_generator.py +137 -0
- datafactory_priogrid/grid_config.py +107 -0
- datafactory_priogrid/land_mask.py +62 -0
- datafactory_priogrid/parity_validation.py +176 -0
- datafactory_priogrid/shapefile_harvester.py +174 -0
- datafactory_priogrid/shapefile_reader.py +125 -0
- datafactory_priogrid/spatiotemporal.py +66 -0
- datafactory_priogrid/temporal_config.py +78 -0
- datafactory_priogrid/temporal_generator.py +65 -0
- datafactory_provenance/ARCHITECTURE.md +61 -0
- datafactory_provenance/__init__.py +81 -0
- datafactory_provenance/constants.py +3 -0
- datafactory_provenance/digests_and_ledgers.py +370 -0
- datafactory_provenance/health.py +310 -0
- datafactory_provenance/pipeline_lock.py +200 -0
- datafactory_provenance/registry.py +92 -0
- datafactory_provenance/skip.py +212 -0
- datafactory_provenance/source_registry.py +466 -0
- datafactory_query/ARCHITECTURE.md +60 -0
- datafactory_query/__init__.py +36 -0
- datafactory_query/africa_me_gaul_pgids.json +1 -0
- datafactory_query/africa_me_legacy_pgids.json +1 -0
- datafactory_query/backends_npy.py +90 -0
- datafactory_query/backends_zarr.py +259 -0
- datafactory_query/coverage.py +64 -0
- datafactory_query/dataset.py +263 -0
- datafactory_query/defaults.py +101 -0
- datafactory_query/land_gaul_pgids.json +1 -0
- datafactory_query/land_pgids.json +1 -0
- datafactory_query/output_format.py +52 -0
- datafactory_query/regions.py +293 -0
- datafactory_query/temporal.py +115 -0
- datafactory_viewpoint/ARCHITECTURE.md +68 -0
- datafactory_viewpoint/__init__.py +33 -0
- datafactory_viewpoint/builders/__init__.py +32 -0
- datafactory_viewpoint/builders/acled_v1.py +213 -0
- datafactory_viewpoint/builders/ghsbuilts_v1.py +351 -0
- datafactory_viewpoint/builders/ghspop_v1.py +471 -0
- datafactory_viewpoint/builders/shdi_v1.py +389 -0
- datafactory_viewpoint/builders/ucdp_v1.py +437 -0
- datafactory_viewpoint/builders/vdem_v1.py +419 -0
- datafactory_viewpoint/profiles.py +169 -0
- datafactory_viewpoint/raster_io.py +49 -0
- datafactory_viewpoint/spatial_distribution.py +203 -0
- datafactory_viewpoint/spatial_weights.py +207 -0
- datafactory_viewpoint/survivorship.py +108 -0
- datafactory_viewpoint/temporal.py +111 -0
- datafactory_viewpoint/temporal_distribution.py +295 -0
- datafactory_viewpoint/viewpoint_config.py +124 -0
- datafactory_viewpoint/viewpoint_result.py +33 -0
- views_datafactory-1.9.0.dist-info/METADATA +528 -0
- views_datafactory-1.9.0.dist-info/RECORD +103 -0
- views_datafactory-1.9.0.dist-info/WHEEL +4 -0
- views_datafactory-1.9.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
"""GHS-BUILT-S built-up surface grid harvester — GeoTIFF download from JRC.
|
|
2
|
+
|
|
3
|
+
Downloads GHS-BUILT-S R2023A built-up surface grids as GeoTIFF files from
|
|
4
|
+
the EU Joint Research Centre (JRC/Copernicus). Each epoch is a single
|
|
5
|
+
global WGS84 raster at 30-arcsecond (~1 km) resolution. Values are
|
|
6
|
+
built-up surface area in m² per pixel (uint32).
|
|
7
|
+
|
|
8
|
+
Files are downloaded as ZIP archives, extracted to data_dir with
|
|
9
|
+
original JRC filenames, and recorded in the provenance ledger.
|
|
10
|
+
|
|
11
|
+
Source: EU JRC Global Human Settlement Layer (CC-BY-4.0)
|
|
12
|
+
https://human-settlement.emergency.copernicus.eu/
|
|
13
|
+
|
|
14
|
+
Ref: ADR-034 (GHS-BUILT-S source selection), ADR-030 (tifffile tooling).
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
import io
|
|
20
|
+
import logging
|
|
21
|
+
import time
|
|
22
|
+
import zipfile
|
|
23
|
+
from dataclasses import dataclass
|
|
24
|
+
from pathlib import Path
|
|
25
|
+
|
|
26
|
+
import requests as _requests
|
|
27
|
+
|
|
28
|
+
from datafactory_harvester.sources import register_source
|
|
29
|
+
from datafactory_harvester.validation import validate_positive_int
|
|
30
|
+
from datafactory_http import request_with_retry
|
|
31
|
+
from datafactory_provenance import (
|
|
32
|
+
DIGEST_SCHEME,
|
|
33
|
+
LEDGER_VERSION,
|
|
34
|
+
append_ledger_entry,
|
|
35
|
+
compute_content_digest,
|
|
36
|
+
compute_file_digest,
|
|
37
|
+
last_digest_for_version,
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
logger = logging.getLogger(__name__)
|
|
41
|
+
|
|
42
|
+
DATASET_ID = "ghsbuilts"
|
|
43
|
+
|
|
44
|
+
KNOWN_EPOCHS = (
|
|
45
|
+
1975, 1980, 1985, 1990, 1995,
|
|
46
|
+
2000, 2005, 2010, 2015, 2020, 2025, 2030,
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
BASE_URL = (
|
|
50
|
+
"https://jeodpp.jrc.ec.europa.eu/ftp/jrc-opendata/GHSL"
|
|
51
|
+
"/GHS_BUILT_S_GLOBE_R2023A"
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
# ---- Config ----
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
@dataclass(frozen=True)
|
|
59
|
+
class GhsBuiltSConfig:
|
|
60
|
+
"""Configuration for harvesting GHS-BUILT-S built-up surface grids.
|
|
61
|
+
|
|
62
|
+
Each epoch is a single global GeoTIFF (WGS84, 30 arcsec).
|
|
63
|
+
Downloaded as ZIP from JRC, extracted with original filename.
|
|
64
|
+
"""
|
|
65
|
+
|
|
66
|
+
epochs: tuple[int, ...] = KNOWN_EPOCHS
|
|
67
|
+
resolution: str = "30ss"
|
|
68
|
+
crs: str = "4326"
|
|
69
|
+
release: str = "R2023A"
|
|
70
|
+
data_dir: Path = Path("data/raw/ghsbuilts")
|
|
71
|
+
ledger_path: Path = Path(
|
|
72
|
+
"provenance/ghsbuilts/ingestion_ledger.jsonl"
|
|
73
|
+
)
|
|
74
|
+
timeout: int = 600
|
|
75
|
+
|
|
76
|
+
def __post_init__(self) -> None:
|
|
77
|
+
if not self.epochs:
|
|
78
|
+
msg = "At least one epoch is required"
|
|
79
|
+
raise ValueError(msg)
|
|
80
|
+
for epoch in self.epochs:
|
|
81
|
+
if epoch not in KNOWN_EPOCHS:
|
|
82
|
+
msg = (
|
|
83
|
+
f"Unknown epoch {epoch}. "
|
|
84
|
+
f"Valid epochs: {KNOWN_EPOCHS}"
|
|
85
|
+
)
|
|
86
|
+
raise ValueError(msg)
|
|
87
|
+
validate_positive_int(self.timeout, "timeout")
|
|
88
|
+
|
|
89
|
+
def _stem(self, epoch: int) -> str:
|
|
90
|
+
return (
|
|
91
|
+
f"GHS_BUILT_S_E{epoch}_GLOBE"
|
|
92
|
+
f"_{self.release}_{self.crs}_{self.resolution}"
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
def download_url(self, epoch: int) -> str:
|
|
96
|
+
stem = self._stem(epoch)
|
|
97
|
+
return f"{BASE_URL}/{stem}/V1-0/{stem}_V1_0.zip"
|
|
98
|
+
|
|
99
|
+
def tif_filename(self, epoch: int) -> str:
|
|
100
|
+
return f"{self._stem(epoch)}_V1_0.tif"
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
# ---- Fetch ----
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def fetch_ghsbuilts(
|
|
107
|
+
config: GhsBuiltSConfig | None = None,
|
|
108
|
+
*,
|
|
109
|
+
force_refresh: bool = False,
|
|
110
|
+
) -> list[dict]:
|
|
111
|
+
"""Download GHS-BUILT-S GeoTIFF files from JRC.
|
|
112
|
+
|
|
113
|
+
For each epoch in config.epochs:
|
|
114
|
+
1. Check cache (TIF exists + ledger has digest) -> skip
|
|
115
|
+
2. Download ZIP archive (~178 MB per epoch)
|
|
116
|
+
3. Extract TIF to config.data_dir
|
|
117
|
+
4. Record provenance in ledger
|
|
118
|
+
|
|
119
|
+
Args:
|
|
120
|
+
config: Harvest configuration. Defaults to GhsBuiltSConfig().
|
|
121
|
+
force_refresh: Re-download even if cached.
|
|
122
|
+
|
|
123
|
+
Returns:
|
|
124
|
+
List of result dicts, one per epoch. Each contains
|
|
125
|
+
dataset, epoch, outcome, and content_digest.
|
|
126
|
+
|
|
127
|
+
Raises:
|
|
128
|
+
requests.ConnectionError: Network failure (after retries).
|
|
129
|
+
zipfile.BadZipFile: Downloaded content is not a valid ZIP.
|
|
130
|
+
"""
|
|
131
|
+
if config is None:
|
|
132
|
+
config = GhsBuiltSConfig()
|
|
133
|
+
|
|
134
|
+
config.data_dir.mkdir(parents=True, exist_ok=True)
|
|
135
|
+
|
|
136
|
+
results: list[dict] = []
|
|
137
|
+
for epoch in config.epochs:
|
|
138
|
+
result = _fetch_epoch(config, epoch, force_refresh)
|
|
139
|
+
results.append(result)
|
|
140
|
+
|
|
141
|
+
return results
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def _fetch_epoch(
|
|
145
|
+
config: GhsBuiltSConfig,
|
|
146
|
+
epoch: int,
|
|
147
|
+
force_refresh: bool,
|
|
148
|
+
) -> dict:
|
|
149
|
+
"""Fetch a single GHS-BUILT-S epoch."""
|
|
150
|
+
tif_name = config.tif_filename(epoch)
|
|
151
|
+
tif_path = config.data_dir / tif_name
|
|
152
|
+
version = f"E{epoch}"
|
|
153
|
+
|
|
154
|
+
# Cache check: file exists + ledger digest + file integrity
|
|
155
|
+
if not force_refresh and tif_path.exists():
|
|
156
|
+
previous = last_digest_for_version(
|
|
157
|
+
config.ledger_path, version
|
|
158
|
+
)
|
|
159
|
+
if previous is not None:
|
|
160
|
+
actual = compute_file_digest(tif_path)
|
|
161
|
+
if actual == previous:
|
|
162
|
+
logger.info(
|
|
163
|
+
"Epoch %d cached (digest: %s)",
|
|
164
|
+
epoch, previous,
|
|
165
|
+
)
|
|
166
|
+
return {
|
|
167
|
+
"dataset": DATASET_ID,
|
|
168
|
+
"epoch": epoch,
|
|
169
|
+
"outcome": "cached",
|
|
170
|
+
"content_digest": previous,
|
|
171
|
+
}
|
|
172
|
+
logger.warning(
|
|
173
|
+
"Epoch %d cached file digest mismatch "
|
|
174
|
+
"(expected %s, got %s) — re-downloading",
|
|
175
|
+
epoch, previous, actual,
|
|
176
|
+
)
|
|
177
|
+
|
|
178
|
+
# Download
|
|
179
|
+
url = config.download_url(epoch)
|
|
180
|
+
logger.info("Downloading epoch %d from %s ...", epoch, url)
|
|
181
|
+
t0 = time.monotonic()
|
|
182
|
+
|
|
183
|
+
try:
|
|
184
|
+
resp = request_with_retry(url, timeout=config.timeout)
|
|
185
|
+
except _requests.RequestException:
|
|
186
|
+
logger.error("Download failed for epoch %d: %s", epoch, url)
|
|
187
|
+
append_ledger_entry(config.ledger_path, {
|
|
188
|
+
"dataset": DATASET_ID,
|
|
189
|
+
"version": version,
|
|
190
|
+
"outcome": "failed",
|
|
191
|
+
"ledger_version": LEDGER_VERSION,
|
|
192
|
+
"digest_algorithm": DIGEST_SCHEME,
|
|
193
|
+
})
|
|
194
|
+
raise
|
|
195
|
+
|
|
196
|
+
content = resp.content
|
|
197
|
+
elapsed = time.monotonic() - t0
|
|
198
|
+
size_mb = len(content) / 1e6
|
|
199
|
+
logger.info(
|
|
200
|
+
"Downloaded %.0f MB in %.1fs", size_mb, elapsed
|
|
201
|
+
)
|
|
202
|
+
|
|
203
|
+
# Extract TIF from ZIP
|
|
204
|
+
try:
|
|
205
|
+
zf = zipfile.ZipFile(io.BytesIO(content))
|
|
206
|
+
except zipfile.BadZipFile:
|
|
207
|
+
logger.error("Bad ZIP file for epoch %d: %s", epoch, url)
|
|
208
|
+
append_ledger_entry(config.ledger_path, {
|
|
209
|
+
"dataset": DATASET_ID,
|
|
210
|
+
"version": version,
|
|
211
|
+
"outcome": "failed",
|
|
212
|
+
"ledger_version": LEDGER_VERSION,
|
|
213
|
+
"digest_algorithm": DIGEST_SCHEME,
|
|
214
|
+
})
|
|
215
|
+
raise
|
|
216
|
+
|
|
217
|
+
with zf:
|
|
218
|
+
if tif_name not in zf.namelist():
|
|
219
|
+
append_ledger_entry(config.ledger_path, {
|
|
220
|
+
"dataset": DATASET_ID,
|
|
221
|
+
"version": version,
|
|
222
|
+
"outcome": "failed",
|
|
223
|
+
"reason": (
|
|
224
|
+
f"Expected {tif_name} in ZIP but found: "
|
|
225
|
+
f"{zf.namelist()}"
|
|
226
|
+
),
|
|
227
|
+
"ledger_version": LEDGER_VERSION,
|
|
228
|
+
"digest_algorithm": DIGEST_SCHEME,
|
|
229
|
+
})
|
|
230
|
+
msg = (
|
|
231
|
+
f"Expected {tif_name} in ZIP for epoch {epoch} "
|
|
232
|
+
f"but found: {zf.namelist()} — "
|
|
233
|
+
f"JRC naming convention may have changed"
|
|
234
|
+
)
|
|
235
|
+
logger.error(msg)
|
|
236
|
+
raise ValueError(msg)
|
|
237
|
+
|
|
238
|
+
tif_data = zf.read(tif_name)
|
|
239
|
+
|
|
240
|
+
tif_path.parent.mkdir(parents=True, exist_ok=True)
|
|
241
|
+
tif_path.write_bytes(tif_data)
|
|
242
|
+
|
|
243
|
+
# Provenance
|
|
244
|
+
content_digest = compute_content_digest(tif_data)
|
|
245
|
+
|
|
246
|
+
append_ledger_entry(config.ledger_path, {
|
|
247
|
+
"dataset": DATASET_ID,
|
|
248
|
+
"version": version,
|
|
249
|
+
"epoch": epoch,
|
|
250
|
+
"content_digest": content_digest,
|
|
251
|
+
"outcome": "success",
|
|
252
|
+
"size_bytes": len(tif_data),
|
|
253
|
+
"ledger_version": LEDGER_VERSION,
|
|
254
|
+
"digest_algorithm": DIGEST_SCHEME,
|
|
255
|
+
})
|
|
256
|
+
|
|
257
|
+
logger.info(
|
|
258
|
+
"Epoch %d: %s (%.1f MB, digest %s)",
|
|
259
|
+
epoch, tif_path, len(tif_data) / 1e6, content_digest,
|
|
260
|
+
)
|
|
261
|
+
|
|
262
|
+
return {
|
|
263
|
+
"dataset": DATASET_ID,
|
|
264
|
+
"epoch": epoch,
|
|
265
|
+
"outcome": "success",
|
|
266
|
+
"content_digest": content_digest,
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
# Auto-register
|
|
271
|
+
register_source("ghsbuilts", fetch_ghsbuilts)
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
"""GHS-POP population grid harvester — GeoTIFF download from JRC.
|
|
2
|
+
|
|
3
|
+
Downloads GHS-POP R2023A population grids as GeoTIFF files from
|
|
4
|
+
the EU Joint Research Centre (JRC/Copernicus). Each epoch is a
|
|
5
|
+
single global WGS84 raster at 30-arcsecond (~1 km) resolution.
|
|
6
|
+
|
|
7
|
+
Files are downloaded as ZIP archives, extracted to data_dir with
|
|
8
|
+
original JRC filenames, and recorded in the provenance ledger.
|
|
9
|
+
|
|
10
|
+
Source: EU JRC Global Human Settlement Layer (CC-BY-4.0)
|
|
11
|
+
https://human-settlement.emergency.copernicus.eu/
|
|
12
|
+
|
|
13
|
+
Ref: ADR-029 (GHS-POP source selection), ADR-030 (tifffile tooling).
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import io
|
|
19
|
+
import logging
|
|
20
|
+
import time
|
|
21
|
+
import zipfile
|
|
22
|
+
from dataclasses import dataclass
|
|
23
|
+
from pathlib import Path
|
|
24
|
+
|
|
25
|
+
import requests as _requests
|
|
26
|
+
|
|
27
|
+
from datafactory_harvester.sources import register_source
|
|
28
|
+
from datafactory_harvester.validation import validate_positive_int
|
|
29
|
+
from datafactory_http import request_with_retry
|
|
30
|
+
from datafactory_provenance import (
|
|
31
|
+
DIGEST_SCHEME,
|
|
32
|
+
LEDGER_VERSION,
|
|
33
|
+
append_ledger_entry,
|
|
34
|
+
compute_content_digest,
|
|
35
|
+
compute_file_digest,
|
|
36
|
+
last_digest_for_version,
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
logger = logging.getLogger(__name__)
|
|
40
|
+
|
|
41
|
+
DATASET_ID = "ghspop"
|
|
42
|
+
|
|
43
|
+
KNOWN_EPOCHS = (
|
|
44
|
+
1975, 1980, 1985, 1990, 1995,
|
|
45
|
+
2000, 2005, 2010, 2015, 2020, 2025, 2030,
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
BASE_URL = (
|
|
49
|
+
"https://jeodpp.jrc.ec.europa.eu/ftp/jrc-opendata/GHSL"
|
|
50
|
+
"/GHS_POP_GLOBE_R2023A"
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
# ---- Config ----
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
@dataclass(frozen=True)
|
|
58
|
+
class GhsPopConfig:
|
|
59
|
+
"""Configuration for harvesting GHS-POP population grids.
|
|
60
|
+
|
|
61
|
+
Each epoch is a single global GeoTIFF (WGS84, 30 arcsec).
|
|
62
|
+
Downloaded as ZIP from JRC, extracted with original filename.
|
|
63
|
+
"""
|
|
64
|
+
|
|
65
|
+
epochs: tuple[int, ...] = KNOWN_EPOCHS
|
|
66
|
+
resolution: str = "30ss"
|
|
67
|
+
crs: str = "4326"
|
|
68
|
+
release: str = "R2023A"
|
|
69
|
+
data_dir: Path = Path("data/raw/ghspop")
|
|
70
|
+
ledger_path: Path = Path(
|
|
71
|
+
"provenance/ghspop/ingestion_ledger.jsonl"
|
|
72
|
+
)
|
|
73
|
+
timeout: int = 600
|
|
74
|
+
|
|
75
|
+
def __post_init__(self) -> None:
|
|
76
|
+
for epoch in self.epochs:
|
|
77
|
+
if epoch not in KNOWN_EPOCHS:
|
|
78
|
+
msg = (
|
|
79
|
+
f"Unknown epoch {epoch}. "
|
|
80
|
+
f"Valid epochs: {KNOWN_EPOCHS}"
|
|
81
|
+
)
|
|
82
|
+
raise ValueError(msg)
|
|
83
|
+
validate_positive_int(self.timeout, "timeout")
|
|
84
|
+
|
|
85
|
+
def _stem(self, epoch: int) -> str:
|
|
86
|
+
return (
|
|
87
|
+
f"GHS_POP_E{epoch}_GLOBE"
|
|
88
|
+
f"_{self.release}_{self.crs}_{self.resolution}"
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
def download_url(self, epoch: int) -> str:
|
|
92
|
+
stem = self._stem(epoch)
|
|
93
|
+
return f"{BASE_URL}/{stem}/V1-0/{stem}_V1_0.zip"
|
|
94
|
+
|
|
95
|
+
def tif_filename(self, epoch: int) -> str:
|
|
96
|
+
return f"{self._stem(epoch)}_V1_0.tif"
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
# ---- Fetch ----
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def fetch_ghspop(
|
|
103
|
+
config: GhsPopConfig | None = None,
|
|
104
|
+
*,
|
|
105
|
+
force_refresh: bool = False,
|
|
106
|
+
) -> list[dict]:
|
|
107
|
+
"""Download GHS-POP GeoTIFF files from JRC.
|
|
108
|
+
|
|
109
|
+
For each epoch in config.epochs:
|
|
110
|
+
1. Check cache (TIF exists + ledger has digest) → skip
|
|
111
|
+
2. Download ZIP archive (~450 MB per epoch)
|
|
112
|
+
3. Extract TIF to config.data_dir
|
|
113
|
+
4. Record provenance in ledger
|
|
114
|
+
|
|
115
|
+
Args:
|
|
116
|
+
config: Harvest configuration. Defaults to GhsPopConfig().
|
|
117
|
+
force_refresh: Re-download even if cached.
|
|
118
|
+
|
|
119
|
+
Returns:
|
|
120
|
+
List of result dicts, one per epoch. Each contains
|
|
121
|
+
dataset, epoch, outcome, and content_digest.
|
|
122
|
+
|
|
123
|
+
Raises:
|
|
124
|
+
requests.ConnectionError: Network failure (after retries).
|
|
125
|
+
zipfile.BadZipFile: Downloaded content is not a valid ZIP.
|
|
126
|
+
"""
|
|
127
|
+
if config is None:
|
|
128
|
+
config = GhsPopConfig()
|
|
129
|
+
|
|
130
|
+
config.data_dir.mkdir(parents=True, exist_ok=True)
|
|
131
|
+
|
|
132
|
+
results: list[dict] = []
|
|
133
|
+
for epoch in config.epochs:
|
|
134
|
+
result = _fetch_epoch(config, epoch, force_refresh)
|
|
135
|
+
results.append(result)
|
|
136
|
+
|
|
137
|
+
return results
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def _fetch_epoch(
|
|
141
|
+
config: GhsPopConfig,
|
|
142
|
+
epoch: int,
|
|
143
|
+
force_refresh: bool,
|
|
144
|
+
) -> dict:
|
|
145
|
+
"""Fetch a single GHS-POP epoch."""
|
|
146
|
+
tif_name = config.tif_filename(epoch)
|
|
147
|
+
tif_path = config.data_dir / tif_name
|
|
148
|
+
version = f"E{epoch}"
|
|
149
|
+
|
|
150
|
+
# Cache check: file exists + ledger digest + file integrity
|
|
151
|
+
if not force_refresh and tif_path.exists():
|
|
152
|
+
previous = last_digest_for_version(
|
|
153
|
+
config.ledger_path, version
|
|
154
|
+
)
|
|
155
|
+
if previous is not None:
|
|
156
|
+
actual = compute_file_digest(tif_path)
|
|
157
|
+
if actual == previous:
|
|
158
|
+
logger.info(
|
|
159
|
+
"Epoch %d cached (digest: %s)",
|
|
160
|
+
epoch, previous,
|
|
161
|
+
)
|
|
162
|
+
return {
|
|
163
|
+
"dataset": DATASET_ID,
|
|
164
|
+
"epoch": epoch,
|
|
165
|
+
"outcome": "cached",
|
|
166
|
+
"content_digest": previous,
|
|
167
|
+
}
|
|
168
|
+
logger.warning(
|
|
169
|
+
"Epoch %d cached file digest mismatch "
|
|
170
|
+
"(expected %s, got %s) — re-downloading",
|
|
171
|
+
epoch, previous, actual,
|
|
172
|
+
)
|
|
173
|
+
|
|
174
|
+
# Download
|
|
175
|
+
url = config.download_url(epoch)
|
|
176
|
+
logger.info("Downloading epoch %d from %s ...", epoch, url)
|
|
177
|
+
t0 = time.monotonic()
|
|
178
|
+
|
|
179
|
+
try:
|
|
180
|
+
resp = request_with_retry(url, timeout=config.timeout)
|
|
181
|
+
except _requests.RequestException:
|
|
182
|
+
logger.error("Download failed for epoch %d: %s", epoch, url)
|
|
183
|
+
append_ledger_entry(config.ledger_path, {
|
|
184
|
+
"dataset": DATASET_ID,
|
|
185
|
+
"version": version,
|
|
186
|
+
"outcome": "failed",
|
|
187
|
+
"ledger_version": LEDGER_VERSION,
|
|
188
|
+
"digest_algorithm": DIGEST_SCHEME,
|
|
189
|
+
})
|
|
190
|
+
raise
|
|
191
|
+
|
|
192
|
+
content = resp.content
|
|
193
|
+
elapsed = time.monotonic() - t0
|
|
194
|
+
size_mb = len(content) / 1e6
|
|
195
|
+
logger.info(
|
|
196
|
+
"Downloaded %.0f MB in %.1fs", size_mb, elapsed
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
# Extract TIF from ZIP
|
|
200
|
+
try:
|
|
201
|
+
zf = zipfile.ZipFile(io.BytesIO(content))
|
|
202
|
+
except zipfile.BadZipFile:
|
|
203
|
+
logger.error("Bad ZIP file for epoch %d: %s", epoch, url)
|
|
204
|
+
append_ledger_entry(config.ledger_path, {
|
|
205
|
+
"dataset": DATASET_ID,
|
|
206
|
+
"version": version,
|
|
207
|
+
"outcome": "failed",
|
|
208
|
+
"ledger_version": LEDGER_VERSION,
|
|
209
|
+
"digest_algorithm": DIGEST_SCHEME,
|
|
210
|
+
})
|
|
211
|
+
raise
|
|
212
|
+
|
|
213
|
+
with zf:
|
|
214
|
+
if tif_name not in zf.namelist():
|
|
215
|
+
append_ledger_entry(config.ledger_path, {
|
|
216
|
+
"dataset": DATASET_ID,
|
|
217
|
+
"version": version,
|
|
218
|
+
"outcome": "failed",
|
|
219
|
+
"reason": (
|
|
220
|
+
f"Expected {tif_name} in ZIP but found: "
|
|
221
|
+
f"{zf.namelist()}"
|
|
222
|
+
),
|
|
223
|
+
"ledger_version": LEDGER_VERSION,
|
|
224
|
+
"digest_algorithm": DIGEST_SCHEME,
|
|
225
|
+
})
|
|
226
|
+
msg = (
|
|
227
|
+
f"Expected {tif_name} in ZIP for epoch {epoch} "
|
|
228
|
+
f"but found: {zf.namelist()} — "
|
|
229
|
+
f"JRC naming convention may have changed"
|
|
230
|
+
)
|
|
231
|
+
logger.error(msg)
|
|
232
|
+
raise ValueError(msg)
|
|
233
|
+
|
|
234
|
+
tif_data = zf.read(tif_name)
|
|
235
|
+
|
|
236
|
+
tif_path.parent.mkdir(parents=True, exist_ok=True)
|
|
237
|
+
tif_path.write_bytes(tif_data)
|
|
238
|
+
|
|
239
|
+
# Provenance
|
|
240
|
+
content_digest = compute_content_digest(tif_data)
|
|
241
|
+
|
|
242
|
+
append_ledger_entry(config.ledger_path, {
|
|
243
|
+
"dataset": DATASET_ID,
|
|
244
|
+
"version": version,
|
|
245
|
+
"epoch": epoch,
|
|
246
|
+
"content_digest": content_digest,
|
|
247
|
+
"outcome": "success",
|
|
248
|
+
"size_bytes": len(tif_data),
|
|
249
|
+
"ledger_version": LEDGER_VERSION,
|
|
250
|
+
"digest_algorithm": DIGEST_SCHEME,
|
|
251
|
+
})
|
|
252
|
+
|
|
253
|
+
logger.info(
|
|
254
|
+
"Epoch %d: %s (%.1f MB, digest %s)",
|
|
255
|
+
epoch, tif_path, len(tif_data) / 1e6, content_digest,
|
|
256
|
+
)
|
|
257
|
+
|
|
258
|
+
return {
|
|
259
|
+
"dataset": DATASET_ID,
|
|
260
|
+
"epoch": epoch,
|
|
261
|
+
"outcome": "success",
|
|
262
|
+
"content_digest": content_digest,
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
# Auto-register
|
|
267
|
+
register_source("ghspop", fetch_ghspop)
|