flopy.export.shapefile_utils module

Module for exporting and importing flopy model attributes

class CRS(prj=None, esri_wkt=None, epsg=None)[source]

Bases: object

Container to parse and store coordinate reference system parameters, and translate between different formats.

crs

Dict mapping crs attributes to proj4 parameters

static get_spatialreference(epsg, text='esriwkt')[source]

Gets text for given epsg code and text format from spatialreference.org Fetches the reference text using the url:

See: https://www.epsg-registry.org/

Parameters:
  • epsg (int) – epsg code for coordinate system
  • text (str) – string added to url
Returns:

url

Return type:

str

static getprj(epsg, addlocalreference=True, text='esriwkt')[source]

Gets projection file (.prj) text for given epsg code from spatialreference.org See: https://www.epsg-registry.org/

Parameters:
  • epsg (int) – epsg code for coordinate system
  • addlocalreference (boolean) – adds the projection file text associated with epsg to a local database, epsgref.json, located in the user’s data directory.
Returns:

prj – text for a projection (*.prj) file.

Return type:

str

static getproj4(epsg)[source]

Gets projection file (.prj) text for given epsg code from spatialreference.org. See: https://www.epsg-registry.org/

Parameters:epsg (int) – epsg code for coordinate system
Returns:prj – text for a projection (*.prj) file.
Return type:str
grid_mapping_attribs

Map parameters for CF Grid Mappings http://http://cfconventions.org/cf-conventions/cf-conventions.html, Appendix F: Grid Mappings

parse_wkt()[source]
proj4

Not implemented yet

class EpsgReference[source]

Bases: object

Sets up a local database of text representations of coordinate reference systems, keyed by EPSG code.

The database is epsgref.json, located in the user’s data directory. If optional ‘appdirs’ package is available, this is in the platform-dependent user directory, otherwise in the user’s ‘HOME/.flopy’ directory.

add(epsg, prj)[source]

add an epsg code to epsgref.json

get(epsg)[source]

returns prj from a epsg code, otherwise None if not found

remove(epsg)[source]

removes an epsg entry from epsgref.json

reset(verbose=True)[source]
static show()[source]
to_dict()[source]

returns dict with EPSG code integer key, and WKT CRS text

enforce_10ch_limit(names)[source]

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

Parameters:names (list of strings) –
Returns:names
Return type:list of unique strings of len <= 10.
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.

import_shapefile(check_version=True)[source]

Import shapefile module from pyshp.

Parameters:check_version (bool) – Checks to ensure that pyshp is at least version 2. Default True, which is usually required for Writer (which has a different API), but can be False if only using Reader.
Returns:
Return type:module
Raises:ImportError – If shapefile module is not found, or major version is less than 2.
model_attributes_to_shapefile(filename, ml, package_names=None, array_dict=None, **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:
  • filename (string) – name of the shapefile to write
  • 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)
  • **kwargs (keyword arguments) –
    modelgrid : fp.modflow.Grid object
    if modelgrid is supplied, user supplied modelgrid is used in lieu of the modelgrid attached to the modflow model object
    epsg : int
    epsg projection information
    prj : str
    user supplied prj file
Returns:

Return type:

None

Examples

>>> import flopy
>>> m = flopy.modflow.Modflow()
>>> flopy.utils.model_attributes_to_shapefile('model.shp', m)
recarray2shp(recarray, geoms, shpname='recarray.shp', mg=None, epsg=None, prj=None, **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) – Path for the output shapefile
  • epsg (int) – EPSG code. See https://www.epsg-registry.org/ or spatialreference.org
  • prj (str) – Existing projection file to be used with new shapefile.

Notes

Uses pyshp. epsg code requires an internet connection the first time to get the projection file text from spatialreference.org, but then stashes the text in the file epsgref.json (located in the user’s data directory) for subsequent use. See flopy.reference for more details.

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 (string) – 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)
Returns:

Return type:

String

Examples

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

Read a shapefile into a numpy recarray.

Parameters:shpname (str) – ESRI Shapefile.
Returns:recarray
Return type:np.recarray
write_grid_shapefile(filename, mg, array_dict, nan_val=nan, epsg=None, prj=None)[source]

Method to write a shapefile of gridded input data

Parameters:
  • filename (str) – shapefile file name path
  • mg (flopy.discretization.Grid object) – flopy model grid
  • array_dict (dict) – dictionary of model input arrays
  • nan_val (float) – value to fill nans
  • epsg (str, int) – epsg code
  • prj (str) – projection file name path
Returns:

Return type:

None

write_gridlines_shapefile(filename, mg)[source]

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

Parameters:
  • filename (string) – name of the shapefile to write
  • mg (model grid) –
Returns:

Return type:

None

write_prj(shpname, mg=None, epsg=None, prj=None, wkt_string=None)[source]