def glacierMaskProcessingEnvi(self): """ Program description: INPUT_PARAMETERS: inputValue_ - COMMENTS: """ #--> Missing license, not finished implemented inFile = tkFileDialog.askopenfilename( defaultextension='TIFF', filetypes=[('ERDAS IMAGINE', '*.img'), ('TIFF', '*.tif')], initialdir=self.workspace, initialfile=INITFILE_RASTER, multiple=False, parent=tkRoot, title= 'Chose input multispectral raster file for glacier mask processing (ArcPy)' ) pRatio = self.pArcPyTools.NDSI(inFile) #Envi Tools in ArcGIS --> Image Processing: Auto-Threshold Difference Raster outFileName = "thrNDSI_arcpy_" + sa.Raster(inFile).name arcpy.AutoThresholdDiff_envi(pRatio, outFileName, "Otsu's") #Envi Tools in ArcGIS: ENVI Filter With Convolution (Median) #Envi Tools in ArcGIS: Classification Raster To Vector #Envi Tools in ArcGIS: Cleanup Classification Raster (Smoothing + Aggregation) return
def glacierMaskProcessingTest(self): """ Program description: INPUT_PARAMETERS: inputValue_ - COMMENTS: """ inFile = tkFileDialog.askopenfilename( defaultextension='TIFF', filetypes=[('ERDAS IMAGINE', '*.img'), ('TIFF', '*.tif')], initialdir=self.workspace, initialfile=INITFILE_RASTER, multiple=False, parent=tkRoot, title='Chose input raster for processing (ArcPy)') pNDSI = self.pArcPyTools.NDSI( inFile) #Using Normalized-Difference Snow Index (NDSI) initOutFileName = "NDSI_arcpy_" + sa.Raster(inFile).name outRasterFile = tkFileDialog.asksaveasfilename( defaultextension='TIFF', filetypes=[('ERDAS IMAGINE', '*.img'), ('TIFF', '*.tif')], initialdir=self.workspace, initialfile=initOutFileName, parent=tkRoot, title='Save output raster file (ArcPy)') pNDSI.save(outRasterFile) return
def floodplain(slop_dem, colorivers): # jeff_dem_slop = sa.Raster(r"D:\Deliverable\source_data\dem_utm_120mclip_aspectslope.tif") dem = r"D:\Deliverable\source_data\dem_utm_120mclip.tif" jeff_dem_ras = sa.Raster(dem) llpnt2 = jeff_dem_ras.extent.lowerLeft demSize2 = jeff_dem_ras.meanCellHeight slop_Arr = arcpy.RasterToNumPyArray(slop_dem) slop_Arr[slop_Arr <= 0] = 0 flat_grid = np.where((slop_Arr <= 25.0),1,0) flatRaster = arcpy.NumPyArrayToRaster(flat_grid,llpnt2,demSize2,demSize2) # steepRaster = arcpy.NumPyArrayToRaster(steep_grid,llpnt,demSize,demSize) # arcpy.Delete_management("flat_FP") arcpy.DefineProjection_management(flatRaster,jeff_dem_ras.spatialReference) flatRaster.save('flat_FP') # floodplain(jeff_dem, colorivers) ZST = ZonalStatisticsAsTable(r"D:\Deliverable\source_data\intersect_dem", "PIN", r"D:\Deliverable\flat_fp", r"D:\Deliverable\ZST_fp_dem", "NODATA", "Maximum") # 7. Perform final joins so that suitability output raster has correct parcel data #---------------------------------------------------------------------------------------------- inFeatures = r"D:\Deliverable\source_data\intersect_dem" joinField = "PIN" joinTable = r"D:\Deliverable\ZST_fp_dem" fieldList = ["MAX"] # Join two feature classes by the zonecode field and only carry over the land use and land cover fields arcpy.JoinField_management(inFeatures, joinField, joinTable, joinField, fieldList) arcpy.RasterToPolygon_conversion(r"D:\Deliverable\source_data\intersect_dem", r"D:\Deliverable\source_data\intersect_dem"+".shp", "NO_SIMPLIFY", "MAX") inter_steep = r"D:\Deliverable\source_data\inter_steep.shp" inter_shp = r"D:\Deliverable\source_data\intersect_dem.shp" agri = """"gridcode" = 0 """ arcpy.Select_analysis(inter_shp, inter_steep,agri) inter_flat = r"D:\Deliverable\source_data\inter_flat.shp" inter_shp = r"D:\Deliverable\source_data\intersect_dem.shp" agri = """"gridcode" = 1 """ arcpy.Select_analysis(inter_shp, inter_flat,agri ) arcpy.Clip_analysis(colorivers, inter_flat, r"D:\Deliverable\flatClip2.shp") arcpy.Clip_analysis(colorivers, inter_steep, r"D:\Deliverable\steepClip2.shp") arcpy.Delete_management("flatBuff") arcpy.Buffer_analysis(r"D:\Deliverable\flatClip2.shp", "flatBuff", "400 meters") arcpy.Delete_management("steepBuff") arcpy.Buffer_analysis(r"D:\Deliverable\steepClip2.shp", "steepBuff", "150 meters")
def calculate_TOAR(outPath, BAND): # For use by IMAGE_TOA only #Calculates Top of Atmosphere Reflectance for USGS processed TM5 imagery with MTL metadata file #Band to process #exec("inRaster = MTL.BAND" + BAND + "_FILE_NAME.strip('\"')") global gl_inraster_dict inRaster = gl_inraster_dict.get(BAND) try: # Set the output raster name for Radiance calculation outRasterRAD = inRaster[:-4] + "_RAD" + ".IMG" outRasterRAD = os.path.join(outPath, outRasterRAD) # Set the output raster name for TOA calculation outRasterTOAR = inRaster[:-4] + "_TOAR" + ".TIF" outRasterTOAR = os.path.join(outPath, outRasterTOAR) # Check out Spatial Analyst extension license gp.CheckOutExtension("Spatial") #Conversion to Radiance #exec("LMAX = MTL.LMAX_BAND" + BAND + ".strip()") LMAX = lmax_dict.get(BAND) #exec("LMIN = MTL.LMIN_BAND" + BAND + ".strip()") LMIN = lmin_dict.get(BAND) #exec("QCALMAX = MTL.QCALMAX_BAND" + BAND + ".strip()") QCALMAX = qcalmax_dict.get(BAND) # Process: Create Constant Raster r0 = (float(LMAX) - float(LMIN)) / float( QCALMAX) #calculation factor # expression to calculate at-sensor spectral radiance #saExpRAD = "(" + str(r0) + " * " + inRaster + ") + (" + LMIN + ")" #Calculate TOA Reflectance #Calulate the Julian Day JD = MTL.getJulianDay() #-- Calculate earth sun distance REF - http://earth.esa.int/pub/ESA_DOC/landsat_FAQ/#_Toc235345965 d = 1 / (1 - 0.016729 * math.cos(0.9856 * (JD - 4))) d0 = 1 / d #sun-earth distance in astronomical units #product portion of equation p1 = math.pi * pow(float(d0), 2) #top of equation #quotiant portion of equation q1 = ESUN_dict.get(BAND) * math.sin( math.radians(float(MTL.SUN_ELEVATION))) #bottom of equation # Expression to apply to raster - scale by 400 #saExpTOAR = "((" + str(p1) + " * " + outRasterRAD + ") / " + str(q1) + ") * 400" #1/0.00255 scales the image to 1-255 print "Calculating Radiance... Band " + BAND #gp.SingleOutputMapAlgebra_sa(saExpRAD, outRasterRAD) print "Calculating TOA Reflectance... Band " + BAND #gp.SingleOutputMapAlgebra_sa(saExpTOAR, outRasterTOAR) outRadRaster = (r0 * sa.Raster(inRaster)) + float(LMIN) outToaRaster = ((p1 * (outRadRaster)) / q1) * 400 outToaRaster.save(outRasterTOAR) #gp.delete(outToaRaster) print "done TOA" except: # If an error occurred while running a tool, then print the messages print gp.addmessage(gp.GetMessages())
def NDSI(self, inFile_): """ Program description: Normalized-Difference Snow Index (NDSI) INPUT_PARAMETERS: inputValue_ - COMMENTS: """ try: b2 = inFile_ + "\Band_2" b5 = inFile_ + "\Band_5" #Arcpy Calculation pNum = sa.Float(sa.Raster(b2) - sa.Raster(b5)) pDenom = sa.Float(sa.Raster(b2) + sa.Raster(b5)) pNDSI = sa.Divide(pNum, pDenom) except Exception: raise Exception("Error: Error in calculating NDSI") return pNDSI
def nlcd_suit(nlcd2): jeffNlcd = ExtractByMask(nlcd2, jeff) jeffNlcd.save(r"D:\Deliverable\source_data\nlcd_120" + "clp2") jeff_nlcd_dem = r"D:\Deliverable\source_data\nlcd_120clp2" jeff_nlcd_ras = sa.Raster(jeff_nlcd_dem) jeff_nlcd_Arr = arcpy.RasterToNumPyArray(jeff_nlcd_ras) #jeff_nlcd_Arr[jeff_nlcd_Arr <= 0] = 0 jeff_nlcd_grid = np.where((jeff_nlcd_Arr == 52) | (jeff_nlcd_Arr == 71) | (jeff_nlcd_Arr == 81) | (jeff_nlcd_Arr == 82), 1, 0) veg = focalM(jeff_nlcd_grid) veg_A = np.zeros(jeff_nlcd_grid.shape).astype(float) veg_A = np.where((veg > 50.0), 1, 0).astype(float) return veg_A
def weightedCostRaster(outputCodeName, layerOrder=None): """ Source dir: preprocessed Output dir: costRasters outputCodeName: 1311011.asc where value of each digit |--> weight index of each digit |--> layer index in given list (so use the same layer order every time in every function!) layerOrder: ['agl_resis', 'bcmp_resis', 'bestp_resis', ...] --- layer names in order that corresponds to outputCode """ if layerOrder is None: print "No layer order specified, that's kinda important." return sources = layerOrder weights = map(int, list(outputCodeName.replace(".asc", ""))) # outputCode = "".join(map(str, weights)) print "Making weighted cost raster {}".format(outputCodeName) paths.setEnv(env) output = paths.join(paths.costRasters, outputCodeName) if arcpy.Exists(output) and not redoExistingOutput: print "{} already exists, leaving it as is.".format(output) return output active = [(paths.join(paths.preprocessed, source), weight) for source, weight in zip(sources, weights) if weight > 0] if len(active) <= 1: # TODO: add the straight-line-distance raster, so least cost paths don't freak out? pass costRas = sa.CreateConstantRaster(0.0001, "FLOAT", env.cellSize, env.extent) for source, weight in active: costRas += sa.Raster(source) * weight arcpy.RasterToASCII_conversion(costRas, output) arcpy.CalculateStatistics_management(output, x_skip_factor=30, y_skip_factor=30) return output
def arcpy_con(): # arcpy condition # 0-100 arcpy.CheckOutExtension('Spatial') arcpy.env.workspace = r'D:\FVC\FVC_1km_new\fusion_int1\\' out_dir = r'D:\FVC\FVC_1km_new\fusion_int_0-100_1\\' mkdir(out_dir) rasters = arcpy.ListRasters('*.tif*') for raster in rasters: # Save temp raster to disk with new name print(raster) ras = sa.Raster(raster) # arcpy.CalculateStatistics_management(ras) fvc = ras fvc = sa.Con(fvc, 0, fvc, "VALUE < 0") fvc = sa.Con(fvc, 100, fvc, "VALUE > 100") fvc.save(out_dir+raster)
def copy_raster_no_attributes(self, in_raster, out_raster): """ Copy raster Parameters ---------- in_raster : str name of input raster to copy out_raster : str name of copied raster """ print('Copying (w/o attributes) ' + in_raster) try: scratch = sa.Raster(in_raster) scratch.save(out_raster) arcpy.BuildRasterAttributeTable_management(out_raster) except Exception: raise Exception(arcpy.GetMessages())
def calculateShadow(self): """ Program description: INPUT_PARAMETERS: inputValue_ - COMMENTS: """ #arcpy.CheckOutExtension("3D") print "Start creating shadow image..." inFileName = tkFileDialog.askopenfilename( defaultextension='TIFF', filetypes=[('ERDAS IMAGINE', '*.img'), ('TIFF', '*.tif')], initialdir=self.workspace, initialfile=INITFILE_DEM, multiple=False, parent=tkRoot, title= 'Chose input elevation raster file for processing shadow (ArcPy)') inFile = sa.Raster(inFileName) #Determine Shadow Area: Spatial Analyst Tools --> Solar Radiation: Area Solar Radiation #--> Not applicable here #3D Anaylist Tools --> Raster Surface: HillShade azimuth = float(143.1156527) #SUN_AZIMUTH from Landsat altitude = float(51.2588465) #SUN_ELEVATION from Landsat outFileName = "out_shadow.tif" # Problem length of filename! arcpy.HillShade_3d(inFile, outFileName, azimuth, altitude, "SHADOWS", 1) print "Hillshade shadow image '" + str( outFileName) + "' with azimut '" + str( azimuth) + "' and altitude '" + str(altitude) + "' created." return
def iceDivideMapping(self): """ Program description: INPUT_PARAMETERS: inputValue_ - COMMENTS: """ #Ice Divide Determination by flowdirection and watershed print "Start creating basin raster image..." inFileName = tkFileDialog.askopenfilename( defaultextension='TIFF', filetypes=[('ERDAS IMAGINE', '*.img'), ('TIFF', '*.tif')], initialdir=self.workspace, initialfile=INITFILE_DEM, multiple=False, parent=tkRoot, title= 'Chose input elevation raster file for ice divide mapping (ArcPy)') inFile = sa.Raster(inFileName) #Spatial Analyst Tools --> Hydrology: Flow Direction outFlowDirection = sa.FlowDirection(inFile, "NORMAL") #"FORCE" #Spatial Analyst Tools --> Hydrology: Basin outBasin = sa.Basin(outFlowDirection) outFileName = "bas_flow_" + inFile.name outBasin.save(outFileName) print "Basin raster image '" + str(outFileName) + "' created." self.pArcPyTools.rasterToVector( outFileName, "median", 10000 ) #Eliminate areas smaller 0.01 km^2 = 10000 Square Meters (rocaviteanuetal2008); 0.02 km^2 (ICIMOD) return
def calculate_OLI_TOA(outPath,BAND): global gl_inraster_dict inRaster = gl_inraster_dict.get(BAND) outRasterTOAR = inRaster[:-4] + "_TOAR" + ".TIF" outRasterTOAR = os.path.join(outPath, outRasterTOAR) try: #multiplicative rescaling factor Mp_Dict = {'1':MTL.REFLECTANCE_MULT_BAND_1,'2':MTL.REFLECTANCE_MULT_BAND_2,'3':MTL.REFLECTANCE_MULT_BAND_3,\ '4':MTL.REFLECTANCE_MULT_BAND_4,'5':MTL.REFLECTANCE_MULT_BAND_5,'6':MTL.REFLECTANCE_MULT_BAND_6,\ '7':MTL.REFLECTANCE_MULT_BAND_7,'8':MTL.REFLECTANCE_MULT_BAND_8,'9':MTL.REFLECTANCE_MULT_BAND_9} #additive rescaling factor dictionary Ap_Dict = {'1':MTL.REFLECTANCE_ADD_BAND_1,'2':MTL.REFLECTANCE_ADD_BAND_2,'3':MTL.REFLECTANCE_ADD_BAND_3,\ '4':MTL.REFLECTANCE_ADD_BAND_4,'5':MTL.REFLECTANCE_ADD_BAND_5,'6':MTL.REFLECTANCE_ADD_BAND_6,\ '7':MTL.REFLECTANCE_ADD_BAND_7,'8':MTL.REFLECTANCE_ADD_BAND_8,'9':MTL.REFLECTANCE_ADD_BAND_9} #create TOA arcpy.env.overwriteOutput = 1 toaRaster = ((float(Mp_Dict.get(BAND)) * sa.Raster(inRaster)) + float(Ap_Dict.get(BAND)))/math.sin(math.radians(float(MTL.SUN_ELEVATION))) toaRaster.save(outRasterTOAR) print 'Done OLI TOA' except: print 'OLI TOA ERROR'
def convert_to_integer(self, in_raster, out_raster): """ Converts a floating point raster to an integer raster Parameters ---------- in_raster : str name of floating point raster to convert to int out_raster : str name of output converted raster Returns ------- None """ print('Converting ' + in_raster + ' to integer') try: # create an integer grid scratch = sa.Int(sa.RoundDown((sa.Raster(in_raster) * 100) + 0.5)) scratch.save(out_raster) except: raise Exception(arcpy.GetMessages())
def main(): arcpy.CheckOutExtension('Spatial') arcpy.env.workspace = this_root+'\\AVHRR_clipped\\' rasters = arcpy.ListRasters('*.tif*') # print(rasters) time_init = time.time() flag = 0 for raster in rasters: # Save temp raster to disk with new name start = time.time() ras = sa.Raster(raster) arcpy.CalculateStatistics_management(ras) max = ras.maximum fvc = ras/8000. fvc = sa.Con(fvc, 0, fvc, "VALUE < 0") fvc = sa.Con(fvc, 1, fvc, "VALUE > 1") fvc.save(this_root+'AVHRR_fvc\\'+raster) end = time.time() logger.process_bar(flag,len(rasters),time_init,start,end,raster) flag += 1 # break pass
def raster_to_polygon (feature, raster, workspace, raster_scaling = 1000): """Convert raster to a features class, clip it to an input feature and calculate the area of each polygon. This new feature class is then returned for calculating statistics. """ # Scale the subset DEM and temporarily save it to file. If it is not # saved an error is sometimes thrown when converting to polygon. # This is no good reason for this VAT error. rand_id = str(random.randrange(10000, 999999)) subset_name = workspace + '\\raster_to_poly_' + rand_id + '.img' subset = spatial.Int(spatial.Raster(raster) * raster_scaling + 0.5) subset.save(subset_name) polygon = ARCPY.RasterToPolygon_conversion(subset, subset_name, "NO_SIMPLIFY") clipped = ARCPY.Clip_analysis(polygon, feature.shape, 'in_memory\\clip_' + rand_id) feature = ARCPY.CalculateAreas_stats(clipped, 'in_memory\\area_'+ rand_id) ARCPY.Delete_management(subset) ARCPY.Delete_management(polygon) ARCPY.Delete_management(clipped) return feature
agri = """"ZTYPE" = 'A-1' OR "ZTYPE" = 'A-2' OR "ZTYPE" = 'A-35' OR "ZTYPE" = 'P-D'""" arcpy.Select_analysis(jeffZ, jeff_Agri, agri) # 5. Suitability Anlaysis of NLCD, Slope, & Aspect #---------------------------------------------------------------------------------------------- #resample and naming for nlcd arcpy.Resample_management(nlcd, r"D:\Deliverable\source_data\nlcd120utm13n", "120 120", "NEAREST") #mask and naming for dem that will be used for slope and aspect outExtractByMask = ExtractByMask(colo_dem, jeff) outExtractByMask.save("D:\Deliverable\source_data\dem_utm_120m" + "clip.tif") jeff_dem = r"D:\Deliverable\source_data\dem_utm_120mclip.tif" #rasterize and make NP array from the clipped colo_dem for the jeff county dem jeff_dem_ras = sa.Raster(jeff_dem) jeff_Arr = arcpy.RasterToNumPyArray(jeff_dem_ras) #derive aspect and name from the jeff dem, the variable will be pased to asp_suit outAspect = Aspect(jeff_dem) outAspect.save("D:\Deliverable\source_data\dem_utm_120mclip" + "_aspect.tif") jeff_asp = "D:\Deliverable\source_data\dem_utm_120mclip_aspect.tif" #derive slope and name from the previous file, the variable will be pased to slope_suit outSlope = Slope("D:\Deliverable\source_data\dem_utm_120mclip_aspect.tif", "PERCENT_RISE") outSlope.save("D:\Deliverable\source_data\dem_utm_120mclip_aspect" + "slope.tif") jeff_slope = r"D:\Deliverable\source_data\dem_utm_120mclip_aspect" + "slope.tif" #set variable input names and rasterize
# Clip slope service layers arcpy.AddMessage("Clipping slope...") slopeClip = os.path.join(scratch,"slopeClip") #arcpy.MakeRasterLayer_management(inputSlope,"SlopeLayer") #arcpy.CopyRaster_management("SlopeLayer",slopeClip) outSlope = sa.ExtractByMask(inputSlope,inputAOI) outSlope.save(slopeClip) deleteme.append(slopeClip) # Set all Slope values greater than the vehicle's off road max to that value arcpy.AddMessage("Reclassifying Slope ...") reclassSlope = os.path.join(os.path.dirname(scratch),"reclassSlope.tif") if debug == True: arcpy.AddMessage("reclassSlope: " + str(reclassSlope)) #float(vehicleParams[5]) if debug == True: arcpy.AddMessage(str(time.strftime("Con: %m/%d/%Y %H:%M:%S", time.localtime()))) outCon = sa.Con(sa.Raster(slopeClip) > float(vehicleParams[5]),float(vehicleParams[5]),sa.Raster(slopeClip)) # FAILS HERE: outCon.save(reclassSlope) # ERROR 010240: Could not save raster dataset to C:\Workspace\MAoT for A4W\A4W\test.gdb\reclassSlope with output format FGDBR. # # 010240 : Could not save raster dataset to <value> with output format <value>. # # Description # The output raster dataset could not be created in the specified format. There may already exist an output raster with the # same name and format. Certain raster formats have limitations on the range of values that are supported. For example, the GIF # format only supports a value range of 0 to 255, which would be a problem if the output raster would have a range of -10 to 365. # # Solution # Check that a raster with the same name and format does not already exist in the output location. Also, check the technical # specifications for raster dataset formats to make sure that the expected range of values in the output is compatible with # the specified format.
import polyInterpolation import inverseDistanecWeighting #Set Arcpy environment properties: Import arc license, set workspace, and set spatial reference path = r"E:\Surface_adjusted\ICC17\Code\Python\main_V3\Results" dataPath = r"E:\Surface_adjusted\ICC17\Code\Data\NC_cub" env.workspace = path env.overwriteOutput = 'True' spatialRef = arcpy.Describe(dataPath + '\dem10m').spatialReference arcpy.env.outputCoordinateSystem = spatialRef arcpy.CheckOutExtension("Spatial") #set the input parameters. # The inputs are DEMs in 4 different resolutions (10m, 30m, 100m, and 1000m) listDEM = [ sa.Raster(dataPath + '\dem10m'), sa.Raster(dataPath + '\dem30m'), sa.Raster(dataPath + '\dem100m'), sa.Raster(dataPath + '\dem1000m') ] # list of DEMs of different resolutions #Generate random points and extract their benchmark elevation from 3m lidar sampleSize = 20000 # Number of random points #extent = dataPath + r"\extent.shp" #arcpy.Buffer_analysis(extent, "extent_Buffer", buffer_distance_or_field="-5000 Meters", line_side="FULL", line_end_type="ROUND", dissolve_option="NONE", dissolve_field="", method="PLANAR") #extent = "405786 3949655 509706 3972169" # "X_West Y_South X_East Y_North" #arcpy.CreateRandomPoints_management(path, "randomPnts.shp", "extent_Buffer.shp", constraining_extent="0 0 250 250", number_of_points_or_field=sampleSize, minimum_allowed_distance="0 Millimeters", create_multipoint_output="POINT", multipoint_size="0") samples = "samples.shp" arcpy.gp.ExtractValuesToPoints_sa( "randomPnts.shp", sa.Raster(dataPath + '\dem3m'), samples, "NONE", "VALUE_ONLY"
import arcpy import numpy as np import math from arcpy import env import arcpy.sa as sa #import my modules import surfaceAdjusted import samples #set the input parameters. temp = arcpy.GetParameterAsText(0) listDEM = temp.split(';') arcpy.AddMessage("\nselected data " + listDEM[0] + "\n") for i in range(len(listDEM)): listDEM[i] = sa.Raster(listDEM[i]) #listDEM = [sa.Raster('dem10m'), sa.Raster('dem30m'), sa.Raster('dem100m'), sa.Raster('dem1000m'), sa.Raster('dem5000m')] # list of DEMs of different resolutions transects = arcpy.GetParameterAsText( 1) # this is a polyline including a set of transects sampleSize = arcpy.GetParameterAsText( 2 ) # transect would be converted to sample points based upon the sample size #import arc license and set workspace path = arcpy.GetParameterAsText(3) env.workspace = path env.overwriteOutput = 'True' arcpy.CheckOutExtension("Spatial") #Deleting extra fields in the transects feature class
This is the version which creates the WCM raster """ # Set environment settings import arcpy from arcpy import env from arcpy.sa import * import arcpy.sa as sa import numpy as np arcpy.CheckOutExtension("Spatial") env.workspace = "D:/finalData" env.overwriteOutput = 1 #Defining variables NationalForestMask = 'vector_data.mdb/National_Forest' themeFile = 'inputs/dem_mask' theme = sa.Raster(themeFile) #Lines 28-39 are necessary so that at the end of the analysis, you can convert from Numpy Array back to a raster with the #correct spatial reference and size. #Find the number of rows and columns theme.height #number of rows - Y axis theme.width #number of columns - X axis #get cell size; cellSize = theme.meanCellHeight #get lower left point of extent to write output after analysis llpnt = theme.extent.lowerLeft #get spatial reference object from the raster object spref = theme.spatialReference #check datatype of original raster grid theme.pixelType
import arcpy import arcpy.sa as sa from glob import glob print("ObjectID,Raster,NCols,NRows,NBands,PixelType,XMin,YMin,XMax,YMax,SRS") for k, f in enumerate(glob(r"e:\raster-types\Data\Tiles\*.tif"), 1): r = sa.Raster(f) e = r.extent print(",".join( (str(k), str(f), str(r.width), str(r.height), str(r.bandCount), str(r.pixelType), str(e.XMin), str(e.YMin), str(e.XMax), str(e.YMax), str(r.spatialReference.factoryCode))))
total_area_all_buildings = 0 # Need to set the Spatial Reference of the out_fc. sr = 'NAD 1983 StatePlane Alaska 4 FIPS 5004 Feet' arcpy.CreateFeatureclass_management(env.workspace, out_fc, "Polygon", fc, "", "", "", sr) # get access to a DEM raster dataset arcpy.CheckOutExtension("Spatial") #create raster object #valerie's raster data location: '1659_2635.tif' # Tom's raster data location: C:\Users\tommarquez\Documents\School\Geospatial-Programming_files\1659_2635\1659_2635.tif nlcd_rstr = sa.Raster(get_raster_path()) arcpy.CheckInExtension("Spatial") # convert to numpy array nlcd = arcpy.RasterToNumPyArray(nlcd_rstr) # Variables to hold the x and y coordinates of downtown #x_coords = [1655468.109, 1663714.632] #y_coords = [2635419.875, 2638805.296] # Tom's original #x_coords = [1655760.854, 1661815.541] #y_coords = [2637870.993, 5637870.993] # buildings in 1659_2635.tif x_coords = [1659059.606, 1661932.002]
def glacierMaskProcessing(self): """ Program description: Die Schwellwerte für die Ratiobilder sollten individuell angepasst werden, liegen aber in der Regel um 2.0. TM4/TM5 hat weniger Fehler in den Schattenbereichen aber es wird weniger Schutt erfasst als bei TM3/TM5. Daher empfehlen Paul und Kääb (2005) noch einen zusätzlichen Schwellwert im ersten Kanal. Was auch immer am Ende verwendet wird, eine visuelle Kontrolle und manuelle Verbesserung ist am Ende notwendig. Bei den Schwellwerten geht es um das mögliche Abbrechen von Eismassen und ist nicht als Schwellwert für das Gletschervorkommen zu sehen. Dennoch ist 45° zu testen, da bei SRTM die wahren Neigungen durch die Glättung/Auflösung eher höher sind. 60° ist m.E. mit SRTM zu hoch. INPUT_PARAMETERS: inputValue_ - COMMENTS: """ inFile = tkFileDialog.askopenfilename( defaultextension='TIFF', filetypes=[('ERDAS IMAGINE', '*.img'), ('TIFF', '*.tif')], initialdir=self.workspace, initialfile=INITFILE_RASTER, multiple=False, parent=tkRoot, title= 'Chose input multispectral raster file for glacier mask processing (ArcPy)' ) #------------------------------------------------------------------------------- #Image ration #ratioMethod = 'ndsi' #best with threshold 0.6 ratioMethod = 'tm3tm5' #best with threshold 2.6 #--> Best method! #ratioMethod = 'tm4tm5' #best with threshold 2.0 print "Start creating ratio image with method '" + str( ratioMethod) + "' ..." if ratioMethod == 'ndsi': pRatio = self.pArcPyTools.NDSI( inFile ) #Using Normalized-Difference Snow Index (NDSI) --> See Function thresholdRatio = 0.6 #Thresholding NDSI > threshold = snow/ice ... 0.5 - 0.6, or 0.7 (rocaviteanuetal2008); #best value: 0.6 elif ratioMethod == 'tm3tm5': #Red/SWIR pRatio = sa.Divide(sa.Float(sa.Raster(inFile + "\Band_3")), sa.Float(sa.Raster(inFile + "\Band_5"))) thresholdRatio = 2.6 #tm3tm5: th=2 (rocaviteanuetal2008); CCI: about 1.8; Paul and Andreassen (2009): 2.6; #best value: 2.6 elif ratioMethod == 'tm4tm5': #VNIR/SWIR pRatio = sa.Divide(sa.Float(sa.Raster(inFile + "\Band_4")), sa.Float(sa.Raster(inFile + "\Band_5"))) thresholdRatio = 2.0 #Tim Golletz threshold = 3 (tm4/tm5) cf. Paul 2001; # best value: 2.0 else: raise Exception("Error: No ratio approach chosen") outFileName = str(ratioMethod) + "_arcpy_" + sa.Raster(inFile).name pRatio.save(outFileName) print "Ratio image '" + str( outFileName) + "' with ratio method '" + str( ratioMethod) + "' created." #------------------------------------------------------------------------------- #Threshold on slope: Criteria: #--> Slope > 60° --> no glacier (ICIMOD); #--> Slope <= 24° --> glacier (Bolch et al. 2005, 2006, 2008) #--> Alean (1985), cited by Bolch et al. 2011: threshold of 45° for the slope of the detachment zone for cold glaciers and 25° for warm glaciers (cold glacier threashold of Alean (1985): 45; ICIMOD: 60) thresholdSlope = float(90) #ignoring value: 90 print "Start creating slope file with threshold '" + str( thresholdSlope) + "' ..." #Resample input DEM and derived slope to resolution of ratio image: Data Management Tools --> Raster --> Raster Processing: Resample inFileNameDem = tkFileDialog.askopenfilename( defaultextension='TIFF', filetypes=[('ERDAS IMAGINE', '*.img'), ('TIFF', '*.tif')], initialdir=self.workspace, initialfile=INITFILE_DEM, multiple=False, parent=tkRoot, title= 'Chose input elevation raster file for slope determination (ArcPy)' ) pInFileDem = sa.Raster(inFileNameDem) outFileNameDem = "res" + str(pRatio.meanCellWidth).replace( ".", "pt") + "_" + pInFileDem.name arcpy.Resample_management( pInFileDem, outFileNameDem, pRatio.meanCellWidth, "CUBIC" ) #BILINEAR #CUBIC #resample to Input scene size = Landsat 30m #Spatial Analyst Tools --> Surface: Slope (Aspect) pOutSlope = sa.Slope(outFileNameDem, "DEGREE", 1) pOutSlope.save("slope_" + outFileNameDem) print "Slope file '" + str( "slope_" + outFileNameDem) + "' created out of DEM input file '" + str( inFileNameDem) + "' and resampled to a resolution of '" + str( pRatio.meanCellWidth) + "'." #------------------------------------------------------------------------------- #Additional threshold on TM1 pB1 = sa.Raster( inFile + "\Band_1" ) #Use of additional threshold in TM1 to improve classification in cast shadow (CCI, Paul2005 --> rocaviteanuetal2008) thresholdB1 = float( 0) #Paul and Andreassen (2009): TM1 (DNs >59) #ignoring value: 0 #------------------------------------------------------------------------------- #Thresholding glacier yes/no print "Start thresholding ratio image..." #Spatial Analyst Tools --> Conditional: Con pBinRatio = sa.Con( ((pRatio > thresholdRatio) & (pOutSlope < thresholdSlope) & (pB1 > thresholdB1)), 1, 0) #Threshold on ratio outFileName = "bin" + str(thresholdRatio).replace( ".", "pt") + "_slope" + str(thresholdSlope).replace( ".", "pt") + "_1tm" + str(thresholdB1).replace( ".", "pt") + "_" + str(outFileName) pBinRatio.save(outFileName) print "Binary ratio image '" + str( outFileName) + "' with ratio method '" + str( ratioMethod) + "', ratio threshold '" + str( thresholdRatio) + "', slope threshold '" + str( thresholdSlope) + "' and TM1 threshold '" + str( thresholdB1) + "' created." #------------------------------------------------------------------------------- #Raster to Vector self.pArcPyTools.rasterToVector( outFileName, "median", 10000 ) #Eliminate areas smaller 0.01 km^2 = 10000 Square Meters (rocaviteanuetal2008); 0.02 km^2 (ICIMOD) #Detect spatial autocorrelation: Spatial Statistic Tools --> Analyzing Patterns: Spatial Autocorrelation (Morans I) #--> Not applicable here return
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