def CreateTiff(inShapefile, fieldName, maskShapefile, outRaster): """" """ outIDW = Idw(inShapefile, fieldName, 500, 2, arcpy.sa.RadiusVariable(8)) outExtractByMask = ExtractByMask(outIDW, maskShapefile) arcpy.RasterToOtherFormat_conversion(outExtractByMask, outRaster, "TIFF")
def convertPRISMTmeanToGrid(): try: outputFolder = "C:\\my\\tmean_grid\\" # iterate through each of the year range folders dateRangeFolder = "C:\\my\\tmean\\" env.workspace = dateRangeFolder rasters = arcpy.ListRasters("*","tif") for r in rasters: print(r) #newName = r.replace(".tif","") #newName = newName.replace("studyarea_30m_prism_mo_tmean_","") #print newName arcpy.RasterToOtherFormat_conversion(r,outputFolder,"GRID") print("All Done!") except: print("*** Error ***") print(sys.exc_info()[0]) print(sys.exc_info()[1]) print(sys.exc_info()[2])
def ReformatRaster (inFilesList,outPath="C:/temp/",Format="TIFF"): inFileString = ';'.join(map(str, myList)) try: ##Convert Multiple Raster Dataset to FGDB arcpy.RasterToOtherFormat_conversion(inFileString,outPath,Format) #http://resources.arcgis.com/en/help/main/10.1/index.html#//001200000032000000 except: print "Raster To Other Format Failed." print arcpy.GetMessages()
def convertMonthlyPRISMImageryToTIFF(prism_variable): try: monthlyOutputFolder = PRISM_DIRECTORY + "geotiff/" + prism_variable + "/Monthly" yearlyOutputFolder = PRISM_DIRECTORY + "geotiff/" + prism_variable + "/Yearly" # iterate through each of the year range folders monthlyRangeFolder = PRISM_DIRECTORY + "/bil/" + prism_variable for root, dirs, files in os.walk(monthlyRangeFolder): for file in files: if not file.startswith('.'): fileExtension = os.path.splitext(file)[1] fileName = os.path.splitext(file)[0] year = fileName.split("_")[4][0:4] month = str(fileName.split("_")[4][4:6]) #if(year == "2016"): if fileExtension == ".bil": currFile = os.path.abspath(os.path.join(root, file)) currFileDir = os.path.dirname(currFile) # export to GeoTif env.workspace = currFileDir if month == "": arcpy.RasterToOtherFormat_conversion(file, yearlyOutputFolder ,"TIFF") print("YEARLY: " + file + " " + year + " " + month) else: arcpy.RasterToOtherFormat_conversion(file, monthlyOutputFolder ,"TIFF") print("MONTHLY: " + file + " " + year + " " + month) #print(currFile) print("*** Processing " + file + " ***") print("All Done!") except: print("*** Error ***") print(sys.exc_info()[0]) print(sys.exc_info()[1]) print(sys.exc_info()[2])
def raster_other_format(in_gdb_path, snap_dict, region_c, c_gdb, out_path): arcpy.env.workspace = in_gdb_path raster_list = arcpy.ListRasters() snap_raster = Raster(snap_dict[region_c]) arcpy.Delete_management("snap") arcpy.MakeRasterLayer_management(snap_dict[region_c], "snap") arcpy.env.snapRaster = "snap" myExtent = snap_raster.extent arcpy.env.extent = myExtent start_conversion = datetime.datetime.now() print 'Starting the conversion for {0} in {1}\n'.format(raster_list, c_gdb) arcpy.RasterToOtherFormat_conversion(raster_list, out_path, "GRID") print 'Completed conversion in: {0}\n'.format( (datetime.datetime.now() - start_conversion))
def arcReformatRaster (FileList=["file1.tif","file2.tif"], inPath=None,outPath=None,Format="TIFF"): import arcpy, os if inPath==None: inPath=os.getcwd() if outPath==None: outPath=os.getcwd() inFileList = list() outFileList = list() for f in FileList: inFilePath = inPath+"\\"+f try: ##Convert Multiple Raster Dataset to FGDB arcpy.RasterToOtherFormat_conversion(inFilePath,outPath,Format) except: print("Raster To Other Format Failed.") print(arcpy.GetMessages())
def RasterClip(rasterPath, maskPath, outDir, tifDir): # Set local variables inRaster = rasterPath inMaskData = maskPath # Check out the ArcGIS Spatial Analyst extension license arcpy.CheckOutExtension("Spatial") # Execute ExtractByMask outExtractByMask = arcpy.sa.ExtractByMask(inRaster, inMaskData) # Save the output and format tif outExtractByMask.save(outDir) outname = os.path.split(outDir)[1] arcpy.RasterToOtherFormat_conversion(outDir, tifDir, "TIFF") return tifDir + '/' + outname + '.tif'
def numpyGeoTiff(numpyarray,fileProcessingFolder,filePrefix,originalTiff): arcpy.env.overwriteOutput = True # get info about the raster r = arcpy.Raster(originalTiff) LL_corner_subset = r.extent.lowerLeft x_cell_size = r.meanCellWidth y_cell_size = r.meanCellHeight stringSpatialRef = r.spatialReference.exporttostring() # DUMP TO GEOTIFF outRaster1 = arcpy.NumPyArrayToRaster(numpyarray,LL_corner_subset,x_cell_size,y_cell_size,value_to_nodata=0) arcpy.DefineProjection_management(outRaster1,stringSpatialRef) arcpy.RasterToOtherFormat_conversion(outRaster1,fileProcessingFolder,"TIFF") outRaster1.save(fileProcessingFolder+filePrefix+".tif")
def export(project_ssurgo_statsgo, soilProperties, MaskRaster, TEMP=TEMP, outDir=outDir): arcpy.env.snapRaster = MaskRaster # Set Snap Raster environment project_ssurgo_statsgo = project_ssurgo_statsgo + ".shp" # soilProperties = [[ "ksat_r_WtAvg", "Ksat-s_UT612" ], ["Ks_WtAvg", "Ksat-t_ut612" ], .... ] for a_soil_property in soilProperties: soil_property_name = a_soil_property[ 1] #e.g. Ksat-s, Bblpr-t, PoreSz-t etc. field_name = a_soil_property[2] outRaster = os.path.join(TEMP, soil_property_name) arcpy.FeatureToRaster_conversion( in_features=project_ssurgo_statsgo, field=field_name, out_raster=outRaster, cell_size=MaskRaster) arcpy.gp.ExtractByMask_sa(outRaster, MaskRaster, outRaster + "X") #c=clipped # to clip the rasters to the consistent extent, so that their (nrows x ncol) matches arcpy.Clip_management(in_raster=outRaster + "X", out_raster=outRaster + "c", in_template_dataset=MaskRaster, nodata_value="-9999", clipping_geometry="NONE", maintain_clipping_extent="MAINTAIN_EXTENT") arcpy.RasterToOtherFormat_conversion(Input_Rasters="'%s'" % (outRaster + "c"), Output_Workspace=outDir, Raster_Format="TIFF") arcpy.AddMessage( "SUCCESS: TIF representing %s values saved in %s" % (soil_property_name, outDir))
def caculatePRISMTMean(): try: tminDir = "H:\\PRISM\\4km\\tmin\\Monthly\\az\\30m\\" tmaxDir = "H:\\PRISM\\4km\\tmax\\Monthly\\az\\30m\\" tmeanDir = "H:\\PRISM\\4km\\tmean\\Monthly\\az\\30m" clip = "H:\\Landsat\\landsat_1984.06.20.tif" tempWorkspace = "C:\\my\\GIS\\temp\\temp_processing\\" env.workspace = tminDir rasters = arcpy.ListRasters() env.workspace = tempWorkspace for r in rasters: year = int(r.split(".")[0].split("_")[2]) month = int(r.split(".")[1]) if(year > 1980 and year < 2012): print("Processing: " + r) r_min = Raster(tminDir + r) r_max = Raster(tmaxDir + r.replace("tmin","tmax")) r_mean = (r_max + r_min)/2 r_mean_by_mask = ExtractByMask(r_mean,clip) r_mean_by_mask.save(tmeanDir + "\\cc" + str(year) + "-" + str(month) + ".tif") arcpy.RasterToOtherFormat_conversion(tmeanDir + "\\cc" + str(year) + "-" + str(month) + ".tif",tmeanDir + "\\grid","GRID") print("All Done!") except: print("*** Error ***") print(sys.exc_info()[0]) print(sys.exc_info()[1]) print(sys.exc_info()[2])
except Exception, e: arcpy.AddMessage(e) if merge_ssurgo_to_raster: # Step4 MatchRaster = os.path.join(raw_files_outDir, "mask_r") STEP4_Join_Merge_Export(path2ssurgoFolders, path2statsgoFolders, ssurgo_outDir, MatchRaster) # To tif, and flt for outRaster in [ "mask_r", "DEM_Prj_fc", "n_Overland", "n_Channel", "fdr_cr", "slope_c", "SD", "str_c", "str_cr9999", "str_cr255" ]: arcpy.RasterToOtherFormat_conversion( Input_Rasters="'%s'" % (os.path.join(raw_files_outDir, outRaster)), Output_Workspace=tiffs_outDir, Raster_Format="TIFF") for outRaster in [ "bbl-tc.tif", "efpo-tc.tif", "ksat-tc.tif", "psd-tc.tif", "por-tc.tif", "rsm-tc.tif", "SD-sc.tif", "AWC-sc.tif" ]: arcpy.RasterToOtherFormat_conversion( Input_Rasters="'%s'" % (os.path.join(ssurgo_outDir, outRaster)), Output_Workspace=tiffs_outDir, Raster_Format="TIFF") # delete unnecessary files # unnecessary files in TIF folder, that are not tif for file in os.listdir(tiffs_outDir): if not file.split(".")[-1] in ['tif', "gdb", "xlsx"]:
Majorit_tif = GDB +"\\" + "Majorit_tif" Boundar_Majo = GDB + "\\" + "Boundar_Majo" RegionG_Boun = GDB + "\\" + "RegionG_Boun" SetNull_Reg = GDB + "\\" + "SetNull_Reg" generalized_flood_extent = GDB + "\\" + "generalized_flood_extent" generalized_flood_shp = Folder + "\\" + "generalized_flood.shp" Input_false_raster_or_constant_value = "0" # Process: Majority Filter arcpy.gp.MajorityFilter_sa(flood_image, Majorit_tif, "FOUR", "MAJORITY") # Process: Boundary Clean arcpy.gp.BoundaryClean_sa(Majorit_tif, Boundar_Majo, "NO_SORT", "TWO_WAY") # Process: Region Group arcpy.gp.RegionGroup_sa(Boundar_Majo, RegionG_Boun, "FOUR", "WITHIN", "ADD_LINK", "") # Process: Set Null arcpy.gp.SetNull_sa(RegionG_Boun, Input_false_raster_or_constant_value, SetNull_Reg, "\"COUNT\" < 50") # Process: Nibble arcpy.gp.Nibble_sa(Boundar_Majo, SetNull_Reg, generalized_flood_extent, "ALL_VALUES") # Process: Raster to Polygon arcpy.RasterToPolygon_conversion(generalized_flood_extent, generalized_flood_shp, "SIMPLIFY", "VALUE") # Process: Raster To Other Format (multiple) arcpy.RasterToOtherFormat_conversion(generalized_flood_extent, Folder, "TIFF")
combined_2017_Clip_IsoCluste01 = arcpy.GetParameterAsText(1) if combined_2017_Clip_IsoCluste01 == '#' or not combined_2017_Clip_IsoCluste01: combined_2017_Clip_IsoCluste01 = "combined_2017_Clip_IsoCluste01" # provide a default value if unspecified Project_Geodatabase_gdb = arcpy.GetParameterAsText(2) if Project_Geodatabase_gdb == '#' or not Project_Geodatabase_gdb: Project_Geodatabase_gdb = "R:\\GIS Project\\Project Geodatabase.gdb" # provide a default value if unspecified # Local variables: Reclass_comb1 = Project_Geodatabase_gdb + "\\Reclass_comb1" Reclass_comb2 = Project_Geodatabase_gdb + "\\Reclass_comb2" Project_Geodatabase_gdb__2_ = Project_Geodatabase_gdb Input_reclass_images = Reclass_comb1 + ";" + Reclass_comb2 # Process: Reclassify arcpy.gp.Reclassify_sa( combined_2002_Clip_IsoCluste01, "Value", "1 1;2 1;3 2;4 4;5 3;6 5;7 2;8 1;9 4;10 5;11 3;12 3;13 3;14 5;15 2;16 4;17 4;18 3;19 4;20 5;21 3;22 5;23 4", Reclass_comb1, "DATA") # Process: Reclassify (3) arcpy.gp.Reclassify_sa( combined_2017_Clip_IsoCluste01, "Value", "1 1;2 1;3 3;4 4;5 5;6 2;7 3;8 4;9 5;10 3;11 2;12 3;13 2;14 4;15 1;16 3;17 5;18 3;19 3;20 3;21 5;22 4;23 3;24 5", Reclass_comb2, "DATA") # Process: Raster To Other Format (multiple) arcpy.RasterToOtherFormat_conversion(Input_reclass_images, Project_Geodatabase_gdb, "TIFF")
def export_custom_datasets(self, dest_folder): list_datasets = [] root = ET.Element("CustomData") for dataset in self.get_custom_datasets(): if not path.exists( dest_folder ): # make dir only if one or more custom dataset exists makedirs(dest_folder) desc = arcpy.Describe(dataset) msg = "" if desc.dataType == "FeatureClass": if int(arcpy.GetCount_management(dataset).getOutput(0)) > 0: try: arcpy.FeatureClassToFeatureClass_conversion( dataset, dest_folder, desc.baseName) exported = "True" list_datasets.append(dataset) except arcpy.ExecuteError: exported = "False" msg = arcpy.GetMessages() else: exported = "False" msg = "No Features found" elif desc.dataType == "Table": if int(arcpy.GetCount_management(dataset).getOutput(0)) > 0: try: arcpy.TableToTable_conversion( dataset, dest_folder, "{}.{}".format(desc.baseName, "dbf")) exported = "True" list_datasets.append(dataset) except arcpy.ExecuteError: exported = "False" msg = arcpy.GetMessages() else: exported = "False" msg = "No Records in Table" elif desc.dataType == "RasterDataset": try: arcpy.RasterToOtherFormat_conversion( dataset, dest_folder, "TIFF") exported = "True" list_datasets.append(dataset) except arcpy.ExecuteError: exported = "False" msg = arcpy.GetMessages() else: exported = "False" msg = "Unsupported dataType for export" nodeDataset = ET.SubElement(root, desc.dataType) nodeDataset.set("exported", str(exported)) ET.SubElement(nodeDataset, "Name").text = desc.baseName ET.SubElement(nodeDataset, "Source").text = dataset if msg: ET.SubElement(nodeDataset, "Message").text = str(msg) if list_datasets: indent(root) tree = ET.ElementTree(root) tree.write(path.join(dest_folder, "CustomData.xml"), 'utf-8', True) return list_datasets
dup_response = input() if dup_response == "1": pass # elif dup_response == "2": # pass elif dup_response == "2": pass else: print("Invalid response. Respond with 1 or 2.") dup_response = input() prog = 0 for file in file_list: prog += 1 print("working on " + file + "(" + str(prog) + "/" + str(len(file_list)) + ")" + "...") name = re.search(r"(.+)\.", file).group(1) path_list = [os.path.join(input_folder, file)] try: arcpy.CreateLasDataset_management(path_list, lasd_output_path + name + ".lasd", "NO_RECURSION", "", arcpy.SpatialReference(4326), "COMPUTE_STATS", "RELATIVE_PATHS", "NO_FILES") arcpy.LasDatasetToRaster_conversion(lasd_output_path + name + ".lasd", output_gdb_path + name, "ELEVATION", 'BINNING AVERAGE SIMPLE', 'FLOAT', 'CELLSIZE', 0.00001, 1) except: if dup_response == "1": pass elif dup_response == "2": arcpy.CreateLasDataset_management(path_list, lasd_output_path + name "_1" + ".lasd", "NO_RECURSION", "", arcpy.SpatialReference(4326), "COMPUTE_STATS", "RELATIVE_PATHS", "NO_FILES") arcpy.LasDatasetToRaster_conversion(lasd_output_path + name + "_1" + ".lasd", output_gdb_path + name, "ELEVATION", 'BINNING AVERAGE SIMPLE', 'FLOAT', 'CELLSIZE', 0.00001, 1) arcpy.RasterToOtherFormat_conversion(output_gdb_path + name, raster_output_path, "TIFF")
def geoprocess(inFilepathname, outPath, outFilename): # Overwrite pre-existing files arcpy.env.overwriteOutput = True SNODAS_errorMessage = "" inFilepath, inFilename = os.path.split(inFilepathname) # define file names of temp layers unsignedRaster = inFilepath + "/unsigned" projectedName = "albers" projectedRaster = inFilepath + "/" + projectedName #prepare the header file hdrFilepathname = inFilepathname[:-3] + "Hdr" o = open(hdrFilepathname, 'w') o.write("byteorder M\n") o.write("layout bil\n") o.write("nbands 1\n") o.write("nbits 16\n") o.write("ncols 6935\n") o.write("nrows 3351\n") o.write("ulxmap -124.729583333331703\n") o.write("ulymap 52.871249516804028\n") o.write("xdim 0.00833333333\n") o.write("ydim 0.00833333333\n") o.close() # CONVERT .BIL FILE FROM SIGNED TO UNSIGNED INTEGER print("CONVERT .BIL FILE FROM SIGNED TO UNSIGNED INTEGER") try: # Check out the ArcGIS Spatial Analyst extension license arcpy.CheckOutExtension("Spatial") # Execute Con to convert raster data from signed integer to unsigned integer outCon = Con( Raster(inFilepathname) >= 32768, Raster(inFilepathname) - 65536, Raster(inFilepathname)) # replace negative values with 0" outCon2 = Con(outCon < 0, 0, outCon) # Save the output outCon2.save(unsignedRaster) except arcpy.ExecuteError: # Get the tool error messages SNODAS_errorMessage = SNODAS_errorMessage + arcpy.GetMessages() + ";" print SNODAS_errorMessage except: # Get the traceback object tb = sys.exc_info()[2] tbinfo = traceback.format_tb(tb)[0] # Concatenate information together concerning the error into a message string pymsg = tbinfo + ";" + str(sys.exc_info()[1]) SNODAS_errorMessage = SNODAS_errorMessage + "Failed at CONVERT SIGNED TO UNSIGNED;" print pymsg # DEFINE PROJECTION FOR CONVERTED FILE print("DEFINE PROJECTION FOR CONVERTED FILE") try: #coordinateSystem = "C:\Program Files (x86)\ArcGIS\Desktop10.0\Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984.prj" coordinateSystem = "GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984' \ ,6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]" arcpy.DefineProjection_management(unsignedRaster, coordinateSystem) except: # Get the tool error messages SNODAS_errorMessage = SNODAS_errorMessage + "Failed at DEFINE PROJECTION;" print arcpy.GetMessages() # REPROJECT CONVERTED FILE FROM WGS84 TO ALBERS EQUAL AREA CONIC USGS print( "REPROJECT CONVERTED FILE FROM WGS84 TO ALBERS EQUAL AREA CONIC USGS") try: #arcpy.ProjectRaster_management(inProj, outProj, "USA Contiguous Albers Equal Area Conic USGS.prj" \ #,"BILINEAR", "#","WGS_1984_(ITRF00)_To_NAD_1983", "#", "#") arcpy.ProjectRaster_management(unsignedRaster, projectedRaster,"PROJCS['USA_Contiguous_Albers_Equal_Area_Conic_USGS_version' \ ,GEOGCS['GCS_North_American_1983',DATUM['D_North_American_1983',SPHEROID['GRS_1980',6378137.0,298.257222101]] \ ,PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Albers'],PARAMETER['False_Easting',0.0] \ ,PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',-96.0],PARAMETER['Standard_Parallel_1',29.5] \ ,PARAMETER['Standard_Parallel_2',45.5],PARAMETER['Latitude_Of_Origin',23.0],UNIT['Meter',1.0]]" , "BILINEAR" \ ,"#", "WGS_1984_(ITRF00)_To_NAD_1983","#", "#") arcpy.Delete_management(unsignedRaster) except: # Get the tool error messages SNODAS_errorMessage = SNODAS_errorMessage + "Failed at REPROJECTION;" print arcpy.GetMessages() # CONVERT GRID FILE TO IMAGINE .IMG FORAMT print "CONVERT REPROJECTED FILE TO IMAGINE .IMG FORAMT" try: arcpy.RasterToOtherFormat_conversion(projectedRaster, outPath, "IMAGINE Image") arcpy.Delete_management(projectedRaster) arcpy.Rename_management(outPath + projectedName + ".img", outPath + outFilename) except: # Get the tool error messages SNODAS_errorMessage = SNODAS_errorMessage + "Failed at CONVERT TO .IMG FORMAT;" print arcpy.GetMessages() pass if SNODAS_errorMessage == "": SNODAS_errorMessage = "OK" writelog(SNODAS_LOGFILE, outFilename, SNODAS_errorMessage) print "File conversion stopped. Status: " + SNODAS_errorMessage
def WVW(city, inDir, workFld): import traceback, time, arcpy, os from arcpy import env arcpy.CheckOutExtension('Spatial') #-------- DIRECTORY SETUP ------------------------------------------------ """ Working Directory """ try: arcpy.CreateFileGDB_management(str(workFld), str(city) + '_WaterWV.gdb') except: print 'WaterWV GDB already exists' workDir = str(workFld) + '/' + city + '_WaterWV.gdb' arcpy.env.workspace = workDir """ Report File Directory """ reportfileDir = str(workFld) + '/Logs' """ Frequent Directory """ freqDir = str(workFld) + '/' + city + '_Freq.gdb' """ Final Geodatabase """ finalDir = str(workFld) + '/' + city + '_Final.gdb' """ Projection File Directory """ prjDir = str(inDir) + '/Prj' prjfile = prjDir + '/USA Contiguous Albers Equal Area Conic USGS.prj' """ Split Raster Directory """ if os.path.isdir(str(workFld) + '/' + city + '_Split') == True: pass else: os.makedirs(str(workFld) + '/' + city + '_Split') splitDir = str(workFld) + '/' + city + '_Split' """ Set Workspace Environments """ arcpy.env.workspace = workDir arcpy.env.scratch = str(inDir) + '/Scratch.gdb' arcpy.env.overwriteOutput = True print("Directory and Environment set-up complete...--" + time.strftime('%Y%m%d--%H%M%S')) #----------------------------------------------------------------------------- # BEGIN ANALYSIS #----------------------------------------------------------------------------- try: #-------- LOGFILE CREATION --------------------------------------------- """ Create report file for each metric """ tmpName = city + '_WaterWV_' + time.strftime('%Y%m%d_%H-%M') reportfileName = reportfileDir + '/' + tmpName + '.txt' reportFile = open(reportfileName, 'w') try: loglist = sorted(f for f in os.listdir(reportfileDir) if f.startswith(str(city) + '_Reuse')) tmpName = loglist[-1] except: tmpName = city + '_Reuse_' + time.strftime('%Y%m%d_%H-%M') + '.txt' reportfileName = reportfileDir + '/' + tmpName try: ReuseRF = open(reportfileName, 'a') except: ReuseRF = open(reportfileName, 'w') print 'Creating Reuse Log' """ Write out first line of report file """ print 'Water Views Start Time: ' + time.asctime() reportFile.write( "Begin with EnviroAtlas 1-meter Land Cover for the EnviroAtlas community--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') #-------- PROCESSING LAYERS ---------------------------------------------- """ Set Environments """ arcpy.env.snapRaster = freqDir + '/LC' arcpy.env.extent = freqDir + '/LC' arcpy.env.overwriteOutput = True """-------- Reclassify LC into Binary Water ----------------------------- """ if arcpy.Exists(str(freqDir) + '/WaterIO') == False: outReclass = arcpy.sa.Reclassify( str(freqDir) + '/LC', 'Value', arcpy.sa.RemapValue([[0, 0], [10, 1], [20, 0], [21, 0], [22, 0], [30, 0], [40, 0], [52, 0], [70, 0], [80, 0], [82, 0], [91, 0], [92, 0]])) outReclass.save(str(freqDir) + '/WaterIO') print("Reclassify to water binary complete...--" + time.strftime('%Y%m%d--%H%M%S')) reportFile.write( "Reclassify the Land Cover into a Water binary. (Water - 10 = 1; All Else = 0).--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') ReuseRF.write("WaterIO--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') else: print("Reclassify to water previously completed...--" + time.strftime('%Y%m%d--%H%M%S')) reportFile.write( "Reclassify the Land Cover into a Water binary. (Water - 10 = 1; All Else = 0).--WaterIO" + '--\n') """-------- Create the Water Bodies Raster ------------------ """ """ Create the Water Region Group """ if arcpy.Exists('WatIO_300') == False: RG = arcpy.sa.RegionGroup(freqDir + '/WaterIO', 'EIGHT', 'WITHIN', 'ADD_LINK') RG.save('WatIO_300') arcpy.AddField_management('WatIO_300', 'Count_2', 'DOUBLE') arcpy.CalculateField_management('WatIO_300', 'Count_2', '!COUNT!', 'PYTHON') print("Water region groups created...--" + time.strftime('%Y%m%d--%H%M%S')) reportFile.write( "Run the region group tool to group waterbodies together with options number of neighbors to use: EIGHT, zone grouping method: WITHIN, and ADD_LINK (add link field to output).--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') ReuseRF.write("WaterRG--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') else: print("Water region groups previously created...--" + time.strftime('%Y%m%d--%H%M%S')) reportFile.write( "Run the region group tool to group waterbodies together with options number of neighbors to use: EIGHT, zone grouping method: WITHIN, and ADD_LINK (add link field to output).--WaterRG" + '\n') """ Extract Water Bodies From the Region Group """ EBA = arcpy.sa.ExtractByAttributes('WatIO_300', 'Count_2 > 299 AND LINK = 1') EBA.save('WatBod_300') reportFile.write( "Extract by Attributes from the Region Group raster groups of pixels where Count > 299 and Link = 1.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """-------- Split the Raster As Needs, Process Each Piece ----------------- """ """ Check if the raster should be split """ columns = arcpy.GetRasterProperties_management( 'WatBod_300', 'COLUMNCOUNT').getOutput(0) xsplit = int(float(columns) / 40000) + 1 rows = arcpy.GetRasterProperties_management('WatBod_300', 'ROWCOUNT').getOutput(0) ysplit = int(float(rows) / 40000) + 1 print("Determining if split is necessary...--" + time.strftime('%Y%m%d--%H%M%S')) """-------- If no split, run the analysis --------------------------------- """ if xsplit * ysplit == 1: """ Convert Raster to Polygon """ arcpy.RasterToPolygon_conversion('WatBod_300', 'Poly_WatBod', 'NO_SIMPLIFY') reportFile.write("Convert the raster into polygons--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') print("\t Raster is small enough, split not necessary...--" + time.strftime('%Y%m%d--%H%M%S')) """-------- If split, run the analysis on each piece and recombine --------- """ else: """ Delete the raster, if necessary """ xy = (xsplit * ysplit) for rast in range(xy): try: arcpy.Delete_management(splitDir + '/watbod_' + str(rast)) except: pass try: arcpy.Delete_management(splitDir + '/watbod_300') except: pass """ Split the Raster """ print("\t Raster is big, spliting into " + str(xsplit) + " rows and " + str(ysplit) + " columns...--" + time.strftime('%Y%m%d--%H%M%S')) arcpy.RasterToOtherFormat_conversion('WatBod_300', splitDir, 'GRID') arcpy.SplitRaster_management(splitDir + '/WatBod_300', splitDir, 'WatBod_', 'NUMBER_OF_TILES', 'GRID', '', str(xsplit) + ' ' + str(ysplit)) reportFile.write( "Split the raster into pieces for easier processing. The Python script determines the number of pieces based on the number of rows and columns in the raster where no piece can have a side larger than 40,000 cells.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ For each raster: """ prjfile = prjDir + '/USA Contiguous Albers Equal Area Conic USGS.prj' xy = xsplit * ysplit for Chunk in range(0, xy): try: result = float( arcpy.GetRasterProperties_management( splitDir + '/WatBod_' + str(Chunk), 'MEAN').getOutput(0)) """ If the raster piece has data: """ if (result > 0): """ Set Environments """ arcpy.env.snapRaster = freqDir + '/WaterIO' arcpy.env.extent = freqDir + '/WaterIO' """ Copy the piece back to the Working Directory """ arcpy.RasterToOtherFormat_conversion( splitDir + '/WatBod_' + str(Chunk), workDir) """ Convert Raster to Polygon """ arcpy.RasterToPolygon_conversion( 'WatBod_' + str(Chunk), 'Poly_' + str(Chunk), 'NO_SIMPLIFY') print("Processed Chunk " + str(Chunk) + " / " + str(xy) + "..." + time.asctime()) else: pass except: pass reportFile.write( "Convert the pieces individually into polygons then recombine them into one feature class.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') WVchunks = arcpy.ListFeatureClasses('Poly_*') """ Merge the polygons back together """ arcpy.Merge_management(WVchunks, 'Poly_WatBod') print( "All raster chunks with data converted to polygons and merged...--" + time.strftime('%Y%m%d--%H%M%S')) reportFile.write("Merge the pieces back together.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """-------- End of Split Processing ---------------------------------------- """ """ Buffer pieces by 50m """ arcpy.Buffer_analysis('Poly_WatBod', 'WatView_Buff', '50 METERS', 'FULL', '', 'NONE') reportFile.write("Buffer the Water Bodies by 50 Meters.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ Project into Albers """ arcpy.Project_management('WatView_Buff', 'WatView_Alb', prjfile) reportFile.write("Project the buffer polygons into Albers.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ Set Environments """ arcpy.env.snapRaster = freqDir + '/Dasy' arcpy.env.extent = freqDir + '/Dasy' """ Extract Daysmetric Pixels """ EbM_P = arcpy.sa.ExtractByMask(freqDir + '/Dasy', 'WatView_Alb') EbM_P.save('Pop_WaterView') print("Dasy pixels extracted for all buffered water polygons...--" + time.strftime('%Y%m%d--%H%M%S')) reportFile.write( "Extract by mask the EnviroAtlas Dasymetric (2011/October 2015) pixels using the projected buffer to produce a raster showing population with potential views of water.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ Calculate the Extracted Dasy Population with Each CBG """ arcpy.sa.ZonalStatisticsAsTable(freqDir + '/BG_Alb', 'bgrp', 'Pop_WaterView', 'BG_WWV', 'DATA', 'SUM') reportFile.write( "Calculate Zonal Statistics as Table for the extracted dasymetrics with the zones being the 2010 block groups for the EnviroAtlas community.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """-------- Calculate Total Dasy Population, if necessary ------------------ """ """ Use the existing data """ fieldNames = [f.name for f in arcpy.ListFields(freqDir + '/BG_Alb')] if 'Dasy_Pop' in fieldNames: reportFile.write( "Calculate Zonal Statistics as a Table for the EnviroAtlas Dasymetrics (2011/October 2015) with the zones being the 2010 block groups within the EnviroAtlas community boundary. Add resulting population sums to the community block groups as attribute Dasy_Pop--Dasy_Pop" + '--\n') """ Create population data """ else: arcpy.AddField_management(freqDir + '/BG_Alb', 'Dasy_Pop', 'LONG') arcpy.sa.ZonalStatisticsAsTable(freqDir + '/BG_Alb', 'bgrp', freqDir + '/Dasy', freqDir + '/Dasy_ZS', '', 'SUM') arcpy.JoinField_management(freqDir + '/BG_Alb', 'bgrp', freqDir + '/Dasy_ZS', 'bgrp', ['SUM']) arcpy.CalculateField_management(freqDir + '/BG_Alb', 'Dasy_Pop', '!SUM!', 'PYTHON_9.3') arcpy.DeleteField_management(freqDir + '/BG_Alb', ['SUM']) arcpy.JoinField_management(freqDir + '/BG', 'bgrp', freqDir + '/BG_Alb', 'bgrp', ['Dasy_Pop']) reportFile.write( "Calculate Zonal Statistics as a Table for the EnviroAtlas Dasymetrics (2011/October 2015) with the zones being the 2010 block groups within the EnviroAtlas community boundary. Add resulting population sums to the community block groups as attribute Dasy_Pop--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') ReuseRF.write("Dasy_Pop--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """-------- Create Final Table --------------------------------------------- """ arcpy.TableToTable_conversion(freqDir + '/BG_Alb', workDir, 'WatWV', '', 'bgrp') arcpy.DeleteField_management('WatWV', [ 'PLx2_Pop', 'PLx2_Pct', 'SUM_HOUSIN', 'NonWhite', 'LandA_M', 'Density', 'under_1', 'under_1pct', 'under_13', 'under_13pc', 'over_70', 'over_70pct', 'Shape_Length', 'Shape_Leng', 'NonWhite_Pop', 'NonWt_Pct', 'Area_M', 'Shape_Le_1', 'Shape_Area', 'ALAND', 'NonWhite_P', 'H_Income_M', 'State' ]) WaterView = 'WatWV' reportFile.write( "Create a new table based on the EnviroAtlas community block groups table retaining the BGRP and Dasy_Pop fields--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ Add fields to new table """ arcpy.AddField_management(WaterView, 'WVW_Pop', 'LONG') arcpy.AddField_management(WaterView, 'WVW_Pct', 'FLOAT', 5, 2) reportFile.write( "Add fields to the new table for WVW_Pop (long), WVW_Pct (float).--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ Join Each Table to the final table and WVW_Pop """ arcpy.JoinField_management(WaterView, 'bgrp', 'BG_WWV', 'bgrp', ['SUM']) arcpy.CalculateField_management(WaterView, 'WVW_Pop', '!SUM!', 'PYTHON_9.3') arcpy.MakeTableView_management(WaterView, 'WaterView_Tbl') arcpy.SelectLayerByAttribute_management('WaterView_Tbl', 'NEW_SELECTION', 'SUM IS NULL') arcpy.CalculateField_management('WaterView_Tbl', 'WVW_Pop', 0, 'PYTHON_9.3') arcpy.SelectLayerByAttribute_management('WaterView_Tbl', 'CLEAR_SELECTION') arcpy.DeleteField_management('WaterView_Tbl', 'SUM') reportFile.write( "Join the zonal statistics table with the new table to calculate the new fields: WVW_Pop = zonal statistics.SUM; remove join--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ Calculate WVW_Pct """ arcpy.SelectLayerByAttribute_management('WaterView_Tbl', 'NEW_SELECTION', 'Dasy_Pop >0') arcpy.CalculateField_management( 'WaterView_Tbl', 'WVW_Pct', '"%.2f" % (float(!WVW_Pop!)/float(!Dasy_Pop!) * 100)', 'PYTHON_9.3') arcpy.SelectLayerByAttribute_management('WaterView_Tbl', 'CLEAR_SELECTION') reportFile.write( "Calculate field WVW_Pct = WVW_Pop / Dasy_Pop * 100 (limited to 2 decimal places).--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ Calculate NULL values, where applicable """ arcpy.SelectLayerByAttribute_management('WaterView_Tbl', 'NEW_SELECTION', 'Dasy_Pop = 0') arcpy.CalculateField_management('WaterView_Tbl', 'WVW_Pct', '-99999', 'PYTHON_9.3') arcpy.CalculateField_management('WaterView_Tbl', 'WVW_Pop', '-99999', 'PYTHON_9.3') arcpy.SelectLayerByAttribute_management('WaterView_Tbl', 'CLEAR_SELECTION') arcpy.DeleteField_management('WaterView_Tbl', [ 'SUM_POP10', 'EAID', 'NonWhite', 'LandA_M', 'Density', 'Dasy_Pop', 'SUM' ]) print( "Statistics for all fields and buffered water bodies calculated...--" + time.strftime('%Y%m%d--%H%M%S')) reportFile.write( "Calculate fields where Dasy_Pop = 0: All Fields = -99999.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """-------- Check that the Analysis Area is covered by the LC -------------- """ """ Create a Polygon Version of the LC """ if arcpy.Exists(freqDir + '/LC_Poly') == False: arcpy.env.snapRaster = freqDir + '/LC' arcpy.env.extent = freqDir + '/LC' ReC = arcpy.sa.Reclassify( str(freqDir) + '/LC', 'Value', arcpy.sa.RemapValue([[0, 0], [10, 1], [20, 1], [21, 1], [22, 1], [30, 1], [40, 1], [52, 1], [70, 1], [80, 1], [82, 1], [91, 1], [92, 1]])) ReC.save(str(freqDir) + '/AreaIO') arcpy.RasterToPolygon_conversion( str(freqDir) + '/AreaIO', str(freqDir) + '/LC_Poly', 'SIMPLIFY') arcpy.EliminatePolygonPart_management( str(freqDir) + '/LC_Poly', str(freqDir) + '/LC_Poly_EP', 'PERCENT', '', '5', 'CONTAINED_ONLY') arcpy.Delete_management(str(freqDir) + '/LC_Poly') arcpy.Rename_management( str(freqDir) + '/LC_Poly_EP', str(freqDir) + '/LC_Poly') """ Buffer the LC Polygon by -500m """ if arcpy.Exists(freqDir + '/Bnd_Cty_500m') == False: arcpy.Buffer_analysis( str(freqDir) + '/Bnd_Cty', str(freqDir) + '/Bnd_Cty_500m', '500 meters') arcpy.EliminatePolygonPart_management( str(freqDir) + '/Bnd_Cty_500m', str(freqDir) + '/Bnd_Cty_500m_EP', 'PERCENT', '', '30', 'CONTAINED_ONLY') arcpy.Delete_management(str(freqDir) + '/Bnd_Cty_500m') arcpy.Rename_management( str(freqDir) + '/Bnd_Cty_500m_EP', str(freqDir) + '/Bnd_Cty_500m') """ Identify whether LC is large enough """ arcpy.MakeFeatureLayer_management(str(freqDir) + '/LC_Poly', 'LClyr') arcpy.MakeFeatureLayer_management( str(freqDir) + '/Bnd_Cty_500m', 'BC_500lyr') arcpy.SelectLayerByLocation_management('BC_500lyr', 'COMPLETELY_WITHIN', 'LClyr', '', 'NEW_SELECTION') bigEnough = float(arcpy.GetCount_management('BC_500lyr').getOutput(0)) arcpy.SelectLayerByAttribute_management('BC_500lyr', 'CLEAR_SELECTION') """ If the LC isn't large enough, edit erroneous BGS """ if bigEnough == 0: """ Identify BGs within 50m of the LC edge """ print( "Land Cover does not extend beyond BG boundary, editing possibly erroneous BGs...--" + time.strftime('%Y%m%d--%H%M%S')) arcpy.Buffer_analysis( str(freqDir) + '/LC_Poly', 'LC_Poly_Minus50', '-50 meters') arcpy.MakeFeatureLayer_management('LC_Poly_Minus50', 'Minus50') arcpy.MakeFeatureLayer_management(freqDir + '/BG', 'BG') arcpy.SelectLayerByLocation_management('BG', 'COMPLETELY_WITHIN', 'Minus50', '', 'NEW_SELECTION', 'INVERT') bgValue = float(arcpy.GetCount_management('BG').getOutput(0)) """ For all BGs too close to the LC edge, assign both fields a value of -99998 """ if bgValue > 0: bgrps = [] cursor = arcpy.SearchCursor('BG') for row in cursor: value = row.getValue('bgrp') bgrps.append(value) bgrps = list(set(bgrps)) expression = '' for bgrp in bgrps: expression = expression + " OR bgrp = '" + str(bgrp) + "'" expression = expression[4:] arcpy.SelectLayerByAttribute_management( 'WaterView_Tbl', 'NEW_SELECTION', expression) arcpy.CalculateField_management('WaterView_Tbl', 'WVW_Pop', '-99998', 'PYTHON_9.3') arcpy.CalculateField_management('WaterView_Tbl', 'WVW_Pct', '-99998', 'PYTHON_9.3') arcpy.SelectLayerByAttribute_management( 'WaterView_Tbl', 'CLEAR_SELECTION') reportFile.write( "Calculate Field for BGs within 50m of the edge of the land cover, WVW_Pop and WVW_Pct = -99998.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ Create final table """ arcpy.CopyRows_management('WaterView_Tbl', 'WaterWV_Fnl') try: arcpy.Delete_management(finalDir + '/' + str(city) + '_WaterWV') except: pass arcpy.TableToTable_conversion('WaterWV_Fnl', finalDir, city + '_WaterWV') allFields = [ f.name for f in arcpy.ListFields(finalDir + '/' + city + '_WaterWV') ] for field in allFields: if field not in ['bgrp', 'OBJECTID', 'WVW_Pop', 'WVW_Pct']: arcpy.DeleteField_management( finalDir + '/' + city + '_WaterWV', [field]) reportFile.write( "Export the fields to be displayed in the EnviroAtlas to a final gdb table. WVW_Pop, WVW_Pct--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') print 'Window Views of Water End Time: ' + time.asctime() + '\n' #-------- COMPELETE LOGFILES --------------------------------------------- reportFile.close() ReuseRF.close() #----------------------------------------------------------------------------- # END ANALYSIS #----------------------------------------------------------------------------- except: """ This part of the script executes if anything went wrong in the main script above """ #-------- PRINT ERRORS --------------------------------------------------- print "\nSomething went wrong.\n\n" print "Python Traceback Message below:" print traceback.format_exc() print "\nArcMap Error Messages below:" print arcpy.GetMessages(2) print "\nArcMap Warning Messages below:" print arcpy.GetMessages(1) #-------- COMPLETE LOGFILE ------------------------------------------------ reportFile.write("\nSomething went wrong.\n\n") reportFile.write("Pyton Traceback Message below:") reportFile.write(traceback.format_exc()) reportFile.write("\nArcMap Error Messages below:") reportFile.write(arcpy.GetMessages(2)) reportFile.write("\nArcMap Warning Messages below:") reportFile.write(arcpy.GetMessages(1)) reportFile.write("\n\nEnded at " + time.asctime() + '\n') reportFile.write("\n---End of Log File---\n") if reportFile: reportFile.close()
def exportToGeoTiff(self, outputPath): arcpy.RasterToOtherFormat_conversion(self.filename, outputPath, "TIFF")
cwu_data = 'U:/grain_storage/data/CropWaterUse_USvalues/' # Collect names for different Crop Water Use (CWU) types based on directory with ESRI GRIDS cwu_types = [ f for f in os.listdir(cwu_data) if os.path.isdir(os.path.join(cwu_data + f)) ] # Iterate over all files and subdirectories in path to collect commodity identifiers for cwu_type in cwu_types: cwu_path = cwu_data + cwu_type commodities = [] for (dirpath, dirnames, filenames) in os.walk(cwu_path): commodities.extend( dirnames ) # add all directory names (commodity names) to commodities list commodities = filter( lambda x: x != 'info', commodities) # remove occurrences of 'info' in commodities list # Iterate over commodities for c in commodities: # Convert ESRI GRID files to TIFF files for all commodities inValueRaster_GRID = cwu_data + cwu_type + '/' + c pathValueRaster = 'U:/grain_storage/data/vwc_tiffs/' + c + '.tif' if os.path.isfile( pathValueRaster): # if TIFF file already exists, grab the file pass else: # if TIFF does not exist, create it arcpy.RasterToOtherFormat_conversion( inValueRaster_GRID, 'U:/grain_storage/data/vwc_tiffs/', 'TIFF')
def globSelectFiles (pattern="*"): import glob return(glob.glob(pattern # globSearchList ---------------------------------------------------------------- # returns a list of files from a list of glob search patterns def globSearchList (extensions = ["*.jp2","*.img"]): # make list of files with the the following extensions import glob files = list() for ext in extensions: files = files + glob.glob(ext) return(files) # >>> ARCPY FUNCTIONS............................................................. # http://pro.arcgis.com/en/pro-app/tool-reference/introduction-anatomy/anatomy-of-a-tool-reference-page.htm # arcDescribeData ----------------------------------------------------------- # returns a dictionary of for each file with data descriptions and projections def arcDescribeData(fileList): import arcpy descriptions = dict() for f in fileList: print(f) dsc = arcpy.Describe(f) prj = dsc.spatialreference descriptions[str(f)]=[dsc,prj] #dictionary return(descriptions) # arcReprojectData ----------------------------------------------------------------- # reprojects a list of data files to a specified output coordinated reference system def arcReprojectData(fileList,outCRS): import arcpy import re for f in fileList: # remove file extensions ext = re.match(".*(\..*)",f) print(ext) if m != None: data = f[:len(ext.group(1))] else: data = f # run the reprojection tool try: if ext=='.shp': arcpy.Project_management(data,"prj"+data,outCRS) if ext!='.shp': arcpy.ProjectRaster_management(data,"prj"+data,outCRS) # print messages when the tool runs successfully print(arcpy.GetMessages(0)) except arcpy.ExecuteError: print(arcpy.GetMessages(2)) except Exception as ex: print(ex.args[0]) # arcSplitShapefileByFeature -------------------------------------------------------- # separate one shapefile in multiple ones by one specific attribute # Need to change Name to a field # debugged? def arcSplitShapefileByFeature (inDir = "C:/Temp/data/",inFile="Points.shp",outDir = "C:/Temp/data/idPoints/",field="Name"): import arcgisscripting # Starts Geoprocessing gp = arcgisscripting.create(9.3) gp.OverWriteOutput = 1 # set input file path inputFile = inDir + inFile # Make the outDir if it doesn't already exist using os import os if not os.path.isdir(outDir): os.mkdir(outDir) # Read Shapefile for different values in the attribute rows = gp.searchcursor(inputFile) row = rows.next() attribute_types = set([]) while row: attribute_types.add(eval("row."+field)) #<-- CHANGE my_attribute to the name of your attribute row = rows.next() # Output a Shapefile for each different attribute for each_attribute in attribute_types: outSHP = outDir + each_attribute + u".shp" print outSHP gp.Select_analysis (inputFile, outSHP, "\""+field+"\" = '" + each_attribute + "'") #<-- CHANGE my_attribute to the name of your attribute del rows, row, attribute_types, gp # arcReformatRaster --------------------------------------------------- # converts list of raster files to a single format in specified path # inFilesList: list of files e.g. # files = ['file.img',file2.jpg','file3.gif'] #http://resources.arcgis.com/en/help/main/10.1/index.html#//001200000032000000 def arcReformatRaster (FileList=["file1.tif","file2.tif"], inPath=None,outPath=None,Format="TIFF"): import arcpy, os if inPath==None: inPath=os.getcwd() if outPath==None: outPath=os.getcwd() inFileList = list() outFileList = list() for f in FileList: inFilePath = inPath+"\\"+f try: ##Convert Multiple Raster Dataset to FGDB arcpy.RasterToOtherFormat_conversion(inFilePath,outPath,Format) except: print "Raster To Other Format Failed." print arcpy.GetMessages() # arcResampleRasters ----------------------------------------------------------------- # takes a list of file names sets cell size to specified scale # FileList: list of strings containing raster file names ["file1.tif","file2.tif"] # inPath(optional): path where input files are located, default is os.getcwd() # outPath(optional): path where output files are created, default is os.getcwd() # scale: You can specify the cell size in 3 different ways: # 1. Using a single number specifying a square cell size # 2. Using two numbers that specify the X and Y cell size, which is space delimited # 3. Using the path of a raster dataset from which the square cell size will be imported # http://pro.arcgis.com/en/pro-app/tool-reference/data-management/resample.htm def arcResampleRasters (FileList=["file1.tif","file2.tif"], inPath=None,outPath=None,resolution="C:\\templateRaster.tif"): import arcpy, os if inPath==None: inPath=os.getcwd() if outPath==None: outPath=os.getcwd() inFileList = list() outFileList = list() for f in FileList: inFilePath = inPath+'\\'+f outFilePath = outPath+"\\scaled_"+f #Rescale Function Call arcpy.Resample_management(inFilePath, outFilePath, resolution,"CUBIC") print("completed rescale of files @ location:", outPath) # arcSaveRasterBands ----------------------------------------------------------------- # saves raster individual bands from a multiband raster file def arcSaveRasterBands (InRaster='C:\\Users\\awiegman\\Downloads\\OtterData\\n_4307316_nw_1_20030912.jp2'): import arcpy, os # get a list of the bands that make up the raster arcpy.env.workspace = InRaster Bands = arcpy.ListRasters() for Bnd in Bands: # loop through the bands and export each one with CopyRaster InBand = '{}\\{}'.format(InRaster,Bnd) bndDesc = arcpy.Describe(InBand) NoData = bndDesc.noDataValue InSplit = os.path.splitext(InRaster) # split the image name and extension # output file name is c:\\some\\path\\raster_Band_X.ext OutRaster = '{}_{}{}'.format(InSplit[0],Bnd,InSplit[1]) arcpy.CopyRaster_management(InBand,OutRaster,nodata_value = NoData) # >>> ARCPY SPATIAL ANALYST FUNCTIONS................................................ # http://pro.arcgis.com/en/pro-app/tool-reference/spatial-analyst/an-overview-of-the-spatial-analyst-toolbox.htm # arcExportNDVI --------------------------------------------------------------------- # export normalized vegetation difference index from multispectral raster image # CURRENTLY CRASHING ArcMap # inRaster: multiband raster image # redBand: band Number where red reflectance is stored # NIRBand: band Number where near infrared reflectance is stored def arcExportNDVI (inRaster,outPath,redBand=3,NIRBand=4): import arcpy, os from arcpy.sa import* red = arcpy.sa.Raster(inRaster+'\\Band_'+str(redBand)) NIR = arcpy.sa.Raster(inRaster+'\\Band_'+str(NIRBand)) # Calculate NDVI as (NIR-red)/(NIR+red) numerator = arcpy.sa.Float(NIR-red) denom = arcpy.sa.Foat(NIR+red) NDVI = arcpy.sa.Divide(num, denom) outRaster = '{}_{}{}'.format(InSplit[0],"NDVI",InSplit[1]) NDVI.Save(outPath+outRaster) # >>> >>> HYDROLOGY FUNCTIONS... # http://pro.arcgis.com/en/pro-app/tool-reference/spatial-analyst/an-overview-of-the-hydrology-tools.htm # basin ------------------------------------------------------------------ # Creates a raster delineating all drainage basins. # fill ------------------------------------------------------------------- # Fills sinks in a surface raster to remove small imperfections in the data. # http://pro.arcgis.com/en/pro-app/tool-reference/spatial-analyst/fill.htm def archyFill(inDEM): return(arcpy.sa.Fill(inDEM)) # flowAccum ----------------------------------------------------------------- # Creates a raster of accumulated flow into each cell. A weight factor can optionally be applied. # http://pro.arcgis.com/en/pro-app/tool-reference/spatial-analyst/flow-accumulation.htm def archyFlowAccum(inFlowDir): return(arcpy.sa.FlowAccumulation(inFlowDir)) # flowDir -------------------------------------------------------------------- # Creates a raster of flow direction from each cell to its downslope neighbor, or neighbors, using D8, Multiple Flow Direction (MFD) or D-Infinity (DINF) methods. # http://pro.arcgis.com/en/pro-app/tool-reference/spatial-analyst/flow-direction.htm def archyFlowDir(inDEM): return(arcpy.sa.FlowDirection(inDEM)) # archyFlowDist ------------------------------------------------------------------- # Computes, for each cell, the horizontal or vertical component of minimum downslope distance, following the flow path(s), to cell(s) on a stream into which they flow. # archyFlowLen -------------------------------------------------------------------- # Calculates the upstream or downstream distance, or weighted distance, along the flow path for each cell. # archySink ----------------------------------------------------------------------- # Creates a raster identifying all sinks or areas of internal drainage. # archySnapPrPnt -------------------------------------------------------------------- # Snaps pour points to the cell of highest flow accumulation within a specified distance. def archySnapPrPnt(inPoint,inFlowAccum, searchDist=30, pourField="Name"): return(arcpy.sa.SnapPourPoint(inPoint, inFlowAccum, searchDist, pourField)) \ # archyStreamLink --------------------------------------------------------------- # Assigns unique values to sections of a raster linear network between intersections. # archyStreamOrder --------------------------------------------------------------- # Assigns a numeric order to segments of a raster representing branches of a linear network. # archyStreamToFeature ----------------------------------------------------------- # Converts a raster representing a linear network to features representing the linear network. # archyWatershed ------------------------------------------------------------------ # Determines the contributing area above a set of cells in a raster. def archyWatershed(inPoint,inFlowDir,pourField): return(arcpy.sa.Watershed(inFlowDir, inPoint, pourField)) # MAIN PROGRAM ########################################################### # load modules import os # set workspace arcpyWorkspace("C:\\Users\\awiegman\\Downloads\\OtterData\\") # resample DEM rasters to 0.7 m resolution files = globSearchList(["*.jp2"]) # select raster files resolution = 0.7 arcResampleRasters(FileList=files,inPath="C:\\Users\\awiegman\\Downloads\\OtterData",outPath="C:\\Temp\\Rescaled",resolution=resolution) # reformat the files to GeoTiff files = globSearchList(["*.jp2"]) arcReformatRaster(FileList=files,outPath="C:\\Temp\\Rescaled",Format="TIFF") # resample color imagery rasters to 5 m resolution arcpyWorkspace("C:\\Users\\awiegman\\Downloads\\OtterData\\NAIP_imagery") files = globSearchList(["*.jp2"]) # select raster files resolution = 10 arcResampleRasters(FileList=files,inPath="C:\\Users\\awiegman\\Downloads\\OtterData\\NAIP_imagery",outPath="C:\\Temp\\Rescaled",resolution=resolution) # save Raster Bands arcpyWorkspace("C:\\Temp\\Rescaled") files = globSearchList(["*.jp2"]) for f in files: raster = arcpy.ListRasters("", "tif") saveRasterBands(raster) # calculate NDVI from raster bands and export result arcpyWorkspace("C:\\Users\\awiegman\Downloads\\OtterData\\NAIP_imagery") arcpyWorkspace("C:\\Temp\\Rescaled") arcpyWorkspace() files = globSearchList(["*.jp2"]) for f in files: raster = os.getcwd()+ "\\" + f exportNDVI(inRaster=raster)
def ImpP(city, inDir, workFld): import traceback, time, arcpy, os from arcpy import env arcpy.CheckOutExtension('Spatial') #-------- DIRECTORY SETUP ------------------------------------------------ """ Working Directory """ try: arcpy.CreateFileGDB_management(str(workFld), str(city) + '_ImpProx.gdb') except: pass workGDB = str(workFld) + '/' + str(city) + '_ImpProx.gdb' """ Window Views of Water Directory """ try: arcpy.CreateFileGDB_management(str(workFld), str(city) + '_WaterWV.gdb') except: pass rgGDB = str(workFld) + '/' + str(city) + '_WaterWV.gdb' """ Report File Directory """ reportfileDir = str(workFld) + '/Logs' """ Frequent Directory """ freqDir = str(workFld) + '/' + city + '_Freq.gdb' """ Final Geodatabase """ finalDir = str(workFld) + '/' + city + '_Final.gdb' """ Projection File Directory """ prjDir = str(inDir) + '/Prj' """ Split Raster Directory """ if os.path.isdir(str(workFld) + '/' + city + '_Split') == True: pass else: os.makedirs(str(workFld) + '/' + city + '_Split') splitDir = str(workFld) + '/' + city + '_Split' """ Set Workspace Environments """ arcpy.env.workspace = workGDB arcpy.env.scratch = str(inDir) + '/Scratch.gdb' arcpy.env.overwriteOutput = True #----------------------------------------------------------------------------- # BEGIN ANALYSIS #----------------------------------------------------------------------------- try: #-------- LOGFILE CREATION --------------------------------------------- """ Create report file for each metric """ tmpName = city + '_ImpProx_' + time.strftime('%Y%m%d_%H-%M') reportfileName = reportfileDir + '/' + tmpName + '.txt' reportFile = open(reportfileName, 'w') try: loglist = sorted(f for f in os.listdir(reportfileDir) if f.startswith(str(city) + '_Reuse')) tmpName = loglist[-1] except: tmpName = city + '_Reuse_' + time.strftime('%Y%m%d_%H-%M') + '.txt' reportfileName = reportfileDir + '/' + tmpName try: ReuseRF = open(reportfileName, 'a') except: ReuseRF = open(reportfileName, 'w') print 'Creating Reuse Log' """ Write out first line of report file """ print 'Impervious Proximity Start Time: ' + time.asctime() reportFile.write( "Begin with EnviroAtlas 1-meter Land Cover for the EnviroAtlas community--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') #-------- PROCESSING LAYERS ---------------------------------------------- """ Set Environments """ arcpy.env.extent = str(freqDir) + '/LC' arcpy.env.snapRaster = str(freqDir) + '/LC' """ Set Projection Files """ prjfileALB = prjDir + '/USA Contiguous Albers Equal Area Conic USGS.prj' prjNumb = arcpy.Describe(str(freqDir) + '/LC').spatialReference.name prjNumb = prjNumb[-3:] prjfileUTM = prjDir + '/NAD 1983 UTM Zone ' + prjNumb + '.prj' """-------- Reclassify LC into Binary Impervious ----------------------------- """ if arcpy.Exists(freqDir + '/ImpIO') == False: #Run the Imp Space reclassification outReclass5 = arcpy.sa.Reclassify( str(freqDir) + '/LC', 'Value', arcpy.sa.RemapValue([[0, 0], [10, 0], [20, 1], [21, 1], [22, 1], [30, 0], [40, 0], [52, 0], [70, 0], [80, 0], [82, 0], [91, 0], [92, 0]])) outReclass5.save(freqDir + '/ImpIO') del outReclass5 reportFile.write( "Reclassify the Land Cover into Binary Impervious. (Impervious - 20 = 1; All Else = 0)--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') ReuseRF.write("ImpIO--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') else: reportFile.write( "Reclassify the Land Cover into Binary Impervious. (Impervious - 20 = 1; All Else = 0)--ImpIO" + '--\n') """-------- Create 1001m Moving Window ------------------------------------- """ outFocalStat4 = arcpy.sa.FocalStatistics( freqDir + '/ImpIO', arcpy.sa.NbrRectangle(1001, 1001, 'CELL'), 'SUM', 'NODATA') outFocalStat4.save('Imp_1001R') arcpy.CalculateStatistics_management('Imp_1001R') del outFocalStat4 print("1001m moving winow complete... " + time.asctime()) reportFile.write( "Compute Focal Statistics on the Impervious Binary with 1001 pixel wide by 1001 pixel high rectangular window with statistics type = sum.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """-------- Reclassify Moving Window into 20% Breaks ----------------------- """ outReclass3 = arcpy.sa.Reclassify( 'Imp_1001R', 'Value', arcpy.sa.RemapRange([[0, 200400.2, 20], [200400.2, 400800.4, 40], [400800.4, 601200.6, 60], [601200.6, 801600.80, 80], [801600.8, 1002001, 100]]), 'NODATA') outReclass3.save('Imp_Pct') del outReclass3 print("1001m moving winow reclassification complete... " + time.asctime()) reportFile.write( "Reclassify the Focal Statistics into 5 classes at 20% intervals.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """-------- Check that the Analysis Area is covered by the LC -------------- """ """ Create a Polygon Version of the LC """ if arcpy.Exists(freqDir + '/LC_Poly') == False: arcpy.env.extent = freqDir + '/LC' arcpy.env.snapRaster = freqDir + '/LC' ReC = arcpy.sa.arcpy.sa.Reclassify( str(freqDir) + '/LC', 'Value', arcpy.sa.RemapValue([[0, 0], [10, 1], [20, 1], [21, 1], [22, 1], [30, 1], [40, 1], [52, 1], [70, 1], [80, 1], [82, 1], [91, 1], [92, 1]])) ReC.save(str(freqDir) + '/AreaIO') del ReC arcpy.RasterToPolygon_conversion( str(freqDir) + '/AreaIO', str(freqDir) + '/LC_Poly', 'SIMPLIFY') arcpy.EliminatePolygonPart_management( str(freqDir) + '/LC_Poly', str(freqDir) + '/LC_Poly_EP', 'PERCENT', '', '5', 'CONTAINED_ONLY') arcpy.Delete_management(str(freqDir) + '/LC_Poly') arcpy.Rename_management( str(freqDir) + '/LC_Poly_EP', str(freqDir) + '/LC_Poly') """ Buffer the LC Polygon by -500m """ if arcpy.Exists(freqDir + '/Bnd_Cty_500m') == False: arcpy.Buffer_analysis( str(freqDir) + '/Bnd_Cty', str(freqDir) + '/Bnd_Cty_500m', '500 meters') arcpy.EliminatePolygonPart_management( str(freqDir) + '/Bnd_Cty_500m', str(freqDir) + '/Bnd_Cty_500m_EP', 'PERCENT', '', '30', 'CONTAINED_ONLY') arcpy.Delete_management(str(freqDir) + '/Bnd_Cty_500m') arcpy.Rename_management( str(freqDir) + '/Bnd_Cty_500m_EP', str(freqDir) + '/Bnd_Cty_500m') """ Identify whether LC is large enough """ arcpy.MakeFeatureLayer_management(str(freqDir) + '/LC_Poly', 'LClyr') arcpy.MakeFeatureLayer_management( str(freqDir) + '/Bnd_Cty_500m', 'BC_500lyr') arcpy.SelectLayerByLocation_management('BC_500lyr', 'COMPLETELY_WITHIN', 'LClyr', '', 'NEW_SELECTION') bigEnough = float(arcpy.GetCount_management('BC_500lyr').getOutput(0)) arcpy.SelectLayerByAttribute_management('BC_500lyr', 'CLEAR_SELECTION') """ Create the Water Binary, if necessary """ if arcpy.Exists(freqDir + '/WaterIO') == True: reportFile.write( "Create a water binary from the 1-M EnviroAtlas Land Cover. (Water - 10 = 1; All Else = 0).--WaterIO" + '--\n') else: outReclass3 = arcpy.sa.Reclassify( freqDir + '/LC', 'Value', arcpy.sa.RemapValue([[0, 0], [10, 1], [20, 0], [21, 0], [22, 0], [30, 0], [40, 0], [52, 0], [70, 0], [80, 0], [82, 0], [91, 0], [92, 0]])) outReclass3.save(freqDir + '/WaterIO') del outReclass3 reportFile.write( "Create a water binary from the 1-M EnviroAtlas Land Cover. (Water - 10 = 1; All Else = 0.)--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') ReuseRF.write("WaterIO--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ Create the Water Region Group, if necessary """ if arcpy.Exists(rgGDB + '/WatIO_300') == True: reportFile.write( "Run the region group tool to group waterbodies together with options number of neighbors to use: EIGHT, zone grouping method: WITHIN, and ADD_LINK (add link field to output).--WaterRG" + '--\n') else: RG = arcpy.sa.RegionGroup(freqDir + '/WaterIO', 'EIGHT', 'WITHIN', 'ADD_LINK') RG.save(rgGDB + '/WatIO_300') del RG arcpy.AddField_management(rgGDB + '/WatIO_300', 'Count_2', 'DOUBLE') arcpy.CalculateField_management(rgGDB + '/WatIO_300', 'Count_2', '!COUNT!', 'PYTHON') reportFile.write( "Run the region group tool to group waterbodies together with options number of neighbors to use: EIGHT, zone grouping method: WITHIN, and ADD_LINK (add link field to output).--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') ReuseRF.write("WaterRG--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ Limit the WatIO_300 to the Extent of Imp_Pct """ ## if bigEnough == 0: ## EbM = arcpy.sa.ExtractByMask(rgGDB + '/WatIO_300', 'Imp_Pct') ## EbM.save('WatRG_Lim_1001') ## del EbM ## print("Extract by mask complete... " + time.asctime()) ## reportFile.write("Restrict the extent of the water bodies raster to that of the percent impervious raster.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') if bigEnough == 0: desc = arcpy.Describe('Imp_Pct') rectangle = "%s %s %s %s" % (desc.extent.XMin, desc.extent.YMin, desc.extent.XMax, desc.extent.YMax) IP01 = arcpy.sa.Reclassify('Imp_Pct', 'Value', arcpy.sa.RemapRange([[0, 100, 1]]), 'NODATA') arcpy.RasterToPolygon_conversion(IP01, 'Imp_Pct_poly', "NO_SIMPLIFY", "Value") del IP01 arcpy.Clip_management(rgGDB + '/WatIO_300', rectangle, 'WatRG_Lim_251', 'Imp_Pct_poly', '#', 'ClippingGeometry') reportFile.write( "Restrict the extent of the water bodies raster to that of the percent impervious raster.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ Burn water into the Imp_Pct """ if bigEnough == 0: burnwater = arcpy.sa.Con('WatRG_Lim_1001', '-99999', 'Imp_Pct', 'Count_2 > 299 AND LINK = 1') else: burnwater = arcpy.sa.Con(rgGDB + '/WatIO_300', '-99999', 'Imp_Pct', 'Count_2 > 299 AND LINK = 1') burnwater.save('Imp_Pct_Wat') del burnwater print("Water burned into Imp_Pct... " + time.asctime()) reportFile.write( "Using the region group output, burn water bodies over 300m2 into the impervious percent raster using a conditional statement if Region Group Count > 299 AND Link = 1; for true: -99999; for false: impervious percent raster--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ Convert to Esri GRID format """ try: arcpy.Delete_management(splitDir + '/imp_pct_wat') except: pass arcpy.RasterToOtherFormat_conversion('Imp_Pct_Wat', splitDir, 'GRID') reportFile.write("Convert the raster into Esri GRID format--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """-------- Split the Raster As Needs, Process Each Piece ----------------- """ """ Check if the raster should be split """ columns = arcpy.GetRasterProperties_management( 'Imp_Pct_Wat', 'COLUMNCOUNT').getOutput(0) xsplit = int(float(columns) / 40000) + 1 rows = arcpy.GetRasterProperties_management('Imp_Pct_Wat', 'ROWCOUNT').getOutput(0) ysplit = int(float(rows) / 40000) + 1 """--------- Clip the EA Boundaries to the County Lines, if necessary ----- """ if arcpy.Exists(str(freqDir) + '/Bnd_Cty') == False: """ Copy Counties to Frequent and Project """ arcpy.MakeFeatureLayer_management( str(inDir) + '/Input.gdb/Counties_Alb', 'Cty') arcpy.SelectLayerByLocation_management('Cty', 'CONTAINS', 'BG_Alb', '', 'NEW_SELECTION') arcpy.FeatureClassToFeatureClass_conversion( str(inDir) + '/Input.gdb/Counties_Alb', str(freqDir), 'Counties_Alb') arcpy.SelectLayerByAttribute_management('Cty', 'CLEAR_SELECTION') descLC = arcpy.Describe(str(freqDir) + '/LC') arcpy.Project_management('Counties_Alb', 'Counties', descLC.spatialReference) """ Clip Boundary to County Lines """ arcpy.Clip_analysis( str(freqDir) + '/Bnd', str(freqDir) + '/Counties', str(freqDir) + '/Bnd_Cty') reportFile.write( "Clip the EnviroAtlas Community Boundary to the county lines for the community to limit the output to land area.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') ReuseRF.write("Bnd_Cty--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') else: reportFile.write( "Clip the EnviroAtlas Community Boundary to the county lines for the community to limit the output to land area.--Bnd_Cty" + '--\n') """-------- If no split, run the analysis --------------------------------- """ if xsplit * ysplit == 1: """ Convert Raster to Polygon """ arcpy.RasterToPolygon_conversion('Imp_Pct_Wat', 'Poly_Imp', 'NO_SIMPLIFY') reportFile.write( "Convert the raster into a polygon feature class.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ Clip the polygons to Bnd_Cty """ arcpy.Clip_analysis('Poly_Imp', freqDir + '/Bnd_Cty', 'Clip_Imp') reportFile.write( "Clip the polygon feature class to the clipped EnviroAtlas community boundary.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ Dissolve the polygons """ arcpy.Dissolve_management('Clip_Imp', 'ImpDiss', 'gridcode') reportFile.write( "Dissolve the clipped polygons based on grid_code.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """-------- If split, run the analysis on each piece and recombine --------- """ else: """ Delete the raster, if necessary """ xy = (xsplit * ysplit) for rast in range(xy): try: arcpy.Delete_management(splitDir + '/impp_' + str(rast)) except: pass """ Split the Raster """ print("Raster too big, splitting into " + str(xy) + " pieces... " + time.asctime()) arcpy.SplitRaster_management(splitDir + '/imp_pct_wat', splitDir, 'impp_', 'NUMBER_OF_TILES', 'GRID', '', str(xsplit) + ' ' + str(ysplit)) print("Raster split complete... " + time.asctime()) reportFile.write( "Split the raster into pieces of no more than 40,000x40,000 pixels.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ For each raster: """ for Chunk in range(0, xy): try: result = float( arcpy.GetRasterProperties_management( splitDir + '/impp_' + str(Chunk), 'MEAN').getOutput(0)) """ If the raster piece has data: """ if (result <> 0): """ Set Environments """ arcpy.env.snapRaster = 'Imp_1001R' arcpy.env.extent = 'Imp_1001R' """ Copy the piece back to the Working Directory """ arcpy.RasterToOtherFormat_conversion( splitDir + '/impp_' + str(Chunk), workGDB) """ Convert Raster to Polygon """ arcpy.RasterToPolygon_conversion( 'impp_' + str(Chunk), 'ImpPoly_' + str(Chunk), 'NO_SIMPLIFY') """ Clip the polygons to Bnd_Cty """ arcpy.Clip_analysis('ImpPoly_' + str(Chunk), freqDir + '/Bnd_Cty', 'ImpClip_' + str(Chunk)) """ Dissolve the polygons """ arcpy.Dissolve_management('ImpClip_' + str(Chunk), 'ImpD1_' + str(Chunk), 'gridcode') print("Processed Chunk " + str(Chunk) + " / " + str(xy) + "..." + time.asctime()) else: pass except: pass reportFile.write( "Convert each of the raster pieces into a polygon feature class.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') reportFile.write( "Clip the polygon feature classes to the clipped EnviroAtlas community boundary.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') reportFile.write("Dissolve each piece based on grid_code.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ Merge the polygons back together """ fcList = arcpy.ListFeatureClasses('ImpD1*') arcpy.Merge_management(fcList, 'ImpDiss') reportFile.write("Merge the pieces back together.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ Dissolve the pieces again """ arcpy.Dissolve_management('ImpDiss', 'ImpProx_UTM', 'gridcode') reportFile.write("Dissolve again based on grid_code.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ Change gridcode to ImpProxP """ arcpy.AlterField_management('ImpProx_UTM', 'gridcode', 'ImpProxP') reportFile.write("Rename field 'gridcode' into field 'ImpProxP'--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ If the LC isn't large enough, edit erroneous BGS """ if bigEnough == 0: ## """ Extract the area where the boundary extends beyond the analysis area """ ## arcpy.Erase_analysis(freqDir + '/Bnd_Cty', 'ImpProx_UTM', 'EE_Area') ## arcpy.AddField_management('EE_Area', 'ImpProxP', 'LONG') ## arcpy.CalculateField_management('EE_Area', 'ImpProxP', -99998, 'PYTHON_9.3') ## """ Append the non-analyzed area onto the main output """ ## arcpy.Append_management('EE_Area', 'ImpProx_UTM') ## """ Dissolve the output, again """ ## arcpy.Dissolve_management('ImpProx_UTM', 'ImpProx_EE_D', 'ImpProxP') ## reportFile.write("Due to the extent of the Land Cover, the analysis area is smaller than the EnviroAtlas Community Boundary. Extract the area within the boundary that is not within the analysis area, add field 'ImpProxP' = -99998 and append on to the output file. Dissolve based on ImpProxP.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ Buffer each GreClip feature by 1 cm and erase from Bnd_Cty """ fcList = arcpy.ListFeatureClasses('ImpClip*') for idx, fc in enumerate(fcList): arcpy.Buffer_analysis(fc, (fc + "_1cmbuff"), "1 Centimeters", "FULL", "ROUND", "ALL") #arcpy.Buffer_analysis(fc + "_inbuff", fc + "outbuff", "1 Centimeter", "FULL", "ROUND", "ALL") if idx == 0: arcpy.Erase_analysis(freqDir + '/Bnd_Cty', (fc + "_1cmbuff"), 'EE_Area_' + str(idx)) else: arcpy.Erase_analysis('EE_Area_' + str(idx - 1), fc + "_1cmbuff", 'EE_Area_' + str(idx)) a = idx print("Processed Chunk " + str(Chunk) + " / " + str(xy) + "..." + time.asctime()) arcpy.Buffer_analysis('EE_Area_' + str(a), 'EE_Area_2cmbuff', '2 Centimeters', "FULL", "ROUND", "ALL") arcpy.Clip_analysis('EE_Area_2cmbuff', freqDir + '/Bnd_Cty', 'EE_Area') arcpy.AddField_management('EE_Area', 'ImpProxP', 'LONG') arcpy.CalculateField_management('EE_Area', 'ImpProxP', -99998, 'PYTHON_9.3') """ Append the non-analyzed area onto the main output """ arcpy.Append_management('EE_Area', 'ImpProx_UTM') """ Dissolve the output, again """ arcpy.Dissolve_management('ImpProx_UTM', 'ImpProx_EE_D', 'ImpProxP') print("Big enough workflow complete... " + time.asctime()) reportFile.write( "Due to the extent of the Land Cover, the analysis area is smaller than the EnviroAtlas Community Boundary. Extract the area within the boundary that is not within the analysis area, add field 'ImpProxP' = -99998 and append on to the output file. Dissolve based on ImpProxP.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ Project the output """ try: arcpy.Project_management('ImpProx_EE_D', city + '_ImpProx', prjfileALB) except: arcpy.Project_management('ImpProx_UTM', city + '_ImpProx', prjfileALB) reportFile.write("Project the feature class into Albers.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ Add to Final Directory """ arcpy.FeatureClassToFeatureClass_conversion(city + '_ImpProx', finalDir, city + '_ImpProx') reportFile.write("Copy the feature class to the final geodatabase.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') print 'Impervious Proximity End Time: ' + time.asctime() + '\n' #-------- COMPELETE LOGFILES --------------------------------------------- reportFile.close() ReuseRF.close() #----------------------------------------------------------------------------- # END ANALYSIS #----------------------------------------------------------------------------- except: """ This part of the script executes if anything went wrong in the main script above """ #-------- PRINT ERRORS --------------------------------------------------- print "\nSomething went wrong.\n\n" print "Python Traceback Message below:" print traceback.format_exc() print "\nArcMap Error Messages below:" print arcpy.GetMessages(2) print "\nArcMap Warning Messages below:" print arcpy.GetMessages(1) #-------- COMPLETE LOGFILE ------------------------------------------------ reportFile.write("\nSomething went wrong.\n\n") reportFile.write("Pyton Traceback Message below:") reportFile.write(traceback.format_exc()) reportFile.write("\nArcMap Error Messages below:") reportFile.write(arcpy.GetMessages(2)) reportFile.write("\nArcMap Warning Messages below:") reportFile.write(arcpy.GetMessages(1)) reportFile.write("\n\nEnded at " + time.asctime() + '\n') reportFile.write("\n---End of Log File---\n") if reportFile: reportFile.close()
# Set a new workspace, overriding the passed-down workspace arcpy.env.workspace = "D:\Python-Class" arcpy.env.scratchWorkspace = "D:\Python-Class" arcpy.AddMessage("The new current workspace is: %s" % arcpy.env.workspace) arcpy.AddMessage("The new scratch workspace is: %s" % arcpy.env.scratchWorkspace) # Task 1 - Set the geoprocessing environment extent to, hint: find the command needed at this link: # http://desktop.arcgis.com/en/arcmap/10.5/tools/environments/what-is-a-geoprocessing-environment.htm # Top - 147999.685356 # Bottom - 147554.685356 # Left - 320172.363053 # Right - 320778.363053 # BTW, we are using the spatial reference: NAD_1983_Rhode_Island_ft for this exercise arcpy.env.extent = arcpy.Extent(320172.363053, 147554.685356, 320778.363053, 147999.685356) # Task 2 - With the environment set undertake a simple processing operation on the data provided # in Step_4_data.zip by editing the code: # input_raster = r"" # output_workspace = "" # raster_format = "TIFF" # arcpy.RasterToOtherFormat_conversion(input_raster, output_workspace, raster_format) input_raster = r"D:\Python-Class\Class5Step4\0320001450.JP2" output_workspace = r"D:\Python-Class" raster_format = "TIFF" arcpy.RasterToOtherFormat_conversion(input_raster, output_workspace, raster_format)
print(arcpy.GetMessages(2)) except Exception as ex: print(ex.args[0]) # Process: Set the NULL value for Raster image try: # Execute Set NULL outSetNull = SetNull(Kernel, Kernel, "VALUE = 0") # Save the output outSetNull.save(Null) # Execute conversion arcpy.RasterToOtherFormat_conversion(Null, ScratchFolder, "TIFF") # Create a raster with 6 Natural class breaks using Slice module outSlice = Slice(Null, "3", "NATURAL_BREAKS", "1") #outSlice.save(Slice) outSlice.save("Slice") except arcpy.ExecuteError: print(arcpy.GetMessages(2)) except Exception as ex: print(ex.args[0]) try: # Create a polygon from the raster with class breaks #arcpy.RasterToPolygon_conversion(Slice, Reclass,"NO_SIMPLIFY","VALUE")
except arcpy.ExecuteError: print(arcpy.GetMessages(2)) except Exception as ex: print(ex.args[0]) # Process: Set the NULL value for Raster image try: # Execute Set NULL outSetNull = SetNull(Kernel, Kernel, "VALUE = 0") # Save the output outSetNull.save(outName) # Execute conversion arcpy.RasterToOtherFormat_conversion(outName, outFolder, "TIFF") except arcpy.ExecuteError: print(arcpy.GetMessages(2)) except Exception as ex: print(ex.args[0]) # Delete extraneous Temp files def DelTemp(w, x, y, z): try: arcpy.Delete_management(w) print(arcpy.GetMessages(0)) arcpy.Delete_management(x) print(arcpy.GetMessages(0))
#Set working environment arcpy.env.workspace = rasterFolder sr = arcpy.SpatialReference("ETRS 1989 UTM Zone N32") path = rasterFolder #Walk through all the folders and sub-folders for root, dirs, files in os.walk(path): arcpy.env.workspace = root #List raster files in the folder files = arcpy.ListRasters() for ThisRas in files: #Define extent of rasters arcpy.DefineProjection_management(ThisRas, sr) arcpy.AddMessage("Raster %s Projected" % ThisRas) rasDesc = arcpy.Describe(ThisRas) rasExt = rasDesc.extent #Compare the extent of the raster and shapefile to select those that fall within if rasExt.disjoint(shapeExt): arcpy.AddMessage("Raster %s fall outside" % (ThisRas)) else: arcpy.AddMessage("Raster %s falls within" % (ThisRas)) outFile = os.path.join(outputFolder, ThisRas) arcpy.RasterToOtherFormat_conversion(ThisRas, outputFolder, Raster_Format="JPEG") arcpy.AddMessage("%s converted to JPEG " % (ThisRas))
print "Importing LAS to " + ground_feature_dataset + " ..." out_fclass = output_gdb_location + "\\" + ground_feature_dataset + "\\g_" + shortname arcpy.LASToMultipoint_3d(lasfile, out_fclass, cell_size, "2", "ANY_RETURNS", "", "", "las") print "Interpolating " + out_fclass + " ..." outgrid = output_gdb_location + "\\DTM_" if arcpy.Exists(out_fclass): arcpy.gp.NaturalNeighbor_sa(out_fclass, "Shape.Z", outgrid, cell_size) print "Creating a nDSM ....." outgrid = output_gdb_location + "\\nDSM_" + shortname grid1 = output_gdb_location + "\\DSM_" grid2 = output_gdb_location + "\\DTM_" if arcpy.Exists(grid1): if arcpy.Exists(grid2): arcpy.Minus_3d(grid1, grid2, outgrid) print "Exporting TIFF ...." if arcpy.Exists(outgrid): arcpy.RasterToOtherFormat_conversion(outgrid, tiff_folder, "TIFF") except Exception as ex: print(ex.message) print 'END ' + time.ctime() seconds = time.time() - start_time m, s = divmod(seconds, 60) h, m = divmod(m, 60) print "Time elapsed: " + "%d:%02d:%02d" % (h, m, s)
os.mkdir(folder) start_time = datetime.datetime.now() print "Start Time: " + start_time.ctime() out_folder = in_folder + os.sep + 'Grids_byProjection' create_folder(out_folder) list_gdb = os.listdir(in_folder) list_gdb = [gdb for gdb in list_gdb if gdb.endswith('gdb')] print list_gdb for gdb in list_gdb: in_gdb = in_folder + os.sep + gdb out_location = out_folder + os.sep + gdb.replace('.gdb', '') if os.path.exists(out_location): continue create_folder(out_location) arcpy.env.workspace = in_gdb raster_list = arcpy.ListRasters() start_conversion = datetime.datetime.now() print 'Starting the conversion for {0} in {1}\n'.format(raster_list, gdb) arcpy.RasterToOtherFormat_conversion(raster_list, out_location, "GRID") print 'Completed conversion in: {0}\n'.format( (datetime.datetime.now() - start_conversion)) end = datetime.datetime.now() print "End Time: " + end.ctime() elapsed = end - start_time print "Elapsed Time: " + str(elapsed)
def IntDen(city, inDir, workFld): import traceback, time, arcpy, os from arcpy import env arcpy.CheckOutExtension('Spatial') #-------- DIRECTORY SETUP ------------------------------------------------ """ Working Directory """ try: arcpy.CreateFileGDB_management(str(workFld), str(city) + '_IntDen.gdb') except: print 'IntDen GDB already exists' workDir = str(workFld) + '/' + str(city) + '_IntDen.gdb' """ Report File Directory """ reportfileDir = str(workFld) + '/Logs' """ Frequent Directory """ freqDir = str(workFld) + '/' + city +'_Freq.gdb' """ Final Geodatabase """ finalDir = str(workFld) + '/' + city + '_Final.gdb' """ Projection File Directory """ prjDir = str(inDir) + '/Prj' prjfileALB = prjDir + '/USA Contiguous Albers Equal Area Conic USGS.prj' prjfileWM = prjDir + '/WGS 1984 Web Mercator (auxiliary sphere).prj' if os.path.isdir(str(workFld) + '/' + city + '_Split') == True: pass else: os.makedirs(str(workFld) + '/' + city + '_Split') splitDir = str(workFld) + '/' + city + '_Split' """ Split Raster Directory """ if os.path.isdir(str(workFld) + '/' + city + '_Split') == True: pass else: os.makedirs(str(workFld) + '/' + city + '_Split') splitDir = str(workFld) + '/' + city + '_Split' """ RdsForIntDen Directory """ rdsIntDen = inDir + '/Input.gdb/RdsForIntDen' """ Set Workspace Environments """ arcpy.env.workspace = workDir arcpy.env.scratch = str(inDir) + '/Scratch.gdb' arcpy.env.overwriteOutput = True #----------------------------------------------------------------------------- # BEGIN ANALYSIS #----------------------------------------------------------------------------- try: #-------- LOGFILE CREATION --------------------------------------------- """ Create report file for each metric """ tmpName = city + '_IntDen_' + time.strftime('%Y%m%d_%H-%M') reportfileName = reportfileDir + '/' + tmpName + '.txt' reportFile = open(reportfileName, 'w') """ Write out first line of report file """ print 'Intersection Density Start Time: ' + time.asctime() reportFile.write("Begin with the 2011 NavTeq Streets layer.--201701" + '--\n') reportFile.write("Limit streets to walkable roads: FUNC_CLASS < 2; SPEED_CAT < 3 AND > 7; AR_PEDEST = 'Y'.--201701" + '--\n') reportFile.write("Convert multipart roads to single part roads.--201701" + '--\n') reportFile.write("Use the 2011 NavTeq LandUseA and LandUseB to identify road segments within airports, amusement parks, beaches, cemeteries, hospitals, industrial complexes, military bases, railyards, shopping centers, and golf course. Remove these roads if they have no street name value (ST_NAME).--201701" + '--\n') reportFile.write("Calculate new variable in the road layer for the direction of travel (0 for two ways, 1 for one way): Clps_Fld (SHORT) = 0 if DIR_TRAVEL=B, otherwise = 1--201701" + '--\n') #-------- PROCESSING LAYERS ---------------------------------------------- """ Set Environments """ arcpy.env.snapRaster = freqDir + '/LC' arcpy.env.extent = freqDir + '/LC' """-------- Finish Roads Prep ----------------------------- """ """ Clip RdsForIntDen """ arcpy.Clip_analysis(rdsIntDen, freqDir + '/Bnd_5km', 'RdsForIntDen') reportFile.write("Clip the National Intersection Density Roads layer to the 5km buffer of the EnviroAtlas community.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ Project into Albers """ arcpy.Project_management('RdsForIntDen', 'RdsForIntDen_Alb', prjfileALB) reportFile.write("Project the clipped roads into Albers.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ Merge Divided Roads """ arcpy.MultipartToSinglepart_management('RdsForIntDen_Alb', 'RdsForIntDen_SglPrt') arcpy.MergeDividedRoads_cartography('RdsForIntDen_SglPrt', "Clps_Fld", "30 meters", "PreppedRoads") reportFile.write("Merge divided roads into one centered segment.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ Unsplit Lines """ arcpy.UnsplitLine_management('PreppedRoads', 'UnSplitRds', "ST_NAME") reportFile.write("Unsplit roads by street name.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ Create Intersections """ arcpy.Intersect_analysis(['UnSplitRds','UnSplitRds'], 'Intersections', "ONLY_FID",'',"POINT") reportFile.write("Create intersection points by intersecting the roads with themselves with output = point.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ Delete multiples """ arcpy.DeleteIdentical_management('Intersections', "Shape") reportFile.write("Delete identical points.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ Run Density Calculation """ den = arcpy.sa.KernelDensity('Intersections', "NONE", 10, 750, "SQUARE_KILOMETERS") den.save(workDir + "/IntDen_Raw") reportFile.write("Use the spatial analyst kernel density tool with a cell size of 10m, search radius of 750m and scale factor of square kilometers.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ Clip to the boundary and convert 0 to Null """ arcpy.Clip_management("IntDen_Raw", '', city + "_IntDen", freqDir + '/Bnd', '0', 'ClippingGeometry', '') reportFile.write("Clip the raster to the community boundary and convert 0 values to No Data.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ Convert to TIFF for publishing """ arcpy.RasterToOtherFormat_conversion(city + "_IntDen", splitDir, "TIFF") reportFile.write("Convert the raster to TIFF format for use in EnviroAtlas.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ Convert to polygons for posting """ newID = arcpy.sa.Reclassify('IntDen_Raw', 'VALUE', arcpy.sa.RemapRange([[0,20,20], [20,40,40], [40,60,60], [60,80,80], [80,100,100], [100,150,150], [150,200,200], [200,250,250]]), 'NODATA') newID.save('IntDen_ReC') reportFile.write("Reclassify the Raster into Qualitative Breaks.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') arcpy.RasterToPolygon_conversion('IntDen_ReC', 'IntDen_Poly', 'SIMPLIFY', 'VALUE') reportFile.write("Convert the Reclassified Raster into a Polygon.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ Clip and Convert to Albers """ arcpy.Clip_analysis('IntDen_Poly', freqDir + '/Bnd', 'IntDen_Clip') reportFile.write("Clip the polygons to the EnviroAtlas Community Boundary--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') arcpy.AlterField_management('IntDen_Clip', 'gridcode', 'IntDen') """ Add to Final Directory """ try: arcpy.Delete_management(finalDir + '/' + city + '_IntDen') except: pass arcpy.FeatureClassToFeatureClass_conversion('IntDen_Clip', finalDir, city + '_IntDen') print 'Intersection Density End Time: ' + time.asctime() + '\n' reportFile.write("Copy the feature class to the final geodatabase.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') #-------- COMPELETE LOGFILES --------------------------------------------- reportFile.close() #----------------------------------------------------------------------------- # END ANALYSIS #----------------------------------------------------------------------------- except: """ This part of the script executes if anything went wrong in the main script above """ #-------- PRINT ERRORS --------------------------------------------------- print "\nSomething went wrong.\n\n" print "Python Traceback Message below:" print traceback.format_exc() print "\nArcMap Error Messages below:" print arcpy.GetMessages(2) print "\nArcMap Warning Messages below:" print arcpy.GetMessages(1) #-------- COMPLETE LOGFILE ------------------------------------------------ reportFile.write("\nSomething went wrong.\n\n") reportFile.write("Pyton Traceback Message below:") reportFile.write(traceback.format_exc()) reportFile.write("\nArcMap Error Messages below:") reportFile.write(arcpy.GetMessages(2)) reportFile.write("\nArcMap Warning Messages below:") reportFile.write(arcpy.GetMessages(1)) reportFile.write( "\n\nEnded at " + time.asctime() + '\n') reportFile.write("\n---End of Log File---\n") if reportFile: reportFile.close()
shutil.copyfile(localfile, fltwork) shutil.copyfile(localfile, pubdir + "data\\" + bin + "_" + isodate + ".bin") shutil.copyfile(localfile, outdir + isodate + "\\" + bin + "_" + isodate + ".bin") shutil.copyfile(Header, binhdr) shutil.copyfile(TFW, ftfw) arcpy.FloatToRaster_conversion(fltwork, rtif) arcpy.Flip_management(rtif, ftif) shutil.copyfile(ftif, fptif) shutil.copyfile(TFW, fptfw) ## Assign a Spatial reference to the GeoTIFFs descRaster = arcpy.Describe(Projection) SR = descRaster.spatialReference arcpy.DefineProjection_management(ftif, SR) arcpy.RasterToOtherFormat_conversion(ftif, workdir, "IMAGINE Image") ## print fimg ## print fpimg ## print FinalPRJ arcpy.ProjectRaster_management(fimg, fpimg, FinalPRJ, "NEAREST", 14225.65114, "NAD_1983_To_WGS_1984_1") #### Mapping the data ## ## Set the path for the layer files ## publyr = layerdir+"GRACE_Red2Blue_img.lyr" ## reglyr = layerdir+"GRACE_WebColors_img.lyr" ## if thetype=="gws": ## print "Processing "+thetype+" maps" ## mxd = arcpy.mapping.MapDocument(mxddir+"GRACE_GWS_automated.mxd") ## mxdweb = arcpy.mapping.MapDocument(mxddir+"GRACE_web.mxd") ## mxdpub = arcpy.mapping.MapDocument(mxddir+"GRACE_pub.mxd") ## graceimg = arcpy.mapping.Layer(fpimg)
def WVT(city, inDir, workFld): import traceback, time, arcpy, os from arcpy import env arcpy.CheckOutExtension('Spatial') #-------- DIRECTORY SETUP ------------------------------------------------ """ Working Directory """ try: arcpy.CreateFileGDB_management(str(workFld), str(city) + '_TreeWV.gdb') except: print 'TreeWV GDB already exists' workDir = str(workFld) + '/' + city + '_TreeWV.gdb' arcpy.env.workspace = workDir """ Report File Directory """ reportfileDir = str(workFld) + '/Logs' """ Frequent Directory """ freqDir = str(workFld) + '/' + city + '_Freq.gdb' """ Final Geodatabase """ finalDir = str(workFld) + '/' + city + '_Final.gdb' """ Projection File Directory """ prjDir = str(inDir) + '/Prj' prjfile = prjDir + '/USA Contiguous Albers Equal Area Conic USGS.prj' """ Split Raster Directory """ if os.path.isdir(str(workFld) + '/' + city + '_Split') == True: pass else: os.makedirs(str(workFld) + '/' + city + '_Split') splitDir = str(workFld) + '/' + city + '_Split' """ Set Workspace Environments """ arcpy.env.workspace = workDir arcpy.env.scratch = str(inDir) + '/Scratch.gdb' arcpy.env.overwriteOutput = True #----------------------------------------------------------------------------- # BEGIN ANALYSIS #----------------------------------------------------------------------------- try: #-------- LOGFILE CREATION --------------------------------------------- """ Create report file for each metric """ tmpName = city + '_TreeWV_' + time.strftime('%Y%m%d_%H-%M') reportfileName = reportfileDir + '/' + tmpName + '.txt' reportFile = open(reportfileName, 'w') try: loglist = sorted(f for f in os.listdir(reportfileDir) if f.startswith(str(city) + '_Reuse')) tmpName = loglist[-1] except: tmpName = city + '_Reuse_' + time.strftime('%Y%m%d_%H-%M') + '.txt' reportfileName = reportfileDir + '/' + tmpName try: ReuseRF = open(reportfileName, 'a') except: ReuseRF = open(reportfileName, 'w') print 'Creating Reuse Log' """ Write out first line of report file """ print 'Window Views of Trees Start Time: ' + time.asctime() reportFile.write( "Begin with EnviroAtlas 1-meter Land Cover for the EnviroAtlas community--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') #-------- PROCESSING LAYERS ---------------------------------------------- """ Set Environments """ arcpy.env.snapRaster = freqDir + '/LC' arcpy.env.extent = freqDir + '/LC' arcpy.env.overwriteOutput = True """-------- Reclassify LC into Binary Forest ----------------------------- """ if arcpy.Exists(str(freqDir) + '/MForestIO') == False: outReclass = arcpy.sa.Reclassify( str(freqDir) + '/LC', 'Value', arcpy.sa.RemapValue([[0, 0], [10, 0], [20, 0], [21, 0], [22, 0], [30, 0], [40, 1], [52, 0], [70, 0], [80, 0], [82, 1], [91, 1], [92, 0]])) outReclass.save(str(freqDir) + '/MForestIO') reportFile.write( "Reclassify the Land Cover into a Forest binary REPLACE-MFE" + time.strftime('%Y%m%d--%H%M%S') + '--\n') print("Forest area reclassified to binary raster..." + time.asctime()) ReuseRF.write("MForestIO--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') else: print("Forest binary raster already exists..." + time.asctime()) reportFile.write( "Reclassify the Land Cover into a Forest binary REPLACE-MFE--MForestIO" + '--\n') """-------- Create 50m Moving Window ------------------------------------- """ MW = arcpy.sa.FocalStatistics(freqDir + '/MForestIO', arcpy.sa.NbrCircle(50, 'CELL'), 'SUM', 'NODATA') MW.save('MFor_50C') print("Moving window complete..." + time.asctime()) reportFile.write( "Run Focal Statistics on the Forest Binary Raster with a circular window of 50 meters and statistics = SUM.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """-------- Reclassify Moving Window into Trees/NoTrees ------------------- """ ReC = arcpy.sa.Reclassify( 'MFor_50C', 'Value', arcpy.sa.RemapRange([[0, 0.99999, 1], [0.99999, 10300, 0]])) ReC.save('NoForView') print( "Moving window completed and reclassified to tree / no trees..." + time.asctime()) reportFile.write( "Reclassify the Focal Statistics into Forest (>0 -> 0) or No Forest (0 -> 1).--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """-------- Split the Raster As Needs, Process Each Piece ----------------- """ """ Check if the raster should be split """ columns = arcpy.GetRasterProperties_management( 'NoForView', 'COLUMNCOUNT').getOutput(0) xsplit = int(float(columns) / 20000) + 1 rows = arcpy.GetRasterProperties_management('NoForView', 'ROWCOUNT').getOutput(0) ysplit = int(float(rows) / 20000) + 1 """-------- If no split, run the analysis --------------------------------- """ if xsplit * ysplit == 1: """ Convert Raster to Polygon """ arcpy.RasterToPolygon_conversion('NoForView', 'NFVP_Whole', 'NO_SIMPLIFY') """ Extract areas with no forest in 50m """ arcpy.Select_analysis('NFVP_Whole', 'NFVS_Whole', 'gridcode=1') reportFile.write( "Convert the raster into a polygon and select the features where gridcode = 1.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ Project into Albers for use with Dasymetric """ arcpy.Project_management('NFVS_Whole', 'NFVA_Whole', prjfile) print("Raster small enough, carry on..." + time.asctime()) reportFile.write("Convert the polygons into Albers projection.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """-------- If split, run the analysis on each piece and recombine --------- """ else: """ Delete the raster, if necessary """ xy = (xsplit * ysplit) for rast in range(xy): try: arcpy.Delete_management(splitDir + '/nfvsp_' + str(rast)) except: pass try: arcpy.Delete_management(splitDir + '/noforview') except: pass """ Split the Raster """ arcpy.RasterToOtherFormat_conversion('NoForView', splitDir, 'GRID') print("Raster too big, splitting into " + str(xsplit) + " rows and " + str(ysplit) + " columns..." + time.asctime()) arcpy.SplitRaster_management(splitDir + '/NoForView', splitDir, 'NFVSp_', 'NUMBER_OF_TILES', 'GRID', '', str(xsplit) + ' ' + str(ysplit)) reportFile.write( "Split the raster into pieces for easier processing. The Python script determines the number of pieces based on the number of rows and columns in the raster where no piece can have a side larger than 20,000 cells--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ For each raster: """ prjfile = prjDir + '/USA Contiguous Albers Equal Area Conic USGS.prj' xy = (xsplit * ysplit) for Chunk in range(0, xy): try: result = float( arcpy.GetRasterProperties_management( splitDir + '/NFVSp_' + str(Chunk), 'MEAN').getOutput(0)) """ If the raster piece has data: """ if (result > 0): """ Set Environments """ arcpy.env.snapRaster = freqDir + '/MForestIO' arcpy.env.extent = freqDir + '/MForestIO' """ Copy the piece back to the Working Directory """ arcpy.RasterToOtherFormat_conversion( splitDir + '/NFVSp_' + str(Chunk), workDir) """ Convert Raster to Polygon """ arcpy.RasterToPolygon_conversion( 'NFVSp_' + str(Chunk), 'NFVP_' + str(Chunk), 'NO_SIMPLIFY') """ Extract areas with no forest in 50m """ arcpy.Select_analysis('NFVP_' + str(Chunk), 'NFVS_' + str(Chunk), 'gridcode=1') """ Project into Albers for use with Dasymetric """ arcpy.Project_management('NFVS_' + str(Chunk), 'NFVA_' + str(Chunk), prjfile) print("Chunk " + str(Chunk) + " / " + str(xy) + " processed..." + time.asctime()) except: pass reportFile.write( "For each piece, convert the raster into a polygon and select the features where gridcode = 1--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') reportFile.write( "For each piece, convert the polygons into Albers projection.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ Combine the resulting polygons """ NFVchunks = arcpy.ListFeatureClasses('NFVA_*') arcpy.Merge_management(NFVchunks, workDir + '/NFVA_Whole') print("All chunks remerged..." + time.asctime()) reportFile.write( "Merge all of the projected polygons together.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ Set Environments """ arcpy.env.snapRaster = freqDir + '/Dasy' arcpy.env.extent = freqDir + '/Dasy' """-------- End of Split Processing ---------------------------------------- """ """ Extract Dasymetric Pixels where there is no forest in 50m """ EbM = arcpy.sa.ExtractByMask(freqDir + '/Dasy', 'NFVA_Whole') EbM.save('Pop_NoForView') reportFile.write( "Extract by Mask the EnviroAtlas Dasymetric (2011/October 2015) pixels within the polygons--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ Calculate the Extracted Dasy Population with Each CBG """ arcpy.sa.ZonalStatisticsAsTable(freqDir + '/BG_Alb', 'bgrp', 'Pop_NoForView', 'BG_TWV', 'DATA', 'SUM') reportFile.write( "Calculate Zonal Statistics as Table for the extracted dasymetrics with the zones being the 2010 block groups for the EnviroAtlas community.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """-------- Calculate Total Dasy Population, if necessary ------------------ """ """ Use the existing data """ fieldNames = [f.name for f in arcpy.ListFields(freqDir + '/BG_Alb')] if 'Dasy_Pop' in fieldNames: reportFile.write( "Calculate Zonal Statistics as a Table for the EnviroAtlas Dasymetrics (2011/October 2015) with the zones being the 2010 block groups within the EnviroAtlas community boundary. Add resulting population sums to the community block groups as attribute Dasy_Pop--Dasy_Pop" + '--\n') """ Create population data """ else: arcpy.AddField_management(freqDir + '/BG_Alb', 'Dasy_Pop', 'LONG') ZonalStatisticsAsTable(freqDir + '/BG_Alb', 'bgrp', freqDir + '/Dasy', freqDir + '/Dasy_ZS', '', 'SUM') arcpy.JoinField_management(freqDir + '/BG_Alb', 'bgrp', freqDir + '/Dasy_ZS', 'bgrp', ['SUM']) arcpy.CalculateField_management(freqDir + '/BG_Alb', 'Dasy_Pop', '!SUM!', 'PYTHON_9.3') arcpy.DeleteField_management(freqDir + '/BG_Alb', ['SUM']) arcpy.JoinField_management(freqDir + '/BG', 'bgrp', freqDir + '/BG_Alb', 'bgrp', ['Dasy_Pop']) reportFile.write( "Calculate Zonal Statistics as a Table for the EnviroAtlas Dasymetrics (2011/October 2015) with the zones being the 2010 block groups within the EnviroAtlas community boundary. Add resulting population sums to the community block groups as attribute Dasy_Pop--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') ReuseRF.write("Dasy_Pop--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """-------- Create Final Table --------------------------------------------- """ arcpy.TableToTable_conversion(freqDir + '/BG_Alb', workDir, 'TreeWV', '', 'bgrp') arcpy.DeleteField_management('TreeWV', [ 'PLx2_Pop', 'PLx2_Pct', 'SUM_HOUSIN', 'NonWhite', 'LandA_M', 'Density', 'under_1', 'under_1pct', 'under_13', 'under_13pc', 'over_70', 'over_70pct', 'Shape_Length', 'Shape_Leng', 'NonWhite_Pop', 'NonWt_Pct', 'Area_M', 'Shape_Le_1', 'Shape_Area', 'ALAND', 'NonWhite_P', 'H_Income_M', 'State' ]) TreeView = 'TreeWV' reportFile.write( "Create a new table based on the EnviroAtlas community block groups table retaining the BGRP and Dasy_Pop fields--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ Add fields to new table """ arcpy.AddField_management(TreeView, 'WVT_Pop', 'LONG') arcpy.AddField_management(TreeView, 'WVT_Pct', 'FLOAT', 5, 2) reportFile.write( "Add fields to the new table for WVT_Pop (long), WVT_Pct (float).--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ Join Each Table to the final table and WVW_Pop """ arcpy.JoinField_management(TreeView, 'bgrp', 'BG_TWV', 'bgrp', ['SUM']) arcpy.CalculateField_management(TreeView, 'WVT_Pop', '!SUM!', 'PYTHON_9.3') arcpy.MakeTableView_management(TreeView, 'TreeView_Tbl') arcpy.SelectLayerByAttribute_management('TreeView_Tbl', 'NEW_SELECTION', 'SUM IS NULL') arcpy.CalculateField_management('TreeView_Tbl', 'WVT_Pop', 0, 'PYTHON_9.3') arcpy.SelectLayerByAttribute_management('TreeView_Tbl', 'CLEAR_SELECTION') arcpy.DeleteField_management('TreeView_Tbl', 'SUM') reportFile.write( "Join the zonal statistics table with the new table to calculate the new fields: WVT_Pop = zonal statistics.SUM; remove join--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ Calculate WVT_Pct """ arcpy.SelectLayerByAttribute_management('TreeView_Tbl', 'NEW_SELECTION', 'Dasy_Pop >0') arcpy.CalculateField_management( 'TreeView_Tbl', 'WVT_Pct', '"%.2f" % (float(!WVT_Pop!)/float(!Dasy_Pop!) * 100)', 'PYTHON_9.3') arcpy.SelectLayerByAttribute_management('TreeView_Tbl', 'CLEAR_SELECTION') reportFile.write( "Calculate field WVT_Pct = WVT_Pop / Dasy_Pop * 100 (limited to 2 decimal places).--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ Calculate NULL values, where applicable """ arcpy.SelectLayerByAttribute_management('TreeView_Tbl', 'NEW_SELECTION', 'Dasy_Pop = 0') arcpy.CalculateField_management('TreeView_Tbl', 'WVT_Pct', '-99999', 'PYTHON_9.3') arcpy.CalculateField_management('TreeView_Tbl', 'WVT_Pop', '-99999', 'PYTHON_9.3') arcpy.SelectLayerByAttribute_management('TreeView_Tbl', 'CLEAR_SELECTION') arcpy.DeleteField_management('TreeView_Tbl', [ 'SUM_POP10', 'EAID', 'NonWhite', 'LandA_M', 'Density', 'Dasy_Pop', 'SUM' ]) print("Dasy raster summarized to BGs and stats calculated..." + time.asctime()) reportFile.write( "Calculate fields where Dasy_Pop = 0: All Fields = -99999.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """-------- Check that the Analysis Area is covered by the LC -------------- """ """ Create a Polygon Version of the LC """ if arcpy.Exists(freqDir + '/LC_Poly') == False: arcpy.env.extent = freqDir + '/LC' arcpy.env.snapRaster = freqDir + '/LC' ReC = arcpy.sa.Reclassify( str(freqDir) + '/LC', 'Value', arcpy.sa.RemapValue([[0, 0], [10, 1], [20, 1], [21, 1], [22, 1], [30, 1], [40, 1], [52, 1], [70, 1], [80, 1], [82, 1], [91, 1], [92, 1]])) ReC.save(str(freqDir) + '/AreaIO') arcpy.RasterToPolygon_conversion( str(freqDir) + '/AreaIO', str(freqDir) + '/LC_Poly', 'SIMPLIFY') arcpy.EliminatePolygonPart_management( str(freqDir) + '/LC_Poly', str(freqDir) + '/LC_Poly_EP', 'PERCENT', '', '5', 'CONTAINED_ONLY') arcpy.Delete_management(str(freqDir) + '/LC_Poly') arcpy.Rename_management( str(freqDir) + '/LC_Poly_EP', str(freqDir) + '/LC_Poly') """ Buffer the LC Polygon by -500m """ if arcpy.Exists(freqDir + '/Bnd_Cty_500m') == False: arcpy.Buffer_analysis( str(freqDir) + '/Bnd_Cty', str(freqDir) + '/Bnd_Cty_500m', '500 meters') arcpy.EliminatePolygonPart_management( str(freqDir) + '/Bnd_Cty_500m', str(freqDir) + '/Bnd_Cty_500m_EP', 'PERCENT', '', '30', 'CONTAINED_ONLY') arcpy.Delete_management(str(freqDir) + '/Bnd_Cty_500m') arcpy.Rename_management( str(freqDir) + '/Bnd_Cty_500m_EP', str(freqDir) + '/Bnd_Cty_500m') """ Identify whether LC is large enough """ arcpy.MakeFeatureLayer_management(str(freqDir) + '/LC_Poly', 'LClyr') arcpy.MakeFeatureLayer_management( str(freqDir) + '/Bnd_Cty_500m', 'BC_500lyr') arcpy.SelectLayerByLocation_management('BC_500lyr', 'COMPLETELY_WITHIN', 'LClyr', '', 'NEW_SELECTION') bigEnough = float(arcpy.GetCount_management('BC_500lyr').getOutput(0)) arcpy.SelectLayerByAttribute_management('BC_500lyr', 'CLEAR_SELECTION') """ If the LC isn't large enough, edit erroneous BGS """ if bigEnough == 0: """ Identify BGs within 50m of the LC edge """ arcpy.Buffer_analysis( str(freqDir) + '/LC_Poly', 'LC_Poly_Minus50', '-50 meters', 'FULL', 'FLAT', 'ALL') arcpy.MakeFeatureLayer_management('LC_Poly_Minus50', 'Minus50') arcpy.MakeFeatureLayer_management(freqDir + '/BG', 'BG') arcpy.SelectLayerByLocation_management('BG', 'COMPLETELY_WITHIN', 'Minus50', '', 'NEW_SELECTION', 'INVERT') bgValue = float(arcpy.GetCount_management('BG').getOutput(0)) print("LC extends beyond BG boundary, carry on..." + time.asctime()) """ For all BGs too close to the LC edge, assign both fields a value of -99998 """ if bgValue > 0: bgrps = [] cursor = arcpy.SearchCursor('BG') for row in cursor: value = row.getValue('bgrp') bgrps.append(value) bgrps = list(set(bgrps)) expression = '' for bgrp in bgrps: expression = expression + " OR bgrp = '" + str(bgrp) + "'" expression = expression[4:] arcpy.SelectLayerByAttribute_management( 'TreeView_Tbl', 'NEW_SELECTION', expression) arcpy.CalculateField_management('TreeView_Tbl', 'WVT_Pop', '-99998', 'PYTHON_9.3') arcpy.CalculateField_management('TreeView_Tbl', 'WVT_Pct', '-99998', 'PYTHON_9.3') arcpy.SelectLayerByAttribute_management( 'TreeView_Tbl', 'CLEAR_SELECTION') print( "LC doesn't extend beyond BGs, removing border BGs from analysis..." + time.asctime()) reportFile.write( "Calculate Field for BGs within 50m of the edge of the land cover, WVT_Pop and WVW_Pct = -99998.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') """ Create final table """ arcpy.CopyRows_management('TreeView_Tbl', 'TreeWV_Fnl') try: arcpy.Delete_management(finalDir + '/' + str(city) + '_TreeWV') except: pass arcpy.TableToTable_conversion('TreeWV_Fnl', finalDir, city + '_TreeWV') allFields = [ f.name for f in arcpy.ListFields(finalDir + '/' + city + '_TreeWV') ] for field in allFields: if field not in ['bgrp', 'OBJECTID', 'WVT_Pop', 'WVT_Pct']: arcpy.DeleteField_management(finalDir + '/' + city + '_TreeWV', [field]) print 'Window Views of Trees End Time: ' + time.asctime() + '\n' reportFile.write( "Export the fields to be displayed in the EnviroAtlas to a final gdb table. WVT_Pop, WVT_Pct--" + time.strftime('%Y%m%d--%H%M%S') + '--\n') #-------- COMPELETE LOGFILES --------------------------------------------- reportFile.close() ReuseRF.close() #----------------------------------------------------------------------------- # END ANALYSIS #----------------------------------------------------------------------------- except: """ This part of the script executes if anything went wrong in the main script above """ #-------- PRINT ERRORS --------------------------------------------------- print "\nSomething went wrong.\n\n" print "Python Traceback Message below:" print traceback.format_exc() print "\nArcMap Error Messages below:" print arcpy.GetMessages(2) print "\nArcMap Warning Messages below:" print arcpy.GetMessages(1) #-------- COMPLETE LOGFILE ------------------------------------------------ reportFile.write("\nSomething went wrong.\n\n") reportFile.write("Pyton Traceback Message below:") reportFile.write(traceback.format_exc()) reportFile.write("\nArcMap Error Messages below:") reportFile.write(arcpy.GetMessages(2)) reportFile.write("\nArcMap Warning Messages below:") reportFile.write(arcpy.GetMessages(1)) reportFile.write("\n\nEnded at " + time.asctime() + '\n') reportFile.write("\n---End of Log File---\n") if reportFile: reportFile.close()