Skip to content

Commit

Permalink
Merge pull request #55 from nismod/feature/damages
Browse files Browse the repository at this point in the history
Initial DamageCurve implementation plus..
  • Loading branch information
tomalrussell authored Aug 3, 2023
2 parents 8b46c10 + 4a315d9 commit e1bfe95
Show file tree
Hide file tree
Showing 24 changed files with 7,488 additions and 946 deletions.
14 changes: 14 additions & 0 deletions .environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,27 @@ dependencies:
- python=3.11
- affine
- black
- contextily
- geopandas
- jupyter
- matplotlib
- nbstripout
- networkx
- numpy
- openpyxl
- pandera
- pip
- pyarrow
- pyogrio
- pytest
- pytest-cov
- rasterio
- seaborn
- shapely
- scipy
- pip:
- hilbertcurve
- irv_autopkg_client
- python-igraph
- snkit
- tqdm
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,10 @@ The `snail.core.intersections` module is built using `pybind11` with
>
> Copyright (c) 2020-23 Tom Russell and all [snail contributors](https://github.com/nismod/snail/graphs/contributors)
This library is developed by researchers in the [Oxford Programme for Sustainable
This library is developed by researchers in the [Oxford Programme for Sustainable
Infrastructure Systems](https://opsis.eci.ox.ac.uk/) at the University of Oxford,
funded by multiple research projects.

This research received funding from the FCDO Climate Compatible Growth Programme.
The views expressed here do not necessarily reflect the UK government's official
This research received funding from the FCDO Climate Compatible Growth Programme.
The views expressed here do not necessarily reflect the UK government's official
policies.
17 changes: 16 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name="nismod-snail"
version="0.3.2"
version="0.4.0"
license={file = "LICENSE"}
description="The spatial networks impact assessment library"
readme="README.md"
Expand All @@ -24,22 +24,37 @@ keywords=[]
requires-python=">=3.8"
dependencies=[
"geopandas",
"matplotlib",
"openpyxl",
"pandera",
"pyarrow",
"pyogrio",
"python-igraph",
"rasterio",
"shapely",
"scipy"
]

[project.optional-dependencies]
dev=[
"affine",
"black",
"hilbertcurve",
"nbstripout",
"numpy",
"pytest-cov",
"pytest",
]
docs=["sphinx", "m2r2"]
tutorials=[
"contextily",
"irv_autopkg_client",
"jupyter",
"networkx",
"seaborn",
"snkit",
"tqdm",
]

[project.urls]
Homepage = "https://github.com/nismod/snail"
Expand Down
7 changes: 4 additions & 3 deletions src/snail/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""snail - the spatial networks impact assessment library
"""
import pkg_resources

# Import things to define what is accessible directly on snail, when a client
# writes::
Expand All @@ -10,8 +9,10 @@
# from snail.network import Network

try:
__version__ = pkg_resources.get_distribution(__name__).version
except pkg_resources.DistributionNotFound:
from importlib.metadata import version

__version__ = version("nismod-snail")
except:
__version__ = "unknown"


Expand Down
35 changes: 23 additions & 12 deletions src/snail/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
import pandas

from snail.intersection import (
Transform,
GridDefinition,
apply_indices,
get_raster_values_for_splits,
prepare_linestrings,
prepare_polygons,
prepare_points,
Expand All @@ -19,7 +20,7 @@
split_points,
)
from snail.io import (
associate_raster_file,
read_raster_band_data,
associate_raster_files,
read_features,
read_raster_metadata,
Expand Down Expand Up @@ -171,7 +172,7 @@ def snail(args=None):
def split(args):
"""snail split command"""
if args.raster:
transform, all_bands = read_raster_metadata(args.raster)
grid, all_bands = read_raster_metadata(args.raster)
else:
crs = None
width = args.width
Expand All @@ -181,26 +182,35 @@ def split(args):
sys.exit(
"Error: Expected either a raster file or transform, width and height of splitting grid"
)
transform = Transform(crs, width, height, affine_transform)
logging.info(f"Splitting grid {transform=}")
grid = GridDefinition(crs, width, height, affine_transform)
logging.info(f"Splitting {grid=}")

features = geopandas.read_file(args.features)
features = read_features(Path(args.features))
features_crs = features.crs
geom_type = _sample_geom_type(features)

if "Point" in geom_type:
logging.info(f"Preparing points")
prepared = prepare_points(features)
splits = split_points(prepared)
logging.info(f"Splitting points")
splits = split_features_for_rasters(prepared, [grid], split_points)
elif "LineString" in geom_type:
logging.info(f"Preparing linestrings")
prepared = prepare_linestrings(features)
splits = split_linestrings(prepared, transform)
logging.info(f"Splitting linestrings")
splits = split_features_for_rasters(
prepared, [grid], split_linestrings
)
elif "Polygon" in geom_type:
logging.info(f"Preparing polygons")
prepared = prepare_polygons(features)
splits = split_polygons(prepared, transform)
logging.info(f"Splitting polygons")
splits = split_features_for_rasters(prepared, [grid], split_polygons)
else:
raise ValueError("Could not process vector data of type %s", geom_type)

splits = apply_indices(splits, transform)
logging.info(f"Applying indices")
splits = apply_indices(splits, grid)

if args.attribute and args.raster:
if args.band:
Expand All @@ -225,9 +235,10 @@ def split(args):
args.raster,
band_index,
)
splits[key] = associate_raster_file(
splits, args.raster, band_number=int(band_index)
band_data = read_raster_band_data(
args.raster, band_number=int(band_index)
)
splits[key] = get_raster_values_for_splits(splits, band_data)

splits.set_crs(features_crs, inplace=True)
splits.to_file(args.output)
Expand Down
2 changes: 1 addition & 1 deletion src/snail/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .intersections import (get_cell_indices, split_linestring, split_polygon)
from .intersections import get_cell_indices, split_linestring, split_polygon

__all__ = [
"get_cell_indices",
Expand Down
Loading

0 comments on commit e1bfe95

Please sign in to comment.