tinerator.dem_class
DEM
DEM(self, filepath:str, lagrit_exe:str=None)
This is the 'main' class of TINerator, and stores all DEM and GIS data related to a particular project.
Attributes
filepath (str)
: Filepath to DEM rasterlagrit_exe (str,None)
: Optional filepath to LaGriT binary. If PyLaGriT is configured correctly, this should be unnecessary.
change_ndv
DEM.change_ndv(self, ndv:float)
Changes no_data_value
of the DEM object.
This should be used instead of resetting dem.no_data_value
manually, as it makes deeper changes.
Example
dem.change_ndv(-9999.) print(dem.no_data_value) # -9999.0
Arguments
- ndv (float): New
no_data_value
set_to_ndv
DEM.set_to_ndv(self, value:float)
Changes all occurances of value
in the DEM data
to no_data_value
.
Example
dem.set_to_ndv(dem.dem[0][0]) print(dem.dem == dem.no_data_value) # True
Arguments
- value (float): raster value to replace
set_verbosity
DEM.set_verbosity(self, verb_level:int, filename:str=None)
Set the verbosity level of printed output.
NOTHING
: Nothing (except warnings and related)INFO
: Log outputFULL
: Log output and LaGriT outputDEBUG
: Log output, LaGriT output, and turns on debug mode
Each of these verbosity levels are variables in tinerator.config
.
Example
dem.set_verbosity(tinerator.config.FULL)
Arguments
- verb_level (int): verbosity level
- filename (str): file to write log output to
fill_depressions
DEM.fill_depressions(self, fill_depressions:bool=True, fill_flats:bool=True)
Fills flats and depressions on DEM. On meshes intended to be high- resolution, leaving flats and depressions untouched may cause solver issues. This method should be called before generating a triplane.
Arguments
- fill_depressions (bool): fill pits and depressions on DEM
- fill_flats (bool): fill flats on DEM
Example
dem1 = tin.load.from_file("example.asc") dem2 = tin.load.from_file("example.asc") dem1.fill_depressions() plt.imshow(dem1.dem - dem2.dem) plt.show()
This example shows the different in topology between a post-processed and unprocessed DEM.
watershed_delineation
DEM.watershed_delineation(self, threshold:float, method:str='D8', exponent:float=None, interactive:bool=False)
Performs watershed delineation on a DEM and returns a set of points corresponding to the feature.
Available methods are:
- D8
- D4
- Rho8
- Rho4
- Dinf
- Quinn
- Holmgren
- Freeman
Arguments
- threshold (float): threshold for determining feature from noise
- method (str): Flow calculation method
- interactive (bool): if True and function is called within a Jupyter notebook, then function params can be controlled with sliders
Returns
Polyline of feature as ordered (x,y) pairs
build_uniform_triplane
DEM.build_uniform_triplane(self, edge_length:float, smooth_boundary:bool=False, flip:str='y', apply_elevation:bool=True, outfile:str=None, rectangular_boundary:bool=False, boundary_distance:float=None, interactive:bool=False)
Generates a triplane with uniformly sized elements.
Attributes
edge_length (float)
: desired lengths for triangle edgesflip (str)
: flips array of the elevation raster along a given axis (x,y,xy)smooth_boundary (bool)
: If True, smooth the DEM boundary for better interpolationapply_elevation (bool)
: If True, interpolate DEM elevations onto surface meshoutfile (str)
: filepath to save generated mesh torectangular_boundary (bool)
: set to true if the DEM domain is rectangularboundary_distance (float)
: Overrides edge length and manually sets spacing between boundary nodesinteractive (bool)
: if True and function is called within a Jupyter notebook, then function params can be controlled with sliders
Returns
PyLaGriT mesh object
build_refined_triplane
DEM.build_refined_triplane(self, min_edge_length:float, max_edge_length:float, outfile:str=None, apply_elevation:bool=True, slope:float=2.0, refine_dist:float=0.5, flip:str='y', smooth_boundary:bool=False, rectangular_boundary:bool=False, boundary_distance:float=None, interactive:bool=False)
Generates a refined triangular mesh, with a minimum refinement length defined by h.
Attributes
min_edge_length (float)
: minimum triangle edge lengthsmax_edge_length (float)
: maximum triangle edge lengthsoutfile (str)
: Filepath to save mesh toapply_elevation (bool)
: If True, interpolate DEM elevations onto surface meshslope (float)
: slope of refine functionrefine_dist (float)
: Threshold for minimum distance in distance mapflip (str)
: flips array of the elevation raster along a given axis ('x','y','xy'
)smooth_boundary (bool)
: If True, smooth the DEM boundary for better interpolationrectangular_boundary (bool)
: set to true if the DEM domain is rectangularboundary_distance (float)
: Overrides edge length and manually sets spacing between boundary nodesinteractive (bool)
: if True and function is called within a Jupyter notebook, then function params can be controlled with sliders
Returns
PyLaGriT mesh object
build_layered_mesh
DEM.build_layered_mesh(self, layers, matids=None, outfile:str=None)
Builds a layered mesh from a triplane.
Arguments
- layers (list
) : List of sequential layer thicknesses - matids (list
) : List of material IDs to set each respective layer to - outfile (str): Filepath to save mesh to
Example
layers = [1.,1.,3.,10.,2.] matids = [1,1,2,1,3] dem.build_layered_mesh(layers,matids=matids)
add_attribute
DEM.add_attribute(self, data, layers=None, attribute_name=None, outfile=None, dtype=None)
Adds an attribute to the stacked mesh, over one or more layers. Default is all. Data must be an NxM matrix - it does not necessarily have to be the same size at the DEM, but is recommended as it will be streched to span the domain of it.
attribute_name
will be the element-based attribute the data is written into.
The default is 'material ID' (itetclr
), but can be changed to any
[a-z][A-Z][0-9] string (outside of reserved LaGriT keywords).
Arguments
- data (np.ndarray): NxM matrix of data to be written as matrix
- layers (list
) : Layer IDs to write attributes to. Defaults to 'all'. - attribute_name (str): Attribute name to store data in. Defaults to material ID
- outfile (str): Filename to write mesh to
- dtype (type): Data type of elements in data (
float
orint
)
map_function_to_attribute
DEM.map_function_to_attribute(self, operator='+', layers=None, attribute_name=None, outfile=None, fn=<function DEM.<lambda> at 0x1c337f2400>)
Maps a function and on operator onto mesh data. The function fn should take one parameter: the current layer number. The operator will perform on the data and function result.
In other words, the new attribute data will be a result of:
attribute_data(layer) = attribute_data [operation] fn(layer)
For fn = lambda layer: layer*100
and operator +
,
attribute_data(layer) = attribute_data + layer*100
meaning that if a selection of attribute data is
[1,3,5,10,12...]
then, with operator '+' and lambda layer: layer*100,
layer 1: [101,103,105,110,112...] layer 2: [201,203,205,210,212...] layer 3: [301,103,305,310,312...] ...
getBoundingBox
DEM.getBoundingBox(self, mpl_style:bool=True)
Returns the bounding box (or extent) of the DEM domain.
By default, the format of the extent returned is:
(x_min,x_max,y_min,y_max)
By setting mpl_style=False
, the format changes to:
(x_min,y_min,x_max,y_max)
Extent units are relative to the parent DEM coordinate system.
Arguments
- mpl_style (bool): Change the format of the returned extent
Returns
DEM domain bounding box
plot_dem
DEM.plot_dem(self, hillshade:bool=False, plot_out:str=None)
Plots the loaded DEM.
plot_boundary
DEM.plot_boundary(self)
Plots the DEM domain boundary (if available).
plot_feature
DEM.plot_feature(self)
Displays the feature captured by performing watershed delination (if available).