Skip to content
Merged
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
92 changes: 61 additions & 31 deletions autotest/t007_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,43 +262,72 @@ def test_export_output():
def test_write_shapefile():
from flopy.discretization import StructuredGrid
from flopy.export.shapefile_utils import shp2recarray
from flopy.export.shapefile_utils import write_grid_shapefile, write_grid_shapefile2
from flopy.export.shapefile_utils import write_grid_shapefile

sg = StructuredGrid(delr=np.ones(10) *1.1, # cell spacing along model rows
delc=np.ones(10) *1.1, # cell spacing along model columns
epsg=26715)
outshp1 = os.path.join(tpth, 'junk.shp')
outshp2 = os.path.join(tpth, 'junk2.shp')
write_grid_shapefile(outshp1, sg, array_dict={})
write_grid_shapefile2(outshp2, sg, array_dict={})
outshp = os.path.join(tpth, 'junk.shp')
write_grid_shapefile(outshp, sg, array_dict={})

# test that vertices aren't getting altered by writing shapefile
for outshp in [outshp1, outshp2]:
# check that pyshp reads integers
# this only check that row/column were recorded as "N"
# not how they will be cast by python or numpy
import shapefile as sf
sfobj = sf.Reader(outshp)
for f in sfobj.fields:
if f[0] == 'row' or f[0] == 'column':
assert f[1] == 'N'
recs = list(sfobj.records())
for r in recs[0]:
assert isinstance(r, int)

# check that row and column appear as integers in recarray
ra = shp2recarray(outshp)
assert np.issubdtype(ra.dtype['row'], np.integer)
assert np.issubdtype(ra.dtype['column'], np.integer)

try: # check that fiona reads integers
import fiona
with fiona.open(outshp) as src:
meta = src.meta
assert 'int' in meta['schema']['properties']['row']
assert 'int' in meta['schema']['properties']['column']
except:
pass
# check that pyshp reads integers
# this only check that row/column were recorded as "N"
# not how they will be cast by python or numpy
import shapefile as sf
sfobj = sf.Reader(outshp)
for f in sfobj.fields:
if f[0] == 'row' or f[0] == 'column':
assert f[1] == 'N'
recs = list(sfobj.records())
for r in recs[0]:
assert isinstance(r, int)

# check that row and column appear as integers in recarray
ra = shp2recarray(outshp)
assert np.issubdtype(ra.dtype['row'], np.integer)
assert np.issubdtype(ra.dtype['column'], np.integer)

try: # check that fiona reads integers
import fiona
with fiona.open(outshp) as src:
meta = src.meta
assert 'int' in meta['schema']['properties']['row']
assert 'int' in meta['schema']['properties']['column']
except:
pass


def test_shapefile_polygon_closed():
import os
import flopy
try:
import shapefile
except:
return

xll, yll = 468970, 3478635
xur, yur = 681010, 3716462

spacing = 2000

ncol = int((xur - xll) / spacing)
nrow = int((yur - yll) / spacing)
print(nrow, ncol)

m = flopy.modflow.Modflow("test.nam", proj4_str="EPSG:32614", xll=xll,
yll=yll)

flopy.modflow.ModflowDis(m, delr=spacing, delc=spacing, nrow=nrow,
ncol=ncol)

shp_file = os.path.join(spth, "test_polygon.shp")
m.dis.export(shp_file)

shp = shapefile.Reader(shp_file)
for shape in shp.iterShapes():
if len(shape.points) != 5:
raise AssertionError("Shapefile polygon is not closed!")


def test_export_array():
Expand Down Expand Up @@ -1331,4 +1360,5 @@ def test_export_contourf():
#test_tricontour_NaN()
#test_export_contourf()
#test_sr()
# test_shapefile_polygon_closed()
pass
6 changes: 3 additions & 3 deletions flopy/discretization/structuredgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,11 +386,11 @@ def from_gridspec(cls, gridspec_file, lenuni=0):
# Exporting
def write_shapefile(self, filename='grid.shp', epsg=None, prj=None):
"""Write a shapefile of the grid with just the row and column attributes"""
from ..export.shapefile_utils import write_grid_shapefile2
from ..export.shapefile_utils import write_grid_shapefile
if epsg is None and prj is None:
epsg = self.epsg
write_grid_shapefile2(filename, self, array_dict={}, nan_val=-1.0e9,
epsg=epsg, prj=prj)
write_grid_shapefile(filename, self, array_dict={}, nan_val=-1.0e9,
epsg=epsg, prj=prj)


if __name__ == "__main__":
Expand Down
Loading