def get_cell_size_bottom_corner(DTM): arcpy.AddMessage("-------------------------") # Determine cell size describe_elevation = arcpy.Describe(DTM) cell_size = describe_elevation.meanCellHeight arcpy.AddMessage("The model is working on a cell size of " + str(cell_size) + " metres") arcpy.AddMessage("-------------------------") # The below text takes the input raster and calculates the bottom left corner extent_xmin_result = arcpy.GetRasterProperties_management(DTM, "LEFT") extent_xmin = float(extent_xmin_result.getOutput(0)) extent_ymin_result = arcpy.GetRasterProperties_management(DTM, "BOTTOM") extent_ymin = float(extent_ymin_result.getOutput(0)) # Turns the corner into a point bottom_left_corner = arcpy.Point(extent_xmin, extent_ymin) arcpy.AddMessage("Calculated the bottom left corner of the input raster") arcpy.AddMessage("-------------------------") return cell_size, bottom_left_corner
def InvertRaster(habLayer, costSurf): # Convert Habitat model to cost surface try: arcpy.AddMessage("Inverting values in " + habLayer + "...") except: pass Habmax = arcpy.GetRasterProperties_management(habLayer, "MAXIMUM").getOutput(0) Habmin = arcpy.GetRasterProperties_management(habLayer, "MINIMUM").getOutput(0) ## invHab = "in_memory\\inv_hab" ## plusHab = "in_memory\\plus_hab" invHab = arcpy.sa.Times(habLayer, -1) plusHab = arcpy.sa.Plus(invHab, float(Habmax) + float(Habmin)) #ArcGIS may introduce negative values through rounding errors so this is #a crude way to correct the problem. Costmin = arcpy.GetRasterProperties_management(plusHab, "MINIMUM").getOutput(0) if float(Costmin) < 0: if float(Costmin) < 0 and float(Costmin) > -.1: arcpy.AddWarning( "The cost surface contains negative values. Rounding error assumed. Minimum Value will be truncated to zero..." ) plusHab = arcpy.sa.Con(plusHab, "0", plusHab, "Value < 0") else: string = 'Input contains negative values that cannot be attributed to rounding errors. Check inputs' roundError = 'Error' raise RoundError(string) ## else: ## if costSurf <> "": ## costSurf = plusHab.save(costSurf) ## else: ## costSurf = plusHab return plusHab
def get_total_disc_area(self): """Uses Q_disconnect map to create a list of cumulative stranded area as flows are reduced""" q_disc_ras = Raster(os.path.join(self.out_dir, "Q_disconnect.tif")) cell_size = float( arcpy.GetRasterProperties_management(q_disc_ras, 'CELLSIZEX').getOutput(0)) disc_areas_dict = {} for Q in sorted(self.discharges, reverse=True): # cumulative disconnected area cum_disc_area = Con(q_disc_ras >= Q, 1) mat = arcpy.RasterToNumPyArray(cum_disc_area, nodata_to_value=0) disc_areas_dict[Q] = np.sum(mat) * (cell_size**2) return disc_areas_dict
def _get_cell_arc(_r): # Check if r is a Raster Layer isRaster = checkIfRstIsLayer(_r) lyr = rst_lyr(_r) if not isRaster else _r cellsizeX = arcpy.GetRasterProperties_management( lyr, "CELLSIZEX", "" if not bnd else bnd) cellsizeY = arcpy.GetRasterProperties_management( lyr, "CELLSIZEY", "" if not bnd else bnd) if xy: if str(cellsizeY) != str(cellsizeX): raise ValueError( ('Cellsize is not the same in both dimensions (x, y)')) else: return int(str(cellsizeX)) else: return int(str(cellsizeX)), int(str(cellsizeY))
def normalize_raster(in_raster, normalization_method=NM_MAX, invert=False): """Normalize values in in_raster. Normalize values in in_raster using score range or max score method, with optional inversion. """ lm_util.build_stats(in_raster) result = arcpy.GetRasterProperties_management(in_raster, "MINIMUM") min_val = float(result.getOutput(0)) result = arcpy.GetRasterProperties_management(in_raster, "MAXIMUM") max_val = float(result.getOutput(0)) if max_val > 0: if normalization_method == NM_SCORE: if invert: return (max_val - in_raster) / (max_val - min_val) return (in_raster - min_val) / (max_val - min_val) else: # Max score normalization if invert: return (max_val + min_val - in_raster) / max_val return in_raster / max_val else: return in_raster * 0
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 create_const_raster(base_raster_path, constant): ''' Given a raster dataset and any integer number this function will create a constant raster at the same extent as the input raster. :param base_raster_path: The path to the raster we'll use as a template. :param constant: The integer value to use in the constant. :return: ''' base_raster = Raster(base_raster_path) data_type = "INTEGER" cell_size = arcpy.GetRasterProperties_management(base_raster, "CELLSIZEX") x_min = arcpy.GetRasterProperties_management(base_raster, "LEFT") x_max = arcpy.GetRasterProperties_management(base_raster, "RIGHT") y_min = arcpy.GetRasterProperties_management(base_raster, "BOTTOM") y_max = arcpy.GetRasterProperties_management(base_raster, "TOP") extent = Extent(x_min, y_min, x_max, y_max) constant_raster = CreateConstantRaster(constant, data_type, cell_size, extent) return constant_raster
def Filter_FinalSite(Final_Site, Input_File): ''' This function is for recommending the optimal candidate locations based on some more certeria. :param Final_Site: The name of output polygon feature class showing the optimal candidate locations. :param Input_File: The dictionary with key as data type(input file), and the value as the associated file name. :return: ''' # Find the maximum pixel value within the research area, and make a conditional evaluation on each of the input cells. # Then save the result of conditional evaluation in geodatabase.(almost 3 lines) # Get the highest value we could found Value_Max = arcpy.GetRasterProperties_management("weighted_out", "MAXIMUM") outCon = Con("weighted_out", "weighted_out", where_clause="VALUE = " + str(Value_Max)) outCon.save("conditioned_out") print "Condictional Filter Completed" # Make a majority filter for the result of conditional evaluation. # Then save the result of majority filter in geodatabase.(almost 2 lines) # Using low-pass filter (Majority) to get more general boundary instead of "salt and pepper" cell outMajFilt = MajorityFilter(outCon, "EIGHT", "MAJORITY") outMajFilt.save("filtered_out") print "Majority Filter Completed" # Convert the result of majority filter(raster) to polygon feature class. (almost 1 line) # Convert it to polygon arcpy.RasterToPolygon_conversion(outMajFilt, "polygon_class", "SIMPLIFY", "VALUE") # Create a feature layer from the polygon feature class. Select Layer By Location and Attribute under some certeria. # (almost 3 lines) # Using layer to select the best location arcpy.MakeFeatureLayer_management("polygon_class", "polygon") arcpy.SelectLayerByLocation_management("polygon", "INTERSECT", Input_File["roads"], selection_type="NEW_SELECTION") arcpy.SelectLayerByAttribute_management("polygon", "SUBSET_SELECTION", "\"Shape_Area\">=40469") print "Final Feature Selection: Completed" # Copy the selected layed to a new polygon feature class. (almost 1 line) # Output the final product class arcpy.CopyFeatures_management("polygon",Final_Site) print "Selected Final School Site! "
def rst_ext(rst, gisApi='gdal'): """ Return a array with the extent of one raster dataset array order = Xmin (left), XMax (right), YMin (bottom), YMax (top) API'S Available: * gdal; * arcpy; * arcpy2; """ if gisApi == 'gdal': from osgeo import gdal img = gdal.Open(rst) lnhs = int(img.RasterYSize) cols = int(img.RasterXSize) left, cellx, z, top, c, celly = img.GetGeoTransform() right = left + (cols * cellx) bottom = top - (lnhs * abs(celly)) extent = [left, right, bottom, top] elif gisApi == 'arcpy': import arcpy extent = ["LEFT", "RIGHT", "BOTTOM", "TOP"] for i in range(len(extent)): v = arcpy.GetRasterProperties_management(rst, extent[i]) extent[i] = float(str(v).replace(',', '.')) elif gisApi == 'arcpy2': import arcpy describe = arcpy.Describe(rst) extent = [ describe.extent.XMin, describe.extent.XMax, describe.extent.YMin, describe.extent.YMax ] else: raise ValueError('The api {} is not available'.format(gisApi)) return extent
def statistics_drought_zones(self): pop_out = arcpy.Raster(self.lscan_cut_adm2) scrivi_qui = arcpy.Describe(pop_out).path scrivi_questo = str(pop_out).split("/")[5].split("_")[0] # one or both raster could be empty (no flood in polygon) I chech that sum_val_pop = int( arcpy.GetRasterProperties_management(pop_out, "UNIQUEVALUECOUNT")[0]) for dr_temp in self.adm2_drought_months: valore_taglio = str(dr_temp.split("/")[-1]).count("_") contatore = str( dr_temp.split("/")[-1]).split("_")[valore_taglio].split(".")[0] dr_out = arcpy.Raster(dr_temp) sum_val_fld = int( arcpy.GetRasterProperties_management(dr_out, "UNIQUEVALUECOUNT")[0]) nome_tabella = str(scrivi_questo).split("_")[0] pop_stat_dbf = scrivi_qui + "/" + nome_tabella + "_" + str( contatore) + "_pop_stat.dbf" if sum_val_fld > 0 and sum_val_pop > 0: try: arcpy.gp.ZonalStatisticsAsTable_sa(dr_out, "VALUE", pop_out, pop_stat_dbf, "DATA", "SUM") except Exception as e: print e.message else: template_dbf = "C:/sparc/input_data/drought/template_pop_stat.dbf" tabula = str(scrivi_questo).split("_")[0] + "_" + str( contatore) + "_pop_stat.dbf" print "nome tabula %s " % tabula try: arcpy.CreateTable_management(scrivi_qui, tabula, template_dbf) except Exception as e: print e.message return "People in drought areas....\n"
def Correction(env, band, radiance, saveDir): specificBand = str(band.split("_")[-1][1]) readGainsOffset(env, specificBand) band_RefMul = GainsOffset['REFLECTANCE_MULT_BAND_' + specificBand] band_RefAdd = GainsOffset['REFLECTANCE_ADD_BAND_' + specificBand] print("{0} has a Gain, Offset and Radiance Values of {1}, {2} and {3} Respectively". format(band, band_RefMul, band_RefAdd, radiance)) print("Setting NoData Value and Executing Radiometric Calibration...") ret_name = band.split("_") bandName = "Scene_" + ret_name[2] + "_" + ret_name[3] + "_" + ret_name[7].strip(".TIF") + "_SurRef.tif" arcpy.SetRasterProperties_management(band, nodata="1 0") bandNom_P1 = arcpy.sa.Times(band, band_RefMul) bandNom_P2 = arcpy.sa.Plus(bandNom_P1, band_RefAdd) bandCor = arcpy.sa.Divide(bandNom_P2, radiance) bandMinVal = float((arcpy.GetRasterProperties_management(in_raster=bandCor, property_type="MINIMUM")).getOutput(0)) bandStdVal = float((arcpy.GetRasterProperties_management(in_raster=bandCor, property_type="STD")).getOutput(0)) bandThreshold = float(bandMinVal + bandStdVal) print("{0} has a Minimum, StdDev and Threshold values of {1}, {2} & {3} Respectively". format(band, bandMinVal, bandStdVal, bandThreshold)) band_refCor = arcpy.sa.Minus(bandCor, bandThreshold) print("Saving Surface Reflectance Output...\n") OutRefName = (os.path.join(saveDir, bandName)) band_refCor.save(OutRefName)
def photograph(self): if not path.exists(self.out_dir): os.mkdir(self.out_dir) hmin = float( arcpy.GetRasterProperties_management(self.hydrodem, 'MINIMUM').getOutput(0)) hmax = float( arcpy.GetRasterProperties_management( self.hydrodem, 'MAXIMUM').getOutput(0)) - 500000 vals = { self.catseed: [0, 1, -32768, 50, 50], self.fdr: [0, 128, 255, 25, 25], self.hydrodem: [hmin, hmax, -2147483648, 50, 50] } for tif, values in vals.items(): jpg = path.join( self.out_dir, '{}_{}.jpg'.format( path.splitext(path.basename(tif))[0], self.huc4)) gdal_cmd = 'gdal_translate -a_nodata {} -of JPEG -co worldfile=yes -b 1 -b 1 -b 1 -scale {} {} 0 255 -outsize {}% {}% {} {}'.\ format(values[2], values[0], values[1], values[3], values[4], tif, jpg) print gdal_cmd sp.call(gdal_cmd, stdout=sp.PIPE, stderr=sp.STDOUT)
def testFgdbRaster(self): self.assertTrue(arcpy.Exists(consts.test_fgdb_raster)) desc = arcpy.Describe(consts.test_fgdb_raster) self.assertEqual(desc.dataType, 'RasterDataset') self.assertEqual(desc.format, 'FGDBR') self.assertEqual(desc.pixelType, 'S16') # 16-bit integer values self.assertEqual(desc.width, 431) self.assertEqual(desc.height, 415) # test for an expected mean value ar = arcpy.GetRasterProperties_management(consts.test_fgdb_raster, 'MEAN') mean_value = float(ar.getOutput(0)) self.assertAlmostEqual(mean_value, -1582.89427780728)
def run(self): self.e.load() print "Starting CTI processing..." arcpy.gp.Plus_sa(self.i.fac_mf, "1", self.i.fac_mf1) X_Length = float( arcpy.GetRasterProperties_management(self.i.fac_mf, "CELLSIZEX", "").getOutput(0)) Y_Length = float( arcpy.GetRasterProperties_management(self.i.fac_mf, "CELLSIZEY", "").getOutput(0)) # arcpy.gp.Times_sa(str(X_Length), str(Y_Length), self.i.fac_area) arcpy.gp.Times_sa(self.i.fac_mf1, str(X_Length * Y_Length), self.i.As) # this is all for calculating As arcpy.gp.Times_sa(self.i.sdg_f, "1.570796", self.i.b_times) arcpy.gp.Divide_sa(self.i.b_times, "90", self.i.b_rad) arcpy.gp.Tan_sa(self.i.b_rad, self.i.b_tan) arcpy.gp.Con_sa(self.i.sdg_f, self.i.b_tan, self.i.b_tan_c, "0.001", "\"Value\" > 0") # this is all for calculating tanB arcpy.gp.Divide_sa(self.i.As, self.i.b_tan_c, self.i.As_b_tan) arcpy.gp.Ln_sa(self.i.As_b_tan, self.i.cti) print "Ending CTI processing..."
def main(): # get list of all '*.img' rasters in dem_path folder os.chdir(dem_path) dems = glob.glob('*.img') dem_list = ";".join(dems) # environment settings arcpy.env.workspace = dem_path arcpy.env.resamplingMethod = 'CUBIC' outCS = arcpy.SpatialReference(coord_sys) # mosaic individual tiles to single raster tmp_dem = arcpy.MosaicToNewRaster_management(dem_list, out_path, 'tmp_' + out_name + '.tif', outCS, "32_BIT_FLOAT", "", "1") # clip raster to aoi shapefile out_dem = arcpy.Clip_management(tmp_dem, '', os.path.join(out_path, out_name + '.tif'), aoi_path, '', 'ClippingGeometry', 'NO_MAINTAIN_EXTENT') # create and save hillshade arcpy.ResetEnvironments() out_hs = arcpy.sa.Hillshade(out_dem, '', '', "NO_SHADOWS") out_hs.save(os.path.join(out_path, out_name + '_HS.tif')) # check raster cell size xResult = arcpy.GetRasterProperties_management(tmp_dem, 'CELLSIZEX') print 'Mosaic Raster Cell Size: ' + str(xResult.getOutput(0)) xResult = arcpy.GetRasterProperties_management(out_dem, 'CELLSIZEX') print 'Clipped Raster Cell Size: ' + str(xResult.getOutput(0)) # clear environment settings arcpy.ClearEnvironment("workspace")
def calculateTWI (dem): ''' This function calculates the TWI based on DEM dataset ''' # Calculate the Flow Direction and Accumulation print ("Calculating Flow Direction and Accumulation...") flow_dir = FlowDirection(dem) flow_acc = FlowAccumulation(flow_dir) # Calculate the Slope and convert from Degree to Radians print ("Calculating Slope in radians...") slope_deg = os.path.join (arcpy.env.scratchWorkspace, 'slope_deg') # could'nt write to in_memory (too big?!) arcpy.Slope_3d (dem, slope_deg, "DEGREE") slope_rad = Raster(slope_deg) * (math.pi/180) # Get the DEM pixel area PxsizeX = float(arcpy.GetRasterProperties_management(dem, "CELLSIZEX").getOutput(0)) PxsizeY = float(arcpy.GetRasterProperties_management(dem, "CELLSIZEY").getOutput(0)) pxArea = PxsizeX*PxsizeY # Apply the TWI equation print ("Calculating TWI...") c = 0.0001 exp_1 = Ln(((flow_acc + 1) * pxArea) / Tan(slope_rad)) exp_2 = Ln(((flow_acc + 1) * pxArea) / c + Tan(slope_rad)) TWI = Con(slope_rad > 0, exp_1, exp_2) # Apply a low-pass filter to reduce noise. Only if the DEM Cell size is small (very high resolution) if (PxsizeX <= 2 or PxsizeY <= 2 ): print ("The DEM cell size is {}. A low-pass filter is applied." .format(int(PxsizeX))) TWI_filtered = FocalStatistics(TWI, NbrRectangle(3, 3, "CELL"), "MEAN") TWI_filtered.save(os.path.join (Workspace, 'p_TWI_filtered')) else: TWI.save(os.path.join (Workspace, 'p_TWI'))
def rst_ext(rst): """ Return a array with the extent of one raster dataset array order = Xmin (left), XMax (right), YMin (bottom), YMax (top) """ extent = ["LEFT", "RIGHT", "BOTTOM", "TOP"] for i in range(len(extent)): v = arcpy.GetRasterProperties_management(rst, extent[i]) extent[i] = float(str(v).replace(',', '.')) return extent
def rasterStatistics(feature, featureID, inRaster, calculateStat): try: #create results obj results = {} #get values outExtractByMask = ExtractByMask(inRaster, feature) value = arcpy.GetRasterProperties_management( outExtractByMask, calculateStat).getOutput(0) value = round(float(value), 5) results[calculateStat] = value data = ResultObj(featureID, results) return data except: try: #check raster cell size against input cellsize = float( arcpy.GetRasterProperties_management(inRaster, 'CELLSIZEX').getOutput(0)) print 'in Except block, area check:', feature.area, cellsize**2 #get centroid value if first method failed value = arcpy.GetCellValue_management( inRaster, str(feature.centroid.X) + ' ' + str(feature.centroid.Y)).getOutput(0) value = round(float(value), 5) results[calculateStat] = value data = ResultObj(featureID, results) return data except: tb = format_exc() raise Exception(tb)
def mosiacRasters(): arcpy.env.workspace = 'C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\ancillary\\raster\\cdl.gdb\\' cdl_raster = Raster("cdl30_2015") elevSTDResult = arcpy.GetRasterProperties_management(cdl_raster, "TOP") YMax = elevSTDResult.getOutput(0) elevSTDResult = arcpy.GetRasterProperties_management(cdl_raster, "BOTTOM") YMin = elevSTDResult.getOutput(0) elevSTDResult = arcpy.GetRasterProperties_management(cdl_raster, "LEFT") XMin = elevSTDResult.getOutput(0) elevSTDResult = arcpy.GetRasterProperties_management(cdl_raster, "RIGHT") XMax = elevSTDResult.getOutput(0) arcpy.env.extent = arcpy.Extent(XMin, YMin, XMax, YMax) #### need to wrap these paths with Raster() fct or complains about the paths being a string rasterlist = [ 'resampled_cdl30_2007_p1', 'D:\\projects\\ksu\\v2\\attributes\\rasters\\cdl30_2007.img' ] ######mosiac tiles together into a new raster arcpy.MosaicToNewRaster_management(rasterlist, data['refine']['gdb'], data['refine']['mask_2007']['filename'], cdl_raster.spatialReference, "16_BIT_UNSIGNED", 30, "1", "LAST", "FIRST") #Overwrite the existing attribute table file arcpy.BuildRasterAttributeTable_management( data['refine']['mask_2007']['path'], "Overwrite") # Overwrite pyramids gen.buildPyramids(data['refine']['mask_2007']['path'])
def get_Image_Metadata(imagepath, extension, FID): originalFID = 'NA' bits = 'NA' width = 'NA' length = 'NA' ext = 'NA' geoRef = 'Y' fileSize = 'NA' spatial_res = 'NA' originalFID = str(FID) bits = arcpy.GetRasterProperties_management(imagepath, 'VALUETYPE') width = arcpy.GetRasterProperties_management(imagepath, 'COLUMNCOUNT') length = arcpy.GetRasterProperties_management(imagepath, 'ROWCOUNT') ext = extension.split('.')[1] fileSize = get_filesize(imagepath) spatial_res = get_spatial_res(imagepath) desc = arcpy.Describe(imagepath) year = get_year(desc.baseName, desc.path) return [ originalFID, bits, width, length, ext, geoRef, fileSize, imagepath, spatial_res, year ]
def check_dem_files(dem_folder, huc_name, huc_id, cs_string): if os.path.exists(dem_folder): dem = os.path.join(dem_folder, 'NED_DEM_10m_' + huc_id + '.tif') hs = os.path.join(dem_folder, 'NED_HS_10m.tif') # check DEM if os.path.exists(dem): check_projection(dem, cs_string) min_elev = arcpy.GetRasterProperties_management(dem, "MINIMUM") max_elev = arcpy.GetRasterProperties_management(dem, "MAXIMUM") if min_elev == max_elev: print ' Check DEM values: min = max ' else: print ' DEM missing.' # check hillshade if os.path.exists(hs): check_projection(hs, cs_string) min_elev = arcpy.GetRasterProperties_management(hs, "MINIMUM") max_elev = arcpy.GetRasterProperties_management(hs, "MAXIMUM") if min_elev == max_elev: print ' Check hillshade values: min = max' #else: # print ' ' + hs + ' MISSING.' else: print ' DEM FOLDER MISSING'
def reclass_veg(veg, dem, output_folder, remap, mask): """ Reclassify the original vegetation into the categories classified as Table 2.3 in AS 3959 (2009). :param veg: `file` the input vegetation :param dem: `file` the input dem used as reference projection :param output_folder: `str` the output folder :param remap: `srt` the vegetation reclassification :param mask: `file` the mask for the effective AOI :return: `file` the reclassified vegetation """ arcpy.env.overwriteOutput = True input_folder = os.path.dirname(veg) arcpy.env.workspace = input_folder veg_r_init = 'veg_r_init' veg_r_proj = pjoin(input_folder, 'veg_r_pj') veg_class_r = pjoin(output_folder, 'veg_r') arcpy.AddMessage('Remap the vegetation into classes of 1 ~ 7 ...') # Derive reclassifed veg... reclassify(veg, remap, veg_r_init) # project as dem and change cell size same as that of dem dem_c = arcpy.GetRasterProperties_management(dem, "CELLSIZEX").getOutput(0) arcpy.ProjectRaster_management(veg_r_init, veg_r_proj, dem, "#", dem_c) if arcpy.Exists(veg_r_init): arcpy.Delete_management(veg_r_init) # get the AOI extract_by_mask(veg_r_proj, mask, veg_class_r) g_list = arcpy.ListRasters('g_g*') if len(g_list) != 0: for g_file in g_list: arcpy.Delete_management(g_file) if arcpy.Exists(veg_r_proj): arcpy.Delete_management(veg_r_proj) return veg_class_r
def buildPointGrids(inFeature, tempRaster, cell_size, outShapefile): # Convert from polygon to raster and raster to point to create point grid arcpy.PolygonToRaster_conversion(inFeature, "OBJECTID", tempRaster, "CELL_CENTER", "NONE", cell_size) # Determine if raster contains only NoData values noData = int(arcpy.GetRasterProperties_management(tempRaster, 'ALLNODATA')[0]) if noData == 0: # Convert raster to point grid arcpy.RasterToPoint_conversion(tempRaster, outShapefile, "VALUE") # Add XY Coordinates to feature class in the NAD_1983_Alaska_Albers projection arcpy.AddXY_management(outShapefile) # Delete intermediate files arcpy.Delete_management(tempRaster) elif noData == 1: arcpy.AddMessage("All values for this watershed are nodata...") # Delete intermediate files arcpy.Delete_management(tempRaster)
def create_chm(rootdir): """ Resamples the DTM to the resolution of the DSM and then subtracts the resampled DTM from the DSM to create a Canopy Height Model (CHM) Assumes directory structure of rootdir\refID\sensor\output... """ arcpy.CheckOutExtension("3d") ## Needed ffor the Minus_3d tool refIDs = os.listdir(rootdir) for refID in refIDs: if "Products" in refIDs: refIDs.remove("Products") if "ENV" in refIDs: refIDs.remove("ENV") #sensors = ["Mavic", "Sequoia"] sensors = ["Sequoia"] for sensor in sensors: if sensor == "Mavic": bands = ["Output"] if sensor == "Sequoia": bands = ['gre', 'red', 'reg', 'nir'] for band in bands: for root, dirs, files in os.walk(os.path.join(rootdir, refID, sensor, band)): for dir in dirs: ##excludes directories containing only tiles if "tiles" in dirs: dirs.remove("tiles") for f in files: if f.endswith("dsm.tif"): DSM = os.path.join(root,f) if f.endswith("dtm.tif"): DTM = os.path.join(root,f) CellSize = arcpy.GetRasterProperties_management(DSM, property_type = "CELLSIZEX") ## Defining cell size to resample the DTM to that of the DSM- known ##bug for doing so in the Resample_management tool. ##BUG!!! https://community.esri.com/thread/162982 DTMrsmpl = os.path.join(rootdir, refID, sensor, band, "DTMrsmpl.tif") arcpy.Resample_management(DTM, DTMrsmpl, CellSize ) ## Subtract DTM (resampled) from DSM to create CHM if sensor == "Mavic": arcpy.Minus_3d(DSM, DTMrsmpl, os.path.join(rootdir, refID, sensor, band, "{}_{}_CHM.tif".format(refID, sensor))) print (refID, sensor, " CHM Created") if sensor == "Sequoia": ## have to use the old formatting style (Python 2.7) arcpy.Minus_3d(DSM, DTMrsmpl, os.path.join(rootdir, refID, sensor, band, "%s_%s_CHM.tif"%(refID, band))) #"{}_{}_CHM.tif".format(refID, sensor))) print (refID, band, " CHM Created")
def task1(): """ 该函数用来统计纽约州每一个公园的mean、值,然后将这些mean值存入到New York NDVI parks.csv文件去 :return: """ input_path = os.path.normcase("D:/NDVI/New York/parks/ndvi new york parks") output_path = os.path.normcase( "D:/NDVI/New York/parks/New York ndvi result") if os.path.exists(output_path) is False: os.mkdir(output_path) if os.path.exists(os.path.join( output_path, "New York NDVI parks.csv")): #若存在统计结果csv文件,则该函数不执行 return data_dict = dict() day_list = [81, 97, 113, 129, 145, 161, 177, 193, 209, 225, 241] time_labels = [ str(year) + '_' + str(day) for year in range(2000, 2015) for day in day_list ] input_file_dir_list = [ os.path.join(input_path, directory) for directory in os.listdir(input_path) if os.path.isdir(os.path.join(input_path, directory)) ] for input_directory in input_file_dir_list: input_directory_name = os.path.split(input_directory)[-1] # park name temp_mean_list = list() temp_mean_list.append(input_directory_name) for input_file_name in time_labels: input_file = os.path.join(input_directory, input_file_name) # elevSTDResult = arcpy.GetRasterProperties_management(input_file, "MEAN") # elevSTD = elevSTDResult.getOutput(0) try: elevSTDResult = arcpy.GetRasterProperties_management( input_file, "MEAN") elevSTD = elevSTDResult.getOutput(0) except: print input_file continue temp_mean_list.append(elevSTD) data_dict[input_directory_name] = temp_mean_list output_name = os.path.join(output_path, "New York NDVI parks.csv") with file(output_name, "wb") as output_fd: output_writer = csv.writer(output_fd) output_writer.writerow([' '] + time_labels) for key in sorted(data_dict.keys()): output_writer.writerow(data_dict[key])
def make_covhsi(self, fish_applied, depth_raster_path): # habitat suitability curves from Fish.xlsx # fish_applied is a dictionary with fish species listed in Fish.xlsx arcpy.CheckOutExtension('Spatial') arcpy.env.overwriteOutput = True arcpy.env.workspace = self.cache arcpy.env.extent = "MAXOF" self.logger.info("* * * CREATING " + str(self.cover_type).upper() + " COVER RASTER * * *") for species in fish_applied.keys(): self.logger.info(" >> SPECIES : " + str(species)) for ls in fish_applied[species]: self.logger.info(" LIFESTAGE: " + str(ls)) self.logger.info(" -> Retrieving " + self.cover_type + " curve from Fish.xlsx ...") curve_data = self.fish.get_hsi_curve(species, ls, self.cover_type) if depth_raster_path.__len__() > 0: self.logger.info(" -> Cropping to relevant depth regions ...") self.crop_input_raster(species, ls, depth_raster_path) else: try: self.cell_size = float(arcpy.GetRasterProperties_management(self.input_raster, property_type="CELLSIZEX")[0]) except: self.cell_size = 1.0 self.logger.info(" -> Calculating cover HSI raster ...") try: ras_out = self.call_analysis(curve_data) except: self.logger.info("ERROR: Cover raster calculation (check input data).") arcpy.CheckInExtension('Spatial') self.error = True self.logger.info(" - OK") ras_name = self.cover_type + "_hsi.tif" self.logger.info( " -> Saving: " + self.path_hsi + ras_name + " ...") try: ras_out.save(self.path_hsi + ras_name) self.logger.info(" - OK") except: self.logger.info("ERROR: Could not save " + self.cover_type + " HSI raster (corrupted data?).") self.error = True if not self.error: self.logger.info(" >> " + self.cover_type + " cover HSI raster creation " + str(species).upper() + " complete.") else: self.logger.info(" >> Could not create cover HSI raster. Check errors messages.") arcpy.CheckInExtension('Spatial')
def volume(dem): print print "starting volume calculation..." # get maximum value of raster file elevMAXResult = arcpy.GetRasterProperties_management(dem, "MAXIMUM") elevMax = float(elevMAXResult.getOutput(0)) print "DEM max value: ", elevMax # Get input Raster properties inRas = arcpy.Raster(dem) lowerLeft = arcpy.Point(inRas.extent.XMin, inRas.extent.YMin) cellSize = inRas.meanCellWidth print "lower left coordinate: ", lowerLeft print "DEM cell size: ", cellSize # Convert Raster to numpy array #arr = arcpy.RasterToNumPyArray(inRas,nodata_to_value=0); #print arr arr = arcpy.RasterToNumPyArray(inRas, nodata_to_value=elevMax) print "Line35, arr: ", repr(arr) print "array size: ", arr.shape # Tamano del Raster clipeado (X,Y) x_ext = arr.shape[0] y_ext = arr.shape[1] # ---- Volume calculation # set up a list of lists having the maximum value of DEM list1 = [] list2 = [] for i in range(y_ext): list1.append(elevMax) ###print list1 for i in range(x_ext): list2.append(list1) ### print list2 cell_area = cellSize * cellSize cell_volume = cell_area * (list2 - arr) # get volume for cells #cell_volume = cell_area * (arr - arr+1) # ######print res # Sum of rows from "cell_volume" ###row_sum = [ sum(x) for x in cell_volume ] #Another way to get sum: arrSum_r = res.sum(1) ###print "Sum by rows: ",row_sum # Sum total. First rows, after columns sum_total = sum([sum(x) for x in cell_volume]) # it provides volume total print("volume total: " + str(round(sum_total, 2)) + " m2") print "end" return sum_total
def make_contour(file_raster, name, out_file_path, contour_interval): file_contour = '%s' % out_file_path + '/' + '%s' % name + '_con.shp' min_elevation = arcpy.GetRasterProperties_management( file_raster, "MINIMUM") Contour(file_raster, file_contour, contour_interval, 0, 1) file_contour_point = '%s' % out_file_path + '/' + '%s' % name + '_point.shp' arcpy.FeatureVerticesToPoints_management(file_contour, file_contour_point, "ALL") inFeatures = file_contour_point fieldName1 = "x_degree" fieldName2 = "y_degree" fieldPrecision = 18 fieldScale = 11 arcpy.AddField_management(inFeatures, fieldName1, "DOUBLE", fieldPrecision, fieldScale) arcpy.AddField_management(inFeatures, fieldName2, "DOUBLE", fieldPrecision, fieldScale) arcpy.CalculateField_management(inFeatures, fieldName1, "!shape.firstpoint.X!", "PYTHON_9.3") arcpy.CalculateField_management(inFeatures, fieldName2, "!shape.firstpoint.Y!", "PYTHON_9.3") all_cp = arcpy.da.TableToNumPyArray( inFeatures, ('ID', 'CONTOUR', 'x_degree', 'y_degree')) os.remove('%s' % out_file_path + '/' + '%s' % name + '_con.shp') os.remove('%s' % out_file_path + '/' + '%s' % name + '_point.shp') os.remove('%s' % out_file_path + '/' + '%s' % name + '_con.dbf') os.remove('%s' % out_file_path + '/' + '%s' % name + '_point.dbf') number_point = len(all_cp) file_all_cp = open('%s' % out_file_path + '/' + '%s' % name + '_point.txt', 'w') for p in range(0, number_point): file_all_cp.write('%d' % (all_cp[p][0] - 1)) file_all_cp.write('% f' % all_cp[p][1]) file_all_cp.write('% f' % all_cp[p][2]) file_all_cp.write('% f\n' % all_cp[p][3]) file_all_cp.close()
def ExtractRasterByMultiPolygon(shpFile, filedName, originRasterFile, bufferSize, suffix, outPath): ## Set environment settings if not os.path.isdir(outPath): ## if outPath is not exist, then build it. if outPath != "": os.mkdir(outPath) env.workspace = outPath ## Split polygon by fieldName polyNames, flag = ListFieldValues(shpFile, filedName) if (flag): arcpy.gp.overwriteOutput = 1 ## Get the cellsize of originRasterFile cellSizeResult = arcpy.GetRasterProperties_management( originRasterFile, "CELLSIZEX") cellSize = cellSizeResult.getOutput(0) bufferDistance = float(cellSize) * bufferSize arcpy.Split_analysis(shpFile, shpFile, filedName, outPath) polyFiles = [] polyBufferFiles = [] polyFinalFiles = [] rasterFiles = [] for name in polyNames: polyFile = outPath + os.sep + name + '.shp' polyBufferFile = outPath + os.sep + name + '_buf.shp' polyFinalFile = outPath + os.sep + name + '_final.shp' if suffix is None: rasterFile = outPath + os.sep + name + '.tif' else: rasterFile = outPath + os.sep + name + suffix + '.tif' polyFiles.append(polyFile) polyBufferFiles.append(polyBufferFile) rasterFiles.append(rasterFile) polyFinalFiles.append(polyFinalFile) arcpy.Buffer_analysis(polyFile, polyBufferFile, bufferDistance, "OUTSIDE_ONLY") arcpy.Merge_management([polyFile, polyBufferFile], polyFinalFile) if arcpy.CheckOutExtension("Spatial") == "CheckedOut": for i in range(0, len(polyBufferFiles)): tempRaster = arcpy.sa.ExtractByMask(originRasterFile, polyFinalFiles[i]) tempRaster.save(rasterFiles[i]) else: print "The %s is not exist in %s" % (filedName, shpFile) return None
def getRasterProperties(rasterObjectPath, newRow): cellSize = 0 for PropertyType in CMDRConfig.Raster_PropertyTypes: try: propValue = arcpy.GetRasterProperties_management( rasterObjectPath, PropertyType) if propValue is not None: propValue = propValue[0] newRow.append(propValue) Utility.addToolMessages() if PropertyType == "CELLSIZEX": cellSize = newRow[len(newRow) - 1] except: Utility.addToolMessages() # Print error message if an error occurs newRow.append(None) return cellSize