示例#1
0
 def processAlgorithm(self, progress):
     '''Here is where the processing itself takes place'''
     ncla = self.getParameterValue(self.CLASSES)        
     inputSource = self.getParameterValue(self.SOURCE)
     output = self.getOutputValue(self.OUTPUT_RASTER)
     cs = self.getParameterValue(self.CS)
     
     mask = self.getParameterValue(self.MASK)        
     if mask != None:
         src = gdal.Open(str(mask), gdalconst.GA_ReadOnly)
         mask = src.GetRasterBand(1).ReadAsArray()      
     
     # Source 
     src = gdal.Open(str(inputSource), gdalconst.GA_ReadOnly)
     src_geotrans = src.GetGeoTransform()
     cols = src.RasterXSize
     rows = src.RasterYSize
     nodata = src.GetRasterBand(1).GetNoDataValue() # keep the nodata value
     array = src.GetRasterBand(1).ReadAsArray()  
     
     # Classes
     cl = range(1,ncla+1)
             
     # Do the calc
     result = nlmpy.classifyArray(array,cl,mask)
                 
     # Create output raster
     func.createRaster(output,cols,rows,result,nodata,src_geotrans)
def nlmGen(nlmPar):    
    nlMdlI = nlmPar[0][0]
    cadMdlI = nlmPar[0][1]
    clsNumI = nlmPar[0][2]
    sptlAggI = nlmPar[0][3]
    simItrI = nlmPar[0][4]
    nRowI = nlmPar[1]
    nColI = nlmPar[2]
    outPathI = nlmPar[3]
    
    # calculate weights
    weights = calcWeight(classCount=clsNumI,cadMdl=cadMdlI)
        
    if (nlMdlI == 'nlmMid'):
        # midpoint displacement NLM
        nlmMid = nlmpy.mpd(int(nRowI), int(nColI) , float(sptlAggI))
        # classify midpoint displacement NLM
        nlmItr = (nlmpy.classifyArray(nlmMid, weights) + 1).astype(int)

    if (nlMdlI == 'nlmRndClus'):
        # random cluster nearest-neighbour NLM
        nlmRndClus = nlmpy.randomClusterNN(int(nRowI), int(nColI), float(sptlAggI))    
        # Classified random cluster nearest-neighbour NLM
        nlmItr = (nlmpy.classifyArray(nlmRndClus, weights) + 1).astype(int)
    
    if (nlMdlI == 'nlmRnd'):
        # midpoint displacement NLM
        nlmRnd = nlmpy.random(int(nRowI), int(nColI))
        # classify midpoint displacement NLM
        nlmItr = (nlmpy.classifyArray(nlmRnd, weights) + 1).astype(int)
       
    # generate raster name 
    nlmNme = outPathI + '/' + nlMdlI + '_' + cadMdlI + '_clsNum'  + str(clsNumI) + '_sptlAgg' + sptlAggI + '_simItr' + str(simItrI) + '.csv'
    np.savetxt(nlmNme, nlmItr, delimiter=",")
    
# EOF
textLine = inFile.readline()
xll = int(textLine.split()[1])
textLine = inFile.readline()
yll = int(textLine.split()[1])
textLine = inFile.readline()
cellSize = int(textLine.split()[1])
inFile.close()
# Import the digital elevation model (DEM) ASCII file
demArray = np.loadtxt('DEM.asc', skiprows=6)

# Create the NLMs for the different hierarchical levels
np.random.seed(0)  # So that the same NLMs are produced each time
# Represent the agricultural land in the valley bottom using a random element
# nearest-neighbour NLM, which is then classified
nlm1 = nlmpy.randomElementNN(nRow, nCol, 5000)
nlm1 = nlmpy.classifyArray(nlm1, [1, 1, 1])
# Represent the pastoral and forestry land on the mountain slopes using a
# random cluster nearest-neighbour NLM, which is then classified
nlm2 = nlmpy.randomClusterNN(nRow, nCol, 0.58)
nlm2 = nlmpy.classifyArray(nlm2, [1, 2])
# Create a NlM that is a combination of these two NLMs, using an elevation of
# 380 as a threshold between the two NLMs
nlm = np.where(demArray <= 380, nlm1, nlm2 + 3)
# Replace all values with an elevation of 348 with a no data value to ignore
# the lake.
np.place(nlm, demArray == 348, np.nan)
# Replace any values above an elevation of 1100 with a sixth class representing
# the rough grassland on the mountain tops
np.place(nlm, demArray >= 1100, 5)
# Export the NLM as an ASCII raster grid
nlmpy.exportASCIIGrid("fig2bNLM.asc", nlm, xll, yll, cellSize)
示例#4
0
# Distance gradient NLM
Fig1e = nlmpy.distanceGradient(Fig1d)
# Midpoint displacement NLM
Fig1f = nlmpy.mpd(nRow, nCol, 0.75)
# Random rectangular cluster NLM
Fig1g = nlmpy.randomRectangularCluster(nRow, nCol, 4, 8)
# Random element nearest-neighbour NLM
Fig1h = nlmpy.randomElementNN(nRow, nCol, 200)
# Random cluster nearest-neighbour NLM
Fig1i = nlmpy.randomClusterNN(nRow, nCol, 0.4)
# Blended NLM
Fig1j = nlmpy.blendArray(Fig1f, [Fig1c])
# Patch blended NLM
Fig1k = nlmpy.blendClusterArray(Fig1h, [Fig1e], [1.5])
# Classified random cluster nearest-neighbour NLM
Fig1l = nlmpy.classifyArray(Fig1i, [1, 1, 1, 1])
# Percolation NLM
Fig1m = nlmpy.classifyArray(Fig1a, [1 - 0.5, 0.5])
# Binary random rectangular cluster NLM
Fig1n = nlmpy.classifyArray(Fig1g, [1 - 0.75, 0.75])
# Classified midpoint displacement NLM
Fig1o = nlmpy.classifyArray(Fig1f, [1, 1, 1])
# Classified midpoint displacement NLM, with limited classification extent
Fig1p = nlmpy.classifyArray(Fig1f, [1, 1, 1], classifyMask=Fig1d)
# Masked planar gradient NLM
Fig1q = nlmpy.planarGradient(nRow, nCol, 90, mask=Fig1n)
# Hierarchical NLM
Fig1r = np.where(Fig1o == 2, Fig1m + 2, Fig1o)
# Rotated NLM
Fig1s = np.rot90(Fig1l)
# Transposed NLM
示例#5
0
wkt_projection = 'PROJCS["ETRS89 / UTM zone 32N",GEOGCS["ETRS89",DATUM["European_Terrestrial_Reference_System_1989",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],AUTHORITY["EPSG","6258"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4258"]],UNIT["metre",1,AUTHORITY["EPSG","9001"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",9],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],AUTHORITY["EPSG","25832"],AXIS["Easting",EAST],AXIS["Northing",NORTH]]'

for x in range(share_LT):

    cursor.execute("""CREATE SCHEMA IF NOT EXISTS stream_network_""" +
                   str(schemaNAME[x]) + """;""")
    conn.commit()

    for xx in range(numNLMs):

        ### NLMs see https://besjournals.onlinelibrary.wiley.com/action/downloadSupplement?doi=10.1111%2F2041-210X.12308&file=mee312308-sup-0001-dataS1.pdf

        # random NLM

        nlm_R = nlmpy.random(int(nROW), int(nCOL))
        nlm_R = ((nlmpy.classifyArray(nlm_R, share_LT[x][0]) + 1) *
                 25) + share_LT[x][1]

        np.place(nlm_R, STREAM01_ARRAY == 1, 25)

        # random element NLM

        nlm_RE = nlmpy.randomElementNN(int(nROW), int(nCOL), 1000 * 25)
        nlm_RE = ((nlmpy.classifyArray(nlm_RE, share_LT[x][0]) + 1) *
                  25) + share_LT[x][1]

        np.place(nlm_RE, STREAM01_ARRAY == 1, 25)

        # random cluster NLM

        nlm_RC = nlmpy.randomClusterNN(int(nROW),