diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..d4a2c44 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,21 @@ +# http://editorconfig.org + +root = true + +[*] +indent_style = space +indent_size = 4 +trim_trailing_whitespace = true +insert_final_newline = true +charset = utf-8 +end_of_line = lf + +[*.bat] +indent_style = tab +end_of_line = crlf + +[LICENSE] +insert_final_newline = false + +[Makefile] +indent_style = tab diff --git a/pygridtools/core.py b/pygridtools/core.py index 4d27237..e448728 100644 --- a/pygridtools/core.py +++ b/pygridtools/core.py @@ -951,16 +951,21 @@ def from_dataframe(cls, df, icol='ii', jcol='jj', return cls(xtab[xcol], xtab[ycol]).update_cell_mask() @classmethod - def from_shapefile(cls, shapefile, icol='ii', jcol='jj'): + @numpy.deprecate + def from_shapefile(cls, *args, **kwargs): + return cls.from_gis(*args, **kwargs) + + @classmethod + def from_gis(cls, gisfile, icol='ii', jcol='jj'): """ - Build a ModelGrid from a shapefile of *nodes*. + Build a ModelGrid from a GIS file (e.g, shapefile, geojson) of *nodes*. Parameters ---------- outputfile : str - The name of the shapefile of the grid *nodes*. + The name of the geopandas/fiona-compatible file of the grid *nodes*. icol, jcol : str, optional - The names of the columns in the shapefile containing the + The names of the columns in the file containing the I/J index of the nodes. Returns @@ -969,7 +974,7 @@ def from_shapefile(cls, shapefile, icol='ii', jcol='jj'): """ - df = iotools.read_grid(shapefile, icol=icol, jcol=jcol) + df = iotools.read_grid(gisfile, icol=icol, jcol=jcol) return cls.from_dataframe(df).update_cell_mask() @classmethod diff --git a/pygridtools/tests/__init__.py b/pygridtools/tests/__init__.py index e980576..1739998 100644 --- a/pygridtools/tests/__init__.py +++ b/pygridtools/tests/__init__.py @@ -3,7 +3,6 @@ import pygridtools from .utils import requires - try: import pytest except ImportError: @@ -19,5 +18,5 @@ def test(*args): @requires(pytest, 'pytest') def teststrict(): - options = [resource_filename('pygridtools', ''), '--pep', '--mpl'] + options = [resource_filename('pygridtools', ''), '--pep8', '--mpl'] return pytest.main(options) diff --git a/pygridtools/tests/test_iotools.py b/pygridtools/tests/test_iotools.py index 394d860..3a6f063 100644 --- a/pygridtools/tests/test_iotools.py +++ b/pygridtools/tests/test_iotools.py @@ -62,6 +62,28 @@ def test_read_polygons(as_gdf): nptest.assert_array_almost_equal(res, exp) +@pytest.mark.parametrize(('gisfile', 'error'), [ + ('array_point.shp', None), + ('array_grid.shp', NotImplementedError) +]) +def test_read_grid(gisfile, error): + _file = resource_filename('pygridtools.tests.baseline_files', gisfile) + + data = {'easting': [1., 2., 3.] * 4, 'northing': sorted([4., 5., 6., 7.] * 3)} + index = pandas.MultiIndex.from_product([[2, 3, 4, 5], [2, 3, 4]], names=['jj', 'ii']) + known_df = ( + pandas.DataFrame(data=data, index=index) + .assign(elev=0.0, river='test') + .reset_index() + .set_index(['ii', 'jj']) + .sort_index() + ) + + with utils.raises(error): + result_df = iotools.read_grid(_file, othercols=['elev', 'river']) + pdtest.assert_frame_equal(result_df, known_df) + + @pytest.mark.parametrize(('usemasks', 'fname'), [ (False, 'array_point.shp'), (True, 'mask_point.shp'), @@ -112,18 +134,3 @@ def test_write_cells(usemasks, fname, simple_grid, example_crs): outfile, river=river) utils.assert_shapefiles_equal(basefile, outfile) assert isinstance(gdf, geopandas.GeoDataFrame) - - -def test_read_grid(): - pntfile = resource_filename('pygridtools.tests.baseline_files', 'array_point.shp') - cellfile = resource_filename('pygridtools.tests.baseline_files', 'array_grid.shp') - result_df = iotools.read_grid(pntfile, othercols=['elev', 'river']) - known_df = pandas.DataFrame( - data={'easting': [1., 2., 3.] * 4, 'northing': sorted([4., 5., 6., 7.] * 3)}, - index=pandas.MultiIndex.from_product([[2, 3, 4, 5], [2, 3, 4]], names=['jj', 'ii']) - ).assign(elev=0.0).assign(river='test').reset_index().set_index(['ii', 'jj']).sort_index() - - pdtest.assert_frame_equal(result_df, known_df) - - with utils.raises(NotImplementedError): - result_df = iotools.read_grid(cellfile) diff --git a/pygridtools/tests/utils.py b/pygridtools/tests/utils.py index 8476717..d4a9329 100644 --- a/pygridtools/tests/utils.py +++ b/pygridtools/tests/utils.py @@ -1,10 +1,8 @@ -from pkg_resources import resource_filename from contextlib import contextmanager from functools import wraps import filecmp import pytest -import numpy.testing as nptest import pandas.util.testing as pdtest import geopandas