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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
"""
Based on the logic in the PROJ projsync CLI program
https://github.com/OSGeo/PROJ/blob/9ff543c4ffd86152bc58d0a0164b2ce9ebbb8bec/src/apps/projsync.cpp
"""
import hashlib
import json
import os
from datetime import datetime
from functools import partial
from pathlib import Path
from typing import Any
from urllib.request import urlretrieve
from pyproj._sync import get_proj_endpoint
from pyproj.aoi import BBox
from pyproj.datadir import get_data_dir, get_user_data_dir
def _bbox_from_coords(coords: list) -> BBox | None:
"""
Get the bounding box from coordinates
"""
try:
xxx, yyy = zip(*coords)
return BBox(west=min(xxx), south=min(yyy), east=max(xxx), north=max(yyy))
except ValueError:
pass
coord_bbox = None
for coord_set in coords:
bbox = _bbox_from_coords(coord_set)
if bbox is None:
continue
if coord_bbox is None:
coord_bbox = bbox
else:
coord_bbox.west = min(coord_bbox.west, bbox.west)
coord_bbox.south = min(coord_bbox.south, bbox.south)
coord_bbox.north = max(coord_bbox.north, bbox.north)
coord_bbox.east = max(coord_bbox.east, bbox.east)
return coord_bbox
def _bbox_from_geom(geom: dict[str, Any]) -> BBox | None:
"""
Get the bounding box from geojson geometry
"""
if "coordinates" not in geom or "type" not in geom:
return None
coordinates = geom["coordinates"]
if geom["type"] != "MultiPolygon":
return _bbox_from_coords(coordinates)
found_minus_180 = False
found_plus_180 = False
bboxes = []
for coordinate_set in coordinates:
bbox = _bbox_from_coords(coordinate_set)
if bbox is None:
continue
if bbox.west == -180:
found_minus_180 = True
elif bbox.east == 180:
found_plus_180 = True
bboxes.append(bbox)
grid_bbox = None
for bbox in bboxes:
if found_minus_180 and found_plus_180 and bbox.west == -180:
bbox.west = 180
bbox.east += 360
if grid_bbox is None:
grid_bbox = bbox
else:
grid_bbox.west = min(grid_bbox.west, bbox.west)
grid_bbox.south = min(grid_bbox.south, bbox.south)
grid_bbox.north = max(grid_bbox.north, bbox.north)
grid_bbox.east = max(grid_bbox.east, bbox.east)
return grid_bbox
def _filter_bbox(
feature: dict[str, Any], bbox: BBox, spatial_test: str, include_world_coverage: bool
) -> bool:
"""
Filter by the bounding box. Designed to use with 'filter'
"""
geom = feature.get("geometry")
if geom is not None:
geom_bbox = _bbox_from_geom(geom)
if geom_bbox is None:
return False
if (
geom_bbox.east - geom_bbox.west > 359
and geom_bbox.north - geom_bbox.south > 179
):
if not include_world_coverage:
return False
geom_bbox.west = -float("inf")
geom_bbox.east = float("inf")
elif geom_bbox.east > 180 and bbox.west < -180:
geom_bbox.west -= 360
geom_bbox.east -= 360
return getattr(bbox, spatial_test)(geom_bbox)
return False
def _filter_properties(
feature: dict[str, Any],
source_id: str | None = None,
area_of_use: str | None = None,
filename: str | None = None,
) -> bool:
"""
Filter by the properties. Designed to use with 'filter'
"""
properties = feature.get("properties")
if not properties:
return False
p_filename = properties.get("name")
p_source_id = properties.get("source_id")
if not p_filename or not p_source_id:
return False
source_id__matched = source_id is None or source_id in p_source_id
area_of_use__matched = area_of_use is None or area_of_use in properties.get(
"area_of_use", ""
)
filename__matched = filename is None or filename in p_filename
if source_id__matched and area_of_use__matched and filename__matched:
return True
return False
def _is_download_needed(grid_name: str) -> bool:
"""
Run through all of the PROJ directories to see if the
file already exists.
"""
if Path(get_user_data_dir(), grid_name).exists():
return False
for data_dir in get_data_dir().split(os.pathsep):
if Path(data_dir, grid_name).exists():
return False
return True
def _filter_download_needed(feature: dict[str, Any]) -> bool:
"""
Filter grids so only those that need to be downloaded are included.
"""
properties = feature.get("properties")
if not properties:
return False
filename = properties.get("name")
if not filename:
return False
return _is_download_needed(filename)
def _sha256sum(input_file):
"""
Return sha256 checksum of file given by path.
"""
hasher = hashlib.sha256()
with open(input_file, "rb") as file:
for chunk in iter(lambda: file.read(65536), b""):
hasher.update(chunk)
return hasher.hexdigest()
def _download_resource_file(
file_url, short_name, directory, verbose=False, sha256=None
):
"""
Download resource file from PROJ url
"""
if verbose:
print(f"Downloading: {file_url}")
tmp_path = Path(directory, f"{short_name}.part")
try:
urlretrieve(file_url, tmp_path)
if sha256 is not None and sha256 != _sha256sum(tmp_path):
raise RuntimeError(f"SHA256 mismatch: {short_name}")
tmp_path.replace(Path(directory, short_name))
finally:
try:
os.remove(tmp_path)
except FileNotFoundError:
pass
def _load_grid_geojson(target_directory: str | Path | None = None) -> dict[str, Any]:
"""
Returns
-------
dict[str, Any]:
The PROJ grid data list.
"""
if target_directory is None:
target_directory = get_user_data_dir(True)
local_path = Path(target_directory, "files.geojson")
if not local_path.exists() or (
(datetime.now() - datetime.fromtimestamp(local_path.stat().st_mtime)).days > 0
):
_download_resource_file(
file_url=f"{get_proj_endpoint()}/files.geojson",
short_name="files.geojson",
directory=target_directory,
)
return json.loads(local_path.read_text(encoding="utf-8"))
def get_transform_grid_list(
source_id: str | None = None,
area_of_use: str | None = None,
filename: str | None = None,
bbox: BBox | None = None,
spatial_test: str = "intersects",
include_world_coverage: bool = True,
include_already_downloaded: bool = False,
target_directory: str | Path | None = None,
) -> tuple:
"""
Get a list of transform grids that can be downloaded.
Parameters
----------
source_id: str, optional
area_of_use: str, optional
filename: str, optional
bbox: BBox, optional
spatial_test: str, default="intersects"
Can be "contains" or "intersects".
include_world_coverage: bool, default=True
If True, it will include grids with a global extent.
include_already_downloaded: bool, default=False
If True, it will list grids regardless of if they are downloaded.
target_directory: str | Path, optional
The directory to download the geojson file to.
Default is the user writable directory.
Returns
-------
list[dict[str, Any]]:
A list of geojson data of containing information about features
that can be downloaded.
"""
features = _load_grid_geojson(target_directory=target_directory)["features"]
if bbox is not None:
if bbox.west > 180 and bbox.east > bbox.west:
bbox.west -= 360
bbox.east -= 360
elif bbox.west < -180 and bbox.east > bbox.west:
bbox.west += 360
bbox.east += 360
elif abs(bbox.west) < 180 and abs(bbox.east) < 180 and bbox.east < bbox.west:
bbox.east += 360
features = filter(
partial(
_filter_bbox,
bbox=bbox,
spatial_test=spatial_test,
include_world_coverage=include_world_coverage,
),
features,
)
# filter by properties
features = filter(
partial(
_filter_properties,
source_id=source_id,
area_of_use=area_of_use,
filename=filename,
),
features,
)
if include_already_downloaded:
return tuple(features)
return tuple(filter(_filter_download_needed, features))
|