def create_idarray(projectionFile, idRasterGermany, clipFeature, idRaster, extendedNationalGrid=True): """ Creates an ID Array for a study area. Creates a new ID raster for Germany, clips it to study area and converts the raster to a one-dimensional numpy array. :Parameters: ------------ projectionFile : string Path to a file containing stereographic projection definition. File type may be Feature Class, Shapefile, prj file or grid. idRasGermany : string Path and name for the output ID raster of Germany to be created. clipFeature : string Path to the clip feature defining the extent of the study area. File type may be Shapefile or Feature Class. If clipFeature == None, the nationwide ID raster will not be clipped. idRas : string Path and name for the output ID raster of the study area to be created. extendedNationalGrid : bool (optional, default: True) True: extended 900 x 1100 national RADOLAN grid, False: 900x900 national grid. :Returns: --------- idArr : one-dimensional numpy array containing ID values of dtype int32 :Note: ------ .. note:: The RADOLAN data are provided in a custom stereographic projection defined by the DWD. As there is no transformation method available yet, it is not possible to directly perform any geoprocessing tasks with RADOLAN and geodata with other spatial references. Nevertheless, ArcGIS is able to perform a correct on-the-fly transformation to display the data together. The clip function uses this as a work-around solution to "push" the clip feature into the RADOLAN projection. Hence, the function works with geodata in different projections, but the locations of the cells might be slightly inaccurate. To use the custom RADOLAN projection as projectionFile for the output ID raster, you can specify the prj-file provided in radproc.sampledata: >>> from radproc.sampledata import get_projection_file_path >>> projectionFile = get_projection_file_path() """ idRasGermany = create_idraster_germany(projectionFile=projectionFile, outRaster=idRasterGermany, extendedNationalGrid=extendedNationalGrid) arcpy.BuildRasterAttributeTable_management (in_raster=idRasGermany, overwrite="Overwrite") if not clipFeature == None: idRas = clip_idraster(idRaster=idRasGermany, clipFeature=clipFeature, outRaster=idRaster) arcpy.BuildRasterAttributeTable_management (in_raster=idRas, overwrite="Overwrite") idArr = import_idarray_from_raster(idRaster=idRas) # if no clipFeature is specified, return the ID array for the national grid else: idArr = import_idarray_from_raster(idRaster=idRasGermany) return idArr
def run(self): self.e.load() arcpy.gp.IsNull_sa(self.i.fm_ras, self.i.std_null) # arcpy.gp.MakeRasterLayer(self.i.std_null, "std_null") arcpy.gp.SetNull_sa(self.i.std_null, self.i.std_null, self.i.mask, """"VALUE" = 0""") arcpy.gp.ExtractByMask_sa(self.i.fdr, self.i.mask, self.i.fdr_mf) arcpy.gp.ExtractByMask_sa(self.i.fill, self.i.mask, self.i.fill_mf) arcpy.gp.ExtractByMask_sa(self.i.fac, self.i.mask, self.i.fac_mf) arcpy.BuildRasterAttributeTable_management(self.i.fac_mf, "Overwrite") arcpy.gp.Plus_sa(self.i.fac_mf, "1", self.i.fac_po) arcpy.BuildRasterAttributeTable_management(self.i.fac_po, "Overwrite")
def overlay_rasters(*rasters): fields = [ os.path.splitext(os.path.basename(r))[0].upper() for r in rasters ] for raster in map(arcpy.Raster, rasters): if not raster.hasRAT: print("\tBuilding RAT for {}".format(raster.catalogPath)) arcpy.BuildRasterAttributeTable_management(raster) combined = arcpy.sa.Combine(rasters) arcpy.MakeRasterLayer_management(combined, "temp") arcpy.BuildRasterAttributeTable_management("temp") return np.array( [row for row in arcpy.da.SearchCursor("temp", fields + ["COUNT"])])
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 arcgis_zstat_points_analysis(self, logger=defaultLogger): #arcpy.ImportToolbox("Model Functions") arcpy.ImportToolbox(TBX_LOCATION) #Split points into separate files intersectParam1 = ( BUFFERS_90M_FILE + ' #;' + (BUFFERS_FOLDER + BUFFER_FILE).format(**self.string_args) + ' #' ) intersectSHP = (TEMP_GRID_FOLDER + 'intersect_lakes.shp').format(**self.string_args) self.string_args['ext'] = 'csv' dbfFile2 = (SAMPLE_DBF_FOLDER + SAMPLE_PTS_FILE).format(**self.string_args) self.string_args['ext'] = 'dbf' arcpy.Buffer_analysis( SAMPLE_POINTS_FILE, BUFFERS_90M_FILE, "90 Meters", "FULL", "ROUND", "NONE", "" ) arcpy.Intersect_analysis(intersectParam1, intersectSHP, "ALL", "", "INPUT") arcpy.AddField_management( intersectSHP, "Zone_FID", "LONG", "", "", "", "", "NULLABLE", "NON_REQUIRED", "" ) arcpy.CalculateField_management(intersectSHP, "Zone_FID", "[FID]", "VB", "") fields = "FID_output;SiteCode;Year;Month;Day;Source;Date_freef;DOC;CDOM;CHL;Zone_FID" arcpy.ExportXYv_stats(intersectSHP, fields, "COMMA", dbfFile2, "NO_FIELD_NAMES") arcpy.gp.toolbox = (TBX_STR) arcpy.gp.SplitLayerByAttributes( intersectSHP, "FID", "FID_", POINTS_SPLIT_FOLDER.format(**self.string_args) ) for [band_name, band_folder] in self.band_parameters: print('Processing band {}'.format(band_name)) self.string_args['band']= band_name outFolder1 = (TEMP_GRID_FOLDER + 'ext_{band}').format(**self.string_args) outFolder2 = (TEMP_GRID_FOLDER + 'calc_{band}').format(**self.string_args) #Iterate through each file created when splitting points for iterationFile in glob(POINTS_SPLIT_FOLDER.format(**self.string_args) + '*.shp'): print(iterationFile) FID = iterationFile.split('\\')[-1].split('.')[0] dbfFile1 = (TEMP_DBF_FOLDER + BANDS_FILE_CALC).format(FID=FID, **self.string_args) arcpy.gp.ExtractByMask_sa(band_folder, iterationFile, outFolder1) arcpy.gp.RasterCalculator_sa("Int(\"{}\" * 0)".format(outFolder1), outFolder2) arcpy.BuildRasterAttributeTable_management(outFolder2, "NONE") arcpy.gp.ZonalStatisticsAsTable_sa( outFolder2, "VALUE", outFolder1, dbfFile1, "DATA", "ALL" ) logger.info('Performed zstat points analysis for scene {scene}'.format(**self.string_args)) return None
def createRefinedTrajectory(data): # mask_fp_2007.run(data) # mask_fp_yfc_potential.run(data) # mask_fp_nlcd_yfc.run(data) # mask_fp_nlcd_ytc.run(data) # mask_fp_yfc.run(data) # mask_fp_ytc.run(data) ##### loop through each of the cdl rasters and make sure nlcd is last # filelist = [data['pre']['traj']['path'], data['refine']['masks_yfc']['path'], data['refine']['masks_ytc']['path'], data['refine']['mask_2007']['path'], data['refine']['mask_nlcd']['path']] # filelist = [data['pre']['traj_yfc']['path'], data['refine']['mask_fp_2007']['path'], data['refine']['mask_fp_yfc_potential']['path'], data['refine']['mask_fp_nlcd_yfc']['path'], data['refine']['mask_fp_nlcd_ytc']['path'], data['refine']['mask_fp_yfc']['path'], data['refine']['mask_fp_ytc']['path']] filelist = [ data['pre']['traj_yfc']['path'], data['refine']['mask_fp_2007']['path'], data['refine']['mask_fp_nlcd_yfc']['path'], data['refine']['mask_fp_nlcd_ytc']['path'], data['refine']['mask_fp_yfc']['path'], data['refine']['mask_fp_ytc']['path'] ] print 'filelist:', filelist ##### mosaicRasters(): arcpy.MosaicToNewRaster_management( filelist, data['pre']['traj_rfnd']['gdb'], data['pre']['traj_rfnd']['filename'], Raster(data['pre']['traj']['path']).spatialReference, '16_BIT_UNSIGNED', data['global']['res'], "1", "LAST", "FIRST") #Overwrite the existing attribute table file arcpy.BuildRasterAttributeTable_management( data['pre']['traj_rfnd']['path'], "Overwrite") # Overwrite pyramids gen.buildPyramids(data['pre']['traj_rfnd']['path'])
def mosaicRasters(map_project, tablename): ######Description: mosiac tiles together into a new raster tilelist = glob.glob( "D:/projects/usxp/deliverables/maps/{}/tiles/*.tif".format( map_project)) print 'tilelist:', tilelist #### need to wrap these paths with Raster() fct or complains about the paths being a string inTraj = Raster( 'C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\ancillary\\raster\\cdl.gdb\\cdl30_2017' ) filename = 'gssurgo_{}_30m'.format(tablename) print 'filename:', filename gdb = 'D:\\projects\\usxp\\deliverables\\maps\\{0}\\{0}.gdb'.format( map_project) ####suboptimal need to detrmine the appropraite datatype with a function!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! datatype = "16_BIT_UNSIGNED" ######mosiac tiles together into a new raster arcpy.MosaicToNewRaster_management(tilelist, gdb, filename, inTraj.spatialReference, datatype, 30, "1", "LAST", "FIRST") #Overwrite the existing attribute table file arcpy.BuildRasterAttributeTable_management( '{0}\\{1}'.format(gdb, filename), "Overwrite")
def multi_convert_to_raster(polygon_fc_list, output_workspace): for polygon_fc in polygon_fc_list: env.extent = polygon_fc env.snapRaster = SNAP_RASTER short_name = os.path.splitext(os.path.basename(polygon_fc))[0] arcpy.AddMessage("Converting {}...".format(short_name)) output_raster = os.path.join(output_workspace, short_name + '_raster') lagoslakeid_field = arcpy.ListFields(polygon_fc, '*lagoslakeid')[0].name zoneid_field = arcpy.ListFields(polygon_fc, '*zoneid')[0].name if lagoslakeid_field: zone_field = lagoslakeid_field else: zone_field = zoneid_field output_raster = arcpy.PolygonToRaster_conversion(polygon_fc, zone_field, output_raster, 'CELL_CENTER', cellsize=CELL_SIZE) arcpy.BuildPyramids_management(output_raster, SKIP_FIRST=True) if lagoslakeid_field: arcpy.BuildRasterAttributeTable_management(output_raster, True) arcpy.AddField_management(output_raster, 'lagoslakeid', 'LONG') arcpy.CalculateField_management(output_raster, 'lagoslakeid', '!Value!', 'PYTHON_9.3') arcpy.AddMessage("Completed.")
def compute_zonal(MFgrid, DEM, z_conversion_factor=1, outfile=None): # Settings output_path = os.path.split(MFgrid)[0] arcpy.env.workspace = output_path arcpy.env.overwriteOutput = True arcpy.env.qualifiedFieldNames = False # Check out any necessary licenses arcpy.CheckOutExtension("spatial") #arcpy.AddField_management(MFgrid,"cellnum","LONG") #isn't needed if there is a node column print "multipling DEM z-values by %s to convert units..." %(z_conversion_factor) DEM2 = arcpy.sa.Raster(DEM)*z_conversion_factor print "extracting model top elevations from DEM..." # create raster of gridcells based on node (cellnum) # run zonal statistics on DEM for each grid cell print "\tbuilding raster of model grid cells for sampling DEM..." DEMres = arcpy.GetRasterProperties_management(DEM2, "CELLSIZEX") DEMres = float(DEMres.getOutput(0)) # cell size of DEM in project units arcpy.PolygonToRaster_conversion(MFgrid, "node", os.path.join(output_path, "MFgrid_raster"), "MAXIMUM_AREA", "node", DEMres) print "\tbuilding raster attribute table..." arcpy.BuildRasterAttributeTable_management(os.path.join(output_path, "MFgrid_raster")) print "\trunning zonal statistics... (this step will likely take several minutes or more)" arcpy.sa.ZonalStatisticsAsTable(os.path.join(output_path, "MFgrid_raster"), "VALUE", DEM2, os.path.join(output_path, "demzstats.dbf"), "DATA", "ALL") print "joining elevations back to model grid cells..." arcpy.MakeFeatureLayer_management(MFgrid, "MFgrid") if not outfile: outfile = os.path.join(output_path, MFgrid[:-4]+'_elevs.shp') general_join(outfile, "MFgrid", "node", os.path.join(output_path, "demzstats.dbf"), "VALUE", True)
def mosiacRasters(covertype): ######Description: mosiac tiles together into a new raster tilelist = glob.glob("C:/Users/Bougie/Desktop/Gibbs/data/tiles/*.tif") print 'tilelist:', tilelist #### need to wrap these paths with Raster() fct or complains about the paths being a string inTraj=Raster("C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\pre\\traj\\v4\\v4_traj.gdb\\v4_traj_cdl30_b_2008to2017") gdb='D:\\projects\\intact_land\\abandoned.gdb' def getFileName(covertype): if covertype=='noncrop': return "abandoned_nd" elif covertype=='crop': return "crop_nd" filename = getFileName(covertype) print 'filename:-----------------------------', filename ######mosiac tiles together into a new raster arcpy.MosaicToNewRaster_management(tilelist, gdb, filename, inTraj.spatialReference, "16_BIT_UNSIGNED", 30, "1", "LAST","FIRST") #######Overwrite the existing attribute table file arcpy.BuildRasterAttributeTable_management(gdb+'\\'+filename, "Overwrite") ########Overwrite pyramids gen.buildPyramids(gdb+'\\'+filename)
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) 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 mosiacRasters(data, subtype, year): ######Description: mosiac tiles together into a new raster tilelist = glob.glob("C:/Users/Bougie/Desktop/Gibbs/tiles/*.tif") print 'tilelist:', tilelist #### need to wrap these paths with Raster() fct or complains about the paths being a string inTraj = Raster(data['pre']['traj']['path']) gdb = data['post']['ytc']['gdb'] filename = '{}_{}'.format(data['post']['ytc'][subtype]['filename'], year) print 'filename:', filename ######mosiac tiles together into a new raster arcpy.MosaicToNewRaster_management(tilelist, gdb, filename, inTraj.spatialReference, "16_BIT_UNSIGNED", 30, "1", "LAST", "FIRST") #Overwrite the existing attribute table file arcpy.BuildRasterAttributeTable_management('{}//{}'.format(gdb, filename), "Overwrite") # Overwrite pyramids gen.buildPyramids('{}//{}'.format(gdb, filename))
def adjust2012wra(outDir, unWra2012, iso3): arcpy.CheckOutExtension("Spatial") old_workspace = arcpy.env.workspace old_scratchWorkspace = arcpy.env.scratchWorkspace tmpdir = tempfile.mkdtemp() arcpy.env.workspace = tmpdir arcpy.env.scratchWorkspace = tmpdir try: unadjustedWraRast = os.path.join(outDir, "%s2012unadjustedWRA.tif" % iso3) # Calculate total wra rasterMask = arcpy.Raster(unadjustedWraRast) * 0 arcpy.CopyRaster_management(rasterMask, "rasterMask.tif", pixel_type="1_BIT") arcpy.BuildRasterAttributeTable_management("rasterMask.tif", "Overwrite") arcpy.sa.ZonalStatisticsAsTable("rasterMask.tif", "VALUE", unadjustedWraRast, "in_memory/zonalStats", "DATA", "SUM") with arcpy.da.SearchCursor("in_memory/zonalStats", "SUM") as c: for row in c: unadjustedTotal = row[0] # Calculate adjusted WRA raster adjustmentFactor = unWra2012 / unadjustedTotal adjustedWra = arcpy.sa.Times(unadjustedWraRast, adjustmentFactor) arcpy.CopyRaster_management(adjustedWra, os.path.join(outDir, "%s2012adjustedWRA.tif" % iso3), pixel_type="32_BIT_FLOAT") finally: arcpy.Delete_management("in_memory") arcpy.Delete_management(tmpdir) arcpy.env.workspace = old_workspace arcpy.env.scratchWorkspace = old_scratchWorkspace arcpy.CheckInExtension("Spatial")
def createRefinedTrajectory(data): ##### create a file list of all the masked datasets filelist = [ data['pre']['traj_yfc']['path'], data['refine']['mask_fp_2007']['path'], data['refine']['mask_fp_nlcd_yfc']['path'], data['refine']['mask_fp_nlcd_ytc']['path'], data['refine']['mask_fp_yfc']['path'], data['refine']['mask_fp_ytc']['path'] ] print 'filelist:', filelist ##### mosaicRasters(): arcpy.MosaicToNewRaster_management( filelist, data['pre']['traj_rfnd']['gdb'], data['pre']['traj_rfnd']['filename'], Raster(data['pre']['traj']['path']).spatialReference, '16_BIT_UNSIGNED', data['global']['res'], "1", "LAST", "FIRST") #Overwrite the existing attribute table file arcpy.BuildRasterAttributeTable_management( data['pre']['traj_rfnd']['path'], "Overwrite") # Overwrite pyramids gen.buildPyramids(data['pre']['traj_rfnd']['path'])
def mosiacRasters(): ######Description: mosiac tiles together into a new raster tilelist = glob.glob( "D:/projects/usxp/deliverables/maps/suitability/tiles/*.tif") print 'tilelist:', tilelist #### need to wrap these paths with Raster() fct or complains about the paths being a string inTraj = Raster( 'C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\ancillary\\raster\\cdl.gdb\\cdl30_2017' ) filename = 'gssurgo_nicc_30m' print 'filename:', filename gdb = 'D:\\projects\\usxp\\deliverables\\maps\\suitability\\suitability.gdb' ######mosiac tiles together into a new raster arcpy.MosaicToNewRaster_management(tilelist, gdb, filename, inTraj.spatialReference, "16_BIT_UNSIGNED", 30, "1", "LAST", "FIRST") #Overwrite the existing attribute table file arcpy.BuildRasterAttributeTable_management( '{0}\\{1}'.format(gdb, filename), "Overwrite") # Overwrite pyramids gen.buildPyramids('{0}\\{1}'.format(gdb, filename))
def clipGridByBuffer(inReportingUnitFeature, outName, inLandCoverGrid, inBufferDistance=None): if arcpy.Exists(outName): arcpy.Delete_management(outName) if inBufferDistance: # Buffering Reporting unit features... cellSize = Raster(inLandCoverGrid).meanCellWidth linearUnits = arcpy.Describe( inLandCoverGrid).spatialReference.linearUnitName bufferFloat = cellSize * (int(inBufferDistance) + 1) bufferDistance = "%s %s" % (bufferFloat, linearUnits) arcpy.Buffer_analysis(inReportingUnitFeature, "in_memory/ru_buffer", bufferDistance, "#", "#", "ALL") # Clipping input grid to desired extent... if inBufferDistance: clippedGrid = arcpy.Clip_management(inLandCoverGrid, "#", outName, "in_memory/ru_buffer", "", "NONE") arcpy.Delete_management("in_memory") else: clippedGrid = arcpy.Clip_management(inLandCoverGrid, "#", outName, inReportingUnitFeature, "", "NONE") arcpy.BuildRasterAttributeTable_management(clippedGrid, "Overwrite") return clippedGrid
def mosiacRasters(data, yxc, subtype): ######Description: mosiac tiles together into a new raster tilelist = glob.glob("C:/Users/Bougie/Desktop/Gibbs/data/tiles/*.tif") print 'tilelist:', tilelist # tilelist_complete=['C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\sa\\r2\\s35\\post\\yfc_s35.gdb\\s35_yfc30_2008to2017_mmu5_fnc_61'] + tilelist # print tilelist_complete #### need to wrap these paths with Raster() fct or complains about the paths being a string inTraj = Raster(data['pre']['traj']['path']) mask_61_filename = 'fnc_61_mask' mask_61_path = 'C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\sa\\r2\\s35\\post\\yfc_s35.gdb' + '\\' + mask_61_filename # path_yfc_fnc_61 = 'C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\sa\\r2\\s35\\post\\yfc_s35.gdb\\s35_yfc30_2008to2017_mmu5_fnc_61' ######mosiac tiles together into a new raster arcpy.MosaicToNewRaster_management(tilelist, data['post'][yxc]['gdb'], mask_61_filename, inTraj.spatialReference, "16_BIT_UNSIGNED", 30, "1", "LAST", "FIRST") #Overwrite the existing attribute table file arcpy.BuildRasterAttributeTable_management(mask_61_path, "Overwrite") # Overwrite pyramids gen.buildPyramids(mask_61_path)
def mosiacRasters(): ######Description: mosiac tiles together into a new raster tilelist = glob.glob("C:/Users/Bougie/Desktop/Gibbs/tiles/*.tif") print 'tilelist:', tilelist for tile in tilelist: print tile yo = tile.split('_') hi = yo[2].split('.') print hi[0] arcpy.BuildRasterAttributeTable_management(Raster(tile)) arcpy.AddField_management(Raster(tile), field_name='tile', field_type='TEXT') arcpy.AddField_management(Raster(tile), field_name='obj_id', field_type='TEXT') # # x = '{}_{}'.format(str(fc_count),str("!Value!")) # x = '{}_{}'.format("fdfdff_",str("!Value!")) expression = "{}".format(hi[0]) arcpy.CalculateField_management(tile, "tile", expression, "PYTHON_9.3") # expression:"{}_{}".format(!tile!, !OID!) arcpy.CalculateField_management(tile, "obj_id", "!tile! + '_' + str(!OID!)", "PYTHON_9.3")
def mosiacRasters(schema): ######Description: mosiac tiles together into a new raster tilelist = glob.glob( "D:/projects/usxp/series/s35/maps/{}/tiles/*.tif".format(schema)) print 'tilelist:', tilelist #### need to wrap these paths with Raster() fct or complains about the paths being a string inTraj = Raster( 'E:\\data\\gSSURGO\\gSSURGO_CONUS_10m.gdb\\MapunitRaster_30m') filename = 'gssurgo_{}_30m'.format(schema) print 'filename:', filename gdb = 'D:\\projects\\usxp\\series\\s35\\maps\\{0}\\{0}.gdb'.format(schema) ######mosiac tiles together into a new raster arcpy.MosaicToNewRaster_management(tilelist, gdb, filename, inTraj.spatialReference, "16_BIT_UNSIGNED", 30, "1", "LAST", "FIRST") #Overwrite the existing attribute table file arcpy.BuildRasterAttributeTable_management( '{0}\\{1}'.format(gdb, filename), "Overwrite") # Overwrite pyramids gen.buildPyramids('{0}\\{1}'.format(gdb, filename))
def parallel_create_raster_attribute_table(f): print('worker', os.getpid(), ' running and doing ', f) jnk=randrange(1000000) tmp_dir="D:/temp/arcgis/"+str(jnk)+"/" if not os.path.exists(tmp_dir): os.mkdir(tmp_dir) arcpy.env.scratchWorkspace = tmp_dir arcpy.BuildRasterAttributeTable_management(f, "Overwrite")
def join_attributes_from_info(self, raster, raster_join_field, attribute_join_field, info_table, field_list): # Join the attributes to the raster print('Joining attributes to ' + raster) arcpy.BuildRasterAttributeTable_management(raster) arcpy.JoinField_management(raster, raster_join_field, info_table, attribute_join_field, field_list)
def run_combine(in_directory_species_grids, raster_list_combine, folder, out_folder, region): # sets workspace to locations with input species rasters and generates list arcpy.env.workspace = in_directory_species_grids + os.sep + folder sp_list = arcpy.ListRasters() # combine each species file with the habitat and elevation rasters for spe_raster in sp_list: # loops over each species c_raster_list = raster_list_combine # makes a copy of list with the use layers for combine if spe_raster.split( "_" )[1] in skip_species: # bypasses species in the species skip list set by user continue else: print('Start Generating species overlap files' ) # print for tracking start_raster = datetime.datetime.now( ) # elapse clock for species combine # path species file in_spe = in_directory_species_grids + os.sep + folder + os.sep + spe_raster c_raster_list.insert(0, in_spe) try: # try except loop so an error won't cause the script to stop if not arcpy.Exists(out_folder + os.sep + spe_raster ): # skips files that already exist # inset the path to the species file to list of raster to combine in index position 0 print('Start Combine for {0} with {1}'.format( spe_raster, c_raster_list)) # print for tracking print("Out location will be {0}".format( os.path.join(out_folder, spe_raster[:8]))) out_combine = Combine( c_raster_list ) # runs combine tools from spatial analyst # saves combine; set out name to 8 character to the limit for a ESRI grid out_combine.save(os.path.join(out_folder, spe_raster[:8])) # print for tracking print 'Saved {0} \n Build raster attribute table'.format( out_folder + os.sep + spe_raster[:8]) # Builds raster attribute table and pyramids arcpy.BuildRasterAttributeTable_management( os.path.join(out_folder, spe_raster[:8])) print 'Build pyramids' arcpy.BuildPyramids_management(out_folder + os.sep + spe_raster[:8]) # generates output text files used as lookups to identify habitat, elevation and species output_lookup_tables(spe_raster, c_raster_list, out_folder, region) print 'Completed {0} in {1}'.format( out_folder + os.sep + spe_raster[:8], datetime.datetime.now() - start_raster) except Exception as error: # exceptions for if error trips; print error and deleted any temp file generated print('Error was {0} elapse time was {1}'.format( error.args[0], datetime.datetime.now() - start_raster)) if arcpy.Exists(out_folder + os.sep + spe_raster[:8]): arcpy.Delete_management(out_folder + os.sep + spe_raster[:8]) c_raster_list.remove(in_spe)
def build_attribute(year): print('{} begin'.format(year)) for i in range(0, 224): tif_url = '{0}/urban{1}/URBAN_{2}_{3}.tif'.format(urban_data_base_dir, year, year, i) try: arcpy.BuildRasterAttributeTable_management(tif_url) print 'build ' + tif_url + ' successfully!' except Exception: print 'build ' + tif_url + ' fail!'
def getUniqueValues(fR): try: fR = Int(Raster(fR)) arcpy.BuildRasterAttributeTable_management(fR, "NONE") unqVals = [] for r in arcpy.SearchCursor(fR): unqVals.append(r.getValue('Value')) return (unqVals) except: raise (TypeError( "The input categorical raster cannot have a raster table", fR))
def build_attribute_table(workspace, in_raster): # Set environment settings env.workspace = workspace # Set local variables field_name = 'mukey' expression = 'str(!VALUE!)' exp_type = 'PYTHON_9.3' arcpy.BuildRasterAttributeTable_management(in_raster) arcpy.AddField_management(in_raster, 'mukey', 'text', field_length=30) arcpy.CalculateField_management(in_raster, field_name, expression, exp_type)
def mosiacRasters(prg): # root_in = 'C:\\Users\\Bougie\\Desktop\\Gibbs\\tiles\\' # roo tilelist = glob.glob(prg.dir_tiles+"*.tif") print 'mtr tilelist:', tilelist ######mosiac tiles together into a new raster arcpy.MosaicToNewRaster_management(tilelist, prg.gdb_child, prg.raster_child, Raster(prg.path_parent).spatialReference, prg.pixel_type, prg.res, "1", "LAST","FIRST") #Overwrite the existing attribute table file arcpy.BuildRasterAttributeTable_management(prg.path_child, "Overwrite") # Overwrite pyramids gen.buildPyramids(prg.path_child)
def mosiacRasters(): tilelist = glob.glob(nibble.dir_tiles+'*.tif') print tilelist ######mosiac tiles together into a new raster nbl_raster = nibble.mask_name + '_nbl' print 'nbl_raster: ', nbl_raster arcpy.MosaicToNewRaster_management(tilelist, nibble.gdb_path, nbl_raster, Raster(nibble.in_raster).spatialReference, nibble.pixel_type, nibble.res, "1", "LAST","FIRST") ##Overwrite the existing attribute table file arcpy.BuildRasterAttributeTable_management(nibble.gdb_path + nbl_raster, "Overwrite") ## Overwrite pyramids gen.buildPyramids(nibble.gdb_path + nbl_raster)
def adjustedBirthsEstimates(unBirthsXls, iso3, year, outputDir): arcpy.CheckOutExtension("Spatial") old_workspace = arcpy.env.workspace old_scratchWorkspace = arcpy.env.scratchWorkspace tmpdir = tempfile.mkdtemp() arcpy.env.workspace = tmpdir arcpy.env.scratchWorkspace = tmpdir try: unadjustedBirthsRast = os.path.join( outputDir, "%s%sunadjustedBirths.tif" % (iso3, year)) # Calculate total births rasterMask = arcpy.Raster(unadjustedBirthsRast) * 0 arcpy.CopyRaster_management(rasterMask, "rasterMask.tif", pixel_type="1_BIT") arcpy.BuildRasterAttributeTable_management("rasterMask.tif", "Overwrite") arcpy.sa.ZonalStatisticsAsTable("rasterMask.tif", "VALUE", unadjustedBirthsRast, "in_memory/zonalStats", "DATA", "SUM") with arcpy.da.SearchCursor("in_memory/zonalStats", "SUM") as c: for row in c: unadjustedTotal = row[0] # Retrieve UN total from spreadsheet wb = xlrd.open_workbook(unBirthsXls) ws = wb.sheet_by_name("MEDIUM") for row in range(1, ws.nrows): if ws.cell_value(row, 4) == iso3 and int(ws.cell_value(row, 2)) == year: unTotal = ws.cell_value(row, 3) * 1000 # Calculate adjusted births raster adjustmentFactor = unTotal / unadjustedTotal adjustedBirths = arcpy.sa.Times(unadjustedBirthsRast, adjustmentFactor) arcpy.CopyRaster_management( adjustedBirths, os.path.join(outputDir, "%s%sadjustedBirths.tif" % (iso3, year)), pixel_type="32_BIT_FLOAT") finally: arcpy.Delete_management("in_memory") arcpy.Delete_management(tmpdir) arcpy.env.workspace = old_workspace arcpy.env.scratchWorkspace = old_scratchWorkspace
def create_state_ssurgo(state, replace): state_lcc_dir = constants.lcc_dir + state + os.sep state_ssurgo = state_lcc_dir + state + 'ssurgo' lu_ssurgo = state_lcc_dir + state + '_lu_ssurgo' out_state_sgo = state_lcc_dir + state + '_sgo_' + constants.METRIC.lower() # Join with LCC csv if arcpy.Exists(out_state_sgo) and not (replace): pass else: arcpy.BuildRasterAttributeTable_management(state_ssurgo, "Overwrite") try: if (constants.METRIC == 'LCC'): arcpy.JoinField_management(state_ssurgo, "VALUE", constants.LCC_CR, "mukey", "") else: # DI or PI arcpy.JoinField_management(state_ssurgo, "VALUE", DI_PI_CR, "mukey", "") except: logging.info(arcpy.GetMessages()) # Lookup to create new raster with new VALUE field # Execute Lookup lup_column = '' remap_file = '' if (constants.METRIC == 'LCC'): lup_column = 'NICCDCD' remap_file = constants.SSURGO_REMAP_FILE elif (constants.METRIC == 'PI'): lup_column = 'PI' remap_file = constants.PI_REMAP_FILE elif (constants.METRIC == 'DI'): lup_column = 'DI' remap_file = constants.DI_REMAP_FILE lu_tmp = Lookup(state_ssurgo, lup_column) # Save the output lu_tmp.save(lu_ssurgo) # Reclass raster to group LCC values into 3 classes: # Productive, Moderate and Marginal out_reclass = ReclassByASCIIFile(lu_ssurgo, remap_file, "NODATA") out_reclass.save(out_state_sgo) logging.info('\t SSURGO state ' + state) return out_state_sgo
def mosiacRasters(): tilelist = glob.glob("C:/Users/Bougie/Desktop/Gibbs/tiles/*.tif") print tilelist ######mosiac tiles together into a new raster arcpy.MosaicToNewRaster_management( tilelist, yxc.gdb_path, yxc.ras_name + '_' + yxc.subtype + '_fnl', yxc.in_raster.spatialReference, "8_BIT_UNSIGNED", "30", "1", "LAST", "FIRST") ##Overwrite the existing attribute table file arcpy.BuildRasterAttributeTable_management( yxc.gdb_path + yxc.ras_name + '_' + yxc.subtype + '_fnl', "Overwrite") ## Overwrite pyramids gen.buildPyramids(yxc.gdb_path + yxc.ras_name + '_' + yxc.subtype + '_fnl')