Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(shapefile): add check_version for pyshp>=2; skip tests #1001

Merged
merged 1 commit into from
Oct 15, 2020
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
16 changes: 11 additions & 5 deletions autotest/t006_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
matplotlib = None

import flopy
import shapefile
try:
import shapefile
if int(shapefile.__version__.split('.')[0]) < 2:
shapefile = None
except ImportError:
shapefile = None


cpth = os.path.join('temp', 't006')
Expand Down Expand Up @@ -82,10 +87,11 @@ def test_mflist_reference():
ghb = flopy.modflow.ModflowGhb(ml, stress_period_data=ghb_dict)
assert isinstance(ghb, flopy.modflow.ModflowGhb)

test = os.path.join(cpth, 'test3.shp')
ml.export(test, kper=0)
shp = shapefile.Reader(test)
assert shp.numRecords == nrow * ncol
if shapefile:
test = os.path.join(cpth, 'test3.shp')
ml.export(test, kper=0)
shp = shapefile.Reader(test)
assert shp.numRecords == nrow * ncol

def test_cbc_ts():
fpth = os.path.join('..', 'examples', 'data', 'mf2005_test',
Expand Down
54 changes: 37 additions & 17 deletions autotest/t007_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@
os.makedirs(spth)


def import_shapefile():
try:
import shapefile
except ImportError:
return None
if int(shapefile.__version__.split('.')[0]) < 2:
return None
return shapefile


def remove_shp(shpname):
os.remove(shpname)
for ext in ['prj', 'shx', 'dbf']:
Expand Down Expand Up @@ -94,9 +104,8 @@ def export_netcdf(m):

def export_shapefile(namfile):
print('in export_shapefile: {}'.format(namfile))
try:
import shapefile as shp
except:
shp = import_shapefile()
if shp is None:
return

m = flopy.modflow.Modflow.load(namfile, model_ws=pth, verbose=False)
Expand Down Expand Up @@ -128,9 +137,8 @@ def export_shapefile(namfile):

def export_shapefile_modelgrid_override(namfile):
print('in export_modelgrid_override: {}'.format(namfile))
try:
import shapefile as shp
except:
shp = import_shapefile()
if shp is None:
return

from flopy.discretization import StructuredGrid
Expand Down Expand Up @@ -164,6 +172,8 @@ def export_shapefile_modelgrid_override(namfile):


def test_output_helper_shapefile_export():
if import_shapefile() is None:
return
ws = os.path.join('..', 'examples', 'data', 'freyberg_multilayer_transient')
name = 'freyberg.nam'

Expand All @@ -177,6 +187,9 @@ def test_output_helper_shapefile_export():


def test_freyberg_export():
if import_shapefile() is None:
return

from flopy.discretization import StructuredGrid
namfile = 'freyberg.nam'

Expand Down Expand Up @@ -282,6 +295,10 @@ def test_export_output():


def test_write_shapefile():
sf = import_shapefile()
if not sf:
return

from flopy.discretization import StructuredGrid
from flopy.export.shapefile_utils import shp2recarray
from flopy.export.shapefile_utils import write_grid_shapefile
Expand All @@ -298,7 +315,6 @@ def test_write_shapefile():
# 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':
Expand All @@ -323,11 +339,8 @@ def test_write_shapefile():


def test_shapefile_polygon_closed():
import os
import flopy
try:
import shapefile
except:
shapefile = import_shapefile()
if shapefile is None:
return

xll, yll = 468970, 3478635
Expand Down Expand Up @@ -372,7 +385,9 @@ def test_export_array():
m.dis.top.array, nodata=nodata)
arr = np.loadtxt(os.path.join(tpth, 'fb.asc'), skiprows=6)

m.modelgrid.write_shapefile(os.path.join(tpth, 'grid.shp'))
if import_shapefile() is not None:
m.modelgrid.write_shapefile(os.path.join(tpth, 'grid.shp'))

