Skip to content

Commit

Permalink
Optional dependencies and errors updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfgang committed Apr 25, 2024
1 parent bcc323a commit d85d2d1
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 27 deletions.
11 changes: 7 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@ on:
push:
pull_request:
workflow_dispatch:
schedule:
- cron: '0 0 * * *' # daily
#schedule:
# - cron: '0 0 * * *' # daily

jobs:
build:
name: Build py${{ matrix.python-version }} @ ${{ matrix.os }} 🐍
runs-on: ${{ matrix.os }}
strategy:
matrix:
python-version: ['3.8', '3.9', '3.10']
os: ["ubuntu-latest", "windows-latest"]
include:
- os: "ubuntu-latest"
python-version: '3.8' # first supported
- os: ["ubuntu-latest", "windows-latest", "macos-latest"]
python-version: '3.12' # latest supported
steps:
- uses: actions/checkout@v2
with:
Expand Down
12 changes: 6 additions & 6 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ name: pygeogrids
channels:
- conda-forge
dependencies:
- numpy
- pandas
- netcdf4
- geos
- gdal
- pyproj
- pykdtree
- scipy
- ipykernel
- pip
- pip:
- numpy
- pandas
- netCDF4
- pykdtree
- pyproj
- scipy
- sphinx
- nbsphinx
- sphinx_rtd_theme
Expand Down
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ package_dir =
install_requires =
importlib-metadata; python_version<"3.8"
numpy
pandas
netCDF4
pyproj
pykdtree
# The usage of test_requires is discouraged, see `Dependency Management` docs
tests_require = pytest; pytest-cov
# Require a specific Python version, e.g. Python 2.7 or >= 3.4
python_requires = >=3.6
python_requires = >=3.8

[options.packages.find]
where = src
Expand Down
1 change: 0 additions & 1 deletion src/pygeogrids/grids.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

try:
from osgeo import ogr

ogr_installed = True
except ImportError:
ogr_installed = False
Expand Down
12 changes: 5 additions & 7 deletions src/pygeogrids/shapefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@

try:
from osgeo import ogr

ogr_installed = True
except ImportError:
ogr_installed = False
Expand Down Expand Up @@ -97,9 +96,8 @@ def get_gad_grid_points(grid, gadm_shp_path, level, name=None, oid=None):
raise ValueError("Requested Object not found in shapefile.")

else:
raise ImportError(
"No supported implementation installed." "Please install gdal and osgeo."
)
raise ImportError("Could not import ogr from osgeo. "
"Please install them via `conda install geos gdal` first.")


class ShpReader:
Expand Down Expand Up @@ -138,8 +136,8 @@ def __init__(
to filter out the relevant polygons.
"""
if not ogr_installed:
raise ImportError("Could not import ogr/osr. "
"Please install `gdal` with conda first.")
raise ImportError("Could not import ogr from osgeo. "
"Please install them via `conda install geos gdal` first.")
self.shp_path = shp_path
self.driver = driver
self._init_open_shp()
Expand Down Expand Up @@ -213,7 +211,7 @@ def lookup_id(self, names: Union[str, list, np.ndarray]) -> np.ndarray:
rows = np.unique(np.where(np.isin(self.features.values, names))[0])
return self.features.index.values[rows]

def geom(self, id) -> ogr.Geometry:
def geom(self, id) -> 'ogr.Geometry':
"""
Get geometry of feature with passed id
"""
Expand Down
11 changes: 4 additions & 7 deletions tests/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,11 @@
import unittest
import numpy.testing as nptest
import numpy as np
try:
from osgeo import ogr
ogr_installed = True
except ImportError:
ogr_installed = False

import pytest

from pygeogrids.grids import lonlat2cell, BasicGrid
import pygeogrids as grids
from pygeogrids.grids import ogr_installed


class Test_lonlat2cell(unittest.TestCase):
Expand Down Expand Up @@ -660,12 +655,15 @@ def test_reorder_to_cellsize():
np.array([14, 14, 14, 14]))


@pytest.mark.skipif(not ogr_installed, reason="OGR not installed.")
class Test_ShpGrid(unittest.TestCase):

def setUp(self):
"""
Setup grid and shp_file to check if grid points fall in shp.
"""
from osgeo import ogr

lat = np.arange(-90, 90, 1)
lon = np.arange(-180, 180, 1)
self.lons, self.lats = np.meshgrid(lon, lat)
Expand All @@ -682,7 +680,6 @@ def setUp(self):
poly.AddGeometry(ring)

self.shp = poly

def test_shpgrid(self):
'''
Check if gridpoints fall in polygon.
Expand Down
4 changes: 3 additions & 1 deletion tests/test_shapefile.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from pygeogrids.shapefile import subgrid_for_shp
import pytest
from pygeogrids.shapefile import subgrid_for_shp, ogr_installed
from pygeogrids.grids import genreg_grid

@pytest.mark.skipif(not ogr_installed, reason="OGR not installed.")
def test_subgrid_from_shapefile():
grid = genreg_grid(1)

Expand Down

0 comments on commit d85d2d1

Please sign in to comment.