Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
15 changes: 10 additions & 5 deletions pygridtools/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions pygridtools/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import pygridtools
from .utils import requires


try:
import pytest
except ImportError:
Expand All @@ -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)
37 changes: 22 additions & 15 deletions pygridtools/tests/test_iotools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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)
2 changes: 0 additions & 2 deletions pygridtools/tests/utils.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down