From aa7d39a17afe9ae572634e7d353f11ef80e5e6b0 Mon Sep 17 00:00:00 2001 From: Tom Russell Date: Wed, 4 Oct 2023 16:44:14 +0100 Subject: [PATCH 1/5] Bump workflow versions --- .github/workflows/build.yml | 6 +++--- .github/workflows/docs.yml | 4 ++-- .github/workflows/package.yml | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 0bbc3df..5e513cf 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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 @@ -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 @@ -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 diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index 9cadf61..c50cad4 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -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 @@ -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 diff --git a/.github/workflows/package.yml b/.github/workflows/package.yml index 61c51b2..834d348 100644 --- a/.github/workflows/package.yml +++ b/.github/workflows/package.yml @@ -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 @@ -30,7 +30,7 @@ jobs: os: [ubuntu-latest, macos-latest, windows-latest] steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: pypa/cibuildwheel@v2.13.1 env: From 07d51fc59e3165d729c2da5966f92d8b57e54eec Mon Sep 17 00:00:00 2001 From: Tom Russell Date: Mon, 27 Nov 2023 15:49:27 +0000 Subject: [PATCH 2/5] Add '-x' flag for experimental polygon splitting --- src/snail/cli.py | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/src/snail/cli.py b/src/snail/cli.py index 8ce26af..2698e91 100644 --- a/src/snail/cli.py +++ b/src/snail/cli.py @@ -18,6 +18,7 @@ split_linestrings, split_polygons, split_points, + split_polygons_experimental, ) from snail.io import ( read_raster_band_data, @@ -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( @@ -202,10 +204,18 @@ def split(args): 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) @@ -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) @@ -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}") From 22e0f64d1e6181e2f9c71a93a49a3659755ad2e1 Mon Sep 17 00:00:00 2001 From: Tom Russell Date: Mon, 27 Nov 2023 15:50:11 +0000 Subject: [PATCH 3/5] Bump version to 0.5.0 --- pyproject.toml | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index ce50209..8d2e04d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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="tomalrussell@gmail.com"}, - {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 = "tomalrussell@gmail.com" }, + { name = "Thibault Lestang" }, + { name = "Raghav Pant" }, + { name = "Lena Fuldauer" }, ] -maintainers=[{name="Tom Russell", email="tomalrussell@gmail.com"}] -classifiers=[ +maintainers = [{ name = "Tom Russell", email = "tomalrussell@gmail.com" }] +classifiers = [ "Development Status :: 4 - Beta", "Intended Audience :: Science/Research", "License :: OSI Approved :: MIT License", @@ -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", @@ -36,7 +36,7 @@ dependencies=[ ] [project.optional-dependencies] -dev=[ +dev = [ "affine", "black", "hilbertcurve", @@ -45,8 +45,8 @@ dev=[ "pytest-cov", "pytest", ] -docs=["sphinx", "m2r2"] -tutorials=[ +docs = ["sphinx", "m2r2"] +tutorials = [ "contextily", "irv_autopkg_client", "jupyter", @@ -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"] From a48df1061cea2c454ffd64f5bfad13e73963ddcb Mon Sep 17 00:00:00 2001 From: Tom Russell Date: Mon, 27 Nov 2023 15:55:43 +0000 Subject: [PATCH 4/5] Fix FutureWarning about use of geom_almost_equals FutureWarning: The 'geom_almost_equals()' method is deprecated because the name is confusing. The 'geom_equals_exact()' method should be used instead. --- tests/test_intersection.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_intersection.py b/tests/test_intersection.py index ec9c790..960258a 100644 --- a/tests/test_intersection.py +++ b/tests/test_intersection.py @@ -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) From 837df0e28d628ddd1f80bbb1e3f06956f650c445 Mon Sep 17 00:00:00 2001 From: Tom Russell Date: Mon, 27 Nov 2023 16:02:54 +0000 Subject: [PATCH 5/5] Fix lints --- src/snail/__init__.py | 2 +- src/snail/cli.py | 10 +++++----- src/snail/intersection.py | 4 ++-- src/snail/io.py | 8 ++++---- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/snail/__init__.py b/src/snail/__init__.py index f9fb3e3..8d47c91 100644 --- a/src/snail/__init__.py +++ b/src/snail/__init__.py @@ -12,7 +12,7 @@ from importlib.metadata import version __version__ = version("nismod-snail") -except: +except Exception: __version__ = "unknown" diff --git a/src/snail/cli.py b/src/snail/cli.py index 2698e91..dad5497 100644 --- a/src/snail/cli.py +++ b/src/snail/cli.py @@ -192,14 +192,14 @@ 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 ) @@ -219,7 +219,7 @@ def split(args): 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: diff --git a/src/snail/intersection.py b/src/snail/intersection.py index cfaf77a..82f5a69 100644 --- a/src/snail/intersection.py +++ b/src/snail/intersection.py @@ -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, @@ -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), ) diff --git a/src/snail/io.py b/src/snail/io.py index d56c253..65b2877 100644 --- a/src/snail/io.py +++ b/src/snail/io.py @@ -1,3 +1,4 @@ +import importlib.util import logging from typing import List, Tuple @@ -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: