def createYun(): StopOrStartService(serviceFolder, "DynamicGSMRasterHW", "stop") StopOrStartService(serviceFolder, "DynamicGSMRasterLL", "stop") if (arcpy.Exists(CellThiessYunHW)): arcpy.Delete_management(CellThiessYunHW) if (arcpy.Exists(CellThiessYunLL)): arcpy.Delete_management(CellThiessYunLL) logging.info("PolygonToRaster_conversion HW") print "面转栅格" # Process: 面转栅格 arcpy.PolygonToRaster_conversion(CellThiessFinal, "VOICE_TOTAL_TRAFFIC", CellThiessYunHW, "CELL_CENTER", "NONE", ".01") logging.info("CalculateStatistics_management HW") print "计算统计数据" # Process: 计算统计数据 arcpy.CalculateStatistics_management(CellThiessYunHW, "1", "1", "", "OVERWRITE", "") logging.info("PolygonToRaster_conversion LL") arcpy.PolygonToRaster_conversion(CellThiessFinal, "UL_DL_FLOW", CellThiessYunLL, "CELL_CENTER", "NONE", ".01") logging.info("CalculateStatistics_management LL") print "计算统计数据" # Process: 计算统计数据 arcpy.CalculateStatistics_management(CellThiessYunLL, "1", "1", "", "OVERWRITE", "") StopOrStartService(serviceFolder, "DynamicGSMRasterHW", "start") StopOrStartService(serviceFolder, "DynamicGSMRasterLL", "start")
def StudyAreaWatershed(output_workspace, dem_hydro, processes): # Check out the ArcGIS Spatial Analyst extension license arcpy.CheckOutExtension("Spatial") # Set environment variables arcpy.env.overwriteOutput = True arcpy.env.workspace = output_workspace arcpy.env.scratchWorkspace = output_workspace arcpy.env.cellSize = dem_hydro arcpy.env.parallelProcessingFactor = processes # List parameter values arcpy.AddMessage("Workspace: {}".format(arcpy.env.workspace)) arcpy.AddMessage("DEM hydro: " "{}".format(arcpy.Describe(dem_hydro).baseName)) arcpy.AddMessage("processes: {}".format(processes)) # Fill sinks arcpy.AddMessage("Beginning filling sinks...") dem_fill = arcpy.sa.Fill(in_surface_raster=dem_hydro) arcpy.AddMessage("Fill sinks complete.") # Calculate D8 flow direction (suitable for input into the Watershed tool) arcpy.AddMessage("Beginning flow direction...") flow_dir_d8 = arcpy.sa.FlowDirection(in_surface_raster=dem_fill, flow_direction_type="D8") flow_direction_d8 = os.path.join(output_workspace, "flow_direction_d8") flow_dir_d8.save(flow_direction_d8) # Calculate raster statistics and build pyramids arcpy.AddMessage(" Calculating statistics...") arcpy.CalculateStatistics_management(flow_direction_d8) arcpy.AddMessage(" Building pyraminds...") arcpy.BuildPyramids_management(flow_direction_d8) arcpy.AddMessage("Flow direction complete.") # Calculate flow accumulation arcpy.AddMessage("Beginning flow accumulation...") flow_accum_d8 = arcpy.sa.FlowAccumulation(flow_dir_d8, data_type="FLOAT", flow_direction_type="D8") flow_accumulation_d8 = os.path.join(output_workspace, "flow_accumulation_d8") flow_accum_d8.save(flow_accumulation_d8) # Calculate raster statistics and build pyramids arcpy.AddMessage(" Calculating statistics...") arcpy.CalculateStatistics_management(flow_accumulation_d8) arcpy.AddMessage(" Building pyraminds...") arcpy.BuildPyramids_management(flow_accumulation_d8) arcpy.AddMessage("Flow accumulation complete.") # Return arcpy.SetParameter(3, flow_accumulation_d8)
def setNull(imageList, nodata): if nodata is None: for i in imageList: rast = arcpy.Raster(i) rastNodata = rast[0,0] arcpy.management.SetRasterProperties(in_raster=i, nodata=[[1,rastNodata]]) arcpy.CalculateStatistics_management(in_raster_dataset=i, x_skip_factor=100, y_skip_factor=100, skip_existing='OVERWRITE', ignore_values = [[1,rastNodata]]) else: setNodata = nodata for i in imageList: arcpy.management.SetRasterProperties(in_raster=i, nodata=[[1,setNodata]]) arcpy.CalculateStatistics_management(in_raster_dataset=i, x_skip_factor=100, y_skip_factor=100, skip_existing='OVERWRITE', ignore_values =[[1,setNodata]])
def RastToShp(rastinfile, year_day, flag): """Creates the output extent to be used in the rest of the script. Allows all images to be snapped to a constant grid, for direct matrix to matrix comparison""" if flag == 'N': arcrast = TMP + year_day + "_recasttif.tif" tmptif = arcpy.Raster(rastinfile) tmptif.save(arcrast) #This step can be necessary for Arc tools to work properly on .tif files arcpy.CalculateStatistics_management(arcrast) if flag == 'Y': arcrast = TMP + year_day + "_recasttif.tif" arcpy.CopyRaster_management(rastinfile, arcrast, '', '-9999', '-9999') arcpy.env.extent = arcrast clipshp_tmp = TMP + year_day + "_clipshp_tmp.shp" EXT_data = arcpy.sa.Con(arcrast, 1, 0, '"Value" > 0') #Choose only areas with data tmp_ext = TMP + year_day + "_clipshp_con.tif" EXT_data.save(tmp_ext) del EXT_data arcpy.RasterToPolygon_conversion(tmp_ext, clipshp_tmp, "NO_SIMPLIFY", "Value") #Convert to Shpfile clipshp = TMP + year_day + "_ext.shp" clipshp_tmp2 = TMP + year_day + "_ext_tmp2.shp" arcpy.Select_analysis(clipshp_tmp, clipshp_tmp2, '"GRIDCODE" = 1') arcpy.Buffer_analysis(clipshp_tmp2, clipshp, "-2 Kilometers") #Negative buffer to remove edge effects cliprast = TMP + year_day + '_ext_tif.tif' arcpy.FeatureToRaster_conversion(clipshp, "GRIDCODE", cliprast, 30) #Convert back to TIF file to use as output extent arcpy.Delete_management(clipshp) arcpy.Delete_management(clipshp_tmp) arcpy.Delete_management(clipshp_tmp2) arcpy.Delete_management(tmp_ext) return cliprast, arcrast
def prepare_data(folder, dem, rail, road, river, lake): #create folder for temporary files try: os.makedirs(temp) except OSError: pass #initialize geoprocessing setting to same cellsize and same #extent as the DEM raster demfile = os.path.join(folder, dem) arcpy.CalculateStatistics_management(demfile) dem_raster = arcpy.sa.Raster(demfile) arcpy.AddMessage("DEM file:" + demfile) arcpy.env.extent = dem_raster.extent arcpy.env.snapRaster = dem_raster slopeCost = "cost_slope.tif" elevCost = "cost_elev.tif" create_cost_layer_dem(folder, dem_raster, slopeCost, elevCost) roadCost = "cost_road.tif" create_cost_layer(folder, dem_raster, road, roadCost, 2000) railCost = "cost_rail.tif" create_cost_layer(folder, dem_raster, rail, railCost, 2000) lakeCost = "cost_lake.tif" create_cost_layer(folder, dem_raster, lake, lakeCost, 2000) riverCost = "cost_river.tif" create_cost_layer(folder, dem_raster, river, riverCost, 2000)
def LCP(outputWeightCodedShp): """ Source dir: costRasters Output dir: LCPs """ weightCode = outputWeightCodedShp.split(".")[0] source = paths.join(paths.costRasters, weightCode + ".asc") output = paths.join(paths.LCPs, outputWeightCodedShp) print "Finding least cost paths for weighting {}".format(weightCode) if arcpy.Exists(output) and not redoExistingOutput: print "{} already exists, leaving it as is.".format(output) return output paths.setEnv(env) if arcpy.Raster(source).minimum is None: print " Calculating statistics for {}...".format(source) arcpy.CalculateStatistics_management(source, x_skip_factor=30, y_skip_factor=30) print " Calculating cost distance and backlink..." costDist = sa.CostDistance(paths.destination, source, out_backlink_raster="backlink") print " Finding least-cost path..." costPath = sa.CostPath(paths.sources, costDist, "backlink", "EACH_ZONE") print " Vectorizing..." arcpy.RasterToPolyline_conversion(costPath, output, simplify="SIMPLIFY") arcpy.Delete_management("backlink") return output
def main(overwrite_flag=False): # Hardcoded for now, eventually read this from the INI workspace = r'Y:\justinh\Projects\SNWA\ee\images\spring_valley\landsat' arcpy.CheckOutExtension('Spatial') # arcpy.env.workspace = workspace arcpy.env.overwriteOutput = True arcpy.env.pyramid = 'PYRAMIDS 0' arcpy.env.compression = "LZW" arcpy.env.workspace = 'C:\Temp' arcpy.env.scratchWorkspace = 'C:\Temp' # year_list = range(1985, 2017) year_list = [1984] for year_str in os.listdir(workspace): if not re.match('^\d{4}$', year_str): continue elif int(year_str) not in year_list: continue logging.info(year_str) year_ws = os.path.join(workspace, year_str) for file_name in os.listdir(year_ws): file_path = os.path.join(year_ws, file_name) if (file_name.endswith('.ts.tif') or file_name.endswith('.tasseled_cap.tif')): logging.info(file_name) arcpy.CalculateStatistics_management(file_path) try: os.remove(file_path.replace('.tif', '.tif.xml')) except: pass else: continue
def createRefDTMMosaic(in_md_path, out_md_path, v_unit): a = datetime.now() if arcpy.Exists(out_md_path): arcpy.AddMessage("Referenced mosaic dataset exists " + out_md_path) else: arcpy.CreateReferencedMosaicDataset_management(in_dataset=in_md_path, out_mosaic_dataset=out_md_path, where_clause="TypeID = 1") raster_function_path = Raster.Contour_Meters_function_chain_path v_unit = str(v_unit).upper() if v_unit.find("FEET") >= 0 or v_unit.find("FOOT") >= 0 or v_unit.find("FT") >= 0: raster_function_path = Raster.Contour_IntlFeet_function_chain_path #if v_unit.find("INTL") >= 0 or v_unit.find("INTERNATIONAL") >= 0 or v_unit.find("STANDARD") >= 0 or v_unit.find("STD") >= 0: # raster_function_path = Raster.Contour_IntlFeet_function_chain_path if v_unit.find("US") >= 0 or v_unit.find("SURVEY") >= 0: arcpy.AddMessage("Using US FOOT Raster Function") raster_function_path = Raster.Contour_Feet_function_chain_path else: arcpy.AddMessage("Using INT FOOT Raster Function") else: arcpy.AddMessage("Using METER Raster Function") arcpy.EditRasterFunction_management(in_mosaic_dataset=out_md_path, edit_mosaic_dataset_item="EDIT_MOSAIC_DATASET", edit_options="REPLACE", function_chain_definition=raster_function_path, location_function_name="") Utility.addToolMessages() arcpy.CalculateStatistics_management(in_raster_dataset=out_md_path, x_skip_factor=SKIP_FACTOR, y_skip_factor=SKIP_FACTOR, ignore_values="", skip_existing="OVERWRITE", area_of_interest="Feature Set") doTime(a, "Created referenced mosaic dataset " + out_md_path)
def calc_zonal_statistics(raster_file, polygon_file, zone_field, output_table): """ Calculate zonal statistics for a raster and vector and save result as .dbf and .csv files. For each polygon in the vector file, calculate set of statistics from the raster (sum, mean, max, min, count). Uses ArcPy ZonalStatisticsAsTable function. Requires an ArcGIS SpatialAnalyst licence. Parameters ---------- raster_file : str Filename of raster file polygon_file : str Filename of vector file zone_field : str Name of field labelling the zones within vector file output_table : str Filename of output table (.dbf or .csv) Returns ------- None Returns None """ arcpy.CheckOutExtension("Spatial") arcpy.MakeFeatureLayer_management(polygon_file, "layer") layer = arcpy.mapping.Layer("layer") # First calculate statistics on raster. arcpy.CalculateStatistics_management(in_raster_dataset=raster_file) # set up .dbf and .csv filenames if output_table.endswith('.dbf'): _output_csv = os.path.splitext(output_table)[0] _output_csv = '{0}.csv'.format(_output_csv) _output_dbf = output_table elif output_table.endswith('.csv'): _output_dbf = os.path.splitext(output_table)[0] _output_dbf = '{0}.dbf'.format(_output_dbf) _output_csv = output_table else: _output_dbf = '{0}.dbf'.format(output_table) _output_csv = '{0}.csv'.format(output_table) _output_dbf = '{0}.dbf'.format( os.path.splitext(os.path.basename(raster_file))[0]) if len(_output_dbf) > 12: _output_dbf = 'temp_output.dbf' arcpy.env.workspace = os.path.dirname(raster_file) _output_filename = os.path.basename(_output_dbf) # now calculate zonal statistics as table arcpy.sa.ZonalStatisticsAsTable(in_zone_data=layer, zone_field=zone_field, in_value_raster=raster_file, out_table=_output_dbf, ignore_nodata="DATA", statistics_type="ALL") if arcpy.Exists(layer): arcpy.Delete_management(layer) # convert to .csv csv_utils.convert_dbf_to_csv( os.path.join(os.path.dirname(raster_file), _output_dbf), _output_csv) return None
def image_to_raster(image, fgdb): print('\n\nConverting image to fgdb raster...') print('\t{0:<20}:\t{1}'.format('image', image)) im = Image.open(image) imarray = np.array(im) print('\t{0:<20}:\t{1}'.format('imarray.size', imarray.size)) print('\t{0:<20}:\t{1}'.format('imarray.shape', imarray.shape)) unique = np.unique(imarray, return_counts=True) print('\t{0:<20}:\t{1}'.format('np.unique[0]', unique[0])) print('\t{0:<20}:\t{1}'.format('np.unique[1]', unique[1])) print('\t{0:<20}:\t{1}'.format('np.sum(np.unique[1])', np.sum(unique[1]))) raster = os.path.basename(image) raster = os.path.splitext(raster)[0] raster = os.path.join(fgdb, raster) print('\t{0:<20}:\t{1}'.format('raster', raster)) if arcpy.Exists(raster): arcpy.Delete_management(raster) # arcpy.CopyRaster_management(in_raster=image, # out_rasterdataset=raster, # pixel_type='8_BIT_UNSIGNED') sa_raster = arcpy.NumPyArrayToRaster(in_array=imarray, lower_left_corner=arcpy.Point(0.0, 0.0), x_cell_size=1000.0, y_cell_size=1000.0, value_to_nodata=None) sa_raster.save(raster) arcpy.CalculateStatistics_management(in_raster_dataset=raster) arcpy.BuildRasterAttributeTable_management(in_raster=raster, overwrite=True) describe_raster(raster) print('Converted image to fgdb raster.')
def makePDF(weightCode, mxdPath, outputDir, outHeight=2200): outName = weightCode + ".pdf" output = paths.joinNative(outputDir, outName) print "Rendering PDF for weight {}".format(weightCode) paths.setEnv(env) mxd = arcpy.mapping.MapDocument(mxdPath) df = arcpy.mapping.ListDataFrames(mxd, "")[0] costAsc = paths.join(paths.costRasters, weightCode + ".asc") if arcpy.Raster(costAsc).minimum is None: print "Calculating statistics for {}".format(costAsc) arcpy.CalculateStatistics_management(costAsc, x_skip_factor=30, y_skip_factor=30) # Copy rasters locally costGlob = paths.join(paths.costRasters, weightCode) + ".*" print " Caching {}".format(costGlob) costCache = paths.cache(*glob.glob(costGlob), subdir="cost") print " Replacing cost layer" costLayer = arcpy.mapping.ListLayers(mxd, "Cost", df)[0] costLayer.replaceDataSource(paths.temp + "\\cost", "NONE", weightCode) print "Writing cost to {}".format(output) arcpy.mapping.ExportToPDF(mxd, output, "PAGE_LAYOUT", resolution=400) del mxd paths.dropcache(costCache)
def getRasterStats(ProjectUID, ProjectID, curr_raster, raster_path, group, elevation_type, raster_format, raster_PixelType, nodata, horz_cs_name, horz_unit_name, horz_cs_wkid, vert_cs_name, vert_unit_name, rows): # NOTE: Order here must match field list in CMDRConfig inMem_NameBound = "in_memory\MemBoundary" if arcpy.Exists(inMem_NameBound): arcpy.Delete_management(inMem_NameBound) Utility.addToolMessages() arcpy.RasterDomain_3d(raster_path, inMem_NameBound, "POLYGON")[0] Utility.addToolMessages() boundary = Utility.getExistingRecord(in_table=inMem_NameBound, field_names=['SHAPE@'], uidIndex=-1)[0][0] newRow = [ ProjectUID, ProjectID, boundary, curr_raster, raster_path, group, elevation_type, raster_format, nodata, raster_PixelType ] arcpy.CalculateStatistics_management(in_raster_dataset=raster_path, skip_existing="OVERWRITE") cellSize = getRasterProperties(raster_path, newRow) newRow.append(horz_cs_name) newRow.append(horz_unit_name) newRow.append(horz_cs_wkid) newRow.append(vert_cs_name) newRow.append(vert_unit_name) newRow.append(None) # Vert WKID, we can't know this in python rows.append(newRow) return cellSize
def makeOutcropRaster(dem, controlPoints, inc, sgdb): addMsgAndPrint(' making predicted outcrop raster') # set up raster environment arcpy.env.snapRaster = dem arcpy.env.cellSize = arcpy.Describe(dem).meanCellWidth arcpy.env.extent = controlPoints # TREND addMsgAndPrint(' calculating plane with TREND') outRaster = os.path.join(sgdb, 'xxxPlane') testAndDelete(outRaster) arcpy.Trend_3d(controlPoints, 'SHAPE', outRaster, '', 1, 'LINEAR') # subtract TREND plane from DEM addMsgAndPrint(' subtracting plane from DEM') intersectRaster1 = outRaster + '_i' testAndDelete(intersectRaster1) arcpy.Minus_3d(dem, outRaster, intersectRaster1) arcpy.CalculateStatistics_management(intersectRaster1) # classify results dH = math.tan(math.radians(inc)) * 2.0 * float(arcpy.env.cellSize) addMsgAndPrint(' classifying results, dH=' + str(dH)) outCon = Con(intersectRaster1, 0, 1, 'VALUE > ' + str(dH) + ' OR VALUE < -' + str(dH)) testAndDelete(outRaster) testAndDelete(intersectRaster1) return outCon
def build_stats(outfilename): try: # generate pyramids and statistics for final output gprint('BUILDING OUTPUT STATISTICS & PYRAMIDS' + '\n') arcpy.CalculateStatistics_management(outfilename, "1", "1", "#") arcpy.BuildPyramids_management(outfilename) except: pass
def main(): # Hardcoded for now, eventually read this from the INI workspace = r'Y:\justinh\Projects\SNWA\ee\images\spring_valley\landsat' # year_list = range(1985, 2017) year_list = [1984] # Float32/Float64 float_output_type = 'Float32' float_nodata_value = np.finfo(np.float32).min # int_output_type = 'Byte' # int_nodata_value = 255 raster_re = re.compile('^\d{8}_\d{3}_\w{3}.(tasseled_cap|ts).tif$') for year_str in os.listdir(workspace): if not re.match('^\d{4}$', year_str): continue elif int(year_str) not in year_list: continue logging.info(year_str) year_ws = os.path.join(workspace, year_str) for file_name in os.listdir(year_ws): file_path = os.path.join(year_ws, file_name) temp_path = os.path.join(year_ws, file_name.replace('.tif', '.temp.tif')) if not raster_re.match(file_name): continue elif arcpy.sa.Raster(file_path).pixelType == 'F64': logging.info(file_name) # logging.debug(arcpy.sa.Raster(file_path).pixelType) # Make a 32 bit float copy subprocess.call([ 'gdalwarp', '-ot', float_output_type, '-overwrite', '-of', 'GTiff', '-co', 'COMPRESS=LZW', # '-srcnodata', str(nodata_value), '-dstnodata', '{:f}'.format(float_nodata_value), file_path, temp_path ]) # Remove the old one subprocess.call(['gdalmanage', 'delete', file_path]) # Rename the file back to the original name subprocess.call(['gdalmanage', 'rename', temp_path, file_path]) # Compute raster statistics arcpy.CalculateStatistics_management(file_path) os.remove(file_path.replace('.tif', '.tif.xml'))
def recalculate_raster_statistics(): for ras in rasters_to_symbolize: # First some cleanup (we need to remove the previously displayed layer): remove_intermediary_layers([ras + "1"]) # Now build the pyramids, and calculate the statistics on each rasters # on the drive. The Calculate_Statistics will also show the raster as a layer: print("Calculate Stats:" + ras) arcpy.BatchBuildPyramids_management(gdb_output + "\\" + ras) arcpy.CalculateStatistics_management(gdb_output + "\\" + ras)
def FlowAccumulationDinf(self, ang, Qsurf, numpy_array_location): # Inputs inlyr = ang desc = arcpy.Describe(inlyr) ang=str(desc.catalogPath) arcpy.AddMessage("\nInput Dinf Flow Direction file: "+ang) # Execute flow direction and slope on the first day of model operation or at the end of each month output_asc = numpy_array_location + "\output_asc.asc" output_tiff = numpy_array_location + "\output_tif.tif" Qsurf_ascii = arcpy.RasterToASCII_conversion(Qsurf, output_asc) Qsurf_tiff = arcpy.ASCIIToRaster_conversion(Qsurf_ascii, output_tiff, "FLOAT") weightgrid= Qsurf_tiff if arcpy.Exists(weightgrid): desc = arcpy.Describe(weightgrid) wtgr=str(desc.catalogPath) arcpy.AddMessage("\nInput Weight Grid: "+wtgr) edgecontamination='false' arcpy.AddMessage("\nEdge Contamination: "+edgecontamination) # Input Number of Processes inputProc=str(8) arcpy.AddMessage("\nInput Number of Processes: "+inputProc) # Output sca = str(numpy_array_location + "\osca.tif") arcpy.AddMessage("\nOutput Dinf Specific Catchment Area Grid: "+sca) # Construct command cmd = 'mpiexec -n ' + inputProc + ' AreaDinf -ang ' + '"' + ang + '"' + ' -sca ' + '"' + sca + '"' if arcpy.Exists(weightgrid): cmd = cmd + ' -wg ' + '"' + wtgr + '"' if edgecontamination == 'false': cmd = cmd + ' -nc ' arcpy.AddMessage("\nCommand Line: "+cmd) # Submit command to operating system os.system(cmd) # Capture the contents of shell command and print it to the arcgis dialog box process=subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) arcpy.AddMessage('\nProcess started:\n') for line in process.stdout.readlines(): arcpy.AddMessage(line) # Calculate statistics on the output so that it displays properly arcpy.AddMessage('Executing: Calculate Statistics\n') arcpy.CalculateStatistics_management(sca) return sca
def build_stats(raster): """Builds statistics and pyramids for output rasters""" try: arcpy.CalculateStatistics_management(raster, "1", "1", "#") except Exception: gprint('Statistics failed. They can still be calculated manually.') try: arcpy.BuildPyramids_management(raster) except Exception: gprint('Pyramids failed. They can still be built manually.') return
def get_soc0_999(workspace, in_raster, value_table, out_raster): # Set environment settings env.workspace = workspace # Set local variables field_name = 'mukey' lookup_field = 'soc0_999' arcpy.JoinField_management(in_raster, field_name, value_table, field_name, lookup_field) #arcpy.AddJoin_management(in_raster, field_name, value_table, field_name) outraster = Lookup(in_raster, lookup_field) arcpy.CalculateStatistics_management(outraster) outraster.save(out_raster) arcpy.BuildPyramids_management(out_raster)
def test_raster(self, ref_file, in_file, rep_file): """ Logs differences in raster files. Parameters: ref_file -- reference raster file in_file -- input raster file rep_file -- report file Return value: Logs resulting difference in raster files, error statement otherwise. """ try: logging.info('') logging.info(' Executing: EGSUtility.test_raster') logging.info(' Compare ref: ' + ref_file) logging.info(' Compare product: ' + in_file) logging.info(' Report file: ' + rep_file) if os.path.exists(rep_file): os.remove(rep_file) arcpy.CalculateStatistics_management(ref_file, "", "", "",\ "SKIP_EXISTING") arcpy.CalculateStatistics_management(in_file, "", "", "",\ "SKIP_EXISTING") arcpy.RasterCompare_management(ref_file,in_file,"RASTER_DATASET",\ "","CONTINUE_COMPARE",rep_file,"","","") # open results file, output results to log file with open(rep_file) as infile: for line in infile: logging.info(line) infile.close() except ValueError as e: self.error('test_raster(): {:s}'.format(e))
def streamBurning(DEM_orig, scratch_gdb, seg_network_a, home, home_name): print("stream burning process") arcpy.CalculateStatistics_management(DEM_orig) dem_orig_b = Con(IsNull(DEM_orig), 0, DEM_orig) dem_orig_b.save(scratch_gdb + "/DEM_square") # set up river network raster network_raster = scratch_gdb + "/network_raster" arcpy.env.extent = dem_orig_b arcpy.env.cellsize = dem_orig_b arcpy.env.snapRaster = dem_orig_b arcpy.env.outputCoordinateSystem = dem_orig_b print('convert network to raster') net_fields = [f.name for f in arcpy.ListFields(seg_network_a)] if "burn_val" in net_fields: arcpy.DeleteField_management(seg_network_a, "burn_val") del net_fields arcpy.AddField_management(seg_network_a, "burn_val", "SHORT") arcpy.CalculateField_management(seg_network_a, "burn_val", "10") # arcpy.FeatureToRaster_conversion( seg_network_a, "burn_val", network_raster, dem_orig_b) # THINK IT IS WORTH CHANGING THE ATTRIBUTE VALUE network_raster_a = Con( IsNull(network_raster), 0, 30) # changed to 30 to see if it improves stream ordering network_raster_a.save(scratch_gdb + "/net_ras_fin") arcpy.ResetEnvironments() # This is just a map algebra thing to replace the stuff above that uses numpy (the numpy stuff works but is # limited in terms of how much it can process. print("stream burning DEM") rivers_ting = Raster(scratch_gdb + "/net_ras_fin") dem_ting = Raster(scratch_gdb + "/DEM_square") stream_burn_dem_a = dem_ting - rivers_ting stream_burn_dem_a.save(scratch_gdb + "/raster_burn") sb_DEM = os.path.join(home, "{0}strBurndDEm.tif".format(home_name)) arcpy.CopyRaster_management(scratch_gdb + "/raster_burn", sb_DEM) print("stream burning complete") return stream_burn_dem_a, network_raster, sb_DEM
def weightedCostRaster(outputCodeName, layerOrder=None): """ Source dir: preprocessed Output dir: costRasters outputCodeName: 1311011.asc where value of each digit |--> weight index of each digit |--> layer index in given list (so use the same layer order every time in every function!) layerOrder: ['agl_resis', 'bcmp_resis', 'bestp_resis', ...] --- layer names in order that corresponds to outputCode """ if layerOrder is None: print "No layer order specified, that's kinda important." return sources = layerOrder weights = map(int, list(outputCodeName.replace(".asc", ""))) # outputCode = "".join(map(str, weights)) print "Making weighted cost raster {}".format(outputCodeName) paths.setEnv(env) output = paths.join(paths.costRasters, outputCodeName) if arcpy.Exists(output) and not redoExistingOutput: print "{} already exists, leaving it as is.".format(output) return output active = [(paths.join(paths.preprocessed, source), weight) for source, weight in zip(sources, weights) if weight > 0] if len(active) <= 1: # TODO: add the straight-line-distance raster, so least cost paths don't freak out? pass costRas = sa.CreateConstantRaster(0.0001, "FLOAT", env.cellSize, env.extent) for source, weight in active: costRas += sa.Raster(source) * weight arcpy.RasterToASCII_conversion(costRas, output) arcpy.CalculateStatistics_management(output, x_skip_factor=30, y_skip_factor=30) return output
def taglio_raster_inondazione_aggregato(self): #CUT and SAVE Flooded areas within the admin2 area try: flood_out = self.dirOut + self.admin + "_agg.tif" arcpy.gp.ExtractByMask_sa(self.flood_aggregated, admin_vect, flood_out) except: pass return "NoFloodRaster" arcpy.CalculateStatistics_management(flood_out) try: return "Flood" except: pass return "NoFlood"
def update_mxd_from_metadata(file_name, metadata, verify=False): """ :param file_name: A path to the mxd file. :param metadata: The metadata providing the names, filepaths, and types to add to the mxd. :return: The original file. """ mxd = arcpy.mapping.MapDocument(os.path.abspath(file_name)) df = mxd.activeDataFrame version = get_version() for layer_name, layer_info in metadata['data_sources'].items(): file_path = os.path.abspath(os.path.join(BASE_DIR, layer_info['file_path'])) # If possible calculate the statistics now so that they are correct when opening arcmap. try: print(("Calculating statistics for the file {0}...".format(file_path))) arcpy.CalculateStatistics_management(file_path) except Exception as e: print(e) layer_file = get_layer_file(layer_info['type'], version) if not layer_file: print(( "Skipping layer {0} because the file type is not supported for ArcMap {1}".format(layer_name, version))) if version == '10.5': print("However with your version of ArcMap you can still drag and drop this layer onto the Map.") continue layer_from_file = arcpy.mapping.Layer(layer_file) layer_from_file.name = layer_info['name'] print(('Adding layer: {0}...'.format(layer_from_file.name))) arcpy.mapping.AddLayer(df, layer_from_file, "TOP") # Get instance of layer from MXD, not the template file. del layer_from_file layer = arcpy.mapping.ListLayers(mxd)[0] update_layer(layer, file_path, verify=verify) logger.debug('Getting dataframes...') df = mxd.activeDataFrame df.extent = arcpy.Extent(*metadata['bbox']) mxd.activeView = df.name arcpy.RefreshActiveView() mxd.save() del mxd # remove handle on file return file_name
def raster_properties(input_raster, attribute='MEAN'): """ Wrapper for GetRasterProperties_management which does the right thing.""" # make sure statistics exist, set them if they don't. arcpy.CalculateStatistics_management( input_raster, "1", "1", "#", "SKIP_EXISTING") attr_obj = arcpy.GetRasterProperties_management(input_raster, attribute) attr_val = attr_obj.getOutput(0) numeric_attrs = ['MINIMUM', 'MAXIMUM', 'MEAN', 'STD', 'CELLSIZEX', 'CELLSIZEY'] if config.debug: msg(locale.getlocale()) if attribute in numeric_attrs: # convert these to locale independent floating point numbers value = locale.atof(attr_val) else: # leave anything else untouched value = attr_val return value
def getstat(myraster, what="MINIMUM"): myraster_ = arcpy.Describe(myraster) myraster = myraster_.catalogPath try: raster_st = arcpy.GetRasterProperties_management(myraster, what) temp1 = raster_st.getOutput(0) temp2 = temp1.replace(",", ".") raster_st = float(temp2) return raster_st except arcpy.ExecuteError: msgs = arcpy.GetMessages(2) error_code = str(msgs).replace(":", "").split(" ")[1] if error_code == "001100": arcpy.CalculateStatistics_management(myraster, "1", "1", "", "OVERWRITE", "") raster_st = arcpy.GetRasterProperties_management(myraster, what) temp1 = raster_st.getOutput(0) temp2 = temp1.replace(",", ".") raster_st = float(temp2) return raster_st
def meanCalculationWithExtraction(num, fileList): os.mkdir(path + "Middle\\TEMP" + str(num)) # A MASSIVE C**K TO THE F**KING ArcGIS! env.workspace = path + "Middle\\TEMP" + str( num) # A MASSIVE C**K TO THE F**KING ArcGIS! rasters = fileList file = path + "Middle\\ReprojectedRasters\\" + str(rasters[num]) raster = ExtractByMask(file, path + "Base\\Base.shp") raster.save(path + "Results\\" + str(str(rasters[num]).replace( "GlobSnow_SWE_L3A_", "")).replace("_v2.0.hdf.tif", "") + ".tif") arcpy.CalculateStatistics_management(raster, "1", "1") meanResult = arcpy.GetRasterProperties_management(raster, "MEAN") meanRes = meanResult.getOutput(0) shutil.rmtree(path + "Middle\\TEMP" + str(num)) # A MASSIVE C**K TO THE F**KING ArcGIS! return [ str(str(rasters[num]).replace("GlobSnow_SWE_L3A_", "")).replace("_v2.0.hdf.tif", ""), str(meanRes) ]
def main(): arcpy.CheckOutExtension('Spatial') arcpy.env.workspace = this_root+'\\AVHRR_clipped\\' rasters = arcpy.ListRasters('*.tif*') # print(rasters) time_init = time.time() flag = 0 for raster in rasters: # Save temp raster to disk with new name start = time.time() ras = sa.Raster(raster) arcpy.CalculateStatistics_management(ras) max = ras.maximum fvc = ras/8000. fvc = sa.Con(fvc, 0, fvc, "VALUE < 0") fvc = sa.Con(fvc, 1, fvc, "VALUE > 1") fvc.save(this_root+'AVHRR_fvc\\'+raster) end = time.time() logger.process_bar(flag,len(rasters),time_init,start,end,raster) flag += 1 # break pass
def ReloadMD(): # Empty the mosaic dataset prior to reloading it arcpy.AddMessage("Removing previous forecast data from mosaic dataset...") arcpy.RemoveRastersFromMosaicDataset_management(inputMD, "1=1") # Add the rasters to the mosaic dataset arcpy.AddMessage("Adding new forecast data to mosaic dataset...") arcpy.AddRastersToMosaicDataset_management(inputMD, "Raster Dataset", forecastGDBPath) # Check something was imported result = int(arcpy.GetCount_management(inputMD).getOutput(0)) if result > 0: # Re-calculate statistics on the mosaic dataset arcpy.AddMessage("Calculating statistics on the newly loaded mosaic dataset") arcpy.CalculateStatistics_management(inputMD) # Re-build overviews on the mosaic dataset #arcpy.AddMessage("Building overviews on the mosaic dataset") #arcpy.BuildOverviews_management(inputMD) # Calculate the time fields on the mosaic dataset arcpy.AddMessage("Calculating the time fields on the mosaic dataset") locale.setlocale(locale.LC_TIME, '') mdLayer = "mdLayer" arcpy.MakeMosaicLayer_management(inputMD, mdLayer, "Category = 1") # Leave out overviews - only calculate fields on primary rasters arcpy.CalculateField_management(mdLayer, dateForecastImportedField, """time.strftime("%c")""", "PYTHON","#") arcpy.CalculateField_management(mdLayer, dateForecastEffectiveFromField, """time.strftime("%c", time.strptime(!Name!,""" + "\"" + weatherName + """%Y%m%dT%H%M"))""", "PYTHON", "#") arcpy.CalculateField_management(mdLayer, dateForecastEffectiveToField, "!" + dateForecastEffectiveFromField + "!", "PYTHON", "#")
if arcpy.Exists(ogrfile): cmd = cmd + ' -o ' + '"' + shfl + '"' if delineate == 'true': cmd = cmd + ' -sw ' arcpy.AddMessage("\nCommand Line: " + cmd) # Submit command to operating system os.system(cmd) # Capture the contents of shell command and print it to the arcgis dialog box process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) #arcpy.AddMessage('\nProcess started:\n') message = "\n" for line in process.stdout.readlines(): message = message + line arcpy.AddMessage(message) # Calculate statistics on the output so that it displays properly arcpy.AddMessage('Calculate Statistics\n') arcpy.CalculateStatistics_management(ord) #arcpy.DefineProjection_management(net, coord_sys) arcpy.CalculateStatistics_management(w) # remove converted json file if arcpy.Exists(ogrfile): extn_json = os.path.splitext(shfl)[ 1] # get extension of the converted json file if extn_json == ".json": os.remove(shfl)