flopy.export.shapefile_utils module

Module for exporting and importing flopy model attributes

enforce_10ch_limit(names: List[str], warnings: bool = True) List[str][source]

Enforce 10 character limit for fieldnames. Add suffix for duplicate names starting at 0.

Parameters:
  • names (list of strings) –

  • warnings (whether to warn if names are truncated) –

Returns:

list of unique strings of len <= 10.

Return type:

list

get_pyshp_field_dtypes(code)[source]

Returns a numpy dtype for a pyshp field type.

get_pyshp_field_info(dtypename)[source]

Get pyshp dtype information for a given numpy dtype.

model_attributes_to_shapefile(path: str | PathLike, ml, package_names=None, array_dict=None, verbose=False, **kwargs)[source]

Wrapper function for writing a shapefile of model data. If package_names is not None, then search through the requested packages looking for arrays that can be added to the shapefile as attributes

Parameters:
  • path (str or PathLike) – path to write the shapefile to

  • ml (flopy.mbase) – model instance

  • package_names (list of package names (e.g. ["dis","lpf"])) – Packages to export data arrays to shapefile. (default is None)

  • array_dict (dict of {name:2D array} pairs) – Additional 2D arrays to add as attributes to the shapefile. (default is None)

  • verbose (bool, optional, default False) – whether to print verbose output

  • **kwargs (keyword arguments) –

    modelgridfp.modflow.Grid object

    if modelgrid is supplied, user supplied modelgrid is used in lieu of the modelgrid attached to the modflow model object

    crspyproj.CRS, int, str, optional if prjfile is specified

    Coordinate reference system (CRS) for the model grid (must be projected; geographic CRS are not supported). The value can be anything accepted by pyproj.CRS.from_user_input(), such as an authority string (eg “EPSG:26916”) or a WKT string.

    prjfilestr or pathlike, optional if crs is specified

    ESRI-style projection file with well-known text defining the CRS for the model grid (must be projected; geographic CRS are not supported).

Return type:

None

Examples

>>> import flopy
>>> m = flopy.modflow.Modflow()
>>> flopy.utils.model_attributes_to_shapefile('model.shp', m)
recarray2shp(recarray, geoms, shpname: str | PathLike = 'recarray.shp', mg=None, crs=None, prjfile: str | PathLike | None = None, verbose=False, **kwargs)[source]

Write a numpy record array to a shapefile, using a corresponding list of geometries. Method supports list of flopy geometry objects, flopy Collection object, shapely Collection object, and geojson Geometry Collection objects

Parameters:
  • recarray (np.recarray) – Numpy record array with attribute information that will go in the shapefile

  • geoms (list of flopy.utils.geometry, shapely geometry collection,) –

    flopy geometry collection, shapefile.Shapes,

    list of shapefile.Shape objects, or geojson geometry collection

    The number of geometries in geoms must equal the number of records in recarray.

  • shpname (str or PathLike, default "recarray.shp") – Path for the output shapefile

  • mg (flopy.discretization.Grid object) – flopy model grid

  • crs (pyproj.CRS, int, str, optional if prjfile is specified) – Coordinate reference system (CRS) for the model grid (must be projected; geographic CRS are not supported). The value can be anything accepted by pyproj.CRS.from_user_input(), such as an authority string (eg “EPSG:26916”) or a WKT string.

  • prjfile (str or pathlike, optional if crs is specified) – ESRI-style projection file with well-known text defining the CRS for the model grid (must be projected; geographic CRS are not supported).

  • **kwargs (dict, optional) –

    Support deprecated keyword options.

    Deprecated since version 3.5: The following keyword options will be removed for FloPy 3.6:

    • epsg (int): use crs instead.

    • prj (str or PathLike): use prjfile instead.

Notes

Uses pyshp and optionally pyproj.

shape_attr_name(name, length=6, keep_layer=False)[source]

Function for to format an array name to a maximum of 10 characters to conform with ESRI shapefile maximum attribute name length

Parameters:
  • name (str) – data array name

  • length (int) – maximum length of string to return. Value passed to function is overridden and set to 10 if keep_layer=True. (default is 6)

  • keep_layer (bool) – Boolean that determines if layer number in name should be retained. (default is False)

Return type:

str

Examples

>>> import flopy
>>> name = flopy.utils.shape_attr_name('averylongstring')
>>> name
>>> 'averyl'
shp2recarray(shpname: str | PathLike)[source]

Read a shapefile into a numpy recarray.

Parameters:

shpname (str or PathLike) – ESRI Shapefile path

Return type:

np.recarray

write_grid_shapefile(path: str | PathLike, mg, array_dict, nan_val=nan, crs=None, prjfile: str | PathLike | None = None, verbose=False, **kwargs)[source]

Method to write a shapefile of gridded input data

Parameters:
  • path (str or PathLike) – shapefile file path

  • mg (flopy.discretization.grid.Grid object) – flopy model grid

  • array_dict (dict) – dictionary of model input arrays

  • nan_val (float) – value to fill nans

  • crs (pyproj.CRS, int, str, optional if prjfile is specified) – Coordinate reference system (CRS) for the model grid (must be projected; geographic CRS are not supported). The value can be anything accepted by pyproj.CRS.from_user_input(), such as an authority string (eg “EPSG:26916”) or a WKT string.

  • prjfile (str or pathlike, optional if crs is specified) – ESRI-style projection file with well-known text defining the CRS for the model grid (must be projected; geographic CRS are not supported).

  • **kwargs (dict, optional) –

    Support deprecated keyword options.

    Deprecated since version 3.5: The following keyword options will be removed for FloPy 3.6:

    • epsg (int): use crs instead.

    • prj (str or PathLike): use prjfile instead.

Return type:

None

write_gridlines_shapefile(filename: str | PathLike, mg)[source]

Write a polyline shapefile of the grid lines - a lightweight alternative to polygons.

Parameters:
  • filename (str or PathLike) – path of the shapefile to write

  • mg (flopy.discretization.grid.Grid object) – flopy model grid

Return type:

None

write_prj(shpname, modelgrid=None, crs=None, prjfile=None, **kwargs)[source]