Пример #1
0
def calc_topo(dem_path):
    """
    Calculates slope and aspect from given DEM and saves output.
    The function checks to see whether a slope/aspect file has already been created so as to avoid needless processing.
    
    Parameters:
    dem_path (pathlib.PosixPath): The relative or absolute path to an input DEM file.

    Dependencies: 
    richdem module
    GDAL binaries
    pathlib module
    """
    slope_path = Path(
        str(dem_path).replace("dem", "slope"))
    aspect_path = Path(
        str(dem_path).replace("dem", "aspect"))

    if ((not slope_path.is_file()) or 
            (not aspect_path.is_file())):
        
        dem = rd.LoadGDAL(str(dem_path))

    if not slope_path.is_file():
        slope = rd.TerrainAttribute(
            dem, attrib='slope_riserun')
        rd.SaveGDAL(str(slope_path), slope)
    
    if not aspect_path.is_file():
        aspect = rd.TerrainAttribute(dem, attrib='aspect')
        rd.SaveGDAL(str(aspect_path), aspect)
Пример #2
0
def create_slope_aspect(input_file):
    arr = rd.LoadGDAL(input_file,
                      no_data=-9999)  #rd.rdarray(input_file,no_data=-9999)
    aspect = rd.TerrainAttribute(arr, attrib='aspect')
    slope = rd.TerrainAttribute(arr, attrib='slope_radians')
    aspect_output = input_file[:-4] + '_aspect.tif'
    slope_output = input_file[:-4] + '_slope.tif'
    rd.SaveGDAL(aspect_output, aspect)
    rd.SaveGDAL(slope_output, slope)
    return aspect_output, slope_output
Пример #3
0
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())
Пример #4
0
def TerrainAttribute():
  parser = argparse.ArgumentParser(formatter_class=RawTextHelpFormatter, description="""RichDEM Terrain Attribute

A variety of methods are available.

Parameters:
dem      -- An elevation model
attrib   -- Terrain attribute to calculate. (See below.)
zscale   -- How much to scale the z-axis by prior to calculation

Method:
slope_riserun
slope_percentage
slope_degrees
slope_radians
aspect
curvature
planform_curvature
profile_curvature
""")
  parser.add_argument('dem',              type=str,                help='Elevation model')
  parser.add_argument('outname',          type=str,                help='Name of output file')
  parser.add_argument('-a', '--attrib',   type=str, required=True, help='Terrain attribute to calculate')
  parser.add_argument('-z', '--zscale',   type=float, default=1.0, help='Scale elevations by this factor prior to calculation')
  parser.add_argument('-v', '--version',  action='version', version=rd._RichDEMVersion())
  args = parser.parse_args()

  dem = rd.LoadGDAL(args.dem)
  rd._AddAnalysis(dem, ' '.join(sys.argv))
  tattrib = rd.TerrainAttribute(dem, attrib=args.attrib, zscale=args.zscale)
  rd.SaveGDAL(args.outname, tattrib)
Пример #5
0
    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
Пример #6
0
def mosaic_slope(url, Projection, Geotransform, Height, Width, Extent,
                 Resolution):

    tile_id = url.split('/')[7]
    name = f'{tile_id}.tif'
    urllib.request.urlretrieve(
        url, '/Users/jackson/Desktop/Execute/' + tile_id + '.tif')

    # We must reproject our DEM tile
    gdal.Warp(name, name, dstSRS=Projection)

    # Now we run the the BTH over our DEM tile
    Tile_GeoTiff = gdal.Open(name, 0)
    T = Tile_GeoTiff.GetGeoTransform()
    P = Tile_GeoTiff.GetProjection()
    Tile_Array = rd.LoadGDAL(name)

    Slopes = rd.TerrainAttribute(Tile_Array, attrib='slope_riserun')

    filename = name
    driver = gdal.GetDriverByName('GTiff')
    dataset = driver.Create(filename, Slopes.shape[1], Slopes.shape[0], 1,
                            gdal.GDT_Float32)
    dataset.GetRasterBand(1).WriteArray(Slopes)
    dataset.SetGeoTransform(T)
    dataset.SetProjection(P)
    dataset.FlushCache()
    dataset = None

    # Preparing to add our new tile to the mosaic
    src_files_to_mosaic = []
    src = rasterio.open('Topo_Slope.tif')
    src_files_to_mosaic.append(src)
    src = rasterio.open(name)
    src_files_to_mosaic.append(src)

    os.remove('/Users/jackson/Desktop/Execute/' + tile_id + '.tif')

    # Adding to Mosaic
    mosaic, out = merge(src_files_to_mosaic,
                        method='first',
                        bounds=Extent,
                        res=Resolution,
                        nodata=-9999)
    mosaic = np.reshape(mosaic, (Height, Width))

    # Using GDAL to write the output raster
    filename = 'Topo_Slope.tif'  # Tiff holding Black Top Hat
    driver = gdal.GetDriverByName('GTiff')  # Driver for writing geo-tiffs
    dataset = driver.Create(filename, Width, Height, 1,
                            gdal.GDT_Float32)  # Creating our tiff file
    dataset.GetRasterBand(1).WriteArray(mosaic)  # Writing values to our tiff
    dataset.SetGeoTransform(Geotransform)  # Setting geo-transform
    dataset.SetProjection(Projection)  # Setting the projection
    dataset.FlushCache()  # Saving tiff to disk
    dataset = None
Пример #7
0
def curvature(input_file):
    """Create a curvature layer (combine profile and planform curvature) using richdem."""
    arr = rd.LoadGDAL(input_file)  #rd.rdarray(input_file,no_data=-9999)
    curvature = rd.TerrainAttribute(arr, attrib='curvature')
    output_file = input_file[:-9] + '_curvature_temp.tif'
    head, tail = os.path.split(output_file)
    rd.SaveGDAL(output_file, curvature)
    createMetadata(
        sys.argv,
        head + '/')  #just remove the filename so you are left with the path
Пример #8
0
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
Пример #9
0
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')
def pendiente(srcFolder="./", dstFolder="./"):
    for archivo in os.listdir(srcFolder):
        if archivo.endswith(".tif"):
            if srcFolder.endswith("/"):
                ruta = srcFolder + archivo
            else:
                ruta = srcFolder + "/" + archivo
            dem = richdem.LoadGDAL(ruta)
            slope = richdem.TerrainAttribute(dem, attrib='slope_radians')
            archivo = "pendiente_" + archivo
            if not os.path.exists(dstFolder):
                os.mkdir(dstFolder)
            if srcFolder.endswith("/"):
                dstRuta = dstFolder + archivo
            else:
                dstRuta = dstFolder + "/" + archivo
            richdem.SaveGDAL(dstRuta, slope)
Пример #11
0
    def _richdem2numpy(rda, attribute):
        '''
        Parameters
        ==========
        in_array : richdem array

        attribute : str
            One of 'slope
        '''
        outfile = TemporaryFile()
        np.save(outfile, rd.TerrainAttribute(rda, attrib=attribute))
        _ = outfile.seek(0)
        out_array = np.load(outfile)

        if attribute == 'aspect':
            out_array[out_array > 180] = out_array[out_array > 180] - 360
            return out_array
        else:
            return out_array
def add_slope_aspect_curvature(df, file, indexes):
    for attr in ['slope_percentage', 'aspect', 'profile_curvature']:
        table = None
        try:
            table = rd.TerrainAttribute(rd.LoadGDAL(file, no_data=-9999),
                                        attrib=attr)
            rd.SaveGDAL("./temp.tif", table)
            table = None
            table = gr.from_file("./temp.tif")
            for index in indexes:
                try:
                    row = df.loc[index]
                    val = table.map_pixel(row['lon'], row['lat'])
                    df.loc[index, attr] = float(val)
                except:
                    df.loc[index, attr] = np.nan
            os.remove("./temp.tif")
        except:
            for index in indexes:
                df.loc[index, attr] = np.nan
    return df
def runoff():
    #    return 788
    dem_path = os.path.join(os.getcwd(), 'Konambe_dem_clipped.tif')
    village_dem = rd.LoadGDAL(dem_path, no_data=-9999)
    rd.FillDepressions(village_dem, epsilon=False, in_place=False)
    arr = rd.TerrainAttribute(village_dem,
                              attrib='slope_percentage',
                              zscale=1 / 111120)
    np.save('out.npy', arr)
    demnp = np.load('out.npy')
    dem = copy.copy(arr)
    dem[np.where((arr > 0) & (arr < 5))] = 1
    dem[np.where((arr >= 5) & (arr < 20))] = 2
    dem[np.where((arr >= 20))] = 3

    c1 = np.count_nonzero(dem == 1)
    c2 = np.count_nonzero(dem == 2)
    c3 = np.count_nonzero(dem == 3)

    area_m2_1 = c1 * 900
    area_m2_2 = c2 * 900
    area_m2_3 = c3 * 900
    area_ha1 = area_m2_1 * 0.0001
    area_ha2 = area_m2_2 * 0.0001
    area_ha3 = area_m2_3 * 0.0001
    #print('area',area_ha1+area_ha2)
    worthy_area = area_ha1 + area_ha2
    #coeff for rainfall 775mm
    runoff1 = area_ha1 * 1.0791
    runoff2 = area_ha2 * 1.6186
    runoff3 = area_ha3 * 2.1583
    #coeff for rainfall 725mm
    #runoff1=area_ha1*1.0791
    #runoff2=area_ha2*1.3878
    #runoff3=area_ha3*1.8496
    tot_runoff = runoff1 + runoff2 + runoff3
    return tot_runoff, worthy_area


#r=runoff()
Пример #14
0
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())
Пример #15
0
    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)
#Load the data and convert it to an array for RichDEM
fname = 'L:/feather_river_mixing_paper/dem/pomd_out.tif'
# Loading the raster with rasterio

raster = rd.LoadGDAL(fname)

#rasterfig = rd.rdShow(raster, ignore_colours=[0], axes=False, cmap='gist_earth', figsize=(8,5.5))

raster_filled = rd.FillDepressions(raster, epsilon=True, in_place=False)

#rasterfig_filled = rd.rdShow(raster_filled, ignore_colours=[0], axes=False, cmap='gist_earth', vmin=rasterfig['vmin'], vmax=rasterfig['vmax'], figsize=(8,5.5))

accum_d8 = rd.FlowAccumulation(raster_filled, method='D8')
#d8_fig = rd.rdShow(accum_d8, figsize=(8,5.5), axes=False, cmap='jet')

slope = rd.TerrainAttribute(raster_filled, attrib='slope_riserun')
#rd.rdShow(slope, axes=False, cmap='jet', figsize=(8,5.5))

profile_curvature = rd.TerrainAttribute(raster_filled, attrib='curvature')
#rd.rdShow(profile_curvature, axes=False, cmap='jet', figsize=(8,5.5))

print(raster)
temp = np.flip(raster, 0)
raster = temp
print(raster)
y = np.size(raster, 1)
x = np.size(raster, 0)
print(np.shape(raster))
#Creates a flow direction raster
gradient = np.empty((8, x - 2, y - 2), dtype=np.float)
print(np.shape(gradient))
### If you do not want to aggregate DEM, comment out the following to two lines
if aggregate:
    os.system('gdalwarp -tr ' + aggregate_degree + ' ' + aggregate_degree +
              ' -r average ' + dem_path_tif + ' ' + dem_path_tif_temp2)
    dem_path_tif = dem_path_tif_temp2

