pymarthe.utils.shp_utils

Contains geospatial export utils (not much dependencies required)

Attributes

srefhttp

PYSHP_TYPES

Classes

CRS

Container to parse and store coordinate reference system parameters,

EpsgReference

Sets up a local database of text representations of coordinate reference

Functions

read_shapefile(shpname)

Read a shapefile into a Pandas dataframe with

point_in_polygon(x, y, polygon)

Use the ray casting algorithm to determine if a point is

shp2points(shpname[, stack])

Extract xy-coordinates from a point shapefile.

get_parts(xcc, ycc, dx, dy)

Return list of polygons parts from points

enforce_10ch_limit(names)

Enforce 10 character limit for fieldnames.

get_pyshp_field_info(dtypename)

Get pyshp dtype information for a given numpy dtype.

get_pyshp_field_dtypes(code)

Returns a numpy dtype for a pyshp field type.

recarray2shp(recarray, geoms[, shpname, geomtype, ...])

Write a numpy record array to a shapefile, using a corresponding

write_prj(shpname[, epsg, prj, wkt_string])

Write a projection file (.proj).

get_url_text(url[, error_msg])

Get text from a url.

Module Contents

pymarthe.utils.shp_utils.srefhttp = 'https://spatialreference.org'
pymarthe.utils.shp_utils.PYSHP_TYPES
pymarthe.utils.shp_utils.read_shapefile(shpname)

Read a shapefile into a Pandas dataframe with a ‘coords’ column holding the geometry information.

Parameters:
  • (str) (shpname) –

  • Returns

  • --------

  • (DataFrame) (df) –

  • Examples

  • --------

  • read_shapefile('mypoints.shp') (points =) –

pymarthe.utils.shp_utils.point_in_polygon(x, y, polygon)

Use the ray casting algorithm to determine if a point is within a polygon. Enables very fast intersection calculations.

Parameters:
  • (1D-array) (y) –

  • (1D-array)

  • tuple) (polygon (list of) – Format: [(x0, y0),….(xn, yn)]. Note: polygon can be open or closed.

Returns:

  • mask (1D-array) (boolean mask.) – True -> point within the polygon. False -> point not in the polygon

  • Examples

  • ——–

  • x, y = np.arrange(345, 355), np.arrange(523, 533)

  • polygon = [(234,456), (278,567), …, ()]

  • mask = point_in_polygon(x, y, polygon)

pymarthe.utils.shp_utils.shp2points(shpname, stack=True)

Extract xy-coordinates from a point shapefile.

Parameters:
  • (str) (shpname) –

  • (bool) (stack) – If True: [[x0,y0],[x1,y1],.. , [xN,yN]] If False: np.array([x0,x1,.. xN], [y0,y1,.. yN]) Default is True

pymarthe.utils.shp_utils.get_parts(xcc, ycc, dx, dy)

Return list of polygons parts from points considered as the centers of each polygons

Parameters:
  • xcc ((xy)cellcenter coordinates) –

  • (float) (dy) –

  • dx (width, height of model cell) –

  • (float)

pymarthe.utils.shp_utils.enforce_10ch_limit(names)

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

Parameters:

(list) (names) –

Returns:

names

Return type:

list of unique strings of len <= 10.

pymarthe.utils.shp_utils.get_pyshp_field_info(dtypename)

Get pyshp dtype information for a given numpy dtype.

pymarthe.utils.shp_utils.get_pyshp_field_dtypes(code)

Returns a numpy dtype for a pyshp field type.

pymarthe.utils.shp_utils.recarray2shp(recarray, geoms, shpname='recarray.shp', geomtype='Polygon', epsg=None, prj=None, **kwargs)

Write a numpy record array to a shapefile, using a corresponding list of geometries. Modify from librairy: https://github.com/modflowpy/flopy/blob/develop/flopy/export/shapefile_utils.py

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

  • geoms (list of polygons) – 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.

pymarthe.utils.shp_utils.write_prj(shpname, epsg=None, prj=None, wkt_string=None)

Write a projection file (.proj). Figure which CRS option to use (prioritize args over grid reference) option to create prjfile from proj4 string without OGR or pyproj dependencies.

pymarthe.utils.shp_utils.get_url_text(url, error_msg=None)

Get text from a url.

class pymarthe.utils.shp_utils.CRS(prj=None, esri_wkt=None, epsg=None)

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

property crs
Dict mapping crs attributes to proj4 parameters
property grid_mapping_attribs
Map parameters for CF Grid Mappings
http://http://cfconventions.org/cf-conventions/cf-conventions.html,
Appendix F: Grid Mappings
property proj4
Not implemented yet
parse_wkt()
_gettxt(s1, s2)
_getvalue(k)
_getgcsparam(txt)
_getprojcs_unit()
static getprj(epsg, addlocalreference=True, text='esriwkt')

Gets projection file (.prj) text for given epsg code from spatialreference.org See: https://www.epsg-registry.org/ :param epsg: epsg code for coordinate system :type epsg: int :param addlocalreference: 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 get_spatialreference(epsg, text='esriwkt')

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/ :param epsg: epsg code for coordinate system :type epsg: int :param text: string added to url :type text: str

Returns:

url

Return type:

str

static getproj4(epsg)

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

Returns:

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

Return type:

str

class pymarthe.utils.shp_utils.EpsgReference

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

to_dict()

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

_write(data)
reset(verbose=True)
add(epsg, prj)

add an epsg code to epsgref.json

get(epsg)

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

remove(epsg)

removes an epsg entry from epsgref.json

static show()