def process_DEM(path_to_dem, path_to_slope=None, path_to_aspect=None):
    #reproject DEM to UTM

    #set gdal raster processing options
    processing_options = gdal.DEMProcessingOptions(alg='ZevenbergenThorne',
                                                   slopeFormat='degree')

    #run processing
    if path_to_slope == None and path_to_aspect == None:
        return

    elif path_to_slope != None and path_to_aspect == None:
        gdal.DEMProcessing(path_to_slope,
                           path_to_dem,
                           'slope',
                           options=processing_options)
        return

    elif path_to_slope == None and path_to_aspect != None:
        gdal.DEMProcessing(path_to_aspect,
                           path_to_dem,
                           'aspect',
                           options=processing_options)
        return
    else:
        gdal.DEMProcessing(path_to_slope,
                           path_to_dem,
                           'slope',
                           options=processing_options)
        gdal.DEMProcessing(path_to_aspect,
                           path_to_dem,
                           'aspect',
                           options=processing_options)
        return
示例#2
0
def get_dem_slope_and_angle(dem_path, slope_out_path, aspect_out_path):
    """Produces two .tifs, slope and aspect, from an imput DEM.
    Assumes that the DEM is in meters."""
    log.info("Calculating slope and apsect rasters from")
    dem = gdal.Open(dem_path)
    gdal.DEMProcessing(slope_out_path, dem, "slope")  # For DEM in meters
    gdal.DEMProcessing(aspect_out_path, dem, "aspect")
    dem = None
示例#3
0
def get_dem_slope_and_angle(dem_path, slope_out_path, aspect_out_path):
    """Produces two .tifs, slope and aspect, from an imput DEM.
    Assumes that the DEM is in meters."""
    log.info("Calculating slope and apsect rasters from {}".format(dem_path))
    dem = gdal.Open(dem_path)
    slope_options = gdal.DEMProcessingOptions(
        slopeFormat="degree", scale=111139)  # Scale from latlon to meters
    gdal.DEMProcessing(slope_out_path, dem, "slope",
                       options=slope_options)  # For DEM in latlon
    aspect_options = gdal.DEMProcessingOptions(zeroForFlat=True, scale=111139)
    gdal.DEMProcessing(aspect_out_path, dem, "aspect", options=aspect_options)
    dem = None
示例#4
0
    def getPente(self):
        """ Permet de récupérer un array Numpy de la pente (produit dérivé du modèle numérique de terrain).
                Returns:
                    pente (Numpy.ma.masked_array): Array des valeurs de pente du modèle numérique de terrain (MNT).
        """
        mnt = gdal.Open(self.mnt)

        slope = gdal.DEMProcessing(r'data/slope.tif',
                                   mnt,
                                   'slope',
                                   computeEdges=True)
        slope = None

        ds = gdal.Open(r'data/slope.tif')
        band = ds.GetRasterBand(1)
        array = band.ReadAsArray()

        print('valeur de pente')
        print(array)

        array[array == 0] = -9999

        print('valeur de pente')
        print(array)

        return array
示例#5
0
def create_hillshade(srcDS, destName, zf, multiDirectional):
    '''
    Calculate hillshade for tile
    '''

    print('Creating hillshade for {}'.format(destName))
    gdal.DEMProcessingOptions(zFactor=zf, multiDirectional=multiDirectional)
    gdal.DEMProcessing(destName, srcDS, 'hillshade')
def roughness():
    dstPath, dataset = create_raster_dataset_as_per_other_dataset()
    dst_ds = gdal.DEMProcessing(dstPath, dataset, 'roughness', format='GTiff')
    dst_ds.FlushCache()
    for i in range(dst_ds.RasterCount):
        i = i + 1
        dst_ds.GetRasterBand(i).ComputeStatistics(False)
    dst_ds.BuildOverviews('average',
                          [2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048])
示例#7
0
def calculate_slope(DEM):
    gdal.DEMProcessing('slope.tif',
                       DEM,
                       'slope',
                       alg='ZevenbergenThorne',
                       scale=500000)
    with rasterio.open('slope.tif') as dataset:
        slope = dataset.read(1)
    return slope