### convert DEM from tif to NetCDF
os.system('gdal_translate -of NETCDF ' + dem_path_tif + ' ' + dem_path)

### calculate slope as NetCDF from DEM
os.system('gdaldem slope -of NETCDF ' + dem_path + ' ' + slope_path +
          ' -s 111120')

### calculate aspect from DEM
aspect = np.flipud(
    rd.TerrainAttribute(rd.LoadGDAL(dem_path_tif, no_data=np.nan),
                        attrib='aspect'))

### calculate mask as NetCDF with DEM and shapefile
os.system(
    'gdalwarp -of NETCDF  --config GDALWARP_IGNORE_BAD_CUTLINE YES -cutline ' +
    shape_path + ' ' + dem_path_tif + ' ' + mask_path)

### open intermediate netcdf files
dem = xr.open_dataset(dem_path)
mask = xr.open_dataset(mask_path)
slope = xr.open_dataset(slope_path)

### set NaNs in mask to -9999 and elevation within the shape to 1
mask = mask.Band1.values
mask[np.isnan(mask)] = -9999
mask[mask > 0] = 1
 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')
Пример #19
0
#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)
#x, y = φdtm.shape[0], φdtm.shape[1]
Пример #20
0
from osgeo import gdal

filepath = r"c:\Users\Darshan\Desktop\nagpur\output.tif"

# Open the file:
raster = gdal.Open(filepath)

# In[97]:

raster = os.path.join(os.getcwd(), filepath)
nagpur_dem = rd.LoadGDAL(raster)

# In[98]:

slope = rd.TerrainAttribute(nagpur_dem, attrib='slope_riserun')
rd.rdShow(slope, axes=False, cmap='magma', figsize=(8, 5.5))
plt.show()

# In[161]:

slope

# In[130]:

slope.shape

# In[163]:

driver = "GTiff"
Пример #21
0
# First, specify outputfile path
out_tif = "data/ASTERGDEM3/Processed/Elevation_5min.tif"
with rasterio.open(out_tif, "w", **out_meta) as dest:
    dest.write(elevation_low)

# =============================
# 3. Calculate slope and aspect

# 3.1. 30`` resolution

# Read elevarion with rd.LoadGDAL
elevation_raw = rd.LoadGDAL("data/ASTERGDEM3/Processed/Elevation_30s.tif",
                            no_data=-9999)

# Calculate slope
slope = rd.TerrainAttribute(elevation_raw, attrib='slope_riserun')
rd.rdShow(slope, axes=False, cmap='magma', figsize=(8, 5.5))

# Export as tif
rd.SaveGDAL("data/ASTERGDEM3/Processed/Slope_30s.tif", slope)

# Calculate aspect
aspect = rd.TerrainAttribute(elevation_raw, attrib='aspect')
rd.rdShow(aspect, axes=False, cmap='jet', figsize=(8, 5.5))

# Export as tif
rd.SaveGDAL("data/ASTERGDEM3/Processed/Aspect_30s.tif", aspect)

# 3.2. 2.5` resolution

# Read elevarion with rd.LoadGDAL
Пример #22
0
    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'])
Пример #23
0
dem = xr.open_dataset(dem_path)
dem_tif = rd.LoadGDAL(dem_path_tif)
dem_tif2 = rasterio.open(dem_path_tif)

with fiona.open(shape_path, "r") as shapefile:
    features = [feature["geometry"] for feature in shapefile]

with rasterio.open(dem_path_tif) as src:
    out_image, out_transform = rasterio.mask.mask(src,
                                                  features,
                                                  crop=False,
                                                  nodata=-9999)
    out_meta = src.meta.copy()

slope = xr.DataArray(rd.TerrainAttribute(dem_tif, attrib='slope_degrees'))
#rd.rdShow(slope, axes=False, cmap='magma', figsize=(8, 5.5))
#plt.show()

aspect = xr.DataArray(rd.TerrainAttribute(dem_tif, attrib='aspect'))
#rd.rdShow(aspect, axes=False, cmap='jet', figsize=(8, 5.5))
#plt.show()

mask = out_image[0, :, :]
mask[mask > 0] = 1

ds = xr.Dataset()

ds.coords['longitude'] = dem.lon.values
ds.longitude.attrs['standard_name'] = 'longitude'
ds.longitude.attrs['long_name'] = 'longitude'
Пример #24
0
import richdem as rd
import numpy as np
import matplotlib.pyplot as plt

dem = rd.LoadGDAL("/media/jayanthmouli/54EC-046F/elevation/the_better_one.tif",
                  no_data=0)
slope = rd.TerrainAttribute(dem, attrib='slope_riserun')
# rd.rdShow(slope, axes=False, cmap='magma', figsize=(8, 5.5))
# plt.show()

aspect = rd.TerrainAttribute(dem, attrib='aspect')
rd.rdShow(aspect, axes=False, cmap='jet', figsize=(8, 5.5))
plt.show()
print aspect.shape
Пример #25
0
cnt = 0
for index, row in table_y.iterrows():
    try:
        val = table.map_pixel(row['LONWGS84'], row['LATWGS84'])
        table_y.loc[index, 'ELEVATION'] = float(val)
        cnt += 1
    except:
        table_y.loc[index, 'ELEVATION'] = 0
print("Added elevation information to " + repr(cnt) + " instances out of " +
      repr(len(table_y)) + " data instances...")

