def mosaic(workspace, in_rasters, out_location, out_raster): # Description: Mosaics rasters together, ignoring background/nodata cells # Set environment settings env.workspace = workspace # Set local variables coordinate_system = arcpy.SpatialReference( "NAD 1983 Contiguous USA Albers") data_type = '32_BIT_SIGNED' cell_size = '10' bands = '1' pyramids = 'PYRAMIDS -1 NEAREST JPEG' mosaic_type = 'FIRST' colormap = 'FIRST' background = 0 # CreateRasterDataset_management (out_path, out_name, {cellsize}, # pixel_type, {raster_spatial_reference}, number_of_bands, {config_keyword}, # {pyramids}, {tile_size}, {compression}, {pyramid_origin}) arcpy.CreateRasterDataset_management(out_location, out_raster, cell_size, data_type, coordinate_system, bands, '', pyramids, '', '', '') #Mosaic_management (inputs, target, {mosaic_type}, # {colormap}, {background_value}, {nodata_value}, # {onebit_to_eightbit}, {mosaicking_tolerance}, {MatchingMethod}) arcpy.Mosaic_management(in_rasters, out_location + out_raster, mosaic_type, '', background, background, '', '', '')
def mosiacRasters(): ######Description: mosiac tiles together into a new raster tilelist = glob.glob("C:/Users/Bougie/Desktop/Gibbs/tiles/*.tif") print tilelist arcpy.env.workspace = defineGDBpath(['refine', 'masks']) arcpy.env.extent = nibble.inYTC.extent arcpy.env.snapRaster = nibble.inYTC arcpy.env.cellsize = nibble.inYTC arcpy.env.outputCoordinateSystem = nibble.inYTC # mosaic = 'traj_ytc30_2008to2015_mask' masks_gdb = defineGDBpath(['refine', 'masks']) out_name = nibble.inTraj_name + '_msk36and61_temp' outpath = masks_gdb + out_name ##### CreateRasterDataset_management (out_path, out_name, cellsize=30, pixel_type, raster_spatial_reference, number_of_bands) arcpy.CreateRasterDataset_management(masks_gdb, out_name, 30, "8_BIT_UNSIGNED", nibble.inTraj.spatialReference, 1, "", "", "", "", "") ##### Mosaic_management (inputs, target, {mosaic_type}, {colormap}, {background_value}, {nodata_value}, {onebit_to_eightbit}, {mosaicking_tolerance}, {MatchingMethod}) arcpy.Mosaic_management(tilelist, outpath, "", "", "", 0, "", "", "") ##### copy raster so it "snaps" to the other datasets -------suboptimal ##### CopyRaster_management (in_raster, out_rasterdataset, {config_keyword}, {background_value}, {nodata_value}, {onebit_to_eightbit}, {colormap_to_RGB}, {pixel_type}, {scale_pixel_value}, {RGB_to_Colormap}, {format}, {transform}) arcpy.CopyRaster_management(outpath, nibble.inTraj_name + '_msk36and61') ##### delete the initial raster arcpy.Delete_management(outpath)
def mos1(root,part,imglist): #第一个函数,输入栅格影像根目录root,目标文件夹part和栅格数据名列表 target = root+part+imglist[0] for i in imglist: inputs = root + "/" + i arcpy.Mosaic_management(inputs,target,'','','',255) print("Mosaic %s dem" %i)
def MosaicImages(filePath, target): try: arcpy.Mosaic_management(filePath, target, "LAST", "FIRST", "", "", "NONE", "0", "NONE") except Exception as inst: ST = CommonTasks.ScheduledTask(layerFolder,inst,LogFile,arcpy.GetMessages()) ST.SendErrorMail() ST.UpdateLogFile()
def mosaic(year,tif_id_list): for tif_id in tif_id_list: grid_id = int(id_grid_dict[tif_id]) # 把tif_id镶嵌到grid_id中 input_file = './{0}/{1}.tif'.format(year, tif_id) target_file = '{0}/urban{1}/URBAN_{2}_{3}.tif'.format(urban_data_base_dir, year, year, grid_id) arcpy.Mosaic_management(input_file, target_file, mosaic_type='LAST') print('{0} -> {1} mosaic ok'.format(input_file, target_file))
def mosaicRasters(in_ws, out_ws): env.workspace = in_ws in_rasters = arcpy.ListRasters() for raster in in_rasters: message = "Mosaicking %s to %s" % (raster, os.path.join( out_ws, raster)) say(message) arcpy.Mosaic_management([os.path.join(out_ws, raster), raster], os.path.join(out_ws, raster), "LAST", "LAST", "", "", "NONE")
def exportbands(timeband, inputproperties, inputfilepath, outputpath): ''' Within the NetCDF processing loop we create raster files and save them to new folders. Take date arguments... Along with the dimension value, the input file, and the output file. ''' # Grab a string value and the date value from current band. dimension_date, dimension_value = sub_getdatefromcurrentband( inputproperties, timeband) # Parse date variable of current file into logical chunks. # The string is in the Month/Day/Year format: filemonth, fileday, fileyear = sub_processdatesfromnetcdf(dimension_date) # Make filename from year, month, and day: arguments = [noaafile_type, fileyear, filemonth, fileday] outputrastername = '_'.join(map(str, arguments)) print('Exporting raster {}.tif'.format(outputrastername)) # Take CURRENT dimensions from current NetCDF. # Pull current layer out as raster layer in memory named 'outfilename'. # rainfall (PRATE) is the value that will be mapped. makenetcdf = arcpy.MakeNetCDFRasterLayer_md(inputfilepath, "prate", "lon", "lat", "temporaryraster", "", dimension_value, "BY_VALUE") # Convert in-memory raster layer to a saved layer, also named 'outfilename'. outputrasterfile = os.path.join(outputpath, ''.join([outputrastername, '.tif'])) arcpy.AddMessage("Writing " + outputrastername) # Resample raster such that resultant raster has half the X and Y dimensions # This will make each raster having second column perfectly aligned by prime meridian arcpy.Resample_management("temporaryraster", outputrasterfile, "0.9375 0.94747340425532", "NEAREST") # Clip first column of each raster (the only column that lies in -X dimension) arcpy.Clip_management(outputrasterfile, "-0.9375 -90.054779 0 88.070221", "clip", "", "", "NONE", "NO_MAINTAIN_EXTENT") # Mosaic the first column of each raster to the original raster # ArcGIS will automatically take care of converting -ve X values to +ve arcpy.Mosaic_management("clip", outputrasterfile, "LAST", "LAST", "0", "9", "", "", "") print makenetcdf.getMessages() arcpy.Delete_management(outputrastername) arcpy.Delete_management("in_memory")
def MosaicImages(filePath, target): try: arcpy.Mosaic_management(filePath, target, "LAST", "FIRST", "", "", "NONE", "0", "NONE") print "Successfully Done " + filePath + " --- processed time:" + datetime.now( ).strftime("%Y-%m-%d %H %M %S") except Exception as inst: print "Fails to process " + filePath + " --- failed time:" + datetime.now( ).strftime("%Y-%m-%d %H %M %S") ST = ScheduledTask(filePath, inst, vLogFile, arcpy.GetMessages()) ST.SendErrorMail() ST.UpdateLogFile()
def mosaicCDL(year,res): tif_array = getFilesRecursively(year,res) print tif_array if not tif_array: print("List is empty") else: filename = 'cdl'+res+'_'+year createEmptyRaster(filename) if len(tif_array) == 1: stringit = tif_array[0] print stringit arcpy.Mosaic_management(inputs=stringit, target="D:/projects/ksu/cdl.gdb/"+filename, background_value=0, nodata_value=0) # projectRaster("D:/projects/ksu/cdl.gdb/"+filename) elif len(tif_array) > 1: stringit = ';'.join(tif_array) print stringit arcpy.Mosaic_management(inputs=stringit, target="D:/projects/ksu/cdl.gdb/"+filename, background_value=0, nodata_value=0)
def mosaic_rasters_to_output_gdb(in_workspaces, out_workspace): arcpy.env.workspace = out_workspace dict = {} for raster in arcpy.ListRasters(): rasters = [] rasters.append(os.path.join(out_workspace, raster)) for workspace in in_workspaces: rasters.append(os.path.join(workspace, raster)) dict[raster] = rasters for key, values in dict.iteritems(): message = 'Mosaicking %s with %s' % (values[0], values[1:]) say(message) arcpy.Mosaic_management(values, os.path.join(out_workspace, key), "LAST", "", "", "", "NONE") return dict
def evt_merge(x): print 'Starting EVT merge for ' + x hu8 = os.path.join(pf_path, x, 'NHD/WBDHU8.shp') evt_new = os.path.join(pf_path, x, 'LANDFIRE/LANDFIRE_200EVT.tif') evt_old = os.path.join(pf_path, x, 'LANDFIRE/LANDFIRE_140EVT.tif') evt_erase = os.path.join(pf_path, x, 'LANDFIRE/EVT_2016_Erase.shp') evt_clip_name = os.path.join(pf_path, x, 'LANDFIRE/EVT_2014_clip.tif') evt_new_shp = os.path.join(pf_path, x, 'LANDFIRE/LANDFIRE_200EVT.shp') print ' Converting LANDFIRE 200EVT to shapefile...' arcpy.RasterToPolygon_conversion(evt_new, evt_new_shp) print ' Erasing LANDFIRE 200EVT coverage from watershed boundary & buffering...' arcpy.Erase_analysis(hu8, evt_new_shp, evt_erase) evt_erase_buffer = os.path.join(pf_path, x, 'LANDFIRE/EVT_2016_Erase_30m.shp') arcpy.Buffer_analysis(evt_erase, evt_erase_buffer, "30 Meters") print ' Clipping LANDFIRE 140EVT to watershed area not covered by 200 EVT...' clip = arcpy.Clip_management(in_raster=evt_old, rectangle=None, out_raster=evt_clip_name, in_template_dataset=evt_erase_buffer, nodata_value='', clipping_geometry='ClippingGeometry') print ' Creating dataset to store output...' evt_output = arcpy.CreateRasterDataset_management(os.path.join(pf_path, x, 'LANDFIRE'), evt_out_name_with_ext, None, '16_BIT_SIGNED', outCS, 1) print ' Merging LANDFIRE EVT rasters...' arcpy.Mosaic_management(inputs=[clip, evt_new], target=evt_output, mosaic_type="LAST") print ' Creating attribute table for merged output...' arcpy.BuildRasterAttributeTable_management(evt_output) arcpy.JoinField_management(evt_output, 'Value', proj_evt140, 'VALUE') arcpy.JoinField_management(evt_output, 'Value', proj_evt200, 'VALUE') with arcpy.da.UpdateCursor(evt_output, field_names = ['EVT_NAME', 'CLASSNAME', 'EVT_CLASS', 'EVT_CLAS_1', 'EVT_PHYS', 'EVT_PHYS_1']) as cursor: for row in cursor: if row[0] == ' ': row[0] = row[1] else: pass if row[1] == ' ': row[1] = row[0] else: pass if row[2] == ' ': row[2] = row[3] else: pass if row[4] == ' ': row[4] = row[5] else: pass cursor.updateRow(row) lu_code.main(evt_output)
def repair(raster_dict, ghs_area, city_code, year): '''阈值计算和mosaic ''' year_config = config[year] threshold_max = 10000 threshold_min = 1000 threshold_step = 100 actual_area = 0 target_area = float(ghs_area) print('******* city_code', city_code, 'begin find threshold') threshold_value = threshold_max array_list = [raster_dict[key]['array'] for key in raster_dict.keys()] array_list_b = [array > threshold_value for array in array_list] while (actual_area < target_area) and (threshold_value >= threshold_min): array_list_b = [array > threshold_value for array in array_list] sum_list = [array.sum() * 0.0009 for array in array_list_b] actual_area = sum(sum_list) threshold_value -= threshold_step print('city_code', city_code, 'find threshold', threshold_value, 'target_area', target_area, 'actual_area', actual_area,'threshold_value', threshold_value) if actual_area < target_area * 0.5: print('-------outliar', 'city_code', city_code, '-------------') fo.write('\t'.join([str(city_code), str(actual_area), str(target_area), str(threshold_value), str(year)]) + '\n') for key in raster_dict.keys(): raster = raster_dict[key]['raster'] lower_left = arcpy.Point(raster.extent.XMin,raster.extent.YMin) cell_size = raster.meanCellWidth dsc = arcpy.Describe(raster) sr = dsc.SpatialReference no_data_value = raster.noDataValue array = raster_dict[key]['array'] array_b = array > threshold_value new_raster = arcpy.NumPyArrayToRaster(array_b.astype(array.dtype), lower_left, cell_size, cell_size, value_to_nodata=config['no_data_val']) arcpy.DefineProjection_management(new_raster, sr) #mosaic mosaic_target_path = year_config['mosaic_data_dir'] + year_config['mosaic_base_name'] + str(key) + '.tif' arcpy.Mosaic_management(new_raster, mosaic_target_path, "LAST", "FIRST", "", "", "", "", "") print('mosaic', mosaic_target_path, 'ok')
def mosiacStack(index,type,params): createEmptyRaster() files_list = [] for raster in arcpy.ListDatasets("*fc", "Raster"): print raster files_list.append(raster) files_in=';'.join(files_list) print files_in # file_out=ds.dir_out+'unsign8.img' file_out='ytc_fc_mosaic' print 'file_out: ', file_out # Save the output arcpy.Mosaic_management(files_in,file_out,"LAST","FIRST","0", "0", "", "", "")
def replace_tif(row, src_year, target_year): # src_year的数据路径 data_dir_src = dir_dict[src_year]['data_dir'] data_file_base_src = dir_dict[src_year]['data_file_base'] # target_year的数据路径 data_dir_target = dir_dict[target_year]['data_dir'] data_file_base_target = dir_dict[target_year]['data_file_base'] mask = row.getValue('Shape') grid_code = row.getValue('code') grid_code_file_target = data_dir_target + data_file_base_target + grid_code + '.tif' grid_raster_target = arcpy.Raster(grid_code_file_target) city_raster_target = arcpy.sa.ExtractByMask(grid_raster_target, mask) grid_code_file_src = data_dir_src + data_file_base_src + grid_code + '.tif' arcpy.Mosaic_management(city_raster_target, grid_code_file_src, "LAST", "FIRST", "", "", "", "", "")
def bps_merge(x): print 'Starting BPS merge for ' + x hu8 = os.path.join(pf_path, x, 'NHD/WBDHU8.shp') bps_new = os.path.join(pf_path, x, 'LANDFIRE/LANDFIRE_200BPS.tif') bps_old = os.path.join(pf_path, x, 'LANDFIRE/LANDFIRE_140BPS.tif') bps_erase = os.path.join(pf_path, x, 'LANDFIRE/BPS_2016_Erase.shp') bps_clip_name = os.path.join(pf_path, x, 'LANDFIRE/BPS_2014_clip.tif') bps_new_shp = os.path.join(pf_path, x, 'LANDFIRE/LANDFIRE_200BPS.shp') print ' Converting LANDFIRE 200BPS to shapefile...' arcpy.RasterToPolygon_conversion(bps_new, bps_new_shp) print ' Erasing LANDFIRE 200BPS coverage from watershed boundary & buffering...' arcpy.Erase_analysis(hu8, bps_new_shp, bps_erase) bps_erase_buffer = os.path.join(pf_path, x, 'LANDFIRE/BPS_2016_Erase_30m.shp') arcpy.Buffer_analysis(bps_erase, bps_erase_buffer, "30 Meters") print ' Clipping LANDFIRE 140BPS to watershed area not covered by 200BPS...' clip = arcpy.Clip_management(in_raster=bps_old, rectangle=None, out_raster=bps_clip_name, in_template_dataset=bps_erase_buffer, nodata_value='', clipping_geometry='ClippingGeometry') print ' Creating dataset to store output...' bps_output = arcpy.CreateRasterDataset_management(os.path.join(pf_path, x, 'LANDFIRE'), bps_out_name_with_ext, None, '16_BIT_SIGNED', outCS, 1) print ' Merging LANDFIRE BPS rasters...' arcpy.Mosaic_management(inputs=[clip, bps_new], target=bps_output, mosaic_type="LAST") print ' Creating attribute table for merged output...' arcpy.BuildRasterAttributeTable_management(bps_output) arcpy.JoinField_management(bps_output, 'Value', proj_bps140, 'VALUE') arcpy.JoinField_management(bps_output, 'Value', proj_bps200, 'VALUE') with arcpy.da.UpdateCursor(bps_output, field_names = ['BPS_NAME', 'BPS_NAME_1', 'GROUPVEG', 'GROUPVEG_1']) as cursor: for row in cursor: if row[0] == ' ': row[0] = row[1] else: pass if row[1] == ' ': row[1] = row[0] else: pass if row[2] == ' ': row[2] = row[3] else: pass cursor.updateRow(row)
def CalculateMajorityPercentage(inraster, outraster, snapraster, values, thresh): """for each category, calculate the percentage that is majority type""" print "calculate majority percentage" arcpy.env.snapraster = snapraster outrc = ['%s%d' % (inraster, value) for value in values] for value, rcfile in zip(values, outrc): isval = arcpy.sa.EqualTo(inraster, value) outBlockStat = arcpy.sa.BlockStatistics( isval, arcpy.sa.NbrRectangle(250, 250, "MAP"), 'MEAN', "DATA") outBlockStat.save(rcfile) print '\t' + rcfile arcpy.Mosaic_management(string.join(outrc, sep=';'), outrc[0], "MAXIMUM") keep = arcpy.sa.Reclassify( outrc[0], "Value", arcpy.sa.RemapRange([[0, thresh, 0], [thresh, 1, 1]]), 'NODATA') refined = arcpy.sa.Times(keep, inraster) refined.save(outraster) [arcpy.Delete_management(rcfile) for rcfile in outrc]
def execute_RunSim(str_zonefolder, str_simfolder, str_lisfloodfolder, r_q, str_lakes, field_z, voutput, simtime, channelmanning, r_zbed, messages): str_inbci = str_zonefolder + "\\inbci.shp" str_outbci = str_zonefolder + "\\outbci.shp" zbed = RasterIO(r_zbed) discharge = RasterIO(r_q) # count est utilisé pour compter le nombre de zones, pour la barre de progression lors des simulations count = 0 bcipointcursor = arcpy.da.SearchCursor(str_inbci, ["SHAPE@", "zoneid", "flowacc", "type", "fpid"]) dictsegmentsin = {} for point in bcipointcursor: if point[1] not in dictsegmentsin: dictsegmentsin[point[1]] = [] dictsegmentsin[point[1]].append(point) allzones = list(dictsegmentsin.keys()) allzones.sort() dictzones_fp = {} for zone in allzones: if dictsegmentsin[zone][0][4] not in dictzones_fp: dictzones_fp[dictsegmentsin[zone][0][4]] = [] dictzones_fp[dictsegmentsin[zone][0][4]].append(zone) sortedzones = [] listfp = list(dictzones_fp.keys()) listfp.sort() for fp in listfp: listzones_fp = dictzones_fp[fp] listzones_fp.sort(reverse=True) sortedzones.extend(listzones_fp) # Ajout des information du fichier outbci.shp listzonesout = {} bcipointcursor = arcpy.da.SearchCursor(str_outbci, ["zoneid", "side", "lim1", "lim2", "side2", "lim3", "lim4", "SHAPE@"]) for point in bcipointcursor: listzonesout[point[0]] = point zones = str_zonefolder + "\\envelopezones.shp" # récupération du bci lac zonesscursor = arcpy.da.SearchCursor(zones, ["GRID_CODE", "SHAPE@", "Lake_ID"]) lakeid_byzone = {} for zoneshp in zonesscursor: if zoneshp[2] != -999: lakeid_byzone[zoneshp[0]] = zoneshp[2] # Z BCI fieldidlakes = arcpy.Describe(str_lakes).OIDFieldName shplakes = arcpy.da.SearchCursor(str_lakes, [fieldidlakes, field_z]) fieldz_bylakeid = {} for shplake in shplakes: fieldz_bylakeid[shplake[0]] = shplake[1] # Lancement de LISFLOOD-FP arcpy.SetProgressor("step", "Simulation 2D", 0, count, 1) progres = 0 arcpy.SetProgressorPosition(progres) if os.path.exists(str_simfolder + "\\lisflood_res"): arcpy.Delete_management(str_simfolder + "\\lisflood_res") for zone in sortedzones: segment = dictsegmentsin[zone] for point in sorted(segment, key=lambda q: q[2]): zonename = "zone" + str(point[1]) if point[3]=="main": if not arcpy.Exists(str_simfolder + "\\elev_zone" + str(point[1])): outpointshape = listzonesout[point[1]][7].firstPoint if point[1] in lakeid_byzone: hfix = fieldz_bylakeid[lakeid_byzone[point[1]]] else: if not os.path.exists(str_simfolder + "\\lisflood_res"): messages.addErrorMessage("Condition limite aval non trouvée : zone " + str(point[1])) else: # issue with Mosaic_management, used later with lisflood_res: # crash sometimes if the file is read here # so we make a tmp copy if arcpy.Exists(str_simfolder + "\\tmp_zone" + str(point[1])): arcpy.Delete_management(str_simfolder + "\\tmp_zone" + str(point[1])) arcpy.Copy_management(str_simfolder + "\\lisflood_res", str_simfolder + "\\tmp_zone" + str(point[1])) res_downstream = RasterIO(arcpy.Raster(str_simfolder + "\\tmp_zone" + str(point[1]))) hfix = res_downstream.getValue(res_downstream.YtoRow(outpointshape.Y), res_downstream.XtoCol(outpointshape.X)) if hfix == res_downstream.nodata: messages.addErrorMessage("Condition limite aval non trouvée : zone " + str(point[1])) arcpy.Delete_management(str_simfolder + "\\tmp_zone" + str(point[1])) # par newfile = str_simfolder + "\\zone" + str(point[1]) + ".par" filepar = open(newfile, 'w') filepar.write("DEMfile\tzone" + str(point[1]) + ".txt\n") filepar.write("resroot\tzone" + str(point[1]) + "\n") filepar.write("dirroot\t" + "res\n") filepar.write("manningfile\tnzone" + str(point[1]) + ".txt\n") filepar.write("bcifile\tzone" + str(point[1]) + ".bci" + "\n") filepar.write("sim_time\t" + str(simtime) + "\n") filepar.write("saveint\t" + str(simtime) + "\n") filepar.write("bdyfile\tzone" + str(point[1]) + ".bdy" + "\n") filepar.write("SGCwidth\twzone" + str(point[1]) + ".txt\n") filepar.write("SGCbank\tzone" + str(point[1]) + ".txt\n") filepar.write("SGCbed\tdzone" + str(point[1]) + ".txt\n") filepar.write("SGCn\t" + str(channelmanning) + "\n") filepar.write("chanmask\tmzone" + str(point[1]) + ".txt\n") # Vitesses du courant if voutput: filepar.write("qoutput\n") filepar.write("cfl\t0.5\n") filepar.write("max_Froude\t1\n") # filepar.write("debug\n") filepar.close() # bdy newfilebdy = str_simfolder + "\\zone" + str(point[1]) + ".bdy" for point2 in sorted(segment, key=lambda q: q[2]): raster_q_value = discharge.getValue(discharge.YtoRow(point2[0].firstPoint.Y), discharge.XtoCol(point2[0].firstPoint.X)) if point2[3] == "main": # Création du fichier bdy pointdischarge = raster_q_value / ( (discharge.raster.meanCellHeight + discharge.raster.meanCellWidth) / 2) lastdischarge = raster_q_value latnum = 0 filebdy = open(newfilebdy, 'w') filebdy.write("zone"+str(point[1]) + ".bdy\n") filebdy.write("zone"+str(point[1]) + "\n") filebdy.write("3\tseconds\n") filebdy.write("0\t0\n") filebdy.write("{0:.3f}".format(pointdischarge) + "\t50000\n") filebdy.write("{0:.3f}".format(pointdischarge) + "\t" + str(simtime)) filebdy.close() else: latnum += 1 pointdischarge = (raster_q_value - lastdischarge) / ( (discharge.raster.meanCellHeight + discharge.raster.meanCellWidth) / 2) lastdischarge = raster_q_value filebdy = open(newfilebdy, 'a') filebdy.write("\nzone" + str(point[1]) + "_" + str(latnum) + "\n") filebdy.write("3\tseconds\n") filebdy.write("0\t0\n") filebdy.write("{0:.3f}".format(pointdischarge) + "\t50000\n") filebdy.write("{0:.3f}".format(pointdischarge) + "\t" + str(simtime)) filebdy.close() # condition aval: 30cm au dessus du lit pour commencer zdep = min(zbed.getValue(zbed.YtoRow(outpointshape.Y), zbed.XtoCol(outpointshape.X)) + 0.3, hfix) filebdy = open(newfilebdy, 'a') filebdy.write("\nhvar\n") filebdy.write("4\tseconds\n") filebdy.write("{0:.2f}".format(zdep) + "\t0\n") filebdy.write("{0:.2f}".format(zdep) + "\t50000\n") filebdy.write("{0:.2f}".format(hfix) + "\t55000\n") filebdy.write("{0:.2f}".format(hfix) + "\t" + str(simtime)) filebdy.close() # calcul pour le -steadytol # Divise le débit par 200 et ne conserve qu'un chiffre significatif steadytol = str(round(lastdischarge / 200., - int(math.floor(math.log10(abs(lastdischarge / 200.)))))) subprocess.check_call([str_lisfloodfolder + "\\lisflood_intelRelease_double.exe", "-steady", "-steadytol", steadytol, str_simfolder + "\\zone" + str(point[1]) + ".par"], shell=True, cwd=str_simfolder) progres += 1 arcpy.SetProgressorPosition(progres) # Conversion des fichiers output # on renomme les fichiers créés (nécessaire pour être acceptés par l'outil de convsersion ASCII vers raster) if os.path.exists(str_simfolder + "\\res\\" + zonename + "elev.txt"): os.remove(str_simfolder + "\\res\\" + zonename + "elev.txt") if os.path.exists(str_simfolder + "\\res\\" + zonename + "-9999.elev"): os.rename(str_simfolder + "\\res\\" + zonename + "-9999.elev", str_simfolder + "\\res\\" + zonename + "elev.txt") else: os.rename(str_simfolder + "\\res\\" + zonename + "-0001.elev", str_simfolder + "\\res\\" + zonename + "elev.txt") messages.addWarningMessage("Steady state not reached : " + zonename) # Conversion des fichiers de sortie en raster pour ArcGIS str_elev = str_simfolder + "\\elev_" + zonename arcpy.ASCIIToRaster_conversion(str_simfolder + "\\res\\" + zonename + "elev.txt", str_elev, "FLOAT") # Ajout de la projection arcpy.DefineProjection_management(str_simfolder + "\\elev_" + zonename, r_q.spatialReference) messages.addMessage(str(point[1]) + " done") if not arcpy.Exists(str_simfolder + "\\lisflood_res"): arcpy.Copy_management(str_simfolder + "\\elev_" + zonename, str_simfolder + "\\lisflood_res") else: arcpy.Mosaic_management(str_simfolder + "\\elev_" + zonename, str_simfolder + "\\lisflood_res", mosaic_type="MAXIMUM") #arcpy.Copy_management(str_output + "\\lisflood_res", str_output + "\\tmp_mosaic") #arcpy.Delete_management(str_output + "\\lisflood_res") # arcpy.MosaicToNewRaster_management(';'.join([str_output + "\\tmp_mosaic", str_output + "\\elev_" + point[1]]), # str_output,"lisflood_res", # pixel_type="32_BIT_FLOAT", # number_of_bands=1) return
# e.g., Calculate mean of each cell of all bands. myData -= numpy.mean(myData, axis=0, keepdims=True) # ------------------------------------------------ # Convert data block back to raster myRasterBlock = arcpy.NumPyArrayToRaster(myData, arcpy.Point(mx, my), myRaster.meanCellWidth, myRaster.meanCellHeight) # Save on disk temporarily as 'filename_#.ext' filetemp = ('_%i.' % blocknum).join(fileout.rsplit('.', 1)) myRasterBlock.save(filetemp) # Maintain a list of saved temporary files filelist.append(filetemp) blocknum += 1 # Mosaic temporary files arcpy.Mosaic_management(';'.join(filelist[1:]), filelist[0]) if arcpy.Exists(fileout): arcpy.Delete_management(fileout) arcpy.Rename_management(filelist[0], fileout) # Remove temporary files for fileitem in filelist: if arcpy.Exists(fileitem): arcpy.Delete_management(fileitem) # Release raster objects from memory del myRasterBlock del myRaster
start = time.time(); i = 0 while i < len(os.listdir(orthos)): file = os.listdir(orthos)[i] if file.endswith(".tif") | file.endswith(".jpg"): end = time.time() print (str(i) + " / " + str(len(os.listdir(orthos)))) print("Remaining time: " + str(datetime.timedelta(seconds=int(end - start) / i) * (len(os.listdir(orthos)) - i))) # Expecting to have a .tfw file first. if i == 0 | i == 1: arcpy.RasterToGeodatabase_conversion(orthos + "/" + file, gdb); if file.endswith(".tif"): arcpy.CopyRaster_management(gdb + "/T" + file[:file.index(".")], gdb + "/" + name) arcpy.Delete_management(gdb + "/T" + file[:file.index(".")]) else: arcpy.CopyRaster_management(gdb + "/" + file[:file.index(".")], gdb + "/" + name) arcpy.Delete_management(gdb + "/" + file[:file.index(".")]) start = time.time() else: arcpy.Mosaic_management(inputs = orthos + "/" + file, target = gdb + "/" + name, mosaic_type = "LAST", colormap = "FIRST", background_value = "", nodata_value = "", onebit_to_eightbit = "NONE", mosaicking_tolerance = "0", MatchingMethod = "NONE") i += 1 print("BUILDING PYRAMIDS") arcpy.BuildPyramids_management( in_raster_dataset = gdb + "/" + name, pyramid_level = "-1", SKIP_FIRST = "NONE", resample_technique = "BILINEAR", compression_type = "JPEG", compression_quality = "50", skip_existing = "OVERWRITE")
import arcpy # Set environment settings env.workspace = "C:\\Users\\awiegman\\Downloads\\OtterData\\Lidar_DEMs" import os os.mkdir("C:\\Users\\awiegman\\Downloads\\OtterData\\Lidar_DEMs\\Mosaic") # Set local variables inRaster02 = 'Elevation_DEMHF1p6m2012_AVT_345.img' inRaster03 = 'Elevation_DEMHF1p6m2012_AVT_344.img' inRaster04 = 'Elevation_DEMHF1p6m2012_AVT_343.img' inRaster05 = 'Elevation_DEMHF1p6m2012_AVT_333.img' inRaster06 = 'Elevation_DEMHF1p6m2012_AVT_332.img' inRaster07 = 'Elevation_DEMHF1p6m2012_AVT_331.img' infiles = (inRaster02 + ';' + inRaster03 + ';' + inRaster04 + ';' + inRaster05 + ';' + inRaster06 + ';' + inRaster07) # need to create a target raster data set ##Mosaic two TIFF images to a single TIFF image ##Background value: 0 ##Nodata value: 9 arcpy.Mosaic_management( infiles, "C:\\Users\\awiegman\\Downloads\\OtterData\\Lidar_DEMs\\Mosaic\\otter_mosaic.tif", "LAST", "FIRST", "0", "9", "", "", "") ''' ##Mosaic several 3-band TIFF images to FGDB Raster Dataset with Color Correction ##Set Mosaic Tolerance to 0.3. Mismatch larget than 0.3 will be resampled arcpy.Mosaic_management("rgb1.tif;rgb2.tif;rgb3.tif", "Mosaic.gdb\\rgb","LAST","FIRST","", "", "", "0.3", "HISTOGRAM_MATCHING") '''
def execute_RunSim_prev_2var(str_zonefolder, str_simfolder, str_lisfloodfolder, str_csvq, str_csvz, voutput, simtime, channelmanning, r_zbed, str_log, messages): str_inbci = str_zonefolder + "\\inbci.shp" str_outbci = str_zonefolder + "\\outbci.shp" zbed = RasterIO(r_zbed) bcipointcursor = arcpy.da.SearchCursor(str_inbci, ["SHAPE@", "zoneid", "flowacc", "type", "fpid"]) dictsegmentsin = {} for point in bcipointcursor: if point[1] not in dictsegmentsin: dictsegmentsin[point[1]] = [] dictsegmentsin[point[1]].append(point) allzones = list(dictsegmentsin.keys()) allzones.sort() dictzones_fp = {} for zone in allzones: if dictsegmentsin[zone][0][4] not in dictzones_fp: dictzones_fp[dictsegmentsin[zone][0][4]] = [] dictzones_fp[dictsegmentsin[zone][0][4]].append(zone) sortedzones = [] listfp = list(dictzones_fp.keys()) listfp.sort() for fp in listfp: listzones_fp = dictzones_fp[fp] listzones_fp.sort(reverse=True) sortedzones.extend(listzones_fp) # Ajout des information du fichier outbci.shp listzonesout = {} bcipointcursor = arcpy.da.SearchCursor(str_outbci, ["zoneid", "side", "lim1", "lim2", "side2", "lim3", "lim4", "SHAPE@"]) for point in bcipointcursor: listzonesout[point[0]] = point zones = str_zonefolder + "\\envelopezones.shp" # récupération du bci lac zonesscursor = arcpy.da.SearchCursor(zones, ["GRID_CODE", "SHAPE@", "Lake_ID"]) lakeid_byzone = {} for zoneshp in zonesscursor: if zoneshp[2] != -999: lakeid_byzone[zoneshp[0]] = zoneshp[2] filelog = open(str_log, 'w') csvfilez = open(str_csvz) csv_readerz = csv.DictReader(csvfilez) ref_raster = None for csvzrow in csv_readerz: zname = csvzrow["nom"] hfix = float(csvzrow["z"]) csvfileq = open(str_csvq) csv_readerq = csv.DictReader(csvfileq) for csvqrow in csv_readerq: simname = csvqrow["nom"] + "_" + zname currentsimfolder = str_simfolder + "\\" + simname currentresult = str_simfolder + "\\res_" + simname simq = float(csvqrow["q"]) if not os.path.isdir(currentsimfolder): os.makedirs(currentsimfolder) for zone in sortedzones: segment = dictsegmentsin[zone] for point in sorted(segment, key=lambda q: q[2]): if point[3]=="main": try: if not arcpy.Exists(currentsimfolder + "\\elev_zone" + str(point[1])): if ref_raster is None: ref_raster = arcpy.Raster(str_zonefolder + "\\zone" + str(point[1])) outpointshape = listzonesout[point[1]][7].firstPoint if arcpy.Exists(currentresult): # dans le cas où un fichier résultat existe avec un résulat valide, on prends les résultats de ce fichier comme limite aval if arcpy.Exists(currentsimfolder + "\\tmp_zone" + str(point[1])): arcpy.Delete_management(currentsimfolder + "\\tmp_zone" + str(point[1])) arcpy.Copy_management(currentresult, currentsimfolder + "\\tmp_zone" + str(point[1])) res_downstream = RasterIO(arcpy.Raster(currentsimfolder + "\\tmp_zone" + str(point[1]))) hfix_raster = res_downstream.getValue(res_downstream.YtoRow(outpointshape.Y), res_downstream.XtoCol(outpointshape.X)) if hfix_raster != res_downstream.nodata: hfix_sim = hfix_raster else: hfix_sim = hfix arcpy.Delete_management(currentsimfolder + "\\tmp_zone" + str(point[1])) else: hfix_sim = hfix # par newfile = str_simfolder + "\\zone" + str(point[1]) + ".par" if os.path.isfile(newfile): os.remove(newfile) filepar = open(newfile, 'w') filepar.write("DEMfile\tzone" + str(point[1]) + ".txt\n") filepar.write("resroot\tzone" + str(point[1]) + "\n") filepar.write("dirroot\t" + simname + "\n") filepar.write("manningfile\tnzone" + str(point[1]) + ".txt\n") filepar.write("bcifile\tzone" + str(point[1]) + ".bci" + "\n") filepar.write("sim_time\t" + str(simtime) + "\n") filepar.write("saveint\t" + str(simtime) + "\n") filepar.write("bdyfile\t"+ simname +"\\zone" + str(point[1]) + ".bdy" + "\n") filepar.write("SGCwidth\twzone" + str(point[1]) + ".txt\n") filepar.write("SGCbank\tzone" + str(point[1]) + ".txt\n") filepar.write("SGCbed\tdzone" + str(point[1]) + ".txt\n") filepar.write("SGCn\t" + str(channelmanning) + "\n") filepar.write("chanmask\tmzone" + str(point[1]) + ".txt\n") # Vitesses du courant if voutput: filepar.write("hazard\n") filepar.write("qoutput\n") filepar.write("cfl\t0.3\n") filepar.write("max_Froude\t1\n") # filepar.write("debug\n") filepar.close() # bdy newfilebdy = currentsimfolder + "\\zone" + str(point[1]) + ".bdy" for point2 in sorted(segment, key=lambda q: q[2]): q_value = point2[2]*simq*(ref_raster.meanCellHeight*ref_raster.meanCellWidth)/1000000. if point2[3] == "main": # Création du fichier bdy pointdischarge = q_value / ( (ref_raster.meanCellHeight + ref_raster.meanCellWidth) / 2) lastdischarge = q_value latnum = 0 filebdy = open(newfilebdy, 'w') filebdy.write("zone"+str(point[1]) + ".bdy\n") filebdy.write("zone"+str(point[1]) + "\n") filebdy.write("3\tseconds\n") filebdy.write("0\t0\n") filebdy.write("{0:.3f}".format(pointdischarge) + "\t50000\n") filebdy.write("{0:.3f}".format(pointdischarge) + "\t" + str(simtime)) filebdy.close() else: latnum += 1 pointdischarge = (q_value - lastdischarge) / ( (ref_raster.meanCellHeight + ref_raster.meanCellWidth) / 2) lastdischarge = q_value filebdy = open(newfilebdy, 'a') filebdy.write("\nzone" + str(point[1]) + "_" + str(latnum) + "\n") filebdy.write("3\tseconds\n") filebdy.write("0\t0\n") filebdy.write("{0:.3f}".format(pointdischarge) + "\t50000\n") filebdy.write("{0:.3f}".format(pointdischarge) + "\t" + str(simtime)) filebdy.close() # condition aval: 30cm au dessus du lit pour commencer zdep = min(zbed.getValue(zbed.YtoRow(outpointshape.Y), zbed.XtoCol(outpointshape.X)) + 0.3, hfix_sim) filebdy = open(newfilebdy, 'a') filebdy.write("\nhvar\n") filebdy.write("4\tseconds\n") filebdy.write("{0:.2f}".format(zdep) + "\t0\n") filebdy.write("{0:.2f}".format(zdep) + "\t50000\n") filebdy.write("{0:.2f}".format(hfix_sim) + "\t55000\n") filebdy.write("{0:.2f}".format(hfix_sim) + "\t" + str(simtime)) filebdy.close() # calcul pour le -steadytol # Divise le débit par 200 et ne conserve qu'un chiffre significatif steadytol = str( round(lastdischarge / 200., - int(math.floor(math.log10(abs(lastdischarge / 200.)))))) subprocess.check_call([str_lisfloodfolder + "\\lisflood_intelRelease_double.exe", "-steady", "-steadytol", steadytol, str_simfolder + "\\zone" + str(point[1]) + ".par"], shell=True, cwd=str_simfolder) # Conversion des fichiers output # on renomme les fichiers créés (nécessaire pour être acceptés par l'outil de convsersion ASCII vers raster) zonename = "zone"+str(point[1]) if os.path.exists(currentsimfolder + "\\" + zonename + "elev.txt"): os.remove(currentsimfolder + "\\" + zonename + "elev.txt") if os.path.exists(currentsimfolder + "\\" + zonename + "-9999.elev"): os.rename(currentsimfolder + "\\" + zonename + "-9999.elev", currentsimfolder + "\\" + zonename + "elev.txt") else: os.rename(currentsimfolder + "\\" + zonename + "-0001.elev", currentsimfolder + "\\" + zonename + "elev.txt") filelog.write("Steady state not reached : " + zonename + ", sim " + simname) messages.addWarningMessage("Steady state not reached : " + zonename + ", sim " + simname) if os.path.exists(currentsimfolder + "\\" + zonename + "-9999.Vx") or os.path.exists(currentsimfolder + "\\" + zonename + "-0001.Vx"): if os.path.exists(currentsimfolder + "\\" + zonename + "Vx.txt"): os.remove(currentsimfolder + "\\" + zonename + "Vx.txt") if os.path.exists(currentsimfolder + "\\" + zonename + "Vy.txt"): os.remove(currentsimfolder + "\\" + zonename + "Vy.txt") if os.path.exists(currentsimfolder + "\\" + zonename + "-9999.Vx"): os.rename(currentsimfolder + "\\" + zonename + "-9999.Vx", currentsimfolder+ "\\" + zonename + "Vx.txt") os.rename(currentsimfolder + "\\" + zonename + "-9999.Vy", currentsimfolder + "\\" + zonename + "Vy.txt") else: os.rename(currentsimfolder + "\\" + zonename + "-0001.Vx", currentsimfolder + "\\" + zonename + "Vx.txt") os.rename(currentsimfolder + "\\" + zonename + "-0001.Vy", currentsimfolder + "\\" + zonename + "Vy.txt") arcpy.ASCIIToRaster_conversion(currentsimfolder + "\\" + zonename + "Vx.txt", currentsimfolder + "\\Vx_" + zonename, "FLOAT") arcpy.ASCIIToRaster_conversion(currentsimfolder + "\\" + zonename + "Vy.txt", currentsimfolder + "\\Vy_" + zonename, "FLOAT") arcpy.DefineProjection_management(currentsimfolder + "\\Vx_" + zonename, ref_raster.spatialReference) arcpy.DefineProjection_management(currentsimfolder + "\\Vy_" + zonename, ref_raster.spatialReference) # Conversion des fichiers de sortie en raster pour ArcGIS str_elev = currentsimfolder + "\\elev_" + zonename arcpy.ASCIIToRaster_conversion(currentsimfolder + "\\" + zonename + "elev.txt", str_elev, "FLOAT") # Ajout de la projection arcpy.DefineProjection_management(str_elev, ref_raster.spatialReference) if not arcpy.Exists(currentresult): arcpy.Copy_management(currentsimfolder + "\\elev_" + "zone"+str(point[1]), currentresult) else: arcpy.Mosaic_management(currentsimfolder + "\\elev_" + "zone"+str(point[1]), currentresult, mosaic_type="MAXIMUM") #arcpy.Copy_management(str_output + "\\lisflood_res", str_output + "\\tmp_mosaic") #arcpy.Delete_management(str_output + "\\lisflood_res") # arcpy.MosaicToNewRaster_management(';'.join([str_output + "\\tmp_mosaic", str_output + "\\elev_" + point[1]]), # str_output,"lisflood_res", # pixel_type="32_BIT_FLOAT", # number_of_bands=1) except BaseException as e: filelog.write("ERREUR in " + simname + ": sim aborded during zone "+ str(point[1]) + "\n") messages.addWarningMessage("Some simulations skipped. See log file.") return
def long_time_task(FID): ''' 处理一个FID,也就是一个2度网格的城市阈值计算,提取,和镶嵌。 ''' # arcpy的临时输出文件 # --- 这段代码保证,不同的arcpy进程使用不同的临时空间,在多进程的环境下避免一些错误 newTempDir = u"./tmp/tmp" + time.strftime('%Y%m%d%H%M%S') + str(FID) os.makedirs(newTempDir) os.environ["TEMP"] = newTempDir os.environ["TMP"] = newTempDir # --- for row in arcpy.SearchCursor(config['fc'], '"FID" = {0}'.format(FID)): begin_time = time.time() grid_code = int(row.getValue("grid_id")) # 10度网格号 mask = row.getValue("Shape") # 2度网格shape row_code = row.getValue("code") # 2度网格号 print '**** PID: ' + str(os.getpid()) + ' FID: ' + str( FID) + " grid_code: " + str(grid_code) target_area = row.getValue(config['target_count_field']) # 目标城市数目字段 threshold_max = config['threshold_max'] threshold_min = config['threshold_min'] threshold_step = config['threshold_step'] nuaci_path = '{0}/{1}{2}.tif'.format(config['in_folder_path'], config['input_prefix'], grid_code) # if not arcpy.Exists(nuaci_path): # # arcpy.AddError(nuaci_path + ' not found ') print '####input', nuaci_path in_raster = Raster(nuaci_path) in_raster_mask = ExtractByMask(in_raster, mask) del in_raster lower_left = arcpy.Point(in_raster_mask.extent.XMin, in_raster_mask.extent.YMin) cell_size = in_raster_mask.meanCellWidth dsc = arcpy.Describe(in_raster_mask) sr = dsc.SpatialReference no_data_value = in_raster_mask.noDataValue in_raster_arr = arcpy.RasterToNumPyArray(in_raster_mask, nodata_to_value=no_data_value) del in_raster_mask actual_area = 0 threshold_value = threshold_max in_raster_arr_bi = in_raster_arr > threshold_value while (actual_area < target_area) and (threshold_value >= threshold_min): in_raster_arr_bi = in_raster_arr > threshold_value actual_area = in_raster_arr_bi.sum() * 0.0009 threshold_value -= threshold_step print('------------final area = ' + str(actual_area) + '---------------' + str(row_code)) print('final threshold = ' + str(min([threshold_max, threshold_value + threshold_step]))) in_raster_arr_dtype = in_raster_arr.dtype del in_raster_arr in_raster_arr_bi = in_raster_arr_bi.astype(in_raster_arr_dtype) new_raster = arcpy.NumPyArrayToRaster(in_raster_arr_bi, lower_left, cell_size, cell_size, no_data_value) del in_raster_arr_bi arcpy.DefineProjection_management(new_raster, sr) out_raster_path = '{0}/{1}{2}.tif'.format(config['out_folder_path'], config['output_prefix'], grid_code) arcpy.Mosaic_management(new_raster, out_raster_path, "LAST", "FIRST", "", "", "", "", "") del new_raster print 'Task %d - %d - %d runs %0.2f seconds.' % ( os.getpid(), FID, grid_code, (time.time() - begin_time))
def STEP6_calc_barriers(): """Detects influential barriers given CWD calculations from s3_calcCwds.py. """ try: arcpy.CheckOutExtension("spatial") lu.dashline(0) gprint('Running script ' + _SCRIPT_NAME) if cfg.BARRIER_CWD_THRESH is not None: lu.dashline(1) gprint('Invoking CWD Threshold of ' + str(cfg.BARRIER_CWD_THRESH) + ' map units.') if cfg.SUM_BARRIERS: sumSuffix = '_Sum' cfg.BARRIERBASEDIR = cfg.BARRIERBASEDIR + sumSuffix baseName, extension = path.splitext(cfg.BARRIERGDB) cfg.BARRIERGDB = baseName + sumSuffix + extension gprint('\nBarrier scores will be SUMMED across core pairs.') else: sumSuffix = '' # Delete contents of final ouptut geodatabase # lu.clean_out_workspace(cfg.BARRIERGDB) #xxx try not doing this to allow multiple radii to be analyzed in separate runs if not arcpy.Exists(cfg.BARRIERGDB): # Create output geodatabase arcpy.CreateFileGDB_management(cfg.OUTPUTDIR, path.basename(cfg.BARRIERGDB)) startRadius = int(cfg.STARTRADIUS) endRadius = int(cfg.ENDRADIUS) radiusStep = int(cfg.RADIUSSTEP) if radiusStep == 0: endRadius = startRadius # Calculate at just one radius value radiusStep = 1 linkTableFile = lu.get_prev_step_link_table(step=6) arcpy.env.workspace = cfg.SCRATCHDIR arcpy.env.scratchWorkspace = cfg.ARCSCRATCHDIR arcpy.RefreshCatalog(cfg.PROJECTDIR) PREFIX = path.basename(cfg.PROJECTDIR) # For speed: arcpy.env.pyramid = "NONE" arcpy.env.rasterStatistics = "NONE" # set the analysis extent and cell size to that of the resistance # surface arcpy.OverWriteOutput = True arcpy.env.extent = cfg.RESRAST arcpy.env.cellSize = cfg.RESRAST arcpy.env.snapRaster = cfg.RESRAST spatialref = arcpy.Describe(cfg.RESRAST).spatialReference mapUnits = (str(spatialref.linearUnitName)).lower() if len(mapUnits) > 1 and mapUnits[-1] != 's': mapUnits = mapUnits + 's' if float(arcpy.env.cellSize) > startRadius or startRadius > endRadius: msg = ('Error: minimum detection radius must be greater than ' 'cell size (' + str(arcpy.env.cellSize) + ') \nand less than or equal to maximum detection radius.') lu.raise_error(msg) linkTable = lu.load_link_table(linkTableFile) numLinks = linkTable.shape[0] numCorridorLinks = lu.report_links(linkTable) if numCorridorLinks == 0: lu.dashline(1) msg = ('\nThere are no linkages. Bailing.') lu.raise_error(msg) # set up directories for barrier and barrier mosaic grids dirCount = 0 gprint("Creating intermediate output folder: " + cfg.BARRIERBASEDIR) lu.delete_dir(cfg.BARRIERBASEDIR) lu.create_dir(cfg.BARRIERBASEDIR) arcpy.CreateFolder_management(cfg.BARRIERBASEDIR, cfg.BARRIERDIR_NM) cbarrierdir = path.join(cfg.BARRIERBASEDIR, cfg.BARRIERDIR_NM) coresToProcess = npy.unique(linkTable[:, cfg.LTB_CORE1:cfg.LTB_CORE2 + 1]) maxCoreNum = max(coresToProcess) # Set up focal directories. # To keep there from being > 100 grids in any one directory, # outputs are written to: # barrier\focalX_ for cores 1-99 at radius X # barrier\focalX_1 for cores 100-199 # etc. lu.dashline(0) for radius in range(startRadius, endRadius + 1, radiusStep): core1path = lu.get_focal_path(1, radius) path1, dir1 = path.split(core1path) path2, dir2 = path.split(path1) arcpy.CreateFolder_management(path.dirname(path2), path.basename(path2)) arcpy.CreateFolder_management(path.dirname(path1), path.basename(path1)) if maxCoreNum > 99: gprint('Creating subdirectories for ' + str(radius) + ' ' + str(mapUnits) + ' radius analysis scale.') maxDirCount = int(maxCoreNum / 100) focalDirBaseName = dir2 cp100 = (coresToProcess.astype('int32')) / 100 ind = npy.where(cp100 > 0) dirNums = npy.unique(cp100[ind]) for dirNum in dirNums: focalDir = focalDirBaseName + str(dirNum) gprint('...' + focalDir) arcpy.CreateFolder_management(path2, focalDir) # Create resistance raster with filled-in Nodata values for later use arcpy.env.extent = cfg.RESRAST resistFillRaster = path.join(cfg.SCRATCHDIR, "resist_fill") output = arcpy.sa.Con(IsNull(cfg.RESRAST), 1000000000, Raster(cfg.RESRAST) - 1) output.save(resistFillRaster) coreList = linkTable[:, cfg.LTB_CORE1:cfg.LTB_CORE2 + 1] coreList = npy.sort(coreList) # Loop through each search radius to calculate barriers in each link import time radId = 0 #keep track of number of radii processed- used for temp dir naming for radius in range(startRadius, endRadius + 1, radiusStep): radId = radId + 1 linkTableTemp = linkTable.copy() @retry(10) #can't pass vars in and modify them. def doRadiusLoop(): linkTable = linkTableTemp.copy() startTime = time.clock() randomerror() linkLoop = 0 pctDone = 0 gprint('\nMapping barriers at a radius of ' + str(radius) + ' ' + str(mapUnits)) if cfg.SUM_BARRIERS: gprint('using SUM method') else: gprint('using MAXIMUM method') if numCorridorLinks > 1: gprint('0 percent done') lastMosaicRaster = None lastMosaicRasterPct = None for x in range(0, numLinks): pctDone = lu.report_pct_done(linkLoop, numCorridorLinks, pctDone) linkId = str(int(linkTable[x, cfg.LTB_LINKID])) if ((linkTable[x, cfg.LTB_LINKTYPE] > 0) and (linkTable[x, cfg.LTB_LINKTYPE] < 1000)): linkLoop = linkLoop + 1 # source and target cores corex = int(coreList[x, 0]) corey = int(coreList[x, 1]) # Get cwd rasters for source and target cores cwdRaster1 = lu.get_cwd_path(corex) cwdRaster2 = lu.get_cwd_path(corey) # Mask out areas above CWD threshold cwdTemp1 = None cwdTemp2 = None if cfg.BARRIER_CWD_THRESH is not None: if x == 1: lu.dashline(1) gprint(' Using CWD threshold of ' + str(cfg.BARRIER_CWD_THRESH) + ' map units.') arcpy.env.extent = cfg.RESRAST arcpy.env.cellSize = cfg.RESRAST arcpy.env.snapRaster = cfg.RESRAST cwdTemp1 = path.join(cfg.SCRATCHDIR, "tmp" + str(corex)) outCon = arcpy.sa.Con( cwdRaster1 < float(cfg.BARRIER_CWD_THRESH), cwdRaster1) outCon.save(cwdTemp1) cwdRaster1 = cwdTemp1 cwdTemp2 = path.join(cfg.SCRATCHDIR, "tmp" + str(corey)) outCon = arcpy.sa.Con( cwdRaster2 < float(cfg.BARRIER_CWD_THRESH), cwdRaster2) outCon.save(cwdTemp2) cwdRaster2 = cwdTemp2 focalRaster1 = lu.get_focal_path(corex, radius) focalRaster2 = lu.get_focal_path(corey, radius) link = lu.get_links_from_core_pairs( linkTable, corex, corey) lcDist = float(linkTable[link, cfg.LTB_CWDIST]) # Detect barriers at radius using neighborhood stats # Create the Neighborhood Object innerRadius = radius - 1 outerRadius = radius dia = 2 * radius InNeighborhood = ("ANNULUS " + str(innerRadius) + " " + str(outerRadius) + " MAP") @retry(10) def execFocal(): randomerror() # Execute FocalStatistics if not path.exists(focalRaster1): arcpy.env.extent = cwdRaster1 outFocalStats = arcpy.sa.FocalStatistics( cwdRaster1, InNeighborhood, "MINIMUM", "DATA") if setCoresToNull: outFocalStats2 = arcpy.sa.Con( outFocalStats > 0, outFocalStats ) # Set areas overlapping cores to NoData xxx outFocalStats2.save(focalRaster1) #xxx else: outFocalStats.save(focalRaster1) #xxx arcpy.env.extent = cfg.RESRAST if not path.exists(focalRaster2): arcpy.env.extent = cwdRaster2 outFocalStats = arcpy.sa.FocalStatistics( cwdRaster2, InNeighborhood, "MINIMUM", "DATA") if setCoresToNull: outFocalStats2 = arcpy.sa.Con( outFocalStats > 0, outFocalStats ) # Set areas overlapping cores to NoData xxx outFocalStats2.save(focalRaster2) #xxx else: outFocalStats.save(focalRaster2) #xxx arcpy.env.extent = cfg.RESRAST execFocal() lu.delete_data(cwdTemp1) lu.delete_data(cwdTemp2) barrierRaster = path.join( cbarrierdir, "b" + str(radius) + "_" + str(corex) + "_" + str(corey) + '.tif') if cfg.SUM_BARRIERS: # Need to set nulls to 0, also # create trim rasters as we go outRas = ((lcDist - Raster(focalRaster1) - Raster(focalRaster2) - dia) / dia) outCon = arcpy.sa.Con(IsNull(outRas), 0, outRas) outCon2 = arcpy.sa.Con(outCon < 0, 0, outCon) outCon2.save(barrierRaster) # Execute FocalStatistics to fill out search radii InNeighborhood = "CIRCLE " + str( outerRadius) + " MAP" fillRaster = path.join( cbarrierdir, "b" + str(radius) + "_" + str(corex) + "_" + str(corey) + "_fill.tif") outFocalStats = arcpy.sa.FocalStatistics( barrierRaster, InNeighborhood, "MAXIMUM", "DATA") outFocalStats.save(fillRaster) if cfg.WRITE_TRIM_RASTERS: trmRaster = path.join( cbarrierdir, "b" + str(radius) + "_" + str(corex) + "_" + str(corey) + "_trim.tif") rasterList = [fillRaster, resistFillRaster] outCellStatistics = arcpy.sa.CellStatistics( rasterList, "MINIMUM") outCellStatistics.save(trmRaster) else: #Calculate potential benefit per map unit restored @retry(10) def calcBen(): randomerror() outRas = ((lcDist - Raster(focalRaster1) - Raster(focalRaster2) - dia) / dia) outRas.save(barrierRaster) calcBen() if cfg.WRITE_PCT_RASTERS: #Calculate PERCENT potential benefit per unit restored barrierRasterPct = path.join( cbarrierdir, "b" + str(radius) + "_" + str(corex) + "_" + str(corey) + '_pct.tif') @retry(10) def calcBenPct(): randomerror() outras = (100 * (Raster(barrierRaster) / lcDist)) outras.save(barrierRasterPct) calcBenPct() # Mosaic barrier results across core area pairs mosaicDir = path.join( cfg.SCRATCHDIR, 'mos' + str(radId) + '_' + str(x + 1)) lu.create_dir(mosaicDir) mosFN = 'mos_temp' tempMosaicRaster = path.join(mosaicDir, mosFN) tempMosaicRasterTrim = path.join( mosaicDir, 'mos_temp_trm') arcpy.env.workspace = mosaicDir if linkLoop == 1: #If this is the first grid then copy rather than mosaic arcpy.CopyRaster_management( barrierRaster, tempMosaicRaster) if cfg.SUM_BARRIERS and cfg.WRITE_TRIM_RASTERS: arcpy.CopyRaster_management( trmRaster, tempMosaicRasterTrim) else: if cfg.SUM_BARRIERS: outCon = arcpy.sa.Con( Raster(barrierRaster) < 0, lastMosaicRaster, Raster(barrierRaster) + Raster(lastMosaicRaster)) outCon.save(tempMosaicRaster) if cfg.WRITE_TRIM_RASTERS: outCon = arcpy.sa.Con( Raster(trmRaster) < 0, lastMosaicRasterTrim, Raster(trmRaster) + Raster(lastMosaicRasterTrim)) outCon.save(tempMosaicRasterTrim) else: rasterString = ('"' + barrierRaster + ";" + lastMosaicRaster + '"') @retry(10) def mosaicToNew(): randomerror() arcpy.MosaicToNewRaster_management( rasterString, mosaicDir, mosFN, "", "32_BIT_FLOAT", arcpy.env.cellSize, "1", "MAXIMUM", "MATCH") mosaicToNew() # gprint(str(corex)+'0'+str(corey)) if linkLoop > 1: #Clean up from previous loop lu.delete_data(lastMosaicRaster) lastMosaicDir = path.dirname(lastMosaicRaster) lu.clean_out_workspace(lastMosaicDir) lu.delete_dir(lastMosaicDir) lastMosaicRaster = tempMosaicRaster if cfg.WRITE_TRIM_RASTERS: lastMosaicRasterTrim = tempMosaicRasterTrim if cfg.WRITE_PCT_RASTERS: mosPctFN = 'mos_temp_pct' mosaicDirPct = path.join( cfg.SCRATCHDIR, 'mosP' + str(radId) + '_' + str(x + 1)) lu.create_dir(mosaicDirPct) tempMosaicRasterPct = path.join( mosaicDirPct, mosPctFN) if linkLoop == 1: # If this is the first grid then copy # rather than mosaic if cfg.SUM_BARRIERS: outCon = arcpy.sa.Con( Raster(barrierRasterPct) < 0, 0, arcpy.sa.Con(IsNull(barrierRasterPct), 0, barrierRasterPct)) outCon.save(tempMosaicRasterPct) else: arcpy.CopyRaster_management( barrierRasterPct, tempMosaicRasterPct) else: if cfg.SUM_BARRIERS: @retry(10) def sumBarriers(): randomerror() outCon = arcpy.sa.Con( Raster(barrierRasterPct) < 0, lastMosaicRasterPct, Raster(barrierRasterPct) + Raster(lastMosaicRasterPct)) outCon.save(tempMosaicRasterPct) sumBarriers() else: rasterString = ('"' + barrierRasterPct + ";" + lastMosaicRasterPct + '"') @retry(10) def maxBarriers(): randomerror() arcpy.MosaicToNewRaster_management( rasterString, mosaicDirPct, mosPctFN, "", "32_BIT_FLOAT", arcpy.env.cellSize, "1", "MAXIMUM", "MATCH") maxBarriers() if linkLoop > 1: #Clean up from previous loop lu.delete_data(lastMosaicRasterPct) lastMosaicDirPct = path.dirname( lastMosaicRasterPct) lu.clean_out_workspace(lastMosaicDirPct) lu.delete_dir(lastMosaicDirPct) # lu.delete_data(lastMosaicRasterPct) lastMosaicRasterPct = tempMosaicRasterPct if not cfg.SAVEBARRIERRASTERS: lu.delete_data(barrierRaster) if cfg.WRITE_PCT_RASTERS: lu.delete_data(barrierRasterPct) if cfg.WRITE_TRIM_RASTERS: lu.delete_data(trmRaster) # Temporarily disable links in linktable - # don't want to mosaic them twice for y in range(x + 1, numLinks): corex1 = int(coreList[y, 0]) corey1 = int(coreList[y, 1]) if corex1 == corex and corey1 == corey: linkTable[y, cfg.LTB_LINKTYPE] = ( linkTable[y, cfg.LTB_LINKTYPE] + 1000) elif corex1 == corey and corey1 == corex: linkTable[y, cfg.LTB_LINKTYPE] = ( linkTable[y, cfg.LTB_LINKTYPE] + 1000) if numCorridorLinks > 1 and pctDone < 100: gprint('100 percent done') gprint('Summarizing barrier data for search radius.') #rows that were temporarily disabled rows = npy.where(linkTable[:, cfg.LTB_LINKTYPE] > 1000) linkTable[rows, cfg.LTB_LINKTYPE] = ( linkTable[rows, cfg.LTB_LINKTYPE] - 1000) # ----------------------------------------------------------------- # Set negative values to null or zero and write geodatabase. mosaicFN = (PREFIX + "_BarrierCenters" + sumSuffix + "_Rad" + str(radius)) mosaicRaster = path.join(cfg.BARRIERGDB, mosaicFN) arcpy.env.extent = cfg.RESRAST # if setCoresToNull: # outCon = arcpy.sa.Con(Raster(tempMosaicRaster) < 0, 0, # tempMosaicRaster) #xxx # outCon.save(mosaicRaster) #xxx # else: outSetNull = arcpy.sa.SetNull(tempMosaicRaster, tempMosaicRaster, "VALUE < 0") #xxx orig outSetNull.save(mosaicRaster) lu.delete_data(tempMosaicRaster) if cfg.SUM_BARRIERS and cfg.WRITE_TRIM_RASTERS: mosaicFN = (PREFIX + "_BarrierCircles_RBMin" + sumSuffix + "_Rad" + str(radius)) mosaicRasterTrim = path.join(cfg.BARRIERGDB, mosaicFN) arcpy.CopyRaster_management(tempMosaicRasterTrim, mosaicRasterTrim) lu.delete_data(tempMosaicRaster) if cfg.WRITE_PCT_RASTERS: # Do same for percent raster mosaicPctFN = (PREFIX + "_BarrierCenters_Pct" + sumSuffix + "_Rad" + str(radius)) arcpy.env.extent = cfg.RESRAST outSetNull = arcpy.sa.SetNull(tempMosaicRasterPct, tempMosaicRasterPct, "VALUE < 0") mosaicRasterPct = path.join(cfg.BARRIERGDB, mosaicPctFN) outSetNull.save(mosaicRasterPct) lu.delete_data(tempMosaicRasterPct) # 'Grow out' maximum restoration gain to # neighborhood size for display InNeighborhood = "CIRCLE " + str(outerRadius) + " MAP" # Execute FocalStatistics fillRasterFN = "barriers_fill" + str(outerRadius) + tif fillRaster = path.join(cfg.BARRIERBASEDIR, fillRasterFN) outFocalStats = arcpy.sa.FocalStatistics( mosaicRaster, InNeighborhood, "MAXIMUM", "DATA") outFocalStats.save(fillRaster) if cfg.WRITE_PCT_RASTERS: # Do same for percent raster fillRasterPctFN = "barriers_fill_pct" + str( outerRadius) + tif fillRasterPct = path.join(cfg.BARRIERBASEDIR, fillRasterPctFN) outFocalStats = arcpy.sa.FocalStatistics( mosaicRasterPct, InNeighborhood, "MAXIMUM", "DATA") outFocalStats.save(fillRasterPct) #Place copies of filled rasters in output geodatabase arcpy.env.workspace = cfg.BARRIERGDB fillRasterFN = (PREFIX + "_BarrrierCircles" + sumSuffix + "_Rad" + str(outerRadius)) arcpy.CopyRaster_management(fillRaster, fillRasterFN) if cfg.WRITE_PCT_RASTERS: fillRasterPctFN = (PREFIX + "_BarrrierCircles_Pct" + sumSuffix + "_Rad" + str(outerRadius)) arcpy.CopyRaster_management(fillRasterPct, fillRasterPctFN) if not cfg.SUM_BARRIERS and cfg.WRITE_TRIM_RASTERS: # Create pared-down version of filled raster- remove pixels # that don't need restoring by allowing a pixel to only # contribute its resistance value to restoration gain outRasterFN = "barriers_trm" + str(outerRadius) + tif outRaster = path.join(cfg.BARRIERBASEDIR, outRasterFN) rasterList = [fillRaster, resistFillRaster] outCellStatistics = arcpy.sa.CellStatistics( rasterList, "MINIMUM") outCellStatistics.save(outRaster) #SECOND ROUND TO CLIP BY DATA VALUES IN BARRIER RASTER outRaster2FN = ("barriers_trm" + sumSuffix + str(outerRadius) + "_2" + tif) outRaster2 = path.join(cfg.BARRIERBASEDIR, outRaster2FN) output = arcpy.sa.Con(IsNull(fillRaster), fillRaster, outRaster) output.save(outRaster2) outRasterFN = (PREFIX + "_BarrierCircles_RBMin" + sumSuffix + "_Rad" + str(outerRadius)) outRasterPath = path.join(cfg.BARRIERGDB, outRasterFN) arcpy.CopyRaster_management(outRaster2, outRasterFN) randomerror() startTime = lu.elapsed_time(startTime) # Call the above function doRadiusLoop() # Combine rasters across radii gprint('\nCreating summary rasters...') if startRadius != endRadius: radiiSuffix = ('_Rad' + str(int(startRadius)) + 'To' + str(int(endRadius)) + 'Step' + str(int(radiusStep))) mosaicFN = "bar_radii" mosaicPctFN = "bar_radii_pct" arcpy.env.workspace = cfg.BARRIERBASEDIR for radius in range(startRadius, endRadius + 1, radiusStep): #Fixme: run speed test with gdb mosaicking above and here radiusFN = (PREFIX + "_BarrierCenters" + sumSuffix + "_Rad" + str(radius)) radiusRaster = path.join(cfg.BARRIERGDB, radiusFN) if radius == startRadius: #If this is the first grid then copy rather than mosaic arcpy.CopyRaster_management(radiusRaster, mosaicFN) else: mosaicRaster = path.join(cfg.BARRIERBASEDIR, mosaicFN) arcpy.Mosaic_management(radiusRaster, mosaicRaster, "MAXIMUM", "MATCH") if cfg.WRITE_PCT_RASTERS: radiusPctFN = (PREFIX + "_BarrierCenters_Pct" + sumSuffix + "_Rad" + str(radius)) radiusRasterPct = path.join(cfg.BARRIERGDB, radiusPctFN) if radius == startRadius: #If this is the first grid then copy rather than mosaic arcpy.CopyRaster_management(radiusRasterPct, mosaicPctFN) else: mosaicRasterPct = path.join(cfg.BARRIERBASEDIR, mosaicPctFN) arcpy.Mosaic_management(radiusRasterPct, mosaicRasterPct, "MAXIMUM", "MATCH") # Copy results to output geodatabase arcpy.env.workspace = cfg.BARRIERGDB mosaicFN = PREFIX + "_BarrierCenters" + sumSuffix + radiiSuffix arcpy.CopyRaster_management(mosaicRaster, mosaicFN) if cfg.WRITE_PCT_RASTERS: mosaicPctFN = (PREFIX + "_BarrierCenters_Pct" + sumSuffix + radiiSuffix) arcpy.CopyRaster_management(mosaicRasterPct, mosaicPctFN) #GROWN OUT rasters fillMosaicFN = "barriers_radii_fill" + tif fillMosaicPctFN = "barriers_radii_fill_pct" + tif fillMosaicRaster = path.join(cfg.BARRIERBASEDIR, fillMosaicFN) fillMosaicRasterPct = path.join(cfg.BARRIERBASEDIR, fillMosaicPctFN) arcpy.env.workspace = cfg.BARRIERBASEDIR for radius in range(startRadius, endRadius + 1, radiusStep): radiusFN = "barriers_fill" + str(radius) + tif #fixme- do this when only a single radius too radiusRaster = path.join(cfg.BARRIERBASEDIR, radiusFN) if radius == startRadius: #If this is the first grid then copy rather than mosaic arcpy.CopyRaster_management(radiusRaster, fillMosaicFN) else: arcpy.Mosaic_management(radiusRaster, fillMosaicRaster, "MAXIMUM", "MATCH") if cfg.WRITE_PCT_RASTERS: radiusPctFN = "barriers_fill_pct" + str(radius) + tif #fixme- do this when only a single radius too radiusRasterPct = path.join(cfg.BARRIERBASEDIR, radiusPctFN) if radius == startRadius: #If this is the first grid then copy rather than mosaic arcpy.CopyRaster_management(radiusRasterPct, fillMosaicPctFN) else: arcpy.Mosaic_management(radiusRasterPct, fillMosaicRasterPct, "MAXIMUM", "MATCH") # Copy result to output geodatabase arcpy.env.workspace = cfg.BARRIERGDB fillMosaicFN = PREFIX + "_BarrierCircles" + sumSuffix + radiiSuffix arcpy.CopyRaster_management(fillMosaicRaster, fillMosaicFN) if cfg.WRITE_PCT_RASTERS: fillMosaicPctFN = (PREFIX + "_BarrierCircles_Pct" + sumSuffix + radiiSuffix) arcpy.CopyRaster_management(fillMosaicRasterPct, fillMosaicPctFN) # if not cfg.SUM_BARRIERS: #GROWN OUT AND TRIMMED rasters (Can't do percent) if cfg.WRITE_TRIM_RASTERS: trimMosaicFN = "bar_radii_trm" arcpy.env.workspace = cfg.BARRIERBASEDIR trimMosaicRaster = path.join(cfg.BARRIERBASEDIR, trimMosaicFN) for radius in range(startRadius, endRadius + 1, radiusStep): radiusFN = (PREFIX + "_BarrierCircles_RBMin" + sumSuffix + "_Rad" + str(radius)) #fixme- do this when only a single radius too radiusRaster = path.join(cfg.BARRIERGDB, radiusFN) if radius == startRadius: #If this is the first grid then copy rather than mosaic arcpy.CopyRaster_management(radiusRaster, trimMosaicFN) else: arcpy.Mosaic_management(radiusRaster, trimMosaicRaster, "MAXIMUM", "MATCH") # Copy result to output geodatabase arcpy.env.workspace = cfg.BARRIERGDB trimMosaicFN = (PREFIX + "_BarrierCircles_RBMin" + sumSuffix + radiiSuffix) arcpy.CopyRaster_management(trimMosaicRaster, trimMosaicFN) if not cfg.SAVE_RADIUS_RASTERS: arcpy.env.workspace = cfg.BARRIERGDB rasters = arcpy.ListRasters() for raster in rasters: if 'rad' in raster.lower() and not 'step' in raster.lower(): lu.delete_data(raster) arcpy.env.workspace = cfg.BARRIERGDB rasters = arcpy.ListRasters() for raster in rasters: gprint('\nBuilding output statistics and pyramids\n' 'for raster ' + raster) lu.build_stats(raster) #Clean up temporary files and directories if not cfg.SAVEBARRIERRASTERS: lu.delete_dir(cbarrierdir) lu.delete_dir(cfg.BARRIERBASEDIR) if not cfg.SAVEFOCALRASTERS: for radius in range(startRadius, endRadius + 1, radiusStep): core1path = lu.get_focal_path(1, radius) path1, dir1 = path.split(core1path) path2, dir2 = path.split(path1) lu.delete_dir(path2) # Return GEOPROCESSING specific errors except arcpy.ExecuteError: lu.dashline(1) gprint('****Failed in step 6. Details follow.****') lu.exit_with_geoproc_error(_SCRIPT_NAME) # Return any PYTHON or system specific errors except: lu.dashline(1) gprint('****Failed in step 6. Details follow.****') lu.exit_with_python_error(_SCRIPT_NAME) return
def mosaicRas(): arcpy.Mosaic_management(currentRaster, mosaicRaster, "MAXIMUM", "MATCH")
"", "", "") red_T = arcpy.CreateRasterDataset_management(outWorkspace, "red.tif", cell, '32_BIT_FLOAT', "", 1, "", "", "", "", "") nir_T = arcpy.CreateRasterDataset_management(outWorkspace, "nir.tif", cell, '32_BIT_FLOAT', "", 1, "", "", "", "", "") rededge_T = arcpy.CreateRasterDataset_management(outWorkspace, "rededge.tif", cell, '32_BIT_FLOAT', "", 1, "", "", "", "", "") ##################################################################################### #Merge layers#################################################################### ###combine blue layers##################################################### arcpy.AddMessage("Merging blue...") bluex = arcpy.Mosaic_management(blue, blue_T) ################################################################################# #Combine green layers############################################################# arcpy.AddMessage("Merging green...") greenx = arcpy.Mosaic_management(green, green_T) #Combine red layers############################################################# arcpy.AddMessage("Merging red...") redx = arcpy.Mosaic_management(red, red_T) #Combine red layers############################################################# arcpy.AddMessage("Merging NIR...") nirx = arcpy.Mosaic_management(nir, nir_T) #Combine red layers############################################################# arcpy.AddMessage("Merging red edge...") rededgex = arcpy.Mosaic_management(rededge, rededge_T) ##############################################################################
## | LINEARCORRELATION_MATCHING} try: import arcpy arcpy.env.workspace = r"D:\77211356\CropClass\results" ##Create a empty TIFF format Raster Dataset with the following parameters ##Cellsize: 2 ##Pixel type: 8 Bit Unsigned Integer ##Number of Bands: 3 ##Pyramid: Build full pyramids with NEAREST interpolation and JPEG compression ##Compression: NONE ##Projection: World_Mercator ##Tile size: 128 128 arcpy.CreateRasterDataset_management("CreateRD","EmptyTIFF.tif","10","8_BIT_UNSIGNED",\ "World_Mercator.prj", "1", "", "PYRAMIDS -1 NEAREST JPEG",\ "128 128", "NONE", "") ##Mosaic two TIFF images to a single TIFF image ##Background value: 0 ##Nodata value: 9 arcpy.Mosaic_management("EXGBoundry_Clip.tif;rc.tif","EmptyTIFF.tif","LAST","FIRST","0", "0", "", "", "") ##Mosaic several 3-band TIFF images to FGDB Raster Dataset with Color Correction ##Set Mosaic Tolerance to 0.3. Mismatch larget than 0.3 will be resampled #arcpy.Mosaic_management("rgb1.tif;rgb2.tif;rgb3.tif", "Mosaic.gdb\\rgb","LAST","FIRST","", "", "", "0.3", "HISTOGRAM_MATCHING") except: print "Mosaic example failed." print arcpy.GetMessages()
# -*- coding: utf-8 -*- # --------------------------------------------------------------------------- # Mosaic.py # Created on: 2014-12-10 15:30:14.00000 # (generated by ArcGIS/ModelBuilder) # Description: # --------------------------------------------------------------------------- # Import arcpy module import arcpy # Local variables: v2327NE_ecw = "E:\\ImageFolder\\2327NE.ecw" Imagery2014_DBO_T2327NE = "Database Connections\\[email protected]\\Imagery2014.DBO.T2327NE" # Process: Mosaic arcpy.Mosaic_management("E:\\ImageFolder\\2327NE.ecw", Imagery2014_DBO_T2327NE, "LAST", "FIRST", "", "", "NONE", "0", "NONE") def MosaicImages(filePath, target): try: arcpy.Mosaic_management(filePath, target, "LAST", "FIRST", "", "", "NONE", "0", "NONE") except Exception as inst: ST = CommonTasks.ScheduledTask(layerFolder,inst,LogFile,arcpy.GetMessages()) ST.SendErrorMail() ST.UpdateLogFile()
if (os.path.exists(checkpoint_file)): checkpoint_handle = open(checkpoint_file, "w+") else: checkpoint_handle = open(checkpoint_file, "w") checkpoint_handle.write(txt_path + "\n") checkpoint_handle.close() # wait for a while to avoid overloading time.sleep(180) # Process7: Define coordinate system for the Raster dataset: arcpy.AddMessage( "Processing: Define coordinate system for the Raster dataset...") sr = Reference_Coord for raster in raster_paths: arcpy.DefineProjection_management(raster, sr) # Process8: Mosaic all the Raster dataset into one: arcpy.AddMessage("Processing: Mosaic all the Raster dataset into one...") if len(raster_paths) == 1: shutil.copy(raster_paths[0], Output_Raster) elif len(raster_paths) == 0: arcpy.AddMessage("ERROR: Raster mosaic incomplete!") else: last_raster_path = raster_paths.pop() shutil.copy(last_raster_path, Output_Raster) arcpy.Mosaic_management(";".join(raster_paths), Output_Raster, "BLEND", "MATCH", "", "", "", "", "") arcpy.AddMessage(Choose_Model + " raster data conversion is complete! " + "Check the raster dataset in your selected output folder")
if not direction == "East-West;North-South": corTemp = corTempNS origin = originNS if direction == "East-West;North-South": arcpy.AddMessage("Combining linkage zones...") corTempEW.save(tWorkspace + os.sep + "xxcorTempEW") corTempEW = tWorkspace + os.sep + "xxcorTempEW" corTempNS.save(tWorkspace + os.sep + "xxcorTempNS") corTempNS = tWorkspace + os.sep + "xxcorTempNS" arcpy.AddWarning("Attempting to rescale " + corTempEW) #Type = arcpy.GetRasterProperties (corTempEW, "MINIMUM") Rescale(corTempEW, resclEW, tWorkspace) arcpy.AddWarning("Attempting to rescale " + corTempNS) Rescale(corTempNS, resclNS, tWorkspace) arcpy.Mosaic_management(resclEW, resclNS, "MINIMUM") corTemp = resclNS origin = arcpy.Merge_management([originEW, originNS], tWorkspace + os.sep + "xxorigin.shp") # Create connected area mask arcpy.AddMessage("Creating connected area mask to remove isolated habitat patches...") sliceRaster = arcpy.sa.Slice(corTemp, 5, "NATURAL_BREAKS") # sliceRaster.save(tWorkspace + os.sep + "sliceRaster") movementAreas = arcpy.sa.SetNull(linkLayer, linkLayer, "Value = 0") # movementAreas.save(tWorkspace + os.sep + "movement") region = arcpy.sa.RegionGroup(movementAreas, "EIGHT") # region.save(tWorkspace + os.sep + "region") arcpy.RasterToPolygon_conversion(region, tWorkspace + os.sep + "xxregion", "NO_SIMPLIFY", "VALUE")
def execute(self, parameters, messages): arcpy.env.overwriteOutput = True for param in parameters: arcpy.AddMessage("Parameter: %s = %s" % (param.name, param.valueAsText)) input_bathymetry = parameters[0].valueAsText depths = parameters[1].valueAsText input_environment = parameters[2].valueAsText environment_append = parameters[3].valueAsText temporary_directory = parameters[4].valueAsText output_raster = parameters[5].valueAsText # Define the options the script will use later # load depth strings from Includes.py depths_list = load_depth_string(depths) arcpy.AddMessage(depths_list) if not os.path.exists(temporary_directory): os.makedirs(temporary_directory) arcpy.ResetEnvironments() arcpy.env.overwriteOutput = "true" # Set environment variables env.mask = "" arcpy.AddMessage("Mask is: " + str(arcpy.env.mask)) description = arcpy.Describe(os.path.join(input_bathymetry, "bath" + str(int(float(depths_list[0]))))) cellsize1 = description.children[0].meanCellHeight env.cellSize = cellsize1 arcpy.AddMessage("Cell size is: " + str(arcpy.env.cellSize)) arcpy.AddMessage(os.path.join(input_bathymetry, "bath" + str(int(float(depths_list[0]))))) extraster = Raster(os.path.join(input_bathymetry, "bath" + str(int(float(depths_list[0]))))) extent1 = extraster.extent env.extent = extent1 arcpy.AddMessage("Extent is: " + str(arcpy.env.extent)) arcpy.env.workspace = temporary_directory spf = arcpy.Describe(os.path.join(input_bathymetry, "bath" + str(int(float(depths_list[0]))))).spatialReference arcpy.AddMessage("Coord sys is: " + str(spf.name)) try: # loop through the layers for item in depths_list: depth = int(float(item)) arcpy.AddMessage("Resizing layer " + str(depth) + " " + input_environment + "/" + environment_append + str(depth)) arcpy.ProjectRaster_management(os.path.join(input_environment, environment_append + str(depth)), os.path.join(temporary_directory, environment_append + "a" + str(depth)), spf) TempData = arcpy.sa.ApplyEnvironment(os.path.join(temporary_directory, environment_append + "a" + str(depth))) arcpy.AddMessage("Extracting " + str(depth) + " to mask") outExtractByMask = ExtractByMask(TempData, os.path.join(input_bathymetry, "bath" + str(depth))) outExtractByMask.save(temporary_directory + "/clip" + str(depth)) arcpy.AddMessage("Adding " + str(depth) + " to final layer") arcpy.Mosaic_management(temporary_directory + "/clip" + str(depth), temporary_directory + "/clip" + str(int(float(depths_list[0]))), "LAST") if depth == int(float(depths_list[-1])): arcpy.AddMessage("Creating the final layer for you, which will be called " + str(output_raster)) arcpy.CopyRaster_management(temporary_directory + "/clip" + str(int(float(depths_list[0]))), output_raster) arcpy.AddMessage("next layer " + str(depth)) except: arcpy.AddMessage(arcpy.GetMessages()) arcpy.AddMessage("Something has gone wrong likely with this: " + str(depth)) arcpy.AddMessage("Processing complete")