def test_sync_download(download_mock, parse_args_mock): main() download_mock.assert_called_with( directory=get_user_data_dir(), file_url="https://cdn.proj.org/fr_ign_ntf_r93.tif", sha256="0aa738b3e00fd2d64f8e3cd0e76034d4792374624fa0e133922433c9491bbf2a", short_name="fr_ign_ntf_r93.tif", verbose=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 grids_available(*grid_names, check_network=True, check_all=False): """ Check if the grids are available """ if check_network and os.environ.get("PROJ_NETWORK") == "ON": return True available = [(Path(get_data_dir(), grid_name).exists() or Path(get_user_data_dir(), grid_name).exists()) for grid_name in grid_names] if check_all: return all(available) return any(available)
def grids_available(*grid_names, check_network=True, check_all=False): """ Check if the grids are available """ if check_network and pyproj.network.is_network_enabled(): return True available = [ ( Path(get_data_dir(), grid_name).exists() or Path(get_user_data_dir(), grid_name).exists() ) for grid_name in grid_names ] if check_all: return all(available) return any(available)
def download_grids( self, directory: Optional[Union[str, Path]] = None, open_license: bool = True, verbose: bool = False, ) -> None: """ .. versionadded:: 3.0.0 Download missing grids that can be downloaded automatically. .. warning:: There are cases where the URL to download the grid is missing. In those cases, you can enable enable :ref:`debugging-internal-proj` and perform a transformation. The logs will show the grids PROJ searches for. Parameters ---------- directory: str or Path, optional The directory to download the grids to. Defaults to :func:`pyproj.datadir.get_user_data_dir` open_license: bool, optional If True, will only download grids with an open license. Defaults to False. verbose: bool, optional If True, will print information about grids downloaded. Default is False. """ if directory is None: directory = get_user_data_dir(True) for unavailable_operation in self.unavailable_operations: for grid in unavailable_operation.grids: if ( not grid.available and grid.url.endswith(grid.short_name) and grid.direct_download and (grid.open_license or not open_license) ): _download_resource_file( file_url=grid.url, short_name=grid.short_name, directory=directory, verbose=verbose, ) elif not grid.available and verbose: warnings.warn(f"Skipped: {grid}")
def _load_grid_geojson(target_directory=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.utcnow() - 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 main(): args = parser.parse_args() if hasattr(args, "bbox") and any(( args.bbox, args.list_files, args.all, args.source_id, args.area_of_use, args.file, )): if args.all and (args.list_files or args.source_id or args.area_of_use or args.file or args.bbox): raise RuntimeError( "Cannot use '--all' with '--list-files', '--source-id'," "'--area-of-use', '--bbox', or '--file'.") bbox = None if args.bbox is not None: west, south, east, north = args.bbox.split(",") bbox = BBox( west=float(west), south=float(south), east=float(east), north=float(north), ) if args.target_directory and args.system_directory: raise RuntimeError( "Cannot set both --target-directory and --system-directory.") target_directory = args.target_directory if args.system_directory: target_directory = get_data_dir().split(os.path.sep)[0] elif not target_directory: target_directory = get_user_data_dir(True) grids = get_transform_grid_list( source_id=args.source_id, area_of_use=args.area_of_use, filename=args.file, bbox=bbox, spatial_test=args.spatial_test, include_world_coverage=not args.exclude_world_coverage, include_already_downloaded=args.include_already_downloaded, target_directory=target_directory, ) if args.list_files: print("filename | source_id | area_of_use") print("----------------------------------") else: endpoint = get_proj_endpoint() for grid in grids: if args.list_files: print( grid["properties"]["name"], grid["properties"]["source_id"], grid["properties"].get("area_of_use"), sep=" | ", ) else: filename = grid["properties"]["name"] _download_resource_file( file_url=f"{endpoint}/{filename}", short_name=filename, directory=target_directory, verbose=args.verbose, sha256=grid["properties"]["sha256sum"], ) elif not hasattr(args, "bbox") and args.verbose: _show_versions.show_versions() elif hasattr(args, "bbox"): sync_parser.print_help() else: parser.print_help()
def test_get_user_data_dir(): assert get_user_data_dir().endswith("proj")