print("Computing information on terrain slope...")
for attr in ['slope_percentage', 'aspect', 'profile_curvature']:
    table_y[attr.upper()] = 0.0
    table = None
    table = rd.TerrainAttribute(
        rd.LoadGDAL("./globcover/digital-elevation-model.tif"), attrib=attr)
    rd.SaveGDAL("./slope.tif", table)
    table = None
    NDV, xsize, ysize, GeoT, Projection, DataType = gr.get_geo_info(
        "./slope.tif")
    table = gr.from_file("./slope.tif")
    cnt = 0
    for index, row in table_y.iterrows():
        try:
            val = table.map_pixel(row['LONWGS84'], row['LATWGS84'])
            table_y.loc[index, attr.upper()] = float(val)
            cnt += 1
        except:
            table_y.loc[index, attr.upper()] = 0.0
    os.remove("./slope.tif")
    print("Added " + attr + " information to " + repr(cnt) +
Пример #26
0
def engineTopo(in_path='None',out_path='None',shpfilepath='None',drm_filepath='None',\
               products=[],bandStacks=[],is_topocorrection=False,SunElevation=30,\
               SunAzimuth=180,fileext="tif"):
    time_start = time.time()

    #some sample parameters for the topocorrection
    #is_topocorrection=True; #topocorrection flag
    #SunElevation=28.41189977  #31.23944509
    #SunAzimuth=163.93705102      #163.133415

    #Sun Elevation L1	 31.73425917
    #Sun Azimuth L1	162.99110733
    """
    #for 105-029
    #SunElevation=31.23944509 
    #SunAzimuth=163.133415      
    
    """
    if (drm_filepath == 'None'):
        is_topocorrection = False

    SolarZenith = 90 - SunElevation
    """
    You may exclude files from processing by renaming like ".tiff"
    """

    #files for processing, input and output directory
    #pathrowfolder="104_029"
    #datefolder="2015_11_05"
    #imgfilepath=os.path.join("..","Landsat8",pathrowfolder,datefolder);
    #shpfilepath=os.path.join("..","shp",pathrowfolder+".shp");
    #shpfilepath=os.path.join("..","shp","AOI_tmp"+".shp");

    #fileext="tif"; #extention for files
    #outdir=os.path.join("..","Landsat8_Processed",pathrowfolder,datefolder);
    dir_cropped = "cropped_bands_topo"  #dir for AOI cropped
    dir_crop_path = os.path.join(out_path, dir_cropped)
    dir_products = "products_topo"
    dir_products_path = os.path.join(out_path, dir_products)
    band_number_inname = '_b%N%.'  #%N% - for band number e.g. LC81050292016143LGN00_B6.TIF NOT A CASE SENSITIVE
    band_number_inname = band_number_inname.lower()
    excl_pfix = '_b8'
    #endfile postfix to exclude from processing

    #drm for topocorrection
    #drm_name="mosaic_dem_south_kuril_utm.tif";
    #drm_folder=os.path.join("..","SRTM","files_for_mosaic");
    #drm_filepath=os.path.join(drm_folder,drm_name);

    #nodata srtm -32768

    #check is file/folder exists
    #print(os.path.isdir("/home/el"))
    #print(os.path.exists("/home/el/myfile.txt"))
    #

    file_for_crop = []

    try:
        for file in os.listdir(in_path):
            #file=file.lower();
            if file.lower().endswith("." + fileext.lower()) and (
                    file.lower().endswith(excl_pfix + '.' +
                                          fileext.lower())) == False:
                file_for_crop.append(file)
                print(file + " was added to crop queue.")
    except (FileNotFoundError):
        print("Input image folder doesn\'t exist...")
    """
    ДОПОЛНЕНИЯ в GUI сделать генерацию AOI shp выделением на изображении, если пользователь не генерирует 
    AOI, то AOI задать по размеру изображения
    """
    #STEP 0. Prepare for the topocorrection

    try:
        shp_extent = get_shp_extent(shpfilepath)
    except:
        print("Can not read shp AOI file. Applying extent from geotiff")
        gdal_object_tmp = gdal.Open(os.path.join(in_path, file_for_crop[0]))
        tmp_xsize = gdal_object_tmp.RasterXSize
        tmp_ysize = gdal_object_tmp.RasterYSize  #x and y raster size in pixels
        tmp_gt = gdal_object_tmp.GetGeoTransform()
        tmp_ext = GetExtent(tmp_gt, tmp_ysize, tmp_xsize)
        shp_extent = [
            tmp_ext[0][0], tmp_ext[2][0], tmp_ext[1][1], tmp_ext[3][1]
        ]

    #crop dem file
    if is_topocorrection == True:
        print("Perform cropping of srtm")

        #read DEM geotiff
        srtm_gdal_object = gdal.Open(drm_filepath)
        srtm_band = srtm_gdal_object.GetRasterBand(1)
        srtm_band_array = srtm_band.ReadAsArray()

        #get spatial resolution
        srtm_gt = srtm_gdal_object.GetGeoTransform()
        srtm_xsize = srtm_gdal_object.RasterXSize
        srtm_ysize = srtm_gdal_object.RasterYSize  #x and y raster size in pixels
        srtm_ext = GetExtent(
            srtm_gt, srtm_ysize,
            srtm_xsize)  #[[влx,влy],[нлx,нлy],[нпy, нпy],[впx, впy]]
        #resolution in meters
        srtm_dpx = (srtm_ext[3][0] - srtm_ext[0][0]) / srtm_xsize
        srtm_dpy = (srtm_ext[0][1] - srtm_ext[2][1]) / srtm_ysize
        """
        print("srtm_ext={}".format(srtm_ext))
        print("shp_extent={}".format(shp_extent))
        """
        if check_shp_inside_raster(srtm_ext, shp_extent):
            #        sampleSrtmImage,ColMinIndSRTM,RowMinIndSRTM =crop_by_shp(shp_extent,srtm_ext,\
            #                                                    srtm_dpx,srtm_dpy,srtm_band_array);
            srtm_band = rd.LoadGDAL(drm_filepath)

            slope = rd.TerrainAttribute(srtm_band, attrib='slope_degrees')
            aspect = rd.TerrainAttribute(srtm_band, attrib='aspect')

            rd.SaveGDAL(
                os.path.join(os.path.dirname(drm_filepath),
                             "aspectInitialRes.tif"), aspect)
            rd.SaveGDAL(
                os.path.join(os.path.dirname(drm_filepath),
                             "SlopeInitialRes.tif"), slope)
        else:
            print("AOI shp file" + shpfilepath + "is not inside of DEM" +
                  drm_filepath + ". Stopping.")
            return -1
            #input('Press Enter for exit...')
            #exit;

        #reopening SRTM products
        #read srtm products
        aspect_gdal_object = gdal.Open(
            os.path.join(os.path.dirname(drm_filepath),
                         "aspectInitialRes.tif"))  #aspect
        aspect_band = aspect_gdal_object.GetRasterBand(1)
        aspect_band_array = aspect_band.ReadAsArray()

        slope_gdal_object = gdal.Open(
            os.path.join(os.path.dirname(drm_filepath),
                         "SlopeInitialRes.tif"))  #slope
        slope_band = slope_gdal_object.GetRasterBand(1)
        slope_band_array = slope_band.ReadAsArray()

        #get PRODUCTS spatial resolution
        srtm_gt, srtm_xsize, srtm_ysize, srtm_ext, srtm_dpx, srtm_dpy = getGeotiffParams(
            aspect_gdal_object)

        #check if SRTM products inside of SHP AOI ad crop it
        if check_shp_inside_raster(srtm_ext, shp_extent):
            #do image crop
            aspect_cropped, ColMinInd, RowMinInd = crop_by_shp(
                shp_extent, srtm_ext, srtm_dpx, srtm_dpy, aspect_band_array)
            slope_cropped, ColMinInd, RowMinInd = crop_by_shp(
                shp_extent, srtm_ext, srtm_dpx, srtm_dpy, slope_band_array)

            #for testing purporses
            saveGeoTiff(slope_cropped, 'test_crop_slope.tif',
                        slope_gdal_object, ColMinInd,
                        RowMinInd)  #tryna save cropped geotiff

        else:
            print("SRTM is outside of the AOI, exiting...")
            return -1
            #exit();

    was_corrected = False
    #flag to check if resolution and scale were corrected to landsat8
    #STEP 1. CROP geotiffs one by one with AOI shape file
    print("Step. 1. Starting geotiff crop operation...")
    for myfile in file_for_crop:
        #read geotiff
        gdal_object = gdal.Open(os.path.join(in_path, myfile))
        band = gdal_object.GetRasterBand(1)
        band_array = band.ReadAsArray()

        #get spatial resolution
        #do image crop
        gt, xsize, ysize, ext, dpx, dpy = getGeotiffParams(gdal_object)
        """
        gt=gdal_object.GetGeoTransform()
        xsize = gdal_object.RasterXSize
        ysize = gdal_object.RasterYSize #x and y raster size in pixels
        ext=GetExtent(gt,ysize,xsize) #[[влx,влy],[нлx,нлy],[нпy, нпy],[впx, впy]]
        #resolution in meters
        dpx=(ext[3][0]-ext[0][0])/xsize
        dpy=(ext[0][1]-ext[2][1])/ysize
        print(ext)
        """
        #apply shp file
        #try:
        #    shp_extent=get_shp_extent(shpfilepath);
        #except:
        #    print("Can not read shp AOI file.")

        #check shp posiiton inside of tiff
        if check_shp_inside_raster(ext, shp_extent):
            #do image crop
            sampleImage, ColMinInd, RowMinInd = crop_by_shp(
                shp_extent, ext, dpx, dpy, band_array)

        else:
            print("AOI shp file" + shpfilepath + "is not inside of tiff" +
                  myfile + ". Stopping.")
            #input('Press Enter for exit...')
            return -1
            #exit;

        #topocorrection
        if is_topocorrection == True:  #topocorrection flag
            if was_corrected == False:
                print('compute slope and aspect cropped')
                #коррекция aspect по Landsat8
                #adjust srtm resolution to landsat8
                [hlc, wlc] = np.shape(sampleImage)
                aspect_band_cropped = resize(
                    aspect_cropped, (hlc, wlc),
                    preserve_range=True,
                    mode="wrap")  #it works with scikit-image resize

                #коррекция slope по Landsat8
                slope_band_cropped = resize(
                    slope_cropped, (hlc, wlc),
                    preserve_range=True,
                    mode="wrap")  #it works with scikit-image resize


                Cos_i=np.cos(np.deg2rad(slope_band_cropped))*np.cos(np.deg2rad(SolarZenith))+\
                np.sin(np.deg2rad(slope_band_cropped))*np.sin(np.deg2rad(SolarZenith))*\
                np.cos(np.deg2rad(SunAzimuth-aspect_band_cropped))

                #ЭТОТ РАСЧЕТ КОС I РАССМАТРИВАЕТ ВСЕ СКЛОНЫ КАК ОСВЕЩЕННЫЕ ПОД ПРЯМЫМ УГЛОМ!
                #Cos_i=np.cos(np.deg2rad(SolarZenith-slope_band_cropped));

                #Do SCS+C correction anyway
                """
                print("Check correlation between Cos(i) and Luminocity")
                R_mat=np.corrcoef(Cos_i.ravel(),sampleImage.ravel()) 
                print("R="+str(R_mat[0,1]));            
                if( R_mat[0,1]<0.5):
                    print("No or weak correlation, use SCS algoritm...");
                    C=0;
                else:
                    print("Not a weak correlation, use SCS+C algoritm...");
                    (b,a)=np.polyfit(Cos_i.ravel(),sampleImage.ravel(),1);
                    C=a/b;
                 """
                (b, a) = np.polyfit(Cos_i.ravel(), sampleImage.ravel(), 1)
                C = a / b
                was_corrected = True
                #switch the flag to true

            print("Performing topographic correction.. Please, WAIT..")
            #Sun-Canopy-Sensor Correction (SCS)+C
            band_array=np.uint16(sampleImage*\
                    ((np.cos(np.deg2rad(SolarZenith))*np.cos(np.deg2rad(slope_band_cropped))+C)\
                     /(C+Cos_i)))
            pic_show(sampleImage, "landsat initial")
            hist_show(sampleImage)
            pic_show(band_array, "landsat SCS corrected")
            hist_show(band_array)
        else:  #no topocorrection
            print("No topocorrection was selected..")
            band_array = copy.copy(sampleImage)
            #no operation

        #check shp posiiton inside of tiff
        #if check_shp_inside_raster(ext,shp_extent):
        #    #do image crop
        #sampleImage,ColMinInd,RowMinInd =crop_by_shp(shp_extent,ext,dpx,dpy,band_array)

        #if is_topocorrection==True: #topocorrection flag
        #    #do topocorrection with SCS Algorythm

        #drop image to the disk
        print("drop image to the disk")
        outfilename = os.path.join(dir_crop_path, "crop_" + myfile.lower())
        if not os.path.isdir(dir_crop_path):
            os.makedirs(dir_crop_path)  #create output directory if none
        try:
            saveGeoTiff(band_array, outfilename, gdal_object, ColMinInd,
                        RowMinInd)  #save topocorrected Landsat crop
        except:
            print(
                "Can not write on a disk... and/or error(s) in saveGeoTiff function"
            )

    #STEP 2. COMPUTE pseudocolor RGB stacks and satellite indexes
    """
    автоопределение BANDs для дефолтных имен, если пользователь не задал имена (пока что имена по умолчанию), 
    пропускаем индекс или RGB стек, если не находим BAND NUMBER
    """
    print("Step. 2. Getting names of the cropped files...")
    #getting names of the cropped files, aquire band names
    file_for_processing = []
    try:
        for file in os.listdir(
                dir_crop_path
        ):  #набираем файлы из папки с кадрированными изображениями
            file = file.lower()
            if file.endswith("." + fileext.lower()):
                file_for_processing.append(file)
                print(file + " was added to the processing queue.")
    except (FileNotFoundError):
        print("Input image folder doesn\'t exist...")

    bands = {}
    #dictionary storing band names
    for myfile in file_for_processing:
        for N in range(1, 9):
            #populating bands dictionary
            if band_number_inname.replace('%n%', str(N), 1) in myfile:
                try:
                    gdal_object = gdal.Open(
                        os.path.join(dir_crop_path, myfile)
                    )  #as new gdal_object was created, no more ColMinInd,RowMinInd
                    bands['band' +
                          str(N)] = gdal_object.GetRasterBand(1).ReadAsArray()
                except:
                    print("Error! Can not read cropped bands!")
    #print("Bands dictionary output:")
    #print(bands)

    try:
        #create RGB stacks:
        #truecolor
        if ('rgb' in bandStacks):
            truecolorRGB = image_stack(bands['band4'],
                                       bands['band3'],
                                       bands['band2'],
                                       do_norm8=0,
                                       do_show=0)

        #Комбинация 7-4-2. Изображение близкое к естественным цветам, позволяет анализировать состояние атмосферы и дым. Здоровая растительность выглядит ярко зеленой, ярко розовые участки детектируют открытую почву, коричневые и оранжевые тона характерны для разреженной растительности.
        if ('742' in bandStacks):
            b742RGB = image_stack(bands['band7'],
                                  bands['band4'],
                                  bands['band2'],
                                  do_norm8=0,
                                  do_show=0)
        #Комбинация 5-4-1. Изображение близкое к предыдущему, позволяет анализировать сельскохозяйственные культуры
        if ('652' in bandStacks):
            b652RGB = image_stack(bands['band6'],
                                  bands['band5'],
                                  bands['band2'],
                                  do_norm8=0,
                                  do_show=0)
        #Комбинация 4-5-3. Изображение позволяет четко различить границу между водой и сушей, с большой точностью будут детектироваться водные объекты внутри суши. Эта комбинация отображает растительность в различных оттенках и тонах коричневого, зеленого и оранжевого, дает возможность анализа влажности и полезны при изучении почв и растительного покрова.
        if ('453' in bandStacks):
            b453RGB = image_stack(bands['band4'],
                                  bands['band5'],
                                  bands['band3'],
                                  do_norm8=0,
                                  do_show=0)

        #after Aydal, 2007
        if ('642' in bandStacks):
            b642RGB = image_stack(bands['band6'],
                                  bands['band4'],
                                  bands['band2'],
                                  do_norm8=0,
                                  do_show=0)
        if ('765' in bandStacks):
            b765RGB = image_stack(bands['band7'],
                                  bands['band6'],
                                  bands['band5'],
                                  do_norm8=0,
                                  do_show=0)
        if ('764' in bandStacks):
            b764RGB = image_stack(bands['band7'],
                                  bands['band6'],
                                  bands['band4'],
                                  do_norm8=0,
                                  do_show=0)

        #create indexes
        if ('NDVI' in products):
            NDVI = (bands['band5'] - bands['band4']) / (
                bands['band5'] + bands['band4'])  #NDVI
        if ('IOA' in products) or ('CA' in products):
            IOA = (bands['band4'] / bands['band2']
                   )  #Iron oxide alteration [Doğan Aydal, 2007]
        if ('HA' in products) or ('CA' in products):
            HA = (bands['band7'] / bands['band2']
                  )  #Hydroxyl alteration [Doğan Aydal, 2007]
        if ('CM' in products):
            CM = (bands['band7'] / bands['band6']
                  )  #Clay minerals [Doğan Aydal, 2007]

        #compute PCA
        if ('PC' in products):
            print("Started to compute PCA...")
            print("Flatten image matrix...")
            flattened_img_matrix=mat4pca((bands['band1'],bands['band2'],bands['band3'],\
                                          bands['band4'],bands['band5'],bands['band6'],bands['band7']))
            #mybands=[bands['band1'],bands['band2'],bands['band3'],\
            #                              bands['band4'],bands['band5'],bands['band6'],bands['band7']]
            #tmp_matrix=[mynormalize16to8(tmpband) for tmpband in mybands]
            #flattened_img_matrix=mat4pca(tmp_matrix) #same but images are normalized to uint8
            print("Compute PCA matrix, the variance and the mean...")

            m, n = np.shape(bands['band3'])  #temporary height and width
            (pca, eigenvalues, var_X,
             mean_X) = pca_make(flattened_img_matrix, 7, m, n)

        #create cumulative image composite image of the hydroxyl image(red band), the iron oxide image
        #(green band) and the average of these two images (blue band).
        if ('CA' in products):
            index_composite = image_stack(HA, IOA, (HA + IOA) / 2, 1, 0)

    except:
        print('No bands or bands error!')
        return -1
    #GENERAL OUTPUT
    if ('PC' in products):
        print("Prepare to show PCA images")

        #later incorporate path into functions
        if not os.path.isdir(dir_products_path):
            os.makedirs(
                dir_products_path)  #create output products directory if none

        fig_save_cumsum_path = os.path.join(dir_products_path,
                                            "variance_cumsum.svg")
        fig_save_pca_path = os.path.join(dir_products_path, "pca_comp.png")

        #num_comp=show_pca_cumsum(pca,fig_save_cumsum_path); #pca variance cumsum to determine right number of components
        #show_pca_images(eigenvalues,mean_X,m,n,fig_save_pca_path) #show pca component images

    #COMPUTE Landsat and PCA stat for the CROSTA METHOD
    try:
        stat_bands_save = os.path.join(dir_products_path, "bands_stat.xls")
        cor_bands_save = os.path.join(dir_products_path, "bands_cor_stat.xls")
        cov_bands_pca_save = os.path.join(dir_products_path,
                                          "bands_pca_cov_stat.xls")

        print("Saving band stat to {}".format(stat_bands_save))
        save_landsat_bands_stat(bands, stat_bands_save)

        print("Saving bands mutual correlation to {}".format(cor_bands_save))
        save_landsat_mutual_cor(bands, cor_bands_save)
    except:
        print('can not save band stats')
    try:  #correlation of bands and PCA comp may be potentially errorneous, dep on PCA number
        print(
            "Saving covariance between bands and PCA components to {}".format(
                cov_bands_pca_save))
        save_landsat_pca_cov(bands, eigenvalues, cov_bands_pca_save)
    except:
        print('Can not compute/save pca/bands covariance...')

    #save RGB's and index to the disk
    print("Saving products on a disk")
    if not os.path.isdir(dir_products_path):
        os.makedirs(dir_products_path)  #create output directory if none
    try:
        print("Saving RGBs...")
        ColMinInd = 0
        RowMinInd = 0
        #because we work on already cropped pictures
        if ('rgb' in bandStacks):
            saveGeoTiff(
                truecolorRGB,
                os.path.join(dir_products_path, "truecolorRGB" + ".tif"),
                gdal_object, ColMinInd, RowMinInd)
        if ('742' in bandStacks):
            saveGeoTiff(b742RGB,
                        os.path.join(dir_products_path, "b742RGB" + ".tif"),
                        gdal_object, ColMinInd, RowMinInd)
        if ('652' in bandStacks):
            saveGeoTiff(b652RGB,
                        os.path.join(dir_products_path, "b652RGB" + ".tif"),
                        gdal_object, ColMinInd, RowMinInd)
        if ('453' in bandStacks):
            saveGeoTiff(b453RGB,
                        os.path.join(dir_products_path, "b453RGB" + ".tif"),
                        gdal_object, ColMinInd, RowMinInd)
        #Aydal pseudocolor:
        if ('642' in bandStacks):
            saveGeoTiff(b642RGB,
                        os.path.join(dir_products_path, "b642RGB" + ".tif"),
                        gdal_object, ColMinInd, RowMinInd)
        if ('765' in bandStacks):
            saveGeoTiff(b765RGB,
                        os.path.join(dir_products_path, "b765RGB" + ".tif"),
                        gdal_object, ColMinInd, RowMinInd)
        if ('764' in bandStacks):
            saveGeoTiff(b764RGB,
                        os.path.join(dir_products_path, "b764RGB" + ".tif"),
                        gdal_object, ColMinInd, RowMinInd)

        print("Saving Indexes...")
        if ('NDVI' in products):
            saveGeoTiff(NDVI, os.path.join(dir_products_path, "NDVI" + ".tif"),
                        gdal_object, ColMinInd, RowMinInd)
        if ('IOA' in products):
            saveGeoTiff(IOA, os.path.join(dir_products_path, "IOA" + ".tif"),
                        gdal_object, ColMinInd, RowMinInd)
        if ('HA' in products):
            saveGeoTiff(HA, os.path.join(dir_products_path, "HA" + ".tif"),
                        gdal_object, ColMinInd, RowMinInd)
        if ('CM' in products):
            saveGeoTiff(CM, os.path.join(dir_products_path, "CM" + ".tif"),
                        gdal_object, ColMinInd, RowMinInd)
        if ('CA' in products):
            saveGeoTiff(
                index_composite,
                os.path.join(dir_products_path,
                             "CumulativeAlteration" + ".tif"), gdal_object,
                ColMinInd, RowMinInd)

        if ('PC' in products):
            print("Saving PCA components...")
            print("Result for the RANDOMIZED solver")
            for ev in range(0, len(eigenvalues[:, 1, 1])):
                PCAcomp = eigenvalues[ev, :, :].reshape(m, n)
                saveGeoTiff(
                    PCAcomp,
                    os.path.join(dir_products_path,
                                 "PCA{}_".format(ev + 1) + ".tif"),
                    gdal_object, ColMinInd, RowMinInd)

        print("Products data were saved.")
        return 1
    except:
        print(
            "Can not write PRODUCTS on a disk... and/or error(s) in saveGeoTiff function"
        )
        return -1

    print("Operations were finished. It took {} sec".format(time.time() -
                                                            time_start))
Пример #27
0
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
Пример #28
0
                             f"{topography}_proj.tif", f"dem.tif")  # Clip Hand
        else:
            clip_to_boundary(f"RawFiles/Hand/{huc12[:-6]}", out_dir, geom,
                             f"{topography}_proj.tif",
                             f"{topography}.tif")  # Clip Hand

    # clip_to_boundary("RawFiles/Topography", out_dir, geom, f"elevation.tif",
    #                  f"dem.tif")

    # clip_to_boundary("RawFiles/Topography", out_dir, geom, f"texas_slope.tif",
    #                  f"slope.tif")
    gc.collect()  # clean up ram
    in_elevation = os.path.join(out_dir, f"dem.tif")
    dem = rd.LoadGDAL(in_elevation)
    rd.FillDepressions(dem, epsilon=True, in_place=True)
    slope = rd.TerrainAttribute(dem, attrib='slope_riserun')
    rd.SaveGDAL(os.path.join(out_dir, 'slope.tif'), slope)
    accum_d8 = rd.FlowAccumulation(dem, method='D8')
    rd.SaveGDAL(os.path.join(out_dir, 'FlowAccumulation.tif'), accum_d8)

    # Once slope and flow acculation are clipped then TWI can be calculated.
    clip_twi(out_dir)
    # This clips rainfall intensities for specific storms. Not necessary for the first analysis but needed later down the line.
    # for hr in [1, 2, 3, 4, 8, 12, 24, ]:
    #     for storm in [
    #         'taxday',
    #         'harvey']:
    #         clip_to_boundary(r"F:\test\{}\intensity\projected".format(storm), out_dir, geom,
    #                          f"{storm}{hr}hr.tif",
    #                          f"{storm}{hr}hr.tif")
