Shapefiles#
Reading#
Read shapefile and create a NetworkModelGrid.
There are a number of assumptions that are required about the shapefile.
The shapefile must be a polyline shapefile.
All polylines must be their own object (e.g. no multi-part polylines).
Polyline endpoints match perfectly.
You might notice that there is no write_shapefile
function. If this is
something you need for your work, please make a GitHub issue to start this
process.
- param file:
File path or file-like of a valid polyline shapefile
- type file:
str or file-like
- param dbf:
If file is file-like, the dbf must also be passed.
- type dbf:
file-like, optional
- param store_polyline_vertices:
If True (default), store the vertices of the polylines in the at_link fields
x_of_polyline
andy_of_polyline
.- type store_polyline_vertices:
bool, optional
- param points_shapefile:
File path or file-like of a valid point shapefile.
- type points_shapefile:
str or file-like
- param points_dbf:
If file is file-like, the dbf must also be passed.
- type points_dbf:
file-like, optional
- param link_fields:
List of polyline shapefile attributes to import as landlab at-link fields. Default is to import all.
- type link_fields:
list, optional
- param node_fields:
List of point shapefile attributes to import as landlab at-node fields. Default is to import all.
- type node_fields:
list, optional
- param link_field_conversion:
Dictionary mapping polyline shapefile field names to desired at link field names. Default is no remapping.
- type link_field_conversion:
dict, optional
- param node_field_conversion:
Dictionary mapping node shapefile field names to desired at node field names. Default is no remapping.
- type node_field_conversion:
dict, optional
- param link_field_dtype:
Dictionary mapping node shapefile field names to desired dtype. Default is no change to dtype.
- type link_field_dtype:
dict, optional
- param node_field_dtype:
Dictionary mapping node shapefile field names to desired dtype. Default is no change to dtype.
- type node_field_dtype:
dict, optional
- param threshold:
Maximum distance between a point in the point shapefile and a polyline junction in the polyline shapefile. Units are the same as in the shapefiles. Default is zero (requiring perfect overlap).
- type threshold:
float, optional
- returns:
grid – The network model grid will have nodes at the endpoints of the polylines, and links that connect these nodes. Any fields associated with the shapefile will be added as at-link fields. If a point shapefile is provided those values will be added as at-node fields.
- rtype:
NetworkModelGrid instance
Examples
First, we make a simple shapefile
>>> from io import BytesIO
>>> import os
>>> import shapefile
>>> shp = BytesIO()
>>> shx = BytesIO()
>>> dbf = BytesIO()
>>> w = shapefile.Writer(shp=shp, shx=shx, dbf=dbf)
>>> w.shapeType = shapefile.POLYLINE
>>> w.field("spam", "N")
>>> w.line([[[5, 5], [10, 10]]])
>>> w.record(37)
>>> w.line([[[5, 0], [5, 5]]])
>>> w.record(100)
>>> w.line([[[5, 5], [0, 10]]])
>>> w.record(239)
>>> w.close()
Now create a NetworkModelGrid with read_shapefile:
>>> from landlab.io.shapefile import read_shapefile
>>> grid = read_shapefile(shp, dbf=dbf)
>>> grid.nodes
array([0, 1, 2, 3])
>>> grid.x_of_node
array([ 5., 5., 0., 10.])
>>> grid.y_of_node
array([ 0., 5., 10., 10.])
>>> grid.nodes_at_link
array([[0, 1],
[2, 1],
[1, 3]])
>>> assert "spam" in grid.at_link
>>> grid.at_link["spam"]
array([100, 239, 37])
Next lets also include a points file. First create both shapefiles.
>>> shp = BytesIO()
>>> shx = BytesIO()
>>> dbf = BytesIO()
>>> w = shapefile.Writer(shp=shp, shx=shx, dbf=dbf)
>>> w.shapeType = shapefile.POLYLINE
>>> w.field("spam", "N")
>>> w.line([[[5, 5], [10, 10]]])
>>> w.record(37)
>>> w.line([[[5, 0], [5, 5]]])
>>> w.record(100)
>>> w.line([[[5, 5], [0, 10]]])
>>> w.record(239)
>>> w.close()
>>> p_shp = BytesIO()
>>> p_shx = BytesIO()
>>> p_dbf = BytesIO()
>>> p_w = shapefile.Writer(shp=p_shp, shx=p_shx, dbf=p_dbf)
>>> p_w.shapeType = shapefile.POINT
>>> p_w.field("eggs", "N")
>>> p_w.point(5, 0)
>>> p_w.record(2)
>>> p_w.point(5, 5)
>>> p_w.record(4)
>>> p_w.point(0, 10)
>>> p_w.record(8)
>>> p_w.point(10, 10)
>>> p_w.record(6)
>>> p_w.close()
Now read in both files together.
>>> grid = read_shapefile(shp, dbf=dbf, points_shapefile=p_shp, points_dbf=p_dbf)
>>> grid.nodes
array([0, 1, 2, 3])
>>> grid.x_of_node
array([ 5., 5., 0., 10.])
>>> grid.y_of_node
array([ 0., 5., 10., 10.])
>>> grid.nodes_at_link
array([[0, 1],
[2, 1],
[1, 3]])
>>> assert "spam" in grid.at_link
>>> grid.at_link["spam"]
array([100, 239, 37])
>>> assert "eggs" in grid.at_node
>>> grid.at_node["eggs"]
array([2, 4, 8, 6])
- __call__(*args, **kwargs)#
Call self as a function.
- __delattr__(name, /)#
Implement delattr(self, name).
- __dir__()#
Default dir() implementation.
- __eq__(value, /)#
Return self==value.
- __format__(format_spec, /)#
Default object formatter.
- __ge__(value, /)#
Return self>=value.
- __get__(instance, owner=None, /)#
Return an attribute of instance, which is of type owner.
- __getattribute__(name, /)#
Return getattr(self, name).
- __getstate__()#
Helper for pickle.
- __gt__(value, /)#
Return self>value.
- __hash__()#
Return hash(self).
- __init__(*args, **kwargs)#
Initialize self. See help(type(self)) for accurate signature.
- __init_subclass__()#
This method is called when a class is subclassed.
The default implementation does nothing. It may be overridden to extend subclasses.
- __le__(value, /)#
Return self<=value.
- __lt__(value, /)#
Return self<value.
- __ne__(value, /)#
Return self!=value.
- __new__(*args, **kwargs)#
Create and return a new object. See help(type) for accurate signature.
- __reduce__()#
Helper for pickle.
- __reduce_ex__(protocol, /)#
Helper for pickle.
- __repr__()#
Return repr(self).
- __setattr__(name, value, /)#
Implement setattr(self, name, value).
- __sizeof__()#
Size of object in memory, in bytes.
- __str__()#
Return str(self).
- __subclasshook__()#
Abstract classes can override this to customize issubclass().
This is invoked early on by abc.ABCMeta.__subclasscheck__(). It should return True, False or NotImplemented. If it returns NotImplemented, the normal algorithm is used. Otherwise, it overrides the normal algorithm (and the outcome is cached).