Skip to content

Commit

Permalink
small updates
Browse files Browse the repository at this point in the history
  • Loading branch information
carderne committed Feb 16, 2024
1 parent c0578a7 commit 57729b2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 32 deletions.
10 changes: 4 additions & 6 deletions gridfinder/gridfinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
17 changes: 9 additions & 8 deletions gridfinder/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Post-processing for gridfinder package.
"""

from pathlib import Path
from typing import Optional, Tuple, Union

import geopandas as gpd
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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)
Expand Down
30 changes: 15 additions & 15 deletions gridfinder/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions gridfinder/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 57729b2

Please sign in to comment.