示例#8
0
    def getOrientation(self):
        """ Permet de récupérer un array Numpy de l'orientation (produit dérivé du modèle numérique de terrain).
                Returns:
                    orientation (Numpy.ma.masked_array): Array des valeurs de l'orientation du modèle numérique de terrain (MNT).
        """
        mnt = gdal.Open(self.mnt)

        aspect = gdal.DEMProcessing(r'data/aspect.tif', mnt, 'aspect', computeEdges=True)
        aspect = None

        ds = gdal.Open(r'data/aspect.tif')
        band = ds.GetRasterBand(1)
        array = band.ReadAsArray()

        array[array == 0] = -9999

        return array
def hillshade():
    dstPath, dataset = create_raster_dataset_as_per_other_dataset()
    dst_ds = gdal.DEMProcessing(dstPath,
                                dataset,
                                'hillshade',
                                format='GTiff',
                                alg='ZevenbergenThorne',
                                multiDirectional=True,
                                computeEdges=True,
                                scale=111120,
                                zFactor=30)
    dst_ds.FlushCache()
    for i in range(dst_ds.RasterCount):
        i = i + 1
        dst_ds.GetRasterBand(i).ComputeStatistics(False)
    dst_ds.BuildOverviews('average',
                          [2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048])
def aspect(path):
    datasource, path = open_File(path)
    print "Creating New Dataset for Aspect"
    dstPath, dataset, filepath = create_raster_dataset_as_per_other_dataset(
        path, datasource)
    dst_ds = gdal.DEMProcessing(dstPath,
                                dataset,
                                'aspect',
                                format='GTiff',
                                alg='ZevenbergenThorne',
                                scale=111120,
                                zFactor=30)
    dst_ds.FlushCache()
    for i in range(dst_ds.RasterCount):
        i = i + 1
        dst_ds.GetRasterBand(i).ComputeStatistics(False)
    min, max, d = reading_raster_band(dstPath)
    histogram(min, max, dst_ds)
    dst_ds.BuildOverviews('average',
                          [2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048])
    dst_ds = None
    return dstPath
示例#11
0
def gdaldem(ds, producttype='slope', returnma=True):
    """
    perform gdaldem operations such as slope, hillshade etc via the python api
    Parameters
    -----------
    ds: gdal dataset
        DEM dataset for which derived products are to be computed
    producttype: str
        operation to perform (e.g., slope, aspect, hillshade)
    returnma: bool
        return the product as masked array if true, or a dataset if false
    Returns
    -----------
    out: masked array or dataset
        output product in form of masked array or dataset (see params)
    """
    dem_p_ds = gdal.DEMProcessing('', ds, producttype, format='MEM')
    ma = iolib.ds_getma(dem_p_ds)
    if returnma:
        out = ma
    else:
        out = dem_p_ds
    return out
示例#12
0
def calcular_dem(operacion, fichero_entrada, fichero_salida):
    gdal.DEMProcessing(fichero_salida, fichero_entrada, operacion)
示例#13
0
文件: DEM.py 项目: ECCC-MSC/WSC-SSWM
def gdalslope(DEM, dst, latlon=True):
    if latlon:
        scale = 111120
    else:
        scale = 1
    gdal.DEMProcessing(dst, DEM, "slope", scale=scale)
