Skip to content

Commit

Permalink
Refine properties after discussion
Browse files Browse the repository at this point in the history
 - replace dx/dy with resolution with length multiplier applied
 - return None rather than median of delr/delc
 - geotransform raises ValueError for non-uniform grids
 - check full bounds for recent rasterio only
  • Loading branch information
mwtoews committed Aug 21, 2018
1 parent 51f05b8 commit 3f434b3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 34 deletions.
21 changes: 13 additions & 8 deletions autotest/t007_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,15 +297,19 @@ def test_export_array():

try: # test GeoTIFF export
import rasterio
from distutils.version import LooseVersion
rasterio_post101 = LooseVersion(rasterio.__version__) >= '1.0.1'
m.sr.export_array(os.path.join(tpth, 'fb.tif'), m.dis.top.array,
nodata=nodata)
with rasterio.open(os.path.join(tpth, 'fb.tif')) as src:
arr = src.read(1)
# array should have identical values
np.testing.assert_equal(arr, m.dis.top.array)
assert src.shape == (m.nrow, m.ncol)
# Note: rasterio doesn't properly represent bounds for rotated rasters
np.testing.assert_almost_equal(src.bounds[0:2], m.sr.bounds[0:2])
if rasterio_post101:
np.testing.assert_almost_equal(src.bounds, m.sr.bounds)
else: # Note: older rasterio had incomplete bounds on rotated rasters
np.testing.assert_almost_equal(src.bounds[0:2], m.sr.bounds[0:2])
np.testing.assert_almost_equal(
src.affine,
[176.7766952966369, 176.77669529663686, 619653.0,
Expand Down Expand Up @@ -343,8 +347,10 @@ def test_export_array():
arr = src.read(1)
np.testing.assert_equal(arr, m.dis.top.array)
assert src.shape == (10, 5)
# Note: rasterio doesn't properly represent bounds for rotated rasters
np.testing.assert_almost_equal(src.bounds[0:2], sr.bounds[0:2])
if rasterio_post101:
np.testing.assert_almost_equal(src.bounds, sr.bounds)
else: # Note: older rasterio had incomplete bounds on rotated rasters
np.testing.assert_almost_equal(src.bounds[0:2], sr.bounds[0:2])
np.testing.assert_equal(
src.affine,
[250.0, 0.0, 286.8,
Expand Down Expand Up @@ -611,8 +617,7 @@ def test_dynamic_xll_yll():
xll=xll, yll=yll, rotation=30)
assert sr1.xlength == 1250.0
assert sr1.ylength == 5000.0
assert sr1.dx == 250.0
assert sr1.dy == 500.0
assert sr1.resolution == (250.0, 500.0)
np.testing.assert_almost_equal(
sr1.get_extent(), (-2213.2, 1369.3317547, 29.03, 4984.1570189))
np.testing.assert_almost_equal(
Expand All @@ -626,8 +631,8 @@ def test_dynamic_xll_yll():
assert sr1.yll == yll
assert sr1.xlength == 1250.0
assert sr1.ylength == 5000.0
assert sr1.dx == 250.0
assert sr1.dy == 500.0
np.testing.assert_almost_equal(
sr1.resolution, (76.1962816, 152.3925632))
np.testing.assert_almost_equal(
sr1.get_extent(), (-475.1628162, 616.7395778, 29.03, 1539.2790152))
np.testing.assert_almost_equal(
Expand Down
41 changes: 15 additions & 26 deletions flopy/utils/reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,12 @@ class SpatialReference(object):
1D array of cell vertices for whole grid in C-style (row-major) order
(same as np.ravel())
dx : float
cell column width, or median for non-uniform widths along rows (delr)
dy : float
cell row height, or median for non-uniform widths along columns (delc)
resolution : tuple
model grid resolution (dx, dy) with length multiplier applied
geotransform : tuple
GDAL-ordered geotransform tuple for exporting rasters
GDAL-ordered geotransform tuple for exporting rasters with uniform
grid spacings. Raises ValueError if grid spacing is not uniform.
Notes
-----
Expand Down Expand Up @@ -1036,8 +1034,7 @@ def export_array(self, filename, a, nodata=-9999,
xll, yll = xmin, ymin
else:
xll, yll = self.xll, self.yll
dx = self.dx * self.length_multiplier
dy = self.dy * self.length_multiplier
dx, dy = self.resolution
if dx == dy:
cellsize = dx
fmt = kwargs.get('fmt', '%.18e')
Expand Down Expand Up @@ -1282,31 +1279,23 @@ def _set_vertices(self):
"""

@property
def dx(self):
dx = self.delr[0]
def resolution(self):
"""Returns model grid resolution with length multiplier applied"""
dx = self.delr[0] * self.length_multiplier
dy = self.delc[0] * self.length_multiplier
if self.delr.min() != self.delr.max():
dx = np.median(self.delr)
warn('delr values range from {0} to {1}; '
'using median of {2} for dx'
.format(self.delr.min(), self.delr.max(), dx))
return dx

@property
def dy(self):
dy = self.delc[0]
dx = None
if self.delc.min() != self.delc.max():
dy = np.median(self.delc)
warn('delc values range from {0} to {1}; '
'using median of {2} for dy'
.format(self.delc.min(), self.delc.max(), dy))
return dy
dy = None
return dx, dy

@property
def geotransform(self):
"""Returns GDAL-ordered geotransform tuple"""
c, f = self.xul, self.yul
dx = self.dx * self.length_multiplier
dy = self.dy * self.length_multiplier
dx, dy = self.resolution
if not all([dx, dy]):
raise ValueError('model grid is not uniform')
rotation = self.rotation
if rotation != 0:
cr, sr = cos_sin(rotation)
Expand Down

0 comments on commit 3f434b3

Please sign in to comment.