# check bounds
with open(os.path.join(tpth, 'fb.asc')) as src:
for line in src:
Expand Down Expand Up @@ -1504,12 +1519,11 @@ def test_wkt_parse():


def test_shapefile_ibound():
shapefile = import_shapefile()
if not shapefile:
return
import os
import flopy
try:
import shapefile
except:
return

shape_name = os.path.join(spth, "test.shp")
nam_file = "freyberg.nam"
Expand Down Expand Up @@ -1556,6 +1570,8 @@ def build_sfr_netcdf():


def test_export_array2():
if import_shapefile() is None:
return
from flopy.discretization import StructuredGrid
from flopy.export.utils import export_array
nrow = 7
Expand Down Expand Up @@ -1589,6 +1605,8 @@ def test_export_array2():


def test_export_array_contours():
if import_shapefile() is None:
return
from flopy.discretization import StructuredGrid
from flopy.export.utils import export_array_contours
nrow = 7
Expand Down Expand Up @@ -1626,6 +1644,8 @@ def test_export_contourf():
import shapely
except:
return
if import_shapefile() is None:
return
import matplotlib.pyplot as plt
from flopy.export.utils import export_contourf
filename = os.path.join(spth, 'myfilledcontours.shp')
Expand Down
9 changes: 9 additions & 0 deletions autotest/t009_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
import numpy as np
from flopy.utils.recarray_utils import create_empty_recarray

try:
import shapefile
if int(shapefile.__version__.split('.')[0]) < 2:
shapefile = None
except ImportError:
shapefile = None

try:
import matplotlib
# if os.getenv('TRAVIS'): # are we running https://travis-ci.org/ automated tests ?
Expand Down Expand Up @@ -290,6 +297,8 @@ def test_export():
delr=m.dis.delr.array,
xoff=sr.xll, yoff=sr.yll)
# m.sr.origin_loc = "ll"
if not shapefile:
return # skip
m.export(os.path.join(outpath, 'grid.shp'))
r, d = create_sfr_data()
sfr = flopy.modflow.ModflowSfr2(m, reach_data=r, segment_data={0: d})
Expand Down
13 changes: 12 additions & 1 deletion autotest/t031_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
from flopy.utils.reference import SpatialReference
from flopy.modpath.mp6sim import StartingLocationsFile

try:
import shapefile
if int(shapefile.__version__.split('.')[0]) < 2:
shapefile = None
except ImportError:
shapefile = None

mffiles = glob.glob('../examples/data/mp6/EXAMPLE*')
path = os.path.join('temp', 't031')

Expand Down Expand Up @@ -103,7 +110,8 @@ def test_get_destination_data():

# test deprecation
sr2 = SpatialReference(xll=mg.xoffset, yll=mg.yoffset, rotation=-30)
m.dis.export(path + '/dis.shp')
if shapefile:
m.dis.export(path + '/dis.shp')

pthld = PathlineFile(os.path.join(path, 'EXAMPLE-3.pathline'))
epd = EndpointFile(os.path.join(path, 'EXAMPLE-3.endpoint'))
Expand All @@ -124,6 +132,9 @@ def test_get_destination_data():
dtype=starting_locs.dtype)
assert np.all(np.in1d(starting_locs, pathline_locs))

if shapefile is None:
return # skip remainder

