Ejemplo n.º 1
0
    def test_combinig_hillshades_arrays(self):
        """test combining arrays (band calculator)
        """
        dem_array = raster_funs.raster_2_array(self.input_dem)
        hillshade_1_array = hillshade.hillshade(
            dem_array, self.hill_params['azimuth1'],
            self.hill_params['angle_altitude1'])
        hillshade_2_array = hillshade.hillshade(
            dem_array, self.hill_params['azimuth2'],
            self.hill_params['angle_altitude2'])
        hillshade_3_array = hillshade.hillshade(
            dem_array, self.hill_params['azimuth3'],
            self.hill_params['angle_altitude3'])

        combined_array = bandCalc.merge_arrays(
            [hillshade_1_array, hillshade_2_array, hillshade_3_array], [
                self.hill_params['transparency1'],
                self.hill_params['transparency2'],
                self.hill_params['transparency3']
            ])

        output_folder = os.path.join(tempfile.mkdtemp(), 'hillshader_results')
        if not os.path.exists(output_folder):
            os.mkdir(output_folder)

        out_put = os.path.join(output_folder, 'combined_hillshade.tif')
        raster_funs.array_2_raster(combined_array, self.input_dem, out_put)

        self.assertTrue(os.path.exists(out_put))

        self.assertNotAlmostEquals(0, combined_array.any())
        self.assertEqual(combined_array.shape, (100, 100))
        self.assertAlmostEqual(int(combined_array.max()), 255)
        self.assertAlmostEqual(int(combined_array.min()), 0)
        self.assertAlmostEqual(int(combined_array.mean()), 255 / 2)
Ejemplo n.º 2
0
    def createAlgsList(self):
        # First we populate the list of algorithms with those created
        # extending GeoAlgorithm directly (those that execute GDAL
        # using the console)
        self.preloadedAlgs = [nearblack(), information(), warp(), translate(),
                              rgb2pct(), pct2rgb(), merge(), buildvrt(), polygonize(), gdaladdo(),
                              ClipByExtent(), ClipByMask(), contour(), rasterize(), proximity(),
                              sieve(), fillnodata(), ExtractProjection(), gdal2xyz(),
                              hillshade(), slope(), aspect(), tri(), tpi(), roughness(),
                              ColorRelief(), GridInvDist(), GridAverage(), GridNearest(),
                              GridDataMetrics(), gdaltindex(), gdalcalc(), rasterize_over(),
                              # ----- OGR tools -----
                              OgrInfo(), Ogr2Ogr(), Ogr2OgrClip(), Ogr2OgrClipExtent(),
                              Ogr2OgrToPostGis(), Ogr2OgrToPostGisList(), Ogr2OgrPointsOnLines(),
                              Ogr2OgrBuffer(), Ogr2OgrDissolve(), Ogr2OgrOneSideBuffer(),
                              Ogr2OgrTableToPostGisList(), OgrSql(),
                              ]

        # And then we add those that are created as python scripts
        folder = self.scriptsFolder()
        if os.path.exists(folder):
            for descriptionFile in os.listdir(folder):
                if descriptionFile.endswith('py'):
                    try:
                        fullpath = os.path.join(self.scriptsFolder(),
                                                descriptionFile)
                        alg = GdalScriptAlgorithm(fullpath)
                        self.preloadedAlgs.append(alg)
                    except WrongScriptException as e:
                        ProcessingLog.addToLog(ProcessingLog.LOG_ERROR, e.msg)
Ejemplo n.º 3
0
    def createAlgsList(self):
        # First we populate the list of algorithms with those created
        # extending GeoAlgorithm directly (those that execute GDAL
        # using the console)
        self.preloadedAlgs = [nearblack(), information(), warp(), translate(),
            rgb2pct(), pct2rgb(), merge(), polygonize(), gdaladdo(),
            ClipByExtent(), ClipByMask(), contour(), rasterize(), proximity(),
            sieve(), fillnodata(), ExtractProjection(), gdal2xyz(),
            hillshade(), slope(), aspect(), tri(), tpi(), roughness(),
            ColorRelief(), GridInvDist(), GridAverage(), GridNearest(),
            GridDataMetrics(),
            # ----- OGR tools -----
            OgrInfo(), Ogr2Ogr(), OgrSql(),
            ]

        # And then we add those that are created as python scripts
        folder = self.scriptsFolder()
        if os.path.exists(folder):
            for descriptionFile in os.listdir(folder):
                if descriptionFile.endswith('py'):
                    try:
                        fullpath = os.path.join(self.scriptsFolder(),
                                descriptionFile)
                        alg = GdalScriptAlgorithm(fullpath)
                        self.preloadedAlgs.append(alg)
                    except WrongScriptException, e:
                        ProcessingLog.addToLog(ProcessingLog.LOG_ERROR, e.msg)