示例#14
0
@author: Tobi
"""
#alternativa: from funciones import cargar_raster # selecionar cuales funciones a importar

import funciones as fun  # archivo de funciones.py
import numpy as np

lado = 5

flow_acc = fun.cargar_raster('flowacc.tif')
a = flow_acc * lado**2  # **derivado, funcion tiene que empezar con minuscula

import gdal

gdal.DEMProcessing('aspect.tif', 'filldem.tif', 'aspect')

aspect = fun.cargar_raster('aspect.tif')  #cargar raster
aspect_rad = aspect * np.pi / 180

aspect_sen = np.sin(aspect_rad)  #calcular sinus (seno)
aspect_cos = np.cos(aspect_rad)  #calcular cosinus

aspect_sen_pos = np.abs(aspect_sen)
aspect_cos_pos = np.abs(aspect_cos)  #absolute/positive values

b = 5 * (aspect_sen_pos + aspect_cos_pos)

a_e = a / b

gdal.DEMProcessing('slope.tif', 'filldem.tif', 'slope')
	addAspect = GetParameter(3)

	writeLog(inputFolder, gdb, addSlope, addAspect)

	# Collect raster images
	gTiffs = getRasters(inputFolder)
	virtualMosaic = gdal.BuildVRT('virtualRaster.vrt', gTiffs)
	
	# Process DEM Mosaic
	tempDEM = RasterProcessor(virtualMosaic)
	mDEM = tempDEM.array2raster()
	mDEM.save(os.path.join(gdb, 'DEM_Mosaic'))
	del mDEM, tempDEM
	
	tempDEM = RasterProcessor(virtualMosaic)
	ftDEM = tempDEM.array2raster(footConversion=True)
	ftDEM.save(os.path.join(gdb, 'DEM_Mosaic_ft'))
	del ftDEM, tempDEM

	if addSlope:
		demSlope = gdal.DEMProcessing('slope.vrt', mosaic, "slope")
		tempSlope = RasterProcessor(demSlope)
		outSlope = tempSlope.array2raster()
		outSlope.save(os.path.join(gdb, 'DEM_Slope'))
		
	if addAspect:
		demAspect = gdal.DEMProcessing('aspec.vrt', mosaic, "aspect")
		tempAspect = RasterProcessor(demAspect)
		outAspect = tempAspect.array2raster()
		outAspect.save(os.path.join(gdb, 'DEM_Aspect'))
示例#16
0
                                  dest_folder, feuillet))
            except:
                print('mnt pas disponible')
                continue
        else:
            print('mnt deja download, processing...')
            pass

        print('mnt disponible, processing.... ')
        count_tile = count_tile + 1
        mnt = r'{}\MNT_{}.tif'.format(dest_folder, feuillet)

        ## calcul de pente et des orientations de pente
        slopes = os.path.join(dest_folder, 'slopes.tif')
        aspect = os.path.join(dest_folder, 'aspect.tif')
        gdal.DEMProcessing(slopes, mnt, "slope")
        gdal.DEMProcessing(aspect, mnt, "aspect")

        ## identidfication des portion avec une pente assez prononc�e pour �tre consdid�r�e comme une falaise
        raster = rasterio.open(slopes)
        band = raster.read(1)

        band = band.clip((min_slope - 1), min_slope)

        band[band == (min_slope - 1)] = 0
        band[band == min_slope] = 1

        print(len(band[band == 1]))

        if len(band[band == 1]) == 0:
            print(
def calculate_slope(elevation, dst):
    gdal.DEMProcessing(dst + '_slope.tif', elevation, 'slope')
def calculate_aspect(elevation, dst):
    gdal.DEMProcessing(dst + '_aspect.tif', elevation, 'aspect')
示例#19
0
        outfile = infile.replace('.tif', '.{}_{}_{}.tif'.format(cmap_name, cmin, cmax))


    #%% Create color table
    ### Format: value R G B alpha
    colorfile = '{}_{}_{}.txt'.format(cmap_name, cmin, cmax)
    cmap_RGB = np.int16(np.round(cmap(np.linspace(0, 1, n_color))*255))
    with open(colorfile, "w") as f:
        for i in range(n_color):
            print("{} {} {} {} 255".format(cmin+i*(cmax-cmin)/(n_color-1),
                   cmap_RGB[i, 0], cmap_RGB[i, 1], cmap_RGB[i, 2]), file=f)
        print("nv 0 0 0 0", file=f)


    #%% gdal dem
    gdal.DEMProcessing(outfile, infile, "color-relief", colorFilename=colorfile,
                       format="GTiff", creationOptions=gdal_option, addAlpha=4)


    #%% colorbar
    if cbar_flag:
        fig, ax = plt.subplots(figsize=(3, 1))
        norm = mpl.colors.Normalize(cmin, cmax)
        mpl.colorbar.ColorbarBase(ax, cmap=cmap, norm=norm, orientation='horizontal')
        cbarfile = "{}_{}_{}.pdf".format(cmap_name, cmin, cmax)
        plt.tight_layout()
        plt.savefig(cbarfile, transparent=True)
        plt.close()
        

    #%% Remove intermediate files
    os.remove(colorfile)
def dem_slp_hillshade(gz_folder, dem, slp):
    ## filenames for processed dem, slope and hillshade
    dst_dem = gz_folder + "/" + "DEM.tif"
    dst_slp = gz_folder + "/" + "perslp.tif"
    dst_hshd = gz_folder + "/" + "hillshade.tif"

    ## read in the first tif to crop the dem
    file_list = glob.glob1(gz_folder, "*.tif")
    print file_list, gz_folder

    first_file_ds = gdal.Open(gz_folder + "/" + file_list[0])

    ## grab image size and projection info
    nCol = first_file_ds.RasterXSize
    nRow = first_file_ds.RasterYSize + 1

    # coordinates are for top-left corners of pixels
    geotrans = first_file_ds.GetGeoTransform()    
    
    # clip with geotrans from first Landsat band
    minx = geotrans[0]
    maxy = geotrans[3]
    maxx = minx + geotrans[1] * nCol
    miny = maxy + geotrans[5] * nRow
    
    # open the big DEM and percent slope
    src_dem = gdal.Open(dem)
    src_slp = gdal.Open(slp)

    ##Clip our slope and hillshade mask to the image (have to do this for each image due to the variable extents of each image)
    GeoClip= [minx, maxy, maxx, miny]
    print GeoClip
    print "clipping DEM..."
    dem_clip = gdal.Translate(dst_dem, src_dem, projWin = GeoClip)
    
    print "calculating slope..."
    slp_clip = gdal.Translate(dst_slp, src_slp, projWin = GeoClip)
    #perslp = gdal.DEMProcessing(dst_slp, dem_clip, 'slope', slopeFormat = 'percent', format = 'GTiff')

    # close open datasets
    src_dem = None
    src_slp = None
    dem_clip = None
    slp_clip = None
    
    ## get solar geometry
    print "Getting metadata ..."
    Metadata = glob.glob1(gz_folder, "*.xml")
    xmldoc = etree.parse(gz_folder + "/" + Metadata[0])
    root = xmldoc.getroot()
    SunAngleData=str(root[0][6].attrib)
    charlist="':}{,"
    for char in charlist:
        SunAngleData=SunAngleData.replace(char,"")
    SunAngleList=SunAngleData.split()
    Zen= float(SunAngleList[3])
    Az= float(SunAngleList[5])
    Az = int(Az)
    Alt= float(90 - Zen)
    print Alt
    print Az
    
    ## calculate hillshade
    print "calculate hillshade ..."
    #HillshadeOut="hillshade_mask" + "_%s.tif"%"_".join(raster.split('_'))[0:length]
    #Hoping the altitude issue is repaired shortly, will be an easy fix once it is corrected
    #Hillshade = gdal.DEMProcessing('hillshade.tif', DEM, 'hillshade', format = 'GTiff', azimuth = Az, altitude=Alt)
    dst_dem_ds = gdal.Open(dst_dem)
    geotrans = dst_dem_ds.GetGeoTransform()
    prj = dst_dem_ds.GetProjectionRef()
    hlshd_ds = gdal.DEMProcessing(dst_hshd, dst_dem_ds, 'hillshade', format = 'GTiff', azimuth = Az) # this fails on linux :(
    hlshd_band = hlshd_ds.GetRasterBand(1)
    hlshd_data = hlshd_band.ReadAsArray()
    
    # save the hillshade
    nrows,ncols = np.shape(hlshd_data)
    driver = gdal.GetDriverByName("GTiff")
    dst_hlshd = driver.Create(dst_hshd, ncols, nrows, 1, gdal.GDT_Byte)
    dst_hlshd.SetGeoTransform( geotrans )
    dst_hlshd.SetProjection( prj )
    hlsh_band = dst_hlshd.GetRasterBand(1)       
    hlsh_band.WriteArray( hlshd_data )
    hlsh_band.SetNoDataValue(255)
    hlsh_band = None
    dst_hlshd.FlushCache()
    dst_hlshd = None    
    dst_dem_ds = None
示例#21
0
文件: DEM.py 项目: ECCC-MSC/WSC-SSWM
def gdalTPI(DEM, dst, latlon=True):
    gdal.DEMProcessing(dst, DEM, "TPI")
示例#22
0
#     "{qswatplus_wf_dir}/dem_data.dat".format(qswatplus_wf_dir=os.environ["qswatplus_wf"]))

# for suff in dem_data_variable.suffix_list:   # suff is the last_part of each raster that is appended to dem
#     if suff == "fel.tif":
#         continue
#     with open('{base}/{project_name}/Watershed/Rasters/DEM/{dem_name}{suff}'.format(
#             base=sys.argv[1], project_name=project_name, dem_name=dem_name, suff=suff), 'wb') as fl:
#         fl.write(dem_data_variable.raster_data)

log.info("creating hillshade for DEM", keep_log)
hillshade_name = '{base}/{project_name}/Watershed/Rasters/DEM/{dem_name}hillshade.tif'.format(
    base=sys.argv[1], project_name=project_name, dem_name=dem_file_name_[:-4])
src_ds = gdal.Open(
    '{base}/{project_name}/Watershed/Rasters/DEM/{dem_name}'.format(
        base=sys.argv[1], project_name=project_name, dem_name=dem_file_name_))
ds = gdal.DEMProcessing(hillshade_name, src_ds, 'hillshade', zFactor=30)

# copy shapefile
shapes_dir = '{base}/{project_name}/Watershed/Shapes/'.format(
    base=sys.argv[1], project_name=project_name)
rmtree(shapes_dir, ignore_errors=True)

data_shapes = list_files(
    "{base_dir}/data/shapefiles/".format(base_dir=sys.argv[1]))

# place holding shapefiles
log.info("creating placeholder shapefiles", keep_log)
if not os.path.isdir(shapes_dir):
    os.makedirs(shapes_dir)
with zipfile.ZipFile(
        "{qswatplus_wf_dir}/packages/shapes.dat".format(
示例#23
0
    logger.info('masking variables where ice thickness < 10m')
    nco.ncks(input=exp_file,
             output=exp_nc_wd,
             variable=','.join([x for x in pvars]),
             overwrite=True)
    nco.ncap2(input='-6 -s "{}" {}'.format(ncap2_str, exp_nc_wd),
              output=exp_nc_wd,
              overwrite=True)
    opt = [
        c.Atted(mode="o",
                att_name="_FillValue",
                var_name=myvar,
                value=fill_value) for myvar in ppvars
    ]
    nco.ncatted(input=exp_nc_wd, options=opt)
    for mvar in pvars:
        m_exp_nc_wd = 'NETCDF:{}:{}'.format(exp_nc_wd, mvar)
        m_exp_gtiff_wd = os.path.join(idir, dir_gtiff,
                                      mvar + '_' + exp_basename + '.tif')
        logger.info('Converting variable {} to GTiff and save as {}'.format(
            mvar, m_exp_gtiff_wd))
        gdal.Translate(m_exp_gtiff_wd, m_exp_nc_wd, options=gdal_gtiff_options)
        if mvar == 'usurf':
            m_exp_hs_wd = os.path.join(idir, dir_hs,
                                       mvar + '_' + exp_basename + '_hs.tif')
            logger.info('Generating hillshade {}'.format(m_exp_hs_wd))
            gdal.DEMProcessing(m_exp_hs_wd,
                               m_exp_nc_wd,
                               'hillshade',
                               options=gdal_options)
示例#24
0
 def get_raster_attribute(self, attribute):
     raster = gdal.DEMProcessing('', self.file, attribute, format='MEM')
     raster_values = self.get_values_for_raster(raster)
     del raster
     return raster_values
示例#25
0
    # demSlopeDS = driver.Create(slopeFilename, demSrcDS.RasterXSize, demSrcDS.RasterYSize,
    #                          demSrcDS.RasterCount, gdal.GDT_Byte, options=['COMPRESS=RLE'])
    #
    demResultDS = driver.Create(resultFilename, demSrcDS.RasterXSize, demSrcDS.RasterYSize,
                              demSrcDS.RasterCount, gdal.GDT_Byte, options=['COMPRESS=RLE'])

    GeoTransform = demSrcDS.GetGeoTransform()
    Projection = demSrcDS.GetProjection()

    demResultDS.SetGeoTransform(GeoTransform)
    demResultDS.SetProjection(Projection)
    #
    # demAspectDS.SetGeoTransform(GeoTransform)
    # demAspectDS.SetProjection(Projection)

    demSlopeDS = gdal.DEMProcessing(slopeFilename, srcFilename, "slope", computeEdges=True)
    demAspectDS = gdal.DEMProcessing(aspectFilename, srcFilename, "aspect", computeEdges=True)

    rowNum = demSrcDS.RasterYSize
    columnNum = demSrcDS.RasterXSize

    rowblockNum = int((rowNum - 1) / 1024) + 1
    colblockNum = int((columnNum - 1) / 1024) + 1

    valueRange = (20,8000)
    slopeThreshold = 80

    srcBand = demSrcDS.GetRasterBand(1)
    slopeBand = demSlopeDS.GetRasterBand(1)
    aspectBand = demAspectDS.GetRasterBand(1)
    resultBand = demResultDS.GetRasterBand(1)
示例#26
0
        help="Folder that contains DEM .Tifs or SubFolders containing .Tifs")
    parser.add_argument("outputFolder",
                        type=str,
                        help="Directory Location for Output Files")
    args = parser.parse_args()

    # Collect raster images
    gTiffs = getRasters(args.inputFolder)
    virtualMosaic = gdal.BuildVRT('virtualRaster.vrt', gTiffs)

    # Convert Temporary Mosaic to GeoTiff
    gdal.Translate(os.path.join(args.outputFolder, 'DEM_Mosaic.tif'),
                   virtualMosaic)

    # Process Slope and Aspect
    demSlope = gdal.DEMProcessing(
        os.path.join(args.outputFolder, 'DEM_Slope.tif'), virtualMosaic,
        "slope")
    demAspect = gdal.DEMProcessing(
        os.path.join(args.outputFolder, 'DEM_Aspect.tif'), virtualMosaic,
        "aspect")

    # Convert DEM Mosaic from Meters to Feet
    dem = RasterProcessor(
        gdal.Open(os.path.join(args.outputFolder, 'DEM_Mosaic.tif')))
    demArray = dem.array2raster(footConversion=True)
    dem.raster2array(demArray,
                     os.path.join(args.outputFolder, 'DEM_Mosaic_ft.tif'))

    print('{} Seconds'.format(time.time() - sTime))
示例#27
0
        '/externalCompany/PM2019007_nifs/곰소만_pnu/NDVI/#1_고도_150m'):
    # dem_1970_hs_ds = gdal.DEMProcessing('', ds_list[0], 'hillshade', format='MEM')
    image_start_time = time.time()
    files.sort()
    for file in files:
        start_time = time.time()

        filename = os.path.splitext(file)[0]
        extension = os.path.splitext(file)[1]
        file_path = root + '/' + file

        if extension == '.JPG' or extension == '.jpg':
            print('Read the image - ' + file)
            # https://gis.stackexchange.com/questions/257109/gdaldem-alpha-flag-in-python-bindings-is-missing
            gdal.DEMProcessing(dst_path + file,
                               file_path,
                               "color-relief",
                               colorFilename='NDVI_VGYRM-lut.txt',
                               format='JPEG')
            # colored_vrt = gdal.DEMProcessing('', file_path, "color-relief",
            #                                  colorFilename='NDVI_VGYRM-lut.txt', format='MEM')
            # gdal.Translate(dst_path + file, colored_vrt, format='JPEG')
            print("--- %s seconds ---" % (time.time() - start_time))
        elif extension == ".txt":
            print('Read the image - ' + file)
            shutil.copy(file_path, dst_path + file)
            print("--- %s seconds ---" % (time.time() - start_time))

    print('*** Processing time ***')
    print("--- %s seconds ---" % (time.time() - image_start_time))