Пример #29
0
### If you do not want to aggregate DEM, comment out the following to two lines
if aggregate:
    os.system('gdalwarp -tr ' + aggregate_degree + ' ' + aggregate_degree +
              ' -r average ' + dem_path_tif + ' ' + dem_path_tif_temp2)
    dem_path_tif = dem_path_tif_temp2

### convert DEM from tif to NetCDF
os.system('gdal_translate -of NETCDF ' + dem_path_tif + ' ' + dem_path)

### calculate slope as NetCDF from DEM
os.system('gdaldem slope -of NETCDF ' + dem_path + ' ' + slope_path +
          ' -s 111120')

### calculate aspect from DEM
aspect = np.flipud(
    rd.TerrainAttribute(rd.LoadGDAL(dem_path_tif), attrib='aspect'))

### calculate mask as NetCDF with DEM and shapefile
os.system(
    'gdalwarp -of NETCDF  --config GDALWARP_IGNORE_BAD_CUTLINE YES -cutline ' +
    shape_path + ' ' + dem_path_tif + ' ' + mask_path)

### open intermediate netcdf files
dem = xr.open_dataset(dem_path)
mask = xr.open_dataset(mask_path)
slope = xr.open_dataset(slope_path)

### set NaNs in mask to -9999 and elevation within the shape to 1
mask = mask.Band1.values
mask[np.isnan(mask)] = -9999
mask[mask > 0] = 1
Пример #30
0
import richdem as rd
import matplotlib
import matplotlib.pyplot as plt
import os
import time
import PIL

