diff --git a/gridfinder/gridfinder.py b/gridfinder/gridfinder.py index f0fc333..3762ff2 100644 --- a/gridfinder/gridfinder.py +++ b/gridfinder/gridfinder.py @@ -3,23 +3,21 @@ """ import pickle -import sys from heapq import heapify, heappop, heappush from math import sqrt -from pathlib import Path -from typing import List, Optional, Tuple, Union +from typing import List, Optional, Tuple import numba as nb import numpy as np import rasterio from affine import Affine -sys.setrecursionlimit(100000) +from gridfinder.util import Pathy def get_targets_costs( - targets_in: Union[str, Path], - costs_in: Union[str, Path], + targets_in: Pathy, + costs_in: Pathy, ) -> Tuple[np.ndarray, np.ndarray, Tuple[int, int], Affine]: """Load the targets and costs arrays from the given file paths. diff --git a/gridfinder/post.py b/gridfinder/post.py index f758551..c50e806 100644 --- a/gridfinder/post.py +++ b/gridfinder/post.py @@ -2,7 +2,6 @@ Post-processing for gridfinder package. """ -from pathlib import Path from typing import Optional, Tuple, Union import geopandas as gpd @@ -16,9 +15,11 @@ from shapely.geometry import LineString, Point from skimage.morphology import skeletonize +from gridfinder.util import Pathy + def threshold( - dists_in: Union[str, Path, np.ndarray], cutoff: float = 0.0 + dists_in: Union[Pathy, np.ndarray], cutoff: float = 0.0 ) -> Tuple[np.ndarray, Optional[Affine]]: """Convert distance array into binary array of connected locations. @@ -51,7 +52,7 @@ def threshold( return guess, affine -def thin(guess_in: Union[str, Path, np.ndarray]) -> Tuple[np.ndarray, Optional[Affine]]: +def thin(guess_in: Union[Pathy, np.ndarray]) -> Tuple[np.ndarray, Optional[Affine]]: """ Use scikit-image skeletonize to 'thin' the guess raster. @@ -80,7 +81,7 @@ def thin(guess_in: Union[str, Path, np.ndarray]) -> Tuple[np.ndarray, Optional[A return guess_skel, affine -def raster_to_lines(guess_skel_in: Union[str, Path]) -> gpd.GeoDataFrame: +def raster_to_lines(guess_skel_in: Pathy) -> gpd.GeoDataFrame: """ Convert thinned raster to linestring geometry. @@ -150,9 +151,9 @@ def raster_to_lines(guess_skel_in: Union[str, Path]) -> gpd.GeoDataFrame: def accuracy( - grid_in: Union[str, Path], - guess_in: Union[str, Path], - aoi_in: Union[str, Path], + grid_in: Pathy, + guess_in: Pathy, + aoi_in: Pathy, buffer_amount: float = 0.01, ) -> Tuple[float, float]: """Measure accuracy against a specified grid 'truth' file. @@ -172,7 +173,7 @@ def accuracy( aoi = gpd.read_file(aoi_in) grid_masked = gpd.read_file(grid_in, mask=aoi) - grid = gpd.sjoin(grid_masked, aoi, how="inner", op="intersects") + grid = gpd.sjoin(grid_masked, aoi, how="inner", predicate="intersects") grid = grid[grid_masked.columns] grid_buff = grid.buffer(buffer_amount) diff --git a/gridfinder/prepare.py b/gridfinder/prepare.py index 9e14cfb..45cae23 100644 --- a/gridfinder/prepare.py +++ b/gridfinder/prepare.py @@ -19,13 +19,13 @@ from rasterio.warp import Resampling, reproject from scipy import signal -from gridfinder.util import clip_raster, save_raster +from gridfinder.util import Pathy, clip_raster, save_raster def clip_rasters( - folder_in: Union[str, Path], - folder_out: Union[str, Path], - aoi_in: Union[str, Path], + folder_in: Pathy, + folder_out: Pathy, + aoi_in: Pathy, debug: bool = False, ) -> None: """Read continental rasters one at a time, clip to AOI and save @@ -58,7 +58,7 @@ def clip_rasters( def merge_rasters( - folder: Union[str, Path], + folder: Pathy, percentile: int = 70, ) -> Tuple[np.ndarray, Optional[Affine]]: """Merge a set of monthly rasters keeping the nth percentile value. @@ -119,8 +119,8 @@ def create_filter() -> np.ndarray: def prepare_ntl( - ntl_in: Union[str, Path], - aoi_in: Union[str, Path], + ntl_in: Pathy, + aoi_in: Pathy, ntl_filter: Optional[np.ndarray] = None, threshold: float = 0.1, upsample_by: int = 2, @@ -204,9 +204,9 @@ def prepare_ntl( def drop_zero_pop( - targets_in: Union[str, Path], - pop_in: Union[str, Path], - aoi: Union[str, Path, GeoDataFrame], + targets_in: Pathy, + pop_in: Pathy, + aoi: Union[Pathy, GeoDataFrame], ) -> np.ndarray: """Drop electrified cells with no other evidence of human activity. @@ -297,9 +297,9 @@ def add_around(blob: list, cell: Tuple[int, int]) -> list: def prepare_roads( - roads_in: Union[str, Path], - aoi_in: Union[str, Path, GeoDataFrame], - ntl_in: Union[str, Path], + roads_in: Pathy, + aoi_in: Union[Pathy, GeoDataFrame], + ntl_in: Pathy, ) -> Tuple[np.ndarray, Affine]: """Prepare a roads feature layer for use in algorithm. @@ -327,10 +327,10 @@ def prepare_roads( aoi = gpd.read_file(aoi_in) roads_masked = gpd.read_file(roads_in, mask=aoi) - roads = gpd.sjoin(roads_masked, aoi, how="inner", op="intersects") + roads = gpd.sjoin(roads_masked, aoi, how="inner", predicate="intersects") roads = roads[roads_masked.columns] - roads["weight"] = 1 + roads["weight"] = 1.0 roads.loc[roads["highway"] == "motorway", "weight"] = 1 / 10 roads.loc[roads["highway"] == "trunk", "weight"] = 1 / 9 roads.loc[roads["highway"] == "primary", "weight"] = 1 / 8 diff --git a/gridfinder/util.py b/gridfinder/util.py index 85bb3f4..afe3561 100644 --- a/gridfinder/util.py +++ b/gridfinder/util.py @@ -15,9 +15,11 @@ from rasterio.io import DatasetReader from rasterio.mask import mask +Pathy = Union[str, Path] + def save_raster( - path: Union[str, Path], + path: Pathy, raster: np.ndarray, affine: Affine, crs: Optional[Union[str, Proj]] = None, @@ -57,8 +59,8 @@ def save_raster( def clip_raster( - raster: Union[str, Path, DatasetReader], - boundary: Union[str, Path, GeoDataFrame], + raster: Union[Pathy, DatasetReader], + boundary: Union[Pathy, GeoDataFrame], boundary_layer: Optional[str] = None, ) -> Tuple[ np.ndarray,