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 RichFlow(bassin, zone, root, flowdir, overwrite): """ Calcule le plan de drainage en utilisant RichDEM : FillDepressions + ResolveFlats """ raster_template = os.path.join(root, bassin, zone, 'DEM5M.tif') flow_raster = os.path.join(root, bassin, zone, flowdir) if os.path.exists(flow_raster) and not overwrite: click.secho('Output already exists : %s' % flow_raster, fg=WARNING) return dem = rd.LoadGDAL(raster_template) filled = rd.FillDepressions(dem) rd.ResolveFlats(filled, True) flow = ta.flowdir(filled, filled.no_data) ds = rio.open(raster_template) profile = ds.profile.copy() profile.update(dtype=np.int16, nodata=-1, compress='deflate') with rio.open(flow_raster, 'w', **profile) as dst: dst.write(flow, 1) click.secho('Saved to %s' % flow_raster, fg=SUCCESS)
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 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())