start = time.time()
# Na początku trzeba raster przekonwertować do układu metrycznego, tutaj jest zrobione do UTM34 (gdalwarp)
path = 'C:\\Users\\wojo1\\Desktop\\Doktorat\\Microrelief\\Data\\LIDAR\\NMT'
dem_path = path + '\\nmt_lidar.tif'
dem_plot = path + '\\L_NMT_N-34-138-B-b_utm.tif'
os.chdir("C:\\Users\\wojo1\\Desktop\\Doktorat\\Microrelief\\Data\\TIF")
# string = os.popen("gdalinfo " + dem_plot).read().rstrip()
# min_value = string.splitlines()
# min_float_value = float(min_value[-2][23:])
# print(os.popen('gdalinfo L_NMT_N-34-138-B-b_utm.tif').read().rstrip())

dem = rd.LoadGDAL(dem_plot)
# plt.imshow(dem, interpolation='bilinear')
# plt.clim(vmin=0, vmax=None)
# plt.colorbar(mappable=None, cax=None)
# plt.show()

slope = rd.TerrainAttribute(dem, attrib='slope_degrees')
rd.rdShow(slope, axes=False, cmap='jet', figsize=(10, 8))
plt.show()
# rd.SaveGDAL("nmt_python.tif", slope)
end = time.time()
print('process time: ', end - start)