Source code for pyproj.sync

Based on the logic in the PROJ projsync CLI program
import hashlib
import json
import os
from datetime import datetime
from functools import partial
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple
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) -> Optional[BBox]:
    Get the bounding box from coordinates
        xx, yy = zip(*coords)
        return BBox(west=min(xx), south=min(yy), east=max(xx), north=max(yy))
    except ValueError:
    coord_bbox = None
    for coord_set in coords:
        bbox = _bbox_from_coords(coord_set)
        if coord_bbox is None:
            coord_bbox = bbox
            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]) -> Optional[BBox]:
    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:
        if bbox.west == -180:
            found_minus_180 = True
        elif bbox.east == 180:
            found_plus_180 = True
    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
            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: Optional[str] = None,
    area_of_use: Optional[str] = None,
    filename: Optional[str] = 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
    if (
        (source_id is None or source_id in p_source_id)
        and (area_of_use is None or area_of_use in properties.get("area_of_use", ""))
        and (filename is None or filename in p_filename)
        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:, b""):

    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")
        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))
        except FileNotFoundError:

def _load_grid_geojson(target_directory=None) -> Dict[str, Any]:
    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.utcnow() - datetime.fromtimestamp(local_path.stat().st_mtime)).days
        > 0
    return json.loads(local_path.read_text(encoding="utf-8"))

[docs]def get_transform_grid_list( source_id: Optional[str] = None, area_of_use: Optional[str] = None, filename: Optional[str] = None, bbox: BBox = None, spatial_test: str = "intersects", include_world_coverage: bool = True, include_already_downloaded: bool = False, target_directory: str = 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, optional Can be "contains" or "intersects". Defaults to "intersects". include_world_coverage: bool, optional Defaults to True. include_already_downloaded: bool, optional If True, it will list grids regardless of if they are downloaded. Defaults to False. target_directory: Union[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))