From 048796b3a9bda17666127df81ce494476608cd27 Mon Sep 17 00:00:00 2001 From: Rose Pearson Date: Wed, 18 Jan 2023 12:48:41 +1300 Subject: [PATCH] 34 - Support raster query with MultiPolygons (#39) * Update raster.py - Support multipolygons for raster download. * Update linux-test.yml - Change to fix breaking issue in mamba. * Update package version --- .github/workflows/linux-test.yml | 17 ++++++++++++----- pyproject.toml | 2 +- src/geoapis/__init__.py | 2 +- src/geoapis/raster.py | 28 ++++++++++++++++++++++------ 4 files changed, 36 insertions(+), 13 deletions(-) diff --git a/.github/workflows/linux-test.yml b/.github/workflows/linux-test.yml index 002db41..f1aa9e0 100644 --- a/.github/workflows/linux-test.yml +++ b/.github/workflows/linux-test.yml @@ -10,7 +10,7 @@ jobs: runs-on: ubuntu-latest defaults: run: - shell: bash -l {0} + shell: bash -el {0} strategy: fail-fast: false matrix: @@ -21,6 +21,7 @@ jobs: - name: Checkout github repo uses: actions/checkout@v2 + - name: Cache conda uses: actions/cache@v2 env: @@ -29,27 +30,31 @@ jobs: path: ~/conda_pkgs_dir key: ${{ runner.os }}-conda-${{ env.cache-name }}-${{ hashFiles('environment.yml') }} + - name: Install package dependencies with setup-miniconda@v2 uses: conda-incubator/setup-miniconda@v2 with: + python-version: ${{ matrix.python-version }} + miniforge-variant: Mambaforge + channels: conda-forge + channel-priority: true activate-environment: geoapis environment-file: environment.yml auto-activate-base: false - channels: conda-forge - channel-priority: strict use-only-tar-bz2: true # IMPORTANT: This needs to be set for caching to work properly! - python-version: ${{ matrix.python-version }} - mamba-version: "*" - run: | conda info conda list conda config --show-sources conda config --show + printenv | sort + - name: Install test dependencies run: | mamba install flake8 pytest + - name: Create .env file with API keys env: ENV_BASE64: ${{ secrets.ENV_BASE64 }} @@ -57,6 +62,7 @@ jobs: echo import .env file contents stored in a GitHub secret and regenerate the expected .env file echo $ENV_BASE64 | base64 -d > .env + - name: Run lint with flake8 run: | # stop the build if there are Python syntax errors or undefined names @@ -64,6 +70,7 @@ jobs: # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics + - name: Run tests with pytest run: | pytest diff --git a/pyproject.toml b/pyproject.toml index ed5193d..cdaa869 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta" [project] name = "geoapis" -version = "0.3.1" +version = "0.3.2" description = "A package for downloading geospatial data from web APIs." readme = "README.md" authors = [{ name = "Rose pearson", email = "rose.pearson@niwa.co.nz" }] diff --git a/src/geoapis/__init__.py b/src/geoapis/__init__.py index 220cb3c..9c2ac8e 100644 --- a/src/geoapis/__init__.py +++ b/src/geoapis/__init__.py @@ -4,4 +4,4 @@ @author: pearsonra """ -__version__ = "0.3.1" +__version__ = "0.3.2" diff --git a/src/geoapis/raster.py b/src/geoapis/raster.py index 43e8a95..3f76e7d 100644 --- a/src/geoapis/raster.py +++ b/src/geoapis/raster.py @@ -102,12 +102,28 @@ def _set_up(self): self.bounding_polygon.to_crs(self.crs) # Enforce the bounding_polygon must be a single geometry if it exists if self.bounding_polygon is not None: - assert ( - len(self.bounding_polygon) == 1 - ), "The bounding polygon must be a single geometry" - assert ( - len(numpy.array(self.bounding_polygon.exterior.loc[0].coords)) < 1000 - ), "The bounding polygon must be lass than 1000 points" + self.bounding_polygon = self.bounding_polygon.explode(index_parts=False) + if not (self.bounding_polygon.type == "Polygon").all(): + logging.warning( + "All bounding_polygon parts aren't Polygon's. Ignoring" + f" those that aren't {self.bounding_polygon.geometry}" + ) + + self.bounding_polygon = self.bounding_polygon[ + self.bounding_polygon.type == "Polygon" + ] + number_of_coords = sum( + [ + len(polygon.coords) + for polygon in self.bounding_polygon.explode( + index_parts=False + ).exterior + ] + ) + assert number_of_coords < 1000, ( + "The bounding polygon must be less than 1000 points. Consider using the" + " bbox to simplify the geometry" + ) def run(self, layer: int) -> pathlib.Path: """Query for a specified layer and return a geopandas.GeoDataFrame of the vector