# test writing a shapefile of endpoints
epd.write_shapefile(well_epd, direction='starting',
shpname=os.path.join(path, 'starting_locs.shp'),
Expand Down
10 changes: 10 additions & 0 deletions autotest/t061_test_gridgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
import flopy
from flopy.utils.gridgen import Gridgen

try:
import shapefile
if int(shapefile.__version__.split('.')[0]) < 2:
shapefile = None
except ImportError:
shapefile = None

cpth = os.path.join('temp', 't061')
# delete the directory if it exists
if os.path.isdir(cpth):
Expand Down Expand Up @@ -57,6 +64,9 @@ def test_gridgen():
gu = Gridgen(dis_usg, model_ws=gridgen_ws, exe_name=exe_name,
vertical_pass_through=True)

if shapefile is None:
return # skip remainder

rf0shp = os.path.join(gridgen_ws, 'rf0')
xmin = 7 * delr
xmax = 12 * delr
Expand Down
10 changes: 9 additions & 1 deletion autotest/t504_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
from flopy.mf6.modflow.mfsimulation import MFSimulation
from flopy.mf6.mfbase import VerbosityLevel

try:
import shapefile
if int(shapefile.__version__.split('.')[0]) < 2:
shapefile = None
except ImportError:
shapefile = None

try:
import pymake
except:
Expand Down Expand Up @@ -179,7 +186,8 @@ def test003_gwfs_disv():
assert array_util.array_comp(budget_fjf_valid, budget_frf)

model = sim.get_model(model_name)
model.export('{}/{}.shp'.format(pth, test_ex_name))
if shapefile:
model.export('{}/{}.shp'.format(pth, test_ex_name))

# change some settings
chd_head_left = model.get_package('CHD_LEFT')
Expand Down
11 changes: 9 additions & 2 deletions autotest/t505_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@
from flopy.mf6.utils import testutils
from flopy.mf6.mfbase import MFDataException

try:
import shapefile
if int(shapefile.__version__.split('.')[0]) < 2:
shapefile = None
except ImportError:
shapefile = None

try:
import pymake
Expand Down Expand Up @@ -2110,8 +2116,9 @@ def test006_gwf3_disv():

# export to netcdf - temporarily disabled
# model.export(os.path.join(run_folder, "test006_gwf3.nc"))
# export to shape file
model.export(os.path.join(run_folder, "test006_gwf3.shp"))
if shapefile:
# export to shape file
model.export(os.path.join(run_folder, "test006_gwf3.shp"))

# clean up
sim.delete_output_files()
Expand Down
20 changes: 16 additions & 4 deletions autotest/t550_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
from flopy.utils import SpatialReference as OGsr
from flopy.export.shapefile_utils import shp2recarray

try:
import shapefile
if int(shapefile.__version__.split('.')[0]) < 2:
shapefile = None
except ImportError:
shapefile = None

tmpdir = 'temp/t550/'
if not os.path.isdir(tmpdir):
os.makedirs(tmpdir)
Expand Down Expand Up @@ -79,16 +86,20 @@ def cellid(k, i, j, nrow, ncol):
#irch = np.zeros((nrow, ncol))
riv6 = fp6.ModflowGwfriv(gwf, stress_period_data=spd6)
rch6 = fp6.ModflowGwfrcha(gwf, recharge=rech)
#rch6.export('{}/mf6.shp'.format(tmpdir))
m.export('{}/mfnwt.shp'.format(tmpdir))
gwf.export('{}/mf6.shp'.format(tmpdir))
if shapefile:
#rch6.export('{}/mf6.shp'.format(tmpdir))
m.export('{}/mfnwt.shp'.format(tmpdir))
gwf.export('{}/mf6.shp'.format(tmpdir))

riv6spdarrays = dict(riv6.stress_period_data.masked_4D_arrays_itr())
rivspdarrays = dict(riv.stress_period_data.masked_4D_arrays_itr())
for k, v in rivspdarrays.items():
assert np.abs(np.nansum(v) - np.nansum(riv6spdarrays[k])) < 1e-6, "variable {} is not equal".format(k)
pass

if shapefile is None:
return # skip remainder

# check that the two shapefiles are the same
ra = shp2recarray('{}/mfnwt.shp'.format(tmpdir))
ra6 = shp2recarray('{}/mf6.shp'.format(tmpdir))
Expand Down Expand Up @@ -133,7 +144,8 @@ def test_huge_shapefile():
nper=nper, perlen=perlen, nstp=nstp,
tsmult=tsmult,
top=top, botm=botm)
m.export('{}/huge.shp'.format(tmpdir))
if shapefile:
m.export('{}/huge.shp'.format(tmpdir))


if __name__ == '__main__':
Expand Down
Loading