Ejemplo n.º 4
0
    def test_generating_hillshades_array(self):
        """test generating partial hillshades arrays from dem
        """
        dem_array = raster_funs.raster_2_array(self.input_dem)
        hillshade_1 = hillshade.hillshade(dem_array,
                                          self.hill_params['azimuth1'],
                                          self.hill_params['angle_altitude1'])
        hillshade_2 = hillshade.hillshade(dem_array,
                                          self.hill_params['azimuth2'],
                                          self.hill_params['angle_altitude2'])
        hillshade_3 = hillshade.hillshade(dem_array,
                                          self.hill_params['azimuth3'],
                                          self.hill_params['angle_altitude3'])

        self.assertIsNotNone(hillshade_1)
        self.assertIsNotNone(hillshade_2)
        self.assertIsNotNone(hillshade_3)
Ejemplo n.º 5
0
 def createAlgsList(self):
     # First we populate the list of algorithms with those created
     # extending GeoAlgorithm directly (those that execute GDAL
     # using the console)
     self.preloadedAlgs = [
         nearblack(),
         information(),
         warp(),
         translate(),
         rgb2pct(),
         pct2rgb(),
         merge(),
         buildvrt(),
         polygonize(),
         gdaladdo(),
         ClipByExtent(),
         ClipByMask(),
         contour(),
         rasterize(),
         proximity(),
         sieve(),
         fillnodata(),
         ExtractProjection(),
         gdal2xyz(),
         hillshade(),
         slope(),
         aspect(),
         tri(),
         tpi(),
         roughness(),
         ColorRelief(),
         GridInvDist(),
         GridAverage(),
         GridNearest(),
         GridDataMetrics(),
         gdaltindex(),
         gdalcalc(),
         rasterize_over(),
         retile(),
         gdal2tiles(),
         # ----- OGR tools -----
         OgrInfo(),
         Ogr2Ogr(),
         Ogr2OgrClip(),
         Ogr2OgrClipExtent(),
         Ogr2OgrToPostGis(),
         Ogr2OgrToPostGisList(),
         Ogr2OgrPointsOnLines(),
         Ogr2OgrBuffer(),
         Ogr2OgrDissolve(),
         Ogr2OgrOneSideBuffer(),
         Ogr2OgrTableToPostGisList(),
         OgrSql(),
     ]
Ejemplo n.º 6
0
 def createAlgsList(self):
     # First we populate the list of algorithms with those created
     # extending GeoAlgorithm directly (those that execute GDAL
     # using the console)
     self.preloadedAlgs = [
         nearblack(),
         information(),
         warp(),
         translate(),
         rgb2pct(),
         pct2rgb(),
         merge(),
         buildvrt(),
         polygonize(),
         gdaladdo(),
         ClipByExtent(),
         ClipByMask(),
         contour(),
         rasterize(),
         proximity(),
         sieve(),
         fillnodata(),
         ExtractProjection(),
         gdal2xyz(),
         hillshade(),
         slope(),
         aspect(),
         tri(),
         tpi(),
         roughness(),
         ColorRelief(),
         GridInvDist(),
         GridAverage(),
         GridNearest(),
         GridDataMetrics(),
         gdaltindex(),
         gdalcalc(),
         rasterize_over(),
         retile(),
         gdal2tiles(),
         # ----- OGR tools -----
         OgrInfo(),
         Ogr2Ogr(),
         Ogr2OgrClip(),
         Ogr2OgrClipExtent(),
         Ogr2OgrToPostGis(),
         Ogr2OgrToPostGisList(),
         Ogr2OgrPointsOnLines(),
         Ogr2OgrBuffer(),
         Ogr2OgrDissolve(),
         Ogr2OgrOneSideBuffer(),
         Ogr2OgrTableToPostGisList(),
         OgrSql(),
     ]
