Skip to content

Commit

Permalink
feat(grid.rasterize; shapefile source data): add exclude_features opt…
Browse files Browse the repository at this point in the history
…ion to include all features in a shapefile except for those specified
  • Loading branch information
aleaf committed Aug 8, 2024
1 parent 77c050e commit 85b7816
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
7 changes: 5 additions & 2 deletions mfsetup/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ def write_bbox_shapefile(modelgrid, outshp):


def rasterize(feature, grid, id_column=None,
include_ids=None, names_column=None,
include_ids=None, exclude_ids=None, names_column=None,
crs=None, **kwargs):
"""Rasterize a feature onto the model grid, using
the rasterio.features.rasterize method. Features are intersected
Expand All @@ -649,6 +649,8 @@ def rasterize(feature, grid, id_column=None,
from this column will be assigned to the output raster.
include_ids : sequence
Subset of IDs in id_column to include
exclude_ids : sequence
Subset of IDs in id_column to exclude
names_column : str, optional
By default, the IDs in id_column, or sequential integers
are returned. This option allows another column of strings
Expand Down Expand Up @@ -738,7 +740,8 @@ def rasterize(feature, grid, id_column=None,
# subset to include_ids
if id_column is not None and include_ids is not None:
df = df.loc[df[id_column].isin(include_ids)].copy()

if id_column is not None and exclude_ids is not None:
df = df.loc[~df[id_column].isin(exclude_ids)].copy()
# create list of GeoJSON features, with unique value for each feature
if id_column is None:
numbers = list(range(1, len(df)+1))
Expand Down
8 changes: 4 additions & 4 deletions mfsetup/tests/data/shellmound.yml
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,10 @@ drn:
shapefile:
filename: '../../../mfsetup/tests/data/shellmound/shps/waterbodies.shp'
id_column: 'COMID'
# Note: to include all features in a shapefile,
# simply omit the include_ids: key
# id_column: can also be omitted
include_ids: [18047154, 18046236]
# Include all features in the above shapefile,
# except those associated with these COMIDs
exclude_ids: [18046230, 18046226, 18046238, 17953939, 18046140, 18046162]
boundname_column: 'COMID'
elev:
filename: 'shellmound/rasters/meras_100m_dem.tif'
elevation_units: 'feet'
Expand Down
5 changes: 5 additions & 0 deletions mfsetup/tests/test_mf6_shellmound.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,11 @@ def test_basic_stress_package_setup(shellmound_model_with_dis, pckg_abbrv,
# heads in CHD package should match the CSV input,
# after conversion to meters
assert np.allclose(in_df['head'].values, spd_heads[1:]/.3048)
if pckg_abbrv == 'drn':
exclude_ids = list(map(str, m.cfg[pckg_abbrv]['source_data']['shapefile']['exclude_ids']))
comids = [s.replace('feature-','') for s in np.unique(m.drn.stress_period_data.data[0]['boundname'])]
assert not set(exclude_ids).intersection(comids)
assert comids == ['18046236', '18047154']
# more advanced transient input case with csv
# and conductance values specified via a raster
if pckg_abbrv == 'ghb':
Expand Down

0 comments on commit 85b7816

Please sign in to comment.