Beispiel #1
0
 def get_twi(self):
     dem_export_fn = '%s/dem_%s.tif' % (os.environ['temporary_folder'],
                                        self._id)
     cdem_export_fn = '%s/cdem_%s.tif' % (os.environ['temporary_folder'],
                                          self._id)
     direction_export_fn = '%s/direction_%s.tif' % (
         os.environ['temporary_folder'], self._id)
     accumulation_export_fn = '%s/accumulation_%s.tif' % (
         os.environ['temporary_folder'], self._id)
     dem = self.read_raster('elevation')
     dem.export_as_tif(dem_export_fn)
     routing.fill_pits((dem_export_fn, 1), cdem_export_fn)
     routing.flow_dir_mfd((cdem_export_fn, 1), direction_export_fn)
     routing.flow_accumulation_mfd((direction_export_fn, 1),
                                   accumulation_export_fn)
     ds = gdal.Open(accumulation_export_fn)
     metadata_dict = {}
     metadata_dict['affine_transformation'] = ds.GetGeoTransform()
     metadata_dict['nodata'] = -32767
     twi = Imagee(np.array(ds.GetRasterBand(1).ReadAsArray()),
                  metadata_dict)
     self.add_raster(twi, 'twi')
     os.remove(dem_export_fn)
     os.remove(cdem_export_fn)
     os.remove(direction_export_fn)
     os.remove(accumulation_export_fn)
     return twi
def get_twi(im_object):
    with TemporaryDirectory() as tempdir:
        im_object.export_as_tif('%s/dem.tif' % tempdir)
        routing.fill_pits(('%s/dem.tif' % tempdir, 1), '%s/cdem.tif' % tempdir)
        routing.flow_dir_mfd(('%s/cdem.tif' % tempdir, 1),
                             '%s/dir.tif' % tempdir)
        routing.flow_accumulation_mfd(('%s/dir.tif' % tempdir, 1),
                                      '%s/twi.tif' % tempdir)
        ds = gdal.Open('%s/twi.tif' % tempdir)
        metadata_dict = im_object.get_metadata()
        twi = Imagee(np.array(ds.GetRasterBand(1).ReadAsArray()),
                     metadata_dict)
        return twi
def flow_accumulation(dem, flow_accum, cleanup=True, dinfinity=False, pitfill=False):
    """
    Calculate reach length, slope, drainage area, min and max elevations
    and write them as attributes to the network
    :param dem: Absolute path to a DEM raster.
    :param flow_accum: Absolute path to a flow accumulation raster (cell counts)
    :param cleanup: determines whether intermediate rasters are deleted.
    :param dinfinity: If true then the dInfinity otherwise d8 algorithm
    :param pitfill: If true then DEM is pit filled before flow accumulation
    :return: None
    """

    log = Logger('Flow Accum')

    if os.path.isfile(flow_accum):
        log.info('Skipping flow accumulation because output exists at {}'.format(flow_accum))
        return None

    tempfolder = os.path.join(os.path.dirname(flow_accum), 'temp')
    cleanup_temp_folder(tempfolder)
    if not os.path.isdir(tempfolder):
        os.mkdir(tempfolder)

    outputDir = os.path.dirname(flow_accum)
    tempPitFill = os.path.join(tempfolder, 'temp_pitfill.tif')
    tempFlowDir = os.path.join(tempfolder, 'temp_flowDir.tif')

    prepared_dem = dem
    if pitfill:
        log.info('Filling pits in DEM and writing to {}'.format(tempPitFill))
        fill_pits((dem, 1), tempPitFill, working_dir=outputDir)
        prepared_dem = tempPitFill

    log.info('Calculating flow direction in pit filled raster and writing to: {}'.format(tempFlowDir))
    if dinfinity:
        flow_dir_mfd((prepared_dem, 1), tempFlowDir)
    else:
        flow_dir_d8((prepared_dem, 1), tempFlowDir)

    log.info('Calculating flow accumulation raster and writing to: {}'.format(flow_accum))
    if dinfinity:
        flow_accumulation_mfd((tempFlowDir, 1), flow_accum)
    else:
        flow_accumulation_d8((tempFlowDir, 1), flow_accum)

    if cleanup:
        cleanup_temp_folder(tempfolder)

    log.info('Flow accumulation completed successfully.')
Beispiel #4
0
 def get_twi(self):
     dem_export_fn='/tmp/dem_%s.tif' % self._id
     cdem_export_fn='/tmp/cdem_%s.tif' % self._id
     direction_export_fn='/tmp/direction_%s.tif' % self._id
     accumulation_export_fn='/tmp/accumulation_%s.tif' % self._id
     dem=self.read_raster('dem')
     dem.export_as_tif(dem_export_fn)
     routing.fill_pits((dem_export_fn,1),cdem_export_fn)
     routing.flow_dir_mfd((cdem_export_fn,1),direction_export_fn)
     routing.flow_accumulation_mfd((direction_export_fn,1),accumulation_export_fn)
     ds = gdal.Open(accumulation_export_fn)
     metadata_dict={}
     metadata_dict['affine_transformation']=ds.GetGeoTransform()
     twi=Imagee(np.array(ds.GetRasterBand(1).ReadAsArray()),metadata_dict)
     self.add_raster(twi,'twi')
     return twi
Beispiel #5
0
def drain_area(dem, drain_area_out):
    """
    Creates a raster where each pixel represents the contributing
    upstream drainage area in km2. DEM should be in a desired projected coordinate system.
    PARAMS
    :dem: string - path to dem raster file
    :drain_area_out: string - path to output drainage area raster file
    """

    # sets up intermediate file uris
    os.chdir(os.path.dirname(dem))
    filled = 'filled.tif'
    fd = 'fd.tif'
    fa = 'fa.tif'

    # geoprocessing functions
    rt.fill_pits((dem, 1), filled)
    rt.flow_dir_mfd((filled, 1), fd)
    rt.flow_accumulation_mfd((fd, 1), fa)

    # convert flow accumulation to drainage area
    flow_acc = rasterio.open(fa)
    resolution = flow_acc.res[0]
    flow_acc_array = flow_acc.read(1)
    dr_area_array = (flow_acc_array * resolution**2)/1000000.0

    # write output file
    profile = flow_acc.profile

    with rasterio.open(drain_area_out, 'w', **profile) as dst:
        dst.write(dr_area_array, 1)

    # delete intermediate files
    # os.remove(filled)
    # os.remove(fd)
    # os.remove(fa)

    return