Ejemplo n.º 7
0
def gacode(post, pre, az, al):
    #matrix_x variable is for entering the width of matrix
    matrix_x = 40

    #matrix_y variable is for entering the height of matrix
    matrix_y = 40

    #importin predtm and postdtm
    #predtm= load_workbook(filename='D:\Study\sem 4\scope\matlb_landslide\postdtm.xlsx')
    #postdtm= load_workbook(filename='D:\Study\sem 4\scope\matlb_landslide\predtm.xlsx')
    predtm = pd.read_excel(pre, header=None, index_col=None)
    predtm.as_matrix()
    postdtm = pd.read_excel(post, header=None, index_col=None)
    postdtm.as_matrix()

    #azimuth
    if az == -1:
        az = 135
    if int(al) == -1:
        al = 100

    ar = np.arange(0, 40)
    arx = np.arange(0, matrix_x)
    ary = np.arange(0, matrix_y)

    postimg = hillshade(postdtm, az, al, -1)

    pcsumxy = matrix_y * matrix_x - 3

    generations = 20

    populations = 30

    rang = int(100)

    delta = np.zeros((matrix_x, matrix_y, populations))
    for i in range(0, populations):
        delta[:, :, i] = np.random.rand(matrix_y, matrix_x) * rang * 2 - rang
    best = np.zeros(generations)
    for i in range(0, generations):
        # now_generations_count=i
        pctime = 0

        # pc variable refers to the probability of crossover
        pc = 0.8
        if (pc > np.random.rand()):

            # pcrandnew1 variable and pcrandnew2 variable are the places for new chromosomes
            pcrandnew1 = 20
            pcrandnew2 = 21

            # each crossover supplies two new chromosomes
            for j in range(18, 20):
                delta[:, :,
                      j] = np.random.rand(matrix_y, matrix_x) * rang * 2 - rang

            #each crossover produces two new chromosomes
            while (pctime < 5):

                pcrandbest1 = round(np.random.rand() * (19))
                pcrandbest2 = round(np.random.rand() * (19))

                pcrandpoint1 = 1 + round(np.random.rand() * (pcsumxy))
                pcrandpoint2 = 1 + round(np.random.rand() * (pcsumxy))

                selecttemp1 = delta[:, :, pcrandbest1]
                selecttemp2 = delta[:, :, pcrandbest2]

                selecttemp3 = np.zeros((matrix_x * matrix_y, ))
                selecttemp4 = np.zeros((matrix_x * matrix_y, ))

                j = 0
                for k in range(0, matrix_x * matrix_y, matrix_x):
                    blk1 = selecttemp1[j]
                    blk2 = selecttemp2[j]
                    selecttemp3[k:k + matrix_x] = np.asarray(blk1)
                    selecttemp4[k:k + matrix_x] = np.asarray(blk2)
                    j = j + 1

                selecttemp1 = np.transpose(selecttemp3)
                selecttemp2 = np.transpose(selecttemp4)

                if (pcrandpoint1 > pcrandpoint2):
                    pctemp = pcrandpoint2
                    pcrandpoint2 = pcrandpoint1
                    pcrandpoint1 = pctemp

                for j in range(pcrandpoint1, pcrandpoint2 + 1):
                    pctemp = selecttemp1[j]
                    selecttemp1[j] = selecttemp2[j]
                    selecttemp2[j] = pctemp

                delta[:, :, pcrandnew1] = np.reshape(selecttemp1,
                                                     (matrix_y, matrix_x))
                delta[:, :, pcrandnew2] = np.reshape(selecttemp2,
                                                     (matrix_y, matrix_x))

                pcrandnew1 = pcrandnew1 + 2
                pcrandnew2 = pcrandnew2 + 2
                pctime = (pctime + 1)

        pmtime = 0

        #pm variable refers to the probability of mutation
        pm = 0.06
        t = np.zeros(18)
        for j in range(1, 18):
            t[j] = np.random.rand()

            if (pm > t[j]):
                pmx = round(np.random.rand() * (matrix_x - 1))
                pmy = round(np.random.rand() * (matrix_y - 1))

                pmvolue = delta[pmy, pmx,
                                j] + (np.random.rand() * rang * 2 - rang)

                while (abs(pmvolue) > rang):
                    pmvolue = delta[pmy, pmx,
                                    j] + (np.random.rand(1) * rang * 2 - rang)

                delta[pmy, pmx, j] = pmvolue

        shadow = np.zeros((matrix_x, matrix_y, populations))
        for j in range(0, populations):
            shadow[:, :, j] = hillshade(np.add(predtm, delta[:, :, j]), az, al,
                                        -1)

        F = np.zeros(populations)
        #calculates fitness function
        for j in range(0, populations):
            F[j] = corr2(shadow[:, :, j], postimg)

        indexF = np.argsort(F)

        delta_temp = np.zeros((matrix_x, matrix_y, populations))
        shadow_temp = np.zeros((matrix_x, matrix_y, populations))
        for j in range(0, populations):
            delta_temp[:, :, j] = delta[:, :, indexF[populations - j - 1]]
            shadow_temp[:, :, j] = shadow[:, :, indexF[populations - j - 1]]

        for j in range(0, populations):
            delta[:, :, j] = delta_temp[:, :, j]
            shadow[:, :, j] = shadow_temp[:, :, j]

        best[i] = np.max(F)
        if i % 10000 == 0:
            print(i)

    bestrange = delta[:, :, 1]

    #filter
    bestdtm = np.add(predtm, bestrange)
    print(bestdtm.shape)

    bestrange_filter = np.zeros(bestdtm.shape)
    for i in range(0, bestdtm.shape[0], 12):
        for j in range(0, bestdtm.shape[1], 12):
            block = bestdtm.iloc[i:i + 12, j:j + 12]
            bestrange_filter[i:i + 12, j:j +
                             12] = np.median(block) * np.ones(block.shape)

    for y in range(0, matrix_y):
        for x in range(0, matrix_x):
            if (bestrange_filter[y, x] == 0):
                bestrange_filter[y, x] = bestdtm[y, x]

    end_predtm = np.zeros((matrix_y - 6, matrix_x - 6))
    end_postdtm = np.zeros((matrix_y - 6, matrix_x - 6))
    end_filter = np.zeros((matrix_y - 6, matrix_x - 6))
    end_bestrange = np.zeros((matrix_y - 6, matrix_x - 6))
    for y in range(3, matrix_y - 3):
        for x in range(3, matrix_x - 3):
            end_bestrange[y - 3, x - 3] = bestrange[y, x]

    for y in range(3, matrix_y - 3):
        for x in range(3, matrix_x - 3):
            end_filter[y - 3, x - 3] = bestrange_filter[y, x]

    for y in range(3, matrix_y - 3):
        for x in range(3, matrix_x - 3):
            end_predtm[y - 3, x - 3] = predtm.iloc[y, x]

    for y in range(3, matrix_y - 3):
        for x in range(3, matrix_x - 3):
            end_postdtm[y - 3, x - 3] = postdtm.iloc[y, x]

    X_estimate = end_filter[16, :]
    X_predtm = end_predtm[16, :]
    X_postdtm = end_postdtm[16, :]

    Y_estimate = end_filter[:, 16]
    Y_predtm = end_predtm[:, 16]
    Y_postdtm = end_postdtm[:, 16]

    truedepth_del = 0
    truedepth_add = 0
    estimatedepth_del = 0
    estimatedepth_add = 0

    for y in range(0, matrix_y - 6):
        for x in range(0, matrix_x - 6):
            if (end_predtm[y, x] > end_postdtm[y, x]):
                truedepth = end_predtm[y, x] - end_postdtm[y, x]
                truedepth_del = truedepth + truedepth_del
            if (end_predtm[y, x] < end_postdtm[y, x]):
                truedepth = end_postdtm[y, x] - end_predtm[y, x]
                truedepth_add = truedepth + truedepth_add

    volume_true_del = ((truedepth_del))
    volume_true_add = ((truedepth_add))

    for y in range(0, matrix_y - 6):
        for x in range(0, matrix_x - 6):
            if (end_predtm[y, x] > end_filter[y, x]):
                estimatedepth = end_predtm[y, x] - end_filter[y, x]
                estimatedepth_del = estimatedepth + estimatedepth_del

            if (end_predtm[y, x] < end_filter[y, x]):
                estimatedepth = end_filter[y, x] - end_predtm[y, x]
                estimatedepth_add = estimatedepth + estimatedepth_add

    volume_estimate_del = ((estimatedepth_del))
    volume_estimate_add = ((estimatedepth_add))

    #output result

    best_h = hillshade(np.add(predtm, bestrange), az, al, -1)
    new_h = hillshade(postdtm, az, al, -1)
    #convert postimg,best_h,new_h to img and show
    '''
    img1 = PIL.Image.fromarray(postimg)
    img2 = PIL.Image.fromarray(best_h)
    img3 = PIL.Image.fromarray(new_h)
    cv2.imshow("post-event remote sensing image",img1)
    cv2.imshow("estimate result shadow image",img2)
    cv2.imshow("post-event DTM shadow image",img3)
    '''
    imageio.imwrite("post-eventDTM.png", new_h)
    imageio.imwrite("estimate-result.png", best_h)

    error = (abs(volume_true_del - volume_estimate_del) /
             volume_true_del) * 100
    error1 = 100 - corr2(best_h, new_h) * 100
    row = [generations, volume_estimate_del, error1]
    change = predtm - postdtm

    with open('data.csv', 'a') as csvFile:
        writer = csv.writer(csvFile)
        writer.writerow(row)
    csvFile.close()
    return (volume_estimate_del, error1, change)
gt = DEM.GetGeoTransform()
dx = gt[1]
cellsize = dx
DEM = np.array(
    DEM.GetRasterBand(1).ReadAsArray())  #convert gdal obj into np array
del (gt)

#import meta data regarding raster, for eventual exporting of filtered DEM
import rasterio as rio
with rio.open(dem_path) as src:
    Meta = src.profile

#%% Visualize the imported DEM to confirm it appears as you expect
# the hillshade produced will be rather dark.

hs_array = hillshade(DEM)

#Plot hillshade
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.imshow(hs_array, cmap='Greys', alpha=0.9)
ax.plot()

#%%  TEST 1: CWT
# This test requires patches of both the landslide and non-landslide to compare the power spectra between the two pieces of the landscape.

# This methodology follows Booth et al., (2009).
#### IMPORTANTLY!!! #### EACH TEST PATCH MUST BE CLIPPED SQUARE!
#This code is heavily adapted from code available here: http://web.pdx.edu/~boothad/tools.html
# It was originally writen in Matlab, but I have now adapted it for Python.
Ejemplo n.º 9
0
#%%
os.chdir(r'U:\GHP\Projects\NSF - Morris Landslides\Code\Developmemnt')

matplotlib.rcParams['figure.figsize'] = (8, 5.5)

dem_path = os.path.join(os.getcwd(), 'sb_less_steep.tif')
sbDEM = gdal.Open(dem_path)
sbDEM = np.array(demFLD.GetRasterBand(1).ReadAsArray())

sbDEM[sbDEM == -9999.0] = np.nan
sbDEM[sbDEM == -32767] = np.nan

#View hillshade of the DEM

from hillshade import hillshade
hs_array = hillshade(demFLD)
plt.imshow(hs_array, cmap='Greys')
plt.show()

#%% Now convolve landscape with the MexH at the appropriate scales

from conv2_mexh2 import conv2_mexh2

[C2, _, _] = conv2_mexh2(sbDEM, 13, dx)
[C3, _, _] = conv2_mexh2(sbDEM, 11, dx)
[C4, _, _] = conv2_mexh2(sbDEM, 9, dx)
[C5, _, _] = conv2_mexh2(sbDEM, 7, dx)
[C6, _, _] = conv2_mexh2(sbDEM, 5, dx)
[C7, _, _] = conv2_mexh2(sbDEM, 4, dx)
[C8, _, _] = conv2_mexh2(sbDEM, 3, dx)