Skip to content

Commit

Permalink
Merge pull request #57 from nismod/feature/damages
Browse files Browse the repository at this point in the history
Experimental flag
  • Loading branch information
tomalrussell authored Nov 27, 2023
2 parents 0fd8e06 + 837df0e commit e4b53b2
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 49 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Checkout submodules
run: git submodule update --init --recursive
Expand All @@ -31,7 +31,7 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Checkout submodules
run: git submodule update --init --recursive
Expand All @@ -55,7 +55,7 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Setup Pages
id: pages
uses: actions/configure-pages@v3
Expand All @@ -44,7 +44,7 @@ jobs:
popd
- name: Upload artifact
uses: actions/upload-pages-artifact@v1
uses: actions/upload-pages-artifact@v2
with:
path: docs/build/html

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
name: Build SDist
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Build SDist
run: pipx run build --sdist
Expand All @@ -30,7 +30,7 @@ jobs:
os: [ubuntu-latest, macos-latest, windows-latest]

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: pypa/[email protected]
env:
Expand Down
38 changes: 19 additions & 19 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
[project]
name="nismod-snail"
version="0.4.1"
license={file = "LICENSE"}
description="The spatial networks impact assessment library"
readme="README.md"
authors=[
{name="Tom Russell", email="[email protected]"},
{name="Thibault Lestang"},
{name="Raghav Pant"},
{name="Lena Fuldauer"},
name = "nismod-snail"
version = "0.5.0"
license = { file = "LICENSE" }
description = "The spatial networks impact assessment library"
readme = "README.md"
authors = [
{ name = "Tom Russell", email = "[email protected]" },
{ name = "Thibault Lestang" },
{ name = "Raghav Pant" },
{ name = "Lena Fuldauer" },
]
maintainers=[{name="Tom Russell", email="[email protected]"}]
classifiers=[
maintainers = [{ name = "Tom Russell", email = "[email protected]" }]
classifiers = [
"Development Status :: 4 - Beta",
"Intended Audience :: Science/Research",
"License :: OSI Approved :: MIT License",
Expand All @@ -20,9 +20,9 @@ classifiers=[
"Topic :: Scientific/Engineering :: GIS",
"Topic :: Utilities"
]
keywords=[]
requires-python=">=3.8"
dependencies=[
keywords = []
requires-python = ">=3.8"
dependencies = [
"geopandas",
"matplotlib",
"openpyxl",
Expand All @@ -36,7 +36,7 @@ dependencies=[
]

[project.optional-dependencies]
dev=[
dev = [
"affine",
"black",
"hilbertcurve",
Expand All @@ -45,8 +45,8 @@ dev=[
"pytest-cov",
"pytest",
]
docs=["sphinx", "m2r2"]
tutorials=[
docs = ["sphinx", "m2r2"]
tutorials = [
"contextily",
"irv_autopkg_client",
"jupyter",
Expand All @@ -62,7 +62,7 @@ Documentation = "https://nismod.github.io/snail/"
Repository = "https://github.com/nismod/snail.git"

[project.scripts]
snail="snail.cli:snail"
snail = "snail.cli:snail"

[build-system]
requires = ["scikit-build-core>=0.3.3", "pybind11"]
Expand Down
2 changes: 1 addition & 1 deletion src/snail/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from importlib.metadata import version

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


Expand Down
42 changes: 29 additions & 13 deletions src/snail/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
split_linestrings,
split_polygons,
split_points,
split_polygons_experimental,
)
from snail.io import (
read_raster_band_data,
Expand All @@ -32,6 +33,7 @@ def snail(args=None):
"""snail command"""
parser = argparse.ArgumentParser(prog="snail")
parser.add_argument("--verbose", "-v", action="count", default=0)
parser.add_argument("-x", "--experimental", action="store_true")
subparsers = parser.add_subparsers(help="Run a command")

parser_split = subparsers.add_parser(
Expand Down Expand Up @@ -190,26 +192,34 @@ def split(args):
geom_type = _sample_geom_type(features)

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

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

if args.attribute and args.raster:
Expand Down Expand Up @@ -280,10 +290,10 @@ def process(args):
)

for vector_layer in vector_layers.itertuples():
_process_layer(vector_layer, transforms, rasters)
_process_layer(vector_layer, transforms, rasters, args.experimental)


def _process_layer(vector_layer, transforms, rasters):
def _process_layer(vector_layer, transforms, rasters, experimental=False):
vector_path = Path(vector_layer.path)
layer = getattr(vector_layer, "layer", None)
logging.info("Processing %s", vector_path.name)
Expand All @@ -304,9 +314,15 @@ def _process_layer(vector_layer, transforms, rasters):
with_data = associate_raster_files(split, rasters)
elif "Polygon" in geom_type:
prepared = prepare_polygons(features)
split = split_features_for_rasters(
prepared, transforms, split_polygons
)
if experimental:
logging.info("Split polygons (experimental)")
split = split_features_for_rasters(
prepared, transforms, split_polygons_experimental
)
else:
split = split_features_for_rasters(
prepared, transforms, split_polygons
)
with_data = associate_raster_files(split, rasters)
else:
raise ValueError(f"Could not process vector data of type {geom_type}")
Expand Down
4 changes: 2 additions & 2 deletions src/snail/intersection.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from shapely.geometry import mapping, shape
from shapely.ops import linemerge, polygonize

from snail.core.intersections import (
from snail.core.intersections import ( # type: ignore
get_cell_indices,
split_linestring,
split_polygon,
Expand Down Expand Up @@ -96,7 +96,7 @@ def from_extent(
crs=crs,
width=math.ceil((xmax - xmin) / cell_width),
height=math.ceil((ymax - ymin) / cell_height),
transform=(cell_width, 0, xmin, 0, cell_height, ymin),
transform=(cell_width, 0.0, xmin, 0.0, cell_height, ymin),
)


Expand Down
8 changes: 4 additions & 4 deletions src/snail/io.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import importlib.util
import logging
from typing import List, Tuple

Expand Down Expand Up @@ -106,12 +107,11 @@ def read_features(path, layer=None):
if path.suffix in (".parquet", ".geoparquet"):
features = geopandas.read_parquet(path)
else:
try:
import pyogrio

if importlib.util.find_spec("pyogrio"):
engine = "pyogrio"
except ImportError:
else:
engine = "fiona"

if layer:
features = geopandas.read_file(path, layer=layer, engine=engine)
else:
Expand Down
6 changes: 3 additions & 3 deletions tests/test_intersection.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,11 @@ def test_split_linestrings(self, grid, linestrings, linestrings_split):
# Ideally we'd like to use geopandas.assert_geodataframe_equal to
# to compare both expected and actual geodfs, but this function offers
# little control over tolerance. When using option "check_less_precise",
# it used GeoSeries.geom_almost_equals under the hood, which has an kwarg
# "decimal". But assert_geodataframe_equal does not recognise kwarg "decimal".
# it uses GeoSeries.geom_equals_exact under the hood, which has an kwarg
# "tolerance". But assert_geodataframe_equal does not recognise kwarg "tolerance".
assert (
actual["geometry"]
.geom_almost_equals(expected_gdf["geometry"], decimal=3)
.geom_equals_exact(expected_gdf["geometry"], tolerance=1e-3)
.values.all()
)
assert_array_equal(actual["col1"].values, expected_gdf["col1"].values)
Expand Down

0 comments on commit e4b53b2

Please sign in to comment.