##--FUNCTIONS-- def msg(txt): print txt arcpy.AddMessage(txt) return ##--PROCESSES-- # Extract development from NLCD msg("Extracting developed area from NLCD") devBinary = sa.Con(nlcdRaster, 1, 0, "VALUE IN (22,23,24)") # Compute focal mean of development msg("Calculating focal stats") nbrHood = sa.NbrCircle(distanceThreshold, "MAP") focalMean = sa.FocalStatistics(devBinary, nbrHood, "MEAN") # Compute distance decay msg("Computing distance decayed development") devNodata = sa.Con(nlcdRaster, 1, '', "VALUE IN (22,23,24)") eucDist = sa.EucDistance(devNodata) k = math.log(0.01) / distanceThreshold devDecay = sa.Exp(eucDist * k) # FOCAL MEAN: Compute zonal stats msg("Computing patch threat values") tmpTable = "in_memory/TmpTable" sa.ZonalStatisticsAsTable(patchRaster, "VALUE", devBinary, tmpTable, '', "MEAN") GeoHATutils.RenameField(tmpTable, "MEAN", "FocalMean")
# f2: surface change arcpy.AddMessage("Surface Curvature ...") f2 = os.path.join(env.scratchFolder,"f2.tif") if debug == True: arcpy.AddMessage(str(time.strftime("Curvature: %m/%d/%Y %H:%M:%S", time.localtime()))) # CURVATURE curvature = os.path.join(scratch,"curvature") curveSA = sa.Curvature(elevClip) curveSA.save(curvature) deleteme.append(curvature) if debug == True: arcpy.AddMessage(str(time.strftime("Focal Stats: %m/%d/%Y %H:%M:%S", time.localtime()))) # FOCALSTATISTICS (RANGE) focalStats = os.path.join(scratch,"focalStats") window = sa.NbrCircle(3,"CELL") fstatsSA = sa.FocalStatistics(curvature,window,"RANGE") fstatsSA.save(focalStats) deleteme.append(focalStats) # F2 maxRasStat = float(str(arcpy.GetRasterProperties_management(focalStats,"MAXIMUM"))) fsRasStat = sa.Raster(focalStats) if debug == True: arcpy.AddMessage("maxRasStat: " + str(maxRasStat) + " - " + str(type(maxRasStat))) arcpy.AddMessage("fsRasStat: " + str(fsRasStat) + " - " + str(type(fsRasStat))) f2Calc = (maxRasStat - fsRasStat) / maxRasStat # (max - cell/max) f2Calc.save(f2) deleteme.append(f2) ccmFactorList.append(f2) #TODO: Need more thorough and complete checks of inputs if inputVegetation != types.NoneType and arcpy.Exists(inputVegetation) == True:
def rasterToVector(self, inFileName_, filtering_, delArea_): """ Program description: Normalized-Difference Snow Index (NDSI) INPUT_PARAMETERS: inputValue_ - COMMENTS: """ #------------------------------------------------------------------------------- #Raster Filtering pBinRatio = sa.Raster(inFileName_) print "Start filtering raster..." if filtering_ == 'median': #3x3 Median Filter #--> Best result pFiltBinRatio = sa.FocalStatistics( pBinRatio, sa.NbrRectangle(3, 3, "CELL"), "MEDIAN" ) #Spatial Analyst Tools --> Neighborhood: Focal Statistics elif filtering_ == 'closing': #Erosion / Dilatation (Closing) instead of Median pShrinkBinRatio = sa.Shrink( pBinRatio, 1, [0] ) #Spatial Analyst Tools --> Generalization: Shrink ... Expand: One cell pFiltBinRatio = sa.Expand( pShrinkBinRatio, 1, [0]) # Closing value '0', so no-glacier area, one pixel size else: raise Exception("Error: No filtering approach chosen") outFileName = str(filtering_) + "_" + pBinRatio.name pFiltBinRatio.save(outFileName) print "Binary ratio image '" + str( outFileName) + "' with filtering method '" + str( filtering_) + "' created." #------------------------------------------------------------------------------- #Convert Raster to Polygon outFileName = outFileName.rsplit(".")[ 0] #Get filename without ".tif" extension outFileName = "vec_" + outFileName.rsplit( "arcpy" )[0] + ".shp" #Split filename at code 'arcpy' to shorten filenmame for following methods (methods fail if output filename is too long) #Conversion Tools --> From Rater: Raster to Polygon print "Start converting raster to polygon..." arcpy.RasterToPolygon_conversion( pFiltBinRatio, outFileName, "NO_SIMPLIFY", "VALUE") #No simplify of output polygon print "Raster converted to polygon '" + str(outFileName) + "'." #------------------------------------------------------------------------------- #Postprocessing #Data Management Tools --> Generalization: Eliminate Polygon Part --> Eliminates small islands in the polygon and outside inFileName = outFileName outFileName = "elim_" + inFileName print "Start eliminating polygon parts..." dm.EliminatePolygonPart( inFileName, outFileName, "AREA", delArea_ ) #, "", "ANY") # not CONTAINED_ONLY #Eliminate areas smaller 0.01 km^2 = 10000 Square Meters print "Polygon '" + str( inFileName) + "' eliminated polygon parts of '" + str( delArea_) + "' to '" + str(outFileName) + "'." #Cartography Tools --> Generalization: Aggregate Polygons #ca.AggregatePolygons("buildings.shp", "C:/output/output.gdb/aggregated_buildings", 10) # Not suitable in this case! #Cartography Tools --> Generalization: Simplify Polygon with Point Remove inFileName = outFileName outFileName = "simp_" + inFileName tolerance = float( pBinRatio.meanCellWidth) #Cell Size: SRTM: 90m, Landsat: 30m print "Start simplyfing polygon..." ca.SimplifyPolygon( inFileName, outFileName, "POINT_REMOVE", tolerance, delArea_, "RESOLVE_ERRORS", "KEEP_COLLAPSED_POINTS" ) #SQR(15^2+15^2) ~ 22m; Eliminate areas smaller 0.01 km^2 = 10000 Square Meters print "Polygon '" + str( inFileName) + "' simplified to polygon '" + str( outFileName) + "' with deleted areas of '" + str( delArea_) + "' and maximal tolerance of '" + str( tolerance) + "'." #Cartography Tools --> Generalization: Smooth Polygon with PEAK inFileName = outFileName outFileName = "smooth_" + inFileName tolerance = 2 * float( pBinRatio.meanCellWidth) #2*Cell Size: SRTM: 180m, Landsat: 60m print "Start smoothing polygon..." ca.SmoothPolygon(inFileName, outFileName, "PAEK", tolerance, "FIXED_ENDPOINT", "FLAG_ERRORS") print "Polygon '" + str(inFileName) + "' smoothed to polygon '" + str( outFileName) + "' with maximal tolerance of '" + str( tolerance) + "'." return