def main(): """ RichDEM flat resolution: give a gentle slope """ # lazy import RICHDEM try: import richdem as rd except: g.message( flags="e", message=("RichDEM not detected. Install pip3 and " + "then type at the command prompt: " + '"pip3 install richdem".'), ) _input = options["input"] _output = options["output"] _method = options["method"] _exponent = options["exponent"] _weights = options["weights"] if (_method == "Holmgren") or (_method == "Freeman"): if _exponent == "": g.message( flags="w", message=("Exponent must be defined for " + "Holmgren or Freeman methods. " + "Exiting."), ) return else: _exponent = float(_exponent) else: _exponent = None if _weights == "": rd_weights = None else: g_weights = garray.array(_weights, null=np.nan) rd_weights = rd.rdarray(g_weights, no_data=np.nan) dem = garray.array(_input, null=np.nan) mask = dem * 0 + 1 rd_input = rd.rdarray(dem, no_data=np.nan) del dem rd_output = rd.FlowAccumulation( dem=rd_input, method=_method, exponent=_exponent, weights=rd_weights, in_place=False, ) rd_output *= mask accum = garray.array() accum[:] = rd_output[:] accum.write(_output, overwrite=gscript.overwrite())
def main(): """ RichDEM flat resolution: give a gentle slope """ # lazy import RICHDEM try: import richdem as rd except: g.message(flags='e', message=('RichDEM not detected. Install pip3 and ' + 'then type at the command prompt: ' + '"pip3 install richdem".')) options, flags = gscript.parser() _input = options['input'] _output = options['output'] _method = options['method'] _exponent = options['exponent'] _weights = options['weights'] if (_method == 'Holmgren') or (_method == 'Freeman'): if _exponent == '': g.message(flags='w', message=('Exponent must be defined for ' + 'Holmgren or Freeman methods. ' + 'Exiting.')) return else: _exponent = float(_exponent) else: _exponent = None if _weights == '': rd_weights = None else: g_weights = garray.array() g_weights.read(_weights, null=np.nan) rd_weights = rd.rdarray(g_weights, no_data=np.nan) dem = garray.array() dem.read(_input, null=np.nan) mask = dem * 0 + 1 rd_input = rd.rdarray(dem, no_data=np.nan) del dem rd_output = rd.FlowAccumulation(dem=rd_input, method=_method, exponent=_exponent, weights=rd_weights, in_place=False) rd_output *= mask accum = garray.array() accum[:] = rd_output[:] accum.write(_output, overwrite=gscript.overwrite())
def _accumulate_flow_RD(self, props_Pf, hill_flow=False): """ Function to accumualte flow using the richdem package Parameters ---------- props_Pf : float flow proportions calcualte with the RichDEM package using the FlowProportions function hill_flow : Boolean, optional Defines which instance of flow accumulation is updated. If FALSE, the first, default instance is updated. If TRUE, the second, hillslope, instance is updated. The default is False. Returns ------- None. """ if not hill_flow: a = self._drainage_area q = self._discharges else: a = self._hill_drainage_area q = self._hill_discharges # Create weight for flow accum: both open (status ==1) and closed nodes (status ==4) will have zero weight wg = np.full(self.grid.number_of_nodes, self.grid.dx**2) # Only core nodes (status == 0) need to receive a weight wg[self._grid.status_at_node != NodeStatus.CORE] = 0 wg = rd.rdarray( wg.reshape(self.grid.shape), no_data=-9999, ) wg.geotransform = [0, 1, 0, 0, 0, -1] with self._suppress_output(): a[:] = np.array( rd.FlowAccumFromProps(props=props_Pf, weights=wg).reshape( self.grid.number_of_nodes ) ) if any(self.grid.at_node["water__unit_flux_in"] != 1): wg = self.grid.at_node["water__unit_flux_in"] * self.grid.dx * self.grid.dx # Only core nodes (status == 0) need to receive a weight wg[self._grid.status_at_node != NodeStatus.CORE] = 0 wg = rd.rdarray(wg.reshape(self.grid.shape), no_data=-9999) wg.geotransform = [0, 1, 0, 0, 0, -1] with self._suppress_output(): q_pf = rd.FlowAccumFromProps(props=props_Pf, weights=wg) q[:] = np.array(q_pf.reshape(self.grid.number_of_nodes)) else: q[:] = self._drainage_area
def main(): """ RichDEM depression breaching """ # lazy import RICHDEM try: import richdem as rd except: g.message( flags="e", message=("RichDEM not detected. Install pip3 and " + "then type at the command prompt: " + '"pip3 install richdem".'), ) _input = options["input"] _output = options["output"] _topology = options["topology"] dem = garray.array() dem.read(_input, null=np.nan) rd_inout = rd.rdarray(dem, no_data=np.nan) rd.BreachDepressions(dem=rd_inout, in_place=True, topology=_topology) dem[:] = rd_inout[:] dem.write(_output, overwrite=gscript.overwrite())
def __init__(self, dtm_path, manning_path, cellsize=5, radius=2000, nodata=-99, mute=True): self.dtm_ds = gtif.openf(dtm_path) self.dtm_gt = self.dtm_ds.GetGeoTransform() self.mannings_ds = gtif.openf(manning_path) self.mannings_gt = self.mannings_ds.GetGeoTransform() Matrix.__init__(self, gtif.as_array(self.dtm_ds)) self.cellsize = cellsize self.radius = radius self.nodata = nodata self.dtm = rd.rdarray(self.dtm, no_data=nodata) rd.FillDepressions(self.dtm, in_place=True) self.mannings = self.array(gtif.as_array(self.mannings_ds)) self.mannings = GeoTransformFit(self.mannings, self.mannings_gt, self.dtm_gt) self.mute = mute
def main(): """ RichDEM depression filling """ options, flags = gscript.parser() _input = options['input'] _output = options['output'] _epsilon = options['epsilon'] _topology = options['topology'] if _epsilon == 'true': epsilon = True else: epsilon = False dem = garray.array() dem.read(_input, null=np.nan) rd_inout = rd.rdarray(dem, no_data=np.nan) rd.FillDepressions(dem=rd_inout, epsilon=epsilon, in_place=True, topology=_topology) dem[:] = rd_inout[:] dem.write(_output, overwrite=gscript.overwrite())
def main(): """ RichDEM depression filling """ # lazy import RICHDEM try: import richdem as rd except: g.message(flags='e', message=('RichDEM not detected. Install pip3 and ' + 'then type at the command prompt: ' + '"pip3 install richdem".')) _input = options['input'] _output = options['output'] _epsilon = options['epsilon'] _topology = options['topology'] if _epsilon == 'true': epsilon = True else: epsilon = False dem = garray.array() dem.read(_input, null=np.nan) rd_inout = rd.rdarray(dem, no_data=np.nan) rd.FillDepressions(dem=rd_inout, epsilon=epsilon, in_place=True, topology=_topology) dem[:] = rd_inout[:] dem.write(_output, overwrite=gscript.overwrite())
def main(): """ RichDEM flat resolution: give a gentle slope """ # lazy import RICHDEM try: import richdem as rd except: g.message( flags="e", message=("RichDEM not detected. Install pip3 and " + "then type at the command prompt: " + '"pip3 install richdem".'), ) _input = options["input"] _output = options["output"] _attribute = options["attribute"] _zscale = float(options["zscale"]) dem = garray.array() dem.read(_input, null=np.nan) rd_input = rd.rdarray(dem, no_data=np.nan) del dem rd_output = rd.TerrainAttribute(dem=rd_input, attrib=_attribute, zscale=_zscale) outarray = garray.array() outarray[:] = rd_output[:] outarray.write(_output, overwrite=gscript.overwrite())
def main(): """ RichDEM flat resolution: give a gentle slope """ # lazy import RICHDEM try: import richdem as rd except: g.message(flags='e', message=('RichDEM not detected. Install pip3 and ' + 'then type at the command prompt: ' + '"pip3 install richdem".')) _input = options['input'] _output = options['output'] # Check for overwrite _rasters = np.array(gscript.parse_command('g.list', type='raster').keys()) if (_rasters == _output).any(): g.message(flags='e', message="output would overwrite " + _output) dem = garray.array() dem.read(_input, null=np.nan) rd_input = rd.rdarray(dem, no_data=np.nan) rd_output = rd.ResolveFlats(rd_input) dem[:] = rd_output[:] dem.write(_output, overwrite=gscript.overwrite())
def get_slope(self): """ TODO """ beau = rd.rdarray(np.load('imgs/beauford.npz')['beauford'], no_data=-9999) slope = rd.TerrainAttribute(beau, attrib='slope_riserun') rd.rdShow(slope, axes=False, cmap='jet', figsize=(8,5.5)) pass
def _numpy2richdem(self, in_array, no_data=-9999): ''' slope and aspect calculations are performed by richdem, which requires data in rdarray format. this function converts numpy arrays into rdarrays. ''' out_array = rd.rdarray(in_array, no_data=no_data) out_array.projection = self.projection cell_scale = np.around(a=self.side_len / self.resolution, decimals=5) out_array.geotransform = [0, cell_scale, 0, 0, 0, cell_scale] return out_array
def filled_dem(ctx, cropped_dem_fp, filled_dem_fp): logger = ctx.obj['LOGGER'] logger.info("Cropping DEM to watershed extent") with rasterio.open(cropped_dem_fp) as dataset: dem = dataset.read(1) rdem = richdem.rdarray(dem, no_data=dataset.nodata) filled_dem = richdem.FillDepressions(rdem) logger.info("Filled {} pixels".format(np.sum(filled_dem != rdem))) out_meta = dataset.meta with rasterio.open(filled_dem_fp, 'w', **out_meta) as out_dataset: out_dataset.write(filled_dem, indexes=1) logger.info("DONE")
def _create_richdem_properties(self): self._depression_free_dem = cp.deepcopy( rd.rdarray( self.grid.at_node["topographic__elevation"].reshape(self.grid.shape), no_data=-9999, ) ) self._depression_free_dem.geotransform = [0, 1, 0, 0, 0, -1] # Calculate SQUARED length adjacent self.grid.at_node["squared_length_adjacent"] = np.concatenate( ( np.ones((self.grid.number_of_nodes, 4)), 2 * np.ones((self.grid.number_of_nodes, 4)), ), axis=1, ) self._closed = np.zeros(self._grid.number_of_nodes) self._closed[self._grid.status_at_node == NodeStatus.CLOSED] = 1 self._closed = rd.rdarray(self._closed.reshape(self._grid.shape), no_data=-9999) self._closed.geotransform = [0, 1, 0, 0, 0, -1]
def terrain(elvDs, variable="elevation", fillDem=True, flowMethod='D8'): out = elvDs.copy() zScales = { 0: 0.00000898, 10: 0.00000912, 20: 0.00000956, 30: 0.00001036, 40: 0.00001171, 50: 0.00001395, 60: 0.00001792, 70: 0.00002619, 80: 0.00005156 } # calculat the geospatial information from the dataset elvDim = [elvDs.dims['lon'], elvDs.dims["lat"]] xres = float((elvDs.lon[1] - elvDs.lon[0]).values) yres = float((elvDs.lat[1] - elvDs.lat[0]).values) xinit = float((elvDs.lon[0]).values) - (xres / 2) yinit = float((elvDs.lat[0]).values) + (yres / 2) # get elevation values as np.ndarray elvVals = np.squeeze(elvDs[variable].values) rdem = rd.rdarray(elvVals, no_data=np.nan) rdem.projection = '''GEOGCS["WGS 84", DATUM["WGS_1984", SPHEROID["WGS 84",6378137,298.257223563, AUTHORITY["EPSG","7030"]], AUTHORITY["EPSG","6326"]], PRIMEM["Greenwich",0, AUTHORITY["EPSG","8901"]], UNIT["degree",0.0174532925199433, AUTHORITY["EPSG","9122"]], AUTHORITY["EPSG","4326"]] ''' rdem.geotransform = (xinit, xres, 0, yinit, 0, yres) if fillDem: filled = rd.FillDepressions(rdem, epsilon=True, in_place=False, topology='D8') rdem = rd.ResolveFlats(filled, in_place=False) zScale = zScales[np.around(yinit, decimals=-1)] slope = rd.TerrainAttribute(rdem, attrib='slope_percentage', zscale=zScale) accum = rd.FlowAccumulation(rdem, method=flowMethod) out["slope"] = (('time', 'lat', 'lon'), slope[np.newaxis, :, :]) out["flowAcc"] = (('time', 'lat', 'lon'), accum[np.newaxis, :, :]) return out
def remove_depressions(self): self._depression_free_dem = cp.deepcopy( rd.rdarray( self.grid.at_node["topographic__elevation"].reshape(self.grid.shape), no_data=-9999, ) ) self._depression_free_dem.geotransform = [0, 1, 0, 0, 0, -1] with self._suppress_output(): self._depression_handler(self._depression_free_dem) self._sort[:] = np.argsort( np.array(self._depression_free_dem.reshape(self.grid.number_of_nodes)) ) self.grid.at_node["depression_free_elevation"] = self._depression_free_dem
def np2rdarray(in_array, no_data, projection, geotransform): """Converts an numpy array to rdarray. Args: in_array (np.array): The input numpy array. no_data (float): The no_data value of the array. projection (str): The projection of the image. geotransform (str): The geotransform of the image. Returns: object: The richDEM array. """ out_array = rd.rdarray(in_array, no_data=no_data) out_array.projection = projection out_array.geotransform = geotransform return out_array
def np2rdarray(in_array, no_data, projection, geotransform): """Converts numpy array to rdarray. Args: in_array (np.array): The input numpy array containing the image. no_data (float): The no_data value of the image. projection (str): The projection coordinate system of the image. geotransform (str): The geotransform of the image. Returns: rdarray: The richDEM array containing the image. """ out_array = rd.rdarray(in_array, no_data=no_data) out_array.projection = projection out_array.geotransform = geotransform return out_array
def main(): """ RichDEM depression breaching """ options, flags = gscript.parser() _input = options['input'] _output = options['output'] _topology = options['topology'] dem = garray.array() dem.read(_input, null=np.nan) rd_inout = rd.rdarray(dem, no_data=np.nan) rd.BreachDepressions(dem=rd_inout, in_place=True, topology=_topology) dem[:] = rd_inout[:] dem.write(_output, overwrite=gscript.overwrite())
def main(): """ RichDEM flat resolution: give a gentle slope """ options, flags = gscript.parser() _input = options['input'] _output = options['output'] _attribute = options['attribute'] _zscale = float(options['zscale']) dem = garray.array() dem.read(_input, null=np.nan) rd_input = rd.rdarray(dem, no_data=np.nan) del dem rd_output = rd.TerrainAttribute(dem=rd_input, attrib=_attribute, zscale=_zscale) outarray = garray.array() outarray[:] = rd_output[:] outarray.write(_output, overwrite=gscript.overwrite())
def main(): """ RichDEM flat resolution: give a gentle slope """ options, flags = gscript.parser() _input = options['input'] _output = options['output'] # Check for overwrite _rasters = np.array(gscript.parse_command('g.list', type='raster').keys()) if (_rasters == _output).any(): g.message(flags='e', message="output would overwrite " + _output) dem = garray.array() dem.read(_input, null=np.nan) rd_input = rd.rdarray(dem, no_data=np.nan) rd_output = rd.ResolveFlats(rd_input) dem[:] = rd_output[:] dem.write(_output, overwrite=gscript.overwrite())
def process_dem(self, global_dem=''): ''' Download DEM from AWS, calculate slope ''' # Download DEM if not os.path.exists(self.dem_file) and global_dem == '': tPrint("Downloading DEM") elevation.clip(bounds=self.inD.total_bounds, max_download_tiles=90000, output=self.dem_file, product='SRTM3') if not os.path.exists(self.dem_file) and not global_dem == '': tPrint("Downloading DEM") rMisc.clipRaster(rasterio.open(global_dem), self.inD, self.dem_file) # Calculate slope if not os.path.exists(self.slope_file) and os.path.exists(self.dem_file): tPrint("Calculating slope") in_dem = rasterio.open(self.dem_file) in_dem_data = in_dem.read() beau = richdem.rdarray(in_dem_data[0,:,:], no_data=in_dem.meta['nodata']) slope = richdem.TerrainAttribute(beau, attrib='slope_riserun') meta = in_dem.meta.copy() meta.update(dtype = slope.dtype) with rasterio.open(self.slope_file, 'w', **meta) as outR: outR.write_band(1, slope)
def generate_slope_aspect(): """Generate slope and aspect from DEM.""" dem_ds = xr.open_dataset( '/Users/kenzatazi/Downloads/GMTED2010_15n015_00625deg.nc') dem_ds = dem_ds.assign_coords({ 'nlat': dem_ds.latitude, 'nlon': dem_ds.longitude }) dem_ds = dem_ds.sel(nlat=slice(29, 34), nlon=slice(75, 83)) elev_arr = dem_ds.elevation.values elev_rd_arr = rd.rdarray(elev_arr, no_data=np.nan) slope_rd_arr = rd.TerrainAttribute(elev_rd_arr, attrib='slope_riserun') slope_arr = np.array(slope_rd_arr) aspect_rd_arr = rd.TerrainAttribute(elev_rd_arr, attrib='aspect') aspect_arr = np.array(aspect_rd_arr) dem_ds['slope'] = (('nlat', 'nlon'), slope_arr) dem_ds['aspect'] = (('nlat', 'nlon'), aspect_arr) streamlined_dem_ds = dem_ds[['elevation', 'slope', 'aspect']] streamlined_dem_ds.to_netcdf('_Data/SRTM_data.nc')
#del pre_φdtm φdtm[φdtm == 100000] = np.NaN plt.imshow( φdtm, cmap='Greys') # displays better if first φdtm[φdtm == 100000] = np.NaN plt.title(f'c = {c}') #%% [markdown] # Next we calculate the slope surface ```φslope```; max slope parameter, ```Slmin```; and min slope parameter,```Slmax```. In this case, Slmin # and Slmax are established from the 65% and 90% quantiles of the cell # values of the slope surface. We will use the handy TerrainAttribute function from RichDEM to find. ```φslope``'. #%% rda = rd.rdarray(φdtm, no_data=np.NaN) φslope = rd.TerrainAttribute(rda, attrib='slope_riserun') Slmin, Slmax = np.nanpercentile(φslope, [65, 90]) #%% [markdown] # Then we classify points as ground if they are within δh of the reference surface given # by φdtm, and calculate the penetrability surfac, eφpnt.#%% [markdown] # Then we classify points as ground if they are within δh of the reference surface given # by φdtm, and calculate the penetrability surfac, eφpnt. #%% # Classify as ground (2) if δi <= δh S['Classification'] = (S['Z'] - φdtm[S['gridX'], S['gridY']] <= δh) * 2 # create φpnt #φpnt = np.full_like(φdtm, np.NaN)
def transform_slope(self): self.dem_data = rd.rdarray(self.dem_data, no_data=-9999) self.dem_data = rd.TerrainAttribute(self.dem_data, attrib='slope_riserun')
def loadfigures(pathascii, pathvis16R, pathxy, filename): ''' pathascii = 'D:/ANALYSIS/SIMPLECRATERS_MOON/SLDEM_2013_COLDSPOTS/ascii/' pathvis8R = 'D:/ANALYSIS/SIMPLECRATERS_MOON/SLDEM_2013_COLDSPOTS/ascii_visible8R/' pathvis32R = 'D:/ANALYSIS/SIMPLECRATERS_MOON/SLDEM_2013_COLDSPOTS/ascii_visible/' pathxy = 'D:/ANALYSIS/SIMPLECRATERS_MOON/SLDEM_2013_COLDSPOTS/double_detrending/data/' pathascii = '/uio/kant/geo-ceed-u1/nilscp/Desktop/astra/TMP_DOWNLOAD/ascii/' pathvis8R = '/uio/kant/geo-ceed-u1/nilscp/Desktop/astra/TMP_DOWNLOAD/ascii_visible8R/' pathvis32R = '/uio/kant/geo-ceed-u1/nilscp/Desktop/astra/TMP_DOWNLOAD/ascii_visible/' pathxy = 'D:/ANALYSIS/SIMPLECRATERS_MOON/SLDEM_2013_COLDSPOTS/double_detrending/data/' filename = 'cpcrater0000' ''' name_ascii = pathascii + filename + '.asc' name_crater_txt = pathxy + filename + 'XY.txt' name_vis16R = pathvis16R + filename + '_visible.asc' # should be good this way data = readASCII(name_ascii) datavis16R = readASCII(name_vis16R) # load data and constrain size of the array (xc, yc, data, ncenterx, ncentery) = constrainASCII(pathascii, name_ascii, data) (xcv1, ycv1, datav1, ncenterxv1, ncenteryv1) = constrainASCII(pathvis16R, name_vis16R, datavis16R) # load xy dataxy = np.loadtxt(name_crater_txt, skiprows=1, delimiter=";") datax = dataxy[:, 0] datay = dataxy[:, 1] # slope datareload = rd.rdarray(data, no_data=-9999) slope = rd.TerrainAttribute(datareload, attrib='slope_riserun') # profile_curvatyre pfc = rd.TerrainAttribute(datareload, attrib='profile_curvature') # planform curvature pfc2 = rd.TerrainAttribute(datareload, attrib='planform_curvature') # curvature pfc3 = rd.TerrainAttribute(datareload, attrib='curvature') # I could calculate the slope and the curvature and then plot it # plot figures (1) visible 32R, (1) visible 8R, (2) DTM 8R, (3) Slope 8R, (4) Curvature 8R # when (4) is open, engage fig2 = plt.figure(1) plt.pcolormesh(xcv1, ycv1, datav1) plt.colorbar() fig3 = plt.figure(2) plt.pcolormesh(xc, yc, data) plt.colorbar() fig4 = plt.figure(3) plt.pcolormesh(xc, yc, slope) plt.colorbar() return fig4
def display_slope(self): self.dem_data = rd.rdarray(self.dem_data, no_data=-9999) rd.rdShow(self.dem_data, axes=False, cmap='magma', figsize=(8, 5.5))
bbox = [args.ulx, args.uly, args.lrx, args.lry] jimdem = pj.Jim(Path(demfn), bbox=bbox, tileindex=args.tileindex, tiletotal=args.tiletotal, overlap=args.overlap) else: jimdem = pj.Jim(Path(demfn), tileindex=args.tileindex, tiletotal=args.tiletotal, overlap=args.overlap) if args.t_srs is not None: jimdem.geometry.warp(args.t_srs) if args.output_dem is not None: jimdem.io.write(args.output_dem, co=['COMPRESS=LZW', 'TILED=YES']) jimdem.pixops.convert('GDT_Float32') jimdem[jimdem <= 0] = -9999 dem_richdem = rd.rdarray(jimdem.np(), no_data=-9999) dem_richdem.geotransform = jimdem.properties.getGeoTransform() dem_richdem.projection = jimdem.properties.getProjection() slope = rd.TerrainAttribute(dem_richdem, attrib=args.attribute) jimdem.np()[:] = slope jimdem.properties.setNoDataVals(-9999) jimdem.io.write(args.output, co=['COMPRESS=LZW', 'TILED=YES'])
def get_d8_without_depression(self, dem_img, pad=3): dem_rd = rd.rdarray(dem_img, no_data=-9999) dem_filled_rd = np.array(rd.FillDepressions(dem_rd, in_place=False)) d8_without_depression = self.get_d8(dem_filled_rd, pad=pad) return d8_without_depression
def np2rdarray(in_array, no_data, projection, geotransform): out_array = rd.rdarray(in_array, no_data=no_data) out_array.projection = projection out_array.geotransform = geotransform return out_array
return np.sqrt(dx * dx + dy * dy) def np_curvature(x, y, z): d = y[1, 0] - y[0, 0] dy, dx = np.gradient(z, d) dz = np.sqrt(dx * dx + dy * dy) dy, dx = np.gradient(dz, d) return np.sqrt(dx * dx + dy * dy) # Gaussian hill n = 234 x, y, z = gaussian_hill_elevation(n) d = y[1, 0] - y[0, 0] sca = rd.FlowAccumulation(rd.rdarray(z, no_data=-9999), method='Freeman', exponent=1.1) sca *= d fg, ax = pl.subplots(1, 3) im = ax[0].imshow(z, cmap=pl.cm.cividis_r) cb = fg.colorbar(im, ax=ax[0], orientation='horizontal') cb.set_label('Elevation') im = ax[1].imshow(sca, cmap=pl.cm.viridis_r) cb = fg.colorbar(im, ax=ax[1], orientation='horizontal') cb.set_label('SCA (Freeman 1991 flow accumulation, MFD)') v = gaussian_hill_sca(n) - sca vmin = v.min()