def ExtractRange(outRaster, outFilePath): inSQLClause = "VALUE > 0" try: # Execute ExtractByAttributes attExtract = arcpy.sa.ExtractByAttributes(outRaster, inSQLClause) print('67') sys.stdout.flush() # Save the output #attExtract.save("F:\\ree\\PM25T08.tif") rasfile = os.path.split(outRaster)[1] outFileName = os.path.splitext(rasfile)[0] + ".shp" outLine = os.path.join(outFilePath, outFileName) field = "VALUE" outGeom = "LINE" try: # Execute ExtractByAttributes arcpy.RasterDomain_3d(attExtract, outLine, outGeom) normaltime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) arcpy.AddMessage(normaltime + ":" + outRaster + "污染划分完成") print('90') sys.stdout.flush() except Exception as err: arcpy.AddMessage(outRaster + "RasterDomain_3d Failed") arcpy.AddMessage(err) except Exception as err: arcpy.AddMessage("ExtractByAttributes Failed") arcpy.AddMessage(err) traceback.print_exc() return
def webProducts(rast, project=True, method="POINT_REMOVE", tolerance=15, minimumArea=3000): rastName = arcpy.Describe(rast).baseName if project: arcpy.ProjectRaster_management( rastName, "WEB" + rastName, r"Coordinate Systems/Projected Coordinate Systems/World/WGS 1984 Web Mercator (Auxiliary Sphere).prj", "BILINEAR", "", "NAD_1983_to_WGS_1984_5") raster = "WEB" + rastName q = arcpy.RasterDomain_3d(raster, raster + "q", "POLYGON") qq = arcpy.Union_analysis(q, raster + "qq", "ALL", 0.1, "NO_GAPS") qqq = arcpy.Dissolve_management(qq, raster + "qqq") qqqq = arcpy.cartography.SimplifyPolygon(qqq, raster + "qqqq", method, tolerance, minimumArea, "NO_CHECK", "NO_KEEP") arcpy.Buffer_analysis(qqqq, "out_" + raster, "30 Feet", "FULL", "", "NONE") print "Products created." arcpy.Delete_management(rast) arcpy.Delete_management(raster + "q") arcpy.Delete_management(raster + "qq") arcpy.Delete_management(raster + "qqq") arcpy.Delete_management(raster + "qqqq")
def ExtractRange(outRaster): inSQLClause = "VALUE > 0" try: # Execute ExtractByAttributes attExtract = arcpy.sa.ExtractByAttributes(outRaster, inSQLClause) # Save the output #attExtract.save("F:\\ree\\PM25T08.tif") rasfile = os.path.split(outRaster)[1] outFileName = os.path.splitext(rasfile)[0] + ".shp" outFilePath = "D:\\shapedata\\1617\\" try: os.makedirs(outFilePath) except: print("") outLine = os.path.join(outFilePath, outFileName) field = "VALUE" outGeom = "LINE" try: # Execute ExtractByAttributes arcpy.RasterDomain_3d(attExtract, outLine, outGeom) normaltime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) arcpy.AddMessage(normaltime + ":" + outRaster + "污染划分完成") except Exception as err: arcpy.AddMessage(outRaster + "RasterDomain_3d Failed") arcpy.AddMessage(err) except Exception as err: arcpy.AddMessage("ExtractByAttributes Failed") arcpy.AddMessage(err) return
def getRasterStats(ProjectUID, ProjectID, curr_raster, raster_path, group, elevation_type, raster_format, raster_PixelType, nodata, horz_cs_name, horz_unit_name, horz_cs_wkid, vert_cs_name, vert_unit_name, rows): # NOTE: Order here must match field list in CMDRConfig inMem_NameBound = "in_memory\MemBoundary" if arcpy.Exists(inMem_NameBound): arcpy.Delete_management(inMem_NameBound) Utility.addToolMessages() arcpy.RasterDomain_3d(raster_path, inMem_NameBound, "POLYGON")[0] Utility.addToolMessages() boundary = Utility.getExistingRecord(in_table=inMem_NameBound, field_names=['SHAPE@'], uidIndex=-1)[0][0] newRow = [ ProjectUID, ProjectID, boundary, curr_raster, raster_path, group, elevation_type, raster_format, nodata, raster_PixelType ] arcpy.CalculateStatistics_management(in_raster_dataset=raster_path, skip_existing="OVERWRITE") cellSize = getRasterProperties(raster_path, newRow) newRow.append(horz_cs_name) newRow.append(horz_unit_name) newRow.append(horz_cs_wkid) newRow.append(vert_cs_name) newRow.append(vert_unit_name) newRow.append(None) # Vert WKID, we can't know this in python rows.append(newRow) return cellSize
def ExtractRange2(outRaster, inMaskData): outExtractByMask = None attExtract = None try: # Execute ExtractByMask outExtractByMask = ExtractByMask(outRaster, inMaskData) except Exception as err: arcpy.AddMessage("ExtractByMask Failed") arcpy.AddMessage(err) return #Get the geoprocessing result object elevMAXIMUMResult = arcpy.GetRasterProperties_management( outExtractByMask, "MAXIMUM") #Get the elevation standard deviation value from geoprocessing result object elevMAXIMUM = float(elevMAXIMUMResult.getOutput(0)) elevMINIMUMResult = arcpy.GetRasterProperties_management( outExtractByMask, "MINIMUM") #Get the elevation standard deviation value from geoprocessing result object elevMINIMUM = float(elevMINIMUMResult.getOutput(0)) inSQLClause = "VALUE > " + str(elevMAXIMUM - (elevMAXIMUM - elevMINIMUM) / 20) try: # Execute ExtractByAttributes attExtract = ExtractByAttributes(outExtractByMask, inSQLClause) except Exception as err: arcpy.AddMessage("ExtractByAttributes Failed") arcpy.AddMessage(err) return # Save the output #attExtract.save("F:\\ree\\PM25T08.tif") rasfile = os.path.split(outRaster)[1] nt = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time())) outFileName = nt + ".shp" dt = time.strftime('%m%d%H', time.localtime(time.time())) outFilePath = "F:\\changshu\\result" try: os.makedirs(outFilePath) except: print("") outLine = os.path.join(outFilePath, outFileName) field = "VALUE" outGeom = "LINE" try: # Execute ExtractByAttributes arcpy.RasterDomain_3d(attExtract, outLine, outGeom) normaltime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) arcpy.AddMessage(normaltime + ":" + outRaster + "污染划分完成") except Exception as err: arcpy.AddMessage(outRaster + "RasterDomain_3d Failed") arcpy.AddMessage(err) return
def better_isogram(input_raster, neighborhood, distance, output): """ 直接使用软件自带的等值线工具制作栅格的等值线, 线条比较曲折同时较为零碎,不利于解译; 集成焦点统计工具对原始栅格数据处理后再生产等值线图, 线条清晰光滑,易于解译和使用。 :param input_raster: 输入栅格 :param output: 输出更好的等值线数据集。 :param neighborhood: {Int} 邻域分析,指定的像元范围 :param distance: {Double} 等值线间隔 :return: """ # 进行焦点统计分析 fs = arcpy.sa.FocalStatistics nrt = arcpy.sa.NbrRectangle neighborhood = int(neighborhood) neighborhood = nrt(neighborhood, neighborhood, "cell") fs_result = fs(input_raster, neighborhood, "MEAN") # 等值线计算 contour = "in_memory/contour" arcpy.sa.Contour(fs_result, contour, float(distance)) # 获得输入栅格范围矢量 domain = "in_memory/domain" arcpy.RasterDomain_3d(input_raster, domain, "POLYGON") # 擦除范围外的线条 erase_left = "%scratchFolder%/out172005.shp" # arcpy.AddMessage(neighborhood) arcpy.Erase_analysis(contour, domain, erase_left) # arcpy.AddMessage(neighborhood) arcpy.Erase_analysis(contour, erase_left, output) arcpy.Delete_management(erase_left)
sr) # Process: Buffer arcpy.Buffer_analysis( mask_scratch + "\\GEOG490_maskscratch.gdb\\" + mask_file[:-4] + "_proj", mask_scratch + "\\GEOG490_maskscratch.gdb\\" + mask_file[:-4] + "_buffer", buffer_size, "FULL", "ROUND", "NONE", "", "PLANAR") # Process: Feature Envelope To Polygon arcpy.FeatureEnvelopeToPolygon_management( mask_scratch + "\\GEOG490_maskscratch.gdb\\" + mask_file[:-4] + "_buffer", mask_scratch + "\\GEOG490_maskscratch.gdb\\" + mask_file[:-4] + "_masktemp", "SINGLEPART") # Select Layers by Location, then output layer to scratch # Process: Raster Domain arcpy.RasterDomain_3d( raster_location + raster_file, mask_scratch + "\\GEOG490_maskscratch.gdb\\" + mask_file[:-4] + "_rasterExtent", "POLYGON") # Make temp layer with polygon mask and raster extent arcpy.MakeFeatureLayer_management( mask_scratch + "\\GEOG490_maskscratch.gdb\\" + mask_file[:-4] + "_masktemp", 'temp_mask_within') arcpy.MakeFeatureLayer_management( mask_scratch + "\\GEOG490_maskscratch.gdb\\" + mask_file[:-4] + "_rasterExtent", 'temp_rasterExtent') # Get only polygons that are within the raster arcpy.SelectLayerByLocation_management("temp_mask_within", "COMPLETELY_WITHIN", "temp_rasterExtent", "", "NEW_SELECTION", "NOT_INVERT") # Output new file, only if there are no problems matchcount = int(arcpy.GetCount_management('temp_mask_within')[0]) if matchcount == 0:
def createVectorBoundaryC(f_path, f_name, raster_props, stat_out_folder, vector_bound_path, minZ, maxZ, bound_path, elev_type): a = datetime.now() arcpy.AddMessage("\tCreating {} bound for '{}' using min z '{}' and max z'{}'".format(elev_type, f_path, minZ, maxZ)) vector_1_bound_path = os.path.join(stat_out_folder, "B1_{}.shp".format(f_name)) vector_2_bound_path = os.path.join(stat_out_folder, "B2_{}.shp".format(f_name)) vector_3_bound_path = os.path.join(stat_out_folder, "B3_{}.shp".format(f_name)) vector_4_bound_path = os.path.join(stat_out_folder, "B4_{}.shp".format(f_name)) vector_5_bound_path = os.path.join(stat_out_folder, "B5_{}.shp".format(f_name)) deleteFileIfExists(vector_bound_path, useArcpy=True) deleteFileIfExists(vector_1_bound_path, useArcpy=True) deleteFileIfExists(vector_2_bound_path, useArcpy=True) deleteFileIfExists(vector_3_bound_path, useArcpy=True) deleteFileIfExists(vector_4_bound_path, useArcpy=True) deleteFileIfExists(vector_5_bound_path, useArcpy=True) arcpy.RasterDomain_3d(in_raster=f_path, out_feature_class=vector_5_bound_path, out_geometry_type="POLYGON") Utility.addToolMessages() arcpy.MultipartToSinglepart_management(in_features=vector_5_bound_path, out_feature_class=vector_4_bound_path) Utility.addToolMessages() checkRecordCount(vector_4_bound_path) arcpy.EliminatePolygonPart_management(in_features=vector_4_bound_path, out_feature_class=vector_3_bound_path, condition="AREA", part_area="10000 SquareMiles", part_area_percent="0", part_option="CONTAINED_ONLY") Utility.addToolMessages() checkRecordCount(vector_3_bound_path) arcpy.SimplifyPolygon_cartography( in_features=vector_3_bound_path, out_feature_class=vector_2_bound_path, algorithm="POINT_REMOVE", tolerance="{} Meters".format(C_SIMPLE_DIST), minimum_area="0 Unknown", error_option="RESOLVE_ERRORS", collapsed_point_option="NO_KEEP", in_barriers="" ) Utility.addToolMessages() checkRecordCount(vector_2_bound_path) arcpy.AddMessage('ZFlag: ' + arcpy.env.outputZFlag) arcpy.AddMessage('MFlag: ' + arcpy.env.outputMFlag) arcpy.Dissolve_management(in_features=vector_2_bound_path, out_feature_class=vector_1_bound_path, dissolve_field="", statistics_fields="", multi_part="MULTI_PART", unsplit_lines="DISSOLVE_LINES") Utility.addToolMessages() checkRecordCount(vector_1_bound_path) deleteFields(vector_1_bound_path) record_count = checkRecordCount(vector_1_bound_path) footprint_area = 0 for row in arcpy.da.SearchCursor(vector_1_bound_path, ["SHAPE@"]): # @UndefinedVariable shape = row[0] footprint_area = shape.getArea ("PRESERVE_SHAPE", "SQUAREMETERS") if footprint_area <= 0: arcpy.AddMessage("\tWARNGING: Area is 0 in {} '{}' bound '{}'".format(elev_type, f_path, vector_bound_path)) addField(in_table=vector_1_bound_path, field_name=FIELD_INFO[PATH][0], field_alias=FIELD_INFO[PATH][1], field_type=FIELD_INFO[PATH][2], field_length=FIELD_INFO[PATH][3]) addField(in_table=vector_1_bound_path, field_name=FIELD_INFO[NAME][0], field_alias=FIELD_INFO[NAME][1], field_type=FIELD_INFO[NAME][2], field_length=FIELD_INFO[NAME][3]) addField(in_table=vector_1_bound_path, field_name=FIELD_INFO[AREA][0], field_alias=FIELD_INFO[AREA][1], field_type=FIELD_INFO[AREA][2], field_length=FIELD_INFO[AREA][3]) addField(in_table=vector_1_bound_path, field_name=FIELD_INFO[ELEV_TYPE][0], field_alias=FIELD_INFO[ELEV_TYPE][1], field_type=FIELD_INFO[ELEV_TYPE][2], field_length=FIELD_INFO[ELEV_TYPE][3]) addField(in_table=vector_1_bound_path, field_name=FIELD_INFO[RANGE][0], field_alias=FIELD_INFO[RANGE][1], field_type=FIELD_INFO[RANGE][2], field_length=FIELD_INFO[RANGE][3]) deleteFields(vector_1_bound_path) arcpy.AddMessage(raster_props) for field_name in KEY_LIST: time.sleep(0.25) field_shpname = FIELD_INFO[field_name][0] field_alias = FIELD_INFO[field_name][1] field_type = FIELD_INFO[field_name][2] field_length = FIELD_INFO[field_name][3] field_value = raster_props[field_name] if field_type == "TEXT": if str(field_value).endswith('\\'): field_value = str(field_value)[0:-1] field_value = r'"{}"'.format(field_value) addField(in_table=vector_1_bound_path, field_name=field_shpname, field_alias=field_alias, field_type=field_type, field_length=field_length, expression=field_value) b_f_path, b_f_name = os.path.split(f_path) b_f_name = os.path.splitext(b_f_name)[0] arcpy.CalculateField_management(in_table=vector_1_bound_path, field=FIELD_INFO[PATH][0], expression='"{}"'.format(b_f_path), expression_type="PYTHON_9.3") arcpy.CalculateField_management(in_table=vector_1_bound_path, field=FIELD_INFO[NAME][0], expression='"{}"'.format(b_f_name), expression_type="PYTHON_9.3") arcpy.CalculateField_management(in_table=vector_1_bound_path, field=FIELD_INFO[AREA][0], expression=footprint_area, expression_type="PYTHON_9.3") arcpy.CalculateField_management(in_table=vector_1_bound_path, field=FIELD_INFO[ELEV_TYPE][0], expression='"{}"'.format(elev_type), expression_type="PYTHON_9.3") try: z_expr = "!{}! - !{}!".format(FIELD_INFO[MAX][0], FIELD_INFO[MIN][0]) arcpy.CalculateField_management(in_table=vector_1_bound_path, field=FIELD_INFO[RANGE][0], expression=z_expr, expression_type="PYTHON_9.3") except: pass deleteFileIfExists(vector_bound_path, True) arcpy.Clip_analysis(in_features=vector_1_bound_path, clip_features=bound_path, out_feature_class=vector_bound_path, cluster_tolerance="") Utility.addToolMessages() checkRecordCount(vector_bound_path) deleteFields(vector_bound_path) #debug = False #try: # debug = (str(f_path).find("alamazoo") >= 0) #except: # debug = False #if not debug: deleteFileIfExists(vector_1_bound_path, useArcpy=True) deleteFileIfExists(vector_2_bound_path, useArcpy=True) deleteFileIfExists(vector_3_bound_path, useArcpy=True) deleteFileIfExists(vector_4_bound_path, useArcpy=True) deleteFileIfExists(vector_5_bound_path, useArcpy=True) #else: # arcpy.AddMessage("\tleaving artifacts for {} '{}'".format(elev_type, vector_bound_path)) doTime(a, "\tCreated BOUND {}".format(vector_bound_path))
def main(in_dir, out_dir, dem_dir, hydrog_dir, info1, info2, info3, info4, info5, info6): arcpy.env.overwriteOutput = True # Create the output directory if not os.path.isdir(os.getcwd() + out_dir): os.makedirs(os.getcwd() + out_dir) # Step 1. Use "raster domain" tool from arcpy and create a domain shapefile from the DEM raster file. print("Start the pre-processing steps") arcpy.RasterDomain_3d( in_raster=os.path.normpath(os.getcwd() + os.sep + os.pardir + os.sep + os.pardir) + dem_dir + info1, out_feature_class=os.getcwd() + out_dir + info2, out_geometry_type="POLYGON") # Step 2. Select the domain shapefile and define an internal buffer of the domain shapefile using "buffer" tool. # Remember to use negative sign for the internal buffer. arcpy.Buffer_analysis(in_features=os.getcwd() + out_dir + info2, out_feature_class=os.getcwd() + out_dir + info3, buffer_distance_or_field=info4, line_side="FULL", line_end_type="ROUND", dissolve_option="NONE", dissolve_field="", method="GEODESIC") # Step 3. Use "select by attribute" tool to exclude the canals. name = os.path.normpath(os.getcwd() + os.sep + os.pardir + os.sep + os.pardir) + hydrog_dir + info5 name_temp = name.replace('.shp', '_lyr') arcpy.MakeFeatureLayer_management(name, name_temp) medres_NHDPlusflowline_noCanal = arcpy.SelectLayerByAttribute_management( name_temp, 'NEW_SELECTION', "FTYPE <> " + info6) # Step 4. Clip the medium-resolution NHD (i.e., medres_NHDPlusflowline.shp) for the "internal250mbuffer.shp". arcpy.Clip_analysis(in_features=medres_NHDPlusflowline_noCanal, clip_features=os.getcwd() + out_dir + info3, out_feature_class=os.getcwd() + out_dir + 'medres_NHDPlusflowline_noCanal_to_bfr.shp', cluster_tolerance="") # Step 5. Find the starting points (dangles) that are used as an input to the network delineation process. # In this research study, we used the main stems for the dangles. This was done within the ArcGIS software # and the results are here as input files for the next steps. Following comment explains the steps we took # in the ArcGIS software to create these files. # 1.Select the dangles (using "feature vertices to points" tool and set point_location="DANGLE"). # These are going to be used as the starting points to delineate the stream network. In our work, we only # considered the main stems when generating the dangle points. This is due to the fact that the main stems # had larger contributing areas in our case studies compared to the side tributaries. # 2.Open the attribute table of the dangle shapefile. Add a field using "add field" tool. Then, use # "calculate field" tool to put a value of 1 for all the rows in this field. # 3.Use "feature to raster" tool to rasterize the dangle points. Make sure to set the extent and snap # raster sizes to be the same as the DEM size (an extracted DEM that covers the domain) in the enviroment # settings of the tool. # 4.Use the "reclassify" tool to reclassify the inlets.tif raster layer so that 1 is one and the rest ( # i.e., 0 or NAN) is zero. # Step 6. Generate the evenly dispersed nodes along the main stem. # The evenly dispersed nodes are used as an input to the network delineation process. This was done within # the ArcGIS software and the results are here as input files for the next steps. Following comment explains # the steps we took in the ArcGIS software to create these nodes. # 1.Select the main stem from the buffered medium resolution NHDPlus # dataset and save it as a new feature. Then, use "dissolve" tool to merge the segments (reaches) of this # new feature into one single segment. Next, use "editor -> split" tool to split this feature into smaller # equal segments with 3 km long. Then, create a new point shapefile and use the editor tool to generate # points on the upstream and downstream ends of these equal segments. The new shape file is the evenly # dispersed nodes on the main stems. This is required when delineating the stream network and catchments. # Step 7. Using a non-conditioned DEM and calculating the HAND using the following procedure. # 1.Fill pits in the original dem print('Running PitRemove Function') i_path = os.path.normpath(os.getcwd() + os.sep + os.pardir + os.sep + os.pardir) + dem_dir + info1 o_path = os.getcwd() + out_dir + "fel.tif" bashCommand = "mpiexec -n 10 PitRemove -z " + i_path + " -fel " + o_path os.system(bashCommand) time.sleep(120) # 2.Compute the D8 flow direction. print('Running D8 Flow Direction Function') i_path = os.getcwd() + out_dir + "fel.tif" o1_path = os.getcwd() + out_dir + "p.tif" o2_path = os.getcwd() + out_dir + "sd8.tif" bashCommand = "mpiexec -n 10 D8FlowDir -fel " + i_path + " -p " + o1_path + " -sd8 " + o2_path os.system(bashCommand) time.sleep(360) # 3.Compute D8 area contributing Compute D8 area contributing. print('Running D8 Area Contributing Function') i1_path = os.getcwd() + in_dir + "inlets_on_mainstem.tif" i2_path = os.getcwd() + out_dir + "p.tif" o_path = os.getcwd() + out_dir + "ad8.tif" bashCommand = "mpiexec -n 10 Aread8 -wg " + i1_path + " -p " + i2_path + " -ad8 " + o_path + " -nc " os.system(bashCommand) time.sleep(120) # 4.Use a threshold to delineate the stream print('Running Network Delineation Function') i_path = os.getcwd() + out_dir + "ad8.tif" o_path = os.getcwd() + out_dir + "src.tif" bashCommand = "mpiexec -n 10 Threshold -ssa " + i_path + " -src " + o_path + " -thresh 1" os.system(bashCommand) time.sleep(120) # 5.Generate network and watershed i1_path = os.getcwd() + in_dir + "Evenly_dispersed_nodes.shp" i2_path = os.getcwd() + out_dir + "fel.tif" i3_path = os.getcwd() + out_dir + "p.tif" i4_path = os.getcwd() + out_dir + "ad8.tif" i5_path = os.getcwd() + out_dir + "src.tif" o1_path = os.getcwd() + out_dir + "ord.tif" o2_path = os.getcwd() + out_dir + "tree.dat" o3_path = os.getcwd() + out_dir + "coord.dat" o4_path = os.getcwd() + out_dir + "net.shp" o5_path = os.getcwd() + out_dir + "w.tif" bashCommand = "mpiexec -n 10 Streamnet -o " + i1_path + " -fel " + i2_path + " -p " + i3_path + \ " -ad8 " + i4_path + " -src " + i5_path + " -ord " + o1_path + " -tree " + o2_path + \ " -coord " + o3_path + " -net " + o4_path + " -w " + o5_path os.system(bashCommand) # 6.Compute the D-inf flow direction. This function may take several hours to run. print('Running D-infinity Flow Direction Function') i_path = os.getcwd() + out_dir + "fel.tif" o1_path = os.getcwd() + out_dir + "ang.tif" o2_path = os.getcwd() + out_dir + "slp.tif" bashCommand = "mpiexec -n 10 DinfFlowDir -fel " + i_path + " -ang " + o1_path + " -slp " + o2_path os.system(bashCommand) time.sleep(360) # 7.Compute the HAND using D-inf Distance Down. print('Running D-infinity Distance Down Function to calculate HAND') i1_path = os.getcwd() + out_dir + "fel.tif" i2_path = os.getcwd() + out_dir + "src.tif" i3_path = os.getcwd() + out_dir + "ang.tif" o_path = os.getcwd() + out_dir + "hand.tif" bashCommand = "mpiexec -n 10 DinfDistDown -fel " + i1_path + " -src " + i2_path + " -ang " + i3_path + " -m ave v " + " -dd " + o_path + " -nc " os.system(bashCommand) time.sleep(360) print('Done!')
import arcpy infc = r'C:\Users\adamclark\Desktop\deleteme\1008_1_domain.shp' inraster = r'C:\Users\adamclark\Desktop\deleteme\1008_1_domain.shp' outfc = r'C:\Users\adamclark\Desktop\deleteme\1008_1_domain_300bufpy.shp' bufferDistance = -300 arcpy.Buffer_analysis(infc, outfc, bufferDistance, "", "", "ALL") print "buffer complete" arcpy.RasterDomain_3d("dtm_grd", "raster_domain.shp", "POLYGON") print "domain complete"
def RastDomain(storepath,clip,clipname): print("Creating Raster Domain") clipshape = storepath + clipname + ".shp" arcpy.RasterDomain_3d(clip,clipshape,"POLYGON") print("Raster Domain created") return clipshape
def bianLi(rootDir, wildcard, recursion): # Obtain a license for the ArcGIS 3D Analyst extension,获取3D分析的许可 arcpy.CheckOutExtension("3D") #查询rootDir下所有目录与文件,赋值给dirs dirs = os.listdir(rootDir) #遍历dirs下所有目录与文件 for dir in dirs: #获取完整路径名 fullname = rootDir + '\\' + dir #print "rootDir:",rootDir #判断完整路径是否是文件夹,如果是文件夹,继续迭代查询子文件夹及文件 if (os.path.isdir(fullname) & recursion): bianLi(fullname, wildcard, recursion) # 如果是文件名而非文件夹目录 else: #如果文件名中包含名为wildcard的扩展名,则继续: if (dir.endswith(wildcard)): #输入栅格文件名 InputImage = dir #拆分目录 FileNames = os.path.splitext(InputImage) ImageBaseName = FileNames[0] #创建输出栅格范围矢量文件名,此处不包含路径 outPoly = ImageBaseName + "_domain.shp" #创建完成文件名,包含路径 fullOutPutPoly = rootDir + "\\" + outPoly #print('fullOutPutImageAfterClip=='+fullOutPutImageAfterClip) #creat env.workspace EWSName = rootDir env.workspace = EWSName #判断输出文件是否存在,不存在则处理 if not (os.path.isfile(fullOutPutPoly)): print('fullOutPutPoly:----' + fullOutPutPoly) # Set Local Variables outGeom = "POLYGON" # output geometry type try: print "---------------------------------------------" print " " #调用提取栅格范围的arcpy arcpy.RasterDomain_3d(InputImage, fullOutPutPoly, outGeom) print(outPoly + '-----is ok') #添加字段,将影像文件名写入字段 addNameToDomain(outPoly, ImageBaseName) except: print "RasterDomain_3d example failed." print arcpy.GetMessages() elif (os.path.isfile(fullOutPutPoly)): #添加字段,将影像文件名写入字段 addNameToDomain(outPoly, ImageBaseName)
'''********************************************************************* Name: RasterDomain Example Description: This script demonstrates how to use the Raster Domain tool to generate polygon footprints for all *.img rasters in a given workspace. **********************************************************************''' # Import system modules import arcpy # Set environment settings arcpy.env.workspace = "E:/AbioticData/Bathymetry/Multibeam/Mosaics/Masked" # Create the list of IMG rasters rasterList = arcpy.ListRasters("*", "TIF") # Verify there are rasters in the list if rasterList: # Loop the process for each raster for raster in rasterList: # Set Local Variables outGeom = "POLYGON" # output geometry type # The [:-4] strips the .img from the raster name outPoly = "domain_" + raster[:-4] + ".shp" print("Creating footprint polygon for " + raster + ".") #Execute RasterDomain arcpy.RasterDomain_3d(raster, outPoly, outGeom) print("Finished.") else: print("There are no TIF files in the " + env.workspace + " directory.")
def create_raster(input_source, depth_raster, depth_value, boundary_size, boundary_offset, output_raster, debug): try: # Get Attributes from User if debug == 0: # script variables aprx = arcpy.mp.ArcGISProject("CURRENT") home_directory = aprx.homeFolder tiff_directory = home_directory + "\\Tiffs" tin_directory = home_directory + "\\Tins" scripts_directory = aprx.homeFolder + "\\Scripts" rule_directory = aprx.homeFolder + "\\rule_packages" log_directory = aprx.homeFolder + "\\Logs" layer_directory = home_directory + "\\layer_files" project_ws = aprx.defaultGeodatabase enableLogging = True DeleteIntermediateData = True verbose = 0 in_memory_switch = True else: # debug home_directory = r'D:\Temporary\Flood\3DFloodImpact' tiff_directory = home_directory + "\\Tiffs" log_directory = home_directory + "\\Logs" layer_directory = home_directory + "\\LayerFiles" project_ws = home_directory + "\\Testing.gdb" enableLogging = False DeleteIntermediateData = True verbose = 1 in_memory_switch = False scratch_ws = common_lib.create_gdb(home_directory, "Intermediate.gdb") arcpy.env.workspace = scratch_ws arcpy.env.overwriteOutput = True # fail safe for Eurpose's comma's depth_value = float(re.sub("[,.]", ".", depth_value)) boundary_size = float(re.sub("[,.]", ".", boundary_size)) boundary_offset = float(re.sub("[,.]", ".", boundary_offset)) bail = 0 if debug == 1: use_in_memory = False else: use_in_memory = True common_lib.set_up_logging(log_directory, TOOLNAME) start_time = time.clock() if arcpy.CheckExtension("3D") == "Available": arcpy.CheckOutExtension("3D") if arcpy.CheckExtension("Spatial") == "Available": arcpy.CheckOutExtension("Spatial") # check if input exists if arcpy.Exists(input_source): arcpy.AddMessage( "Processing input source: " + common_lib.get_name_from_feature_class(input_source)) no_initial_depth_raster = False # create isnull from input source if use_in_memory: is_null = "in_memory/isnull_copy" else: is_null = os.path.join(scratch_ws, "isnull_copy") if arcpy.Exists(is_null): arcpy.Delete_management(is_null) # check where we have NULL values is_Null_raster = arcpy.sa.IsNull(input_source) is_Null_raster.save(is_null) # if we have a depth raster as input: make sure it overlaps with input_source if depth_raster: if arcpy.Exists(depth_raster): # Check if same spatial reference!!! if common_lib.check_same_spatial_reference( [input_source], [depth_raster]) == 1: depth_raster = None raise MixOfSR else: if use_in_memory: clip_raster = "in_memory/clip_copy" else: clip_raster = os.path.join( scratch_ws, "clip_copy") if arcpy.Exists(clip_raster): arcpy.Delete_management(clip_raster) # check extents # clip terrain to extent msg_body = create_msg_body( "Clipping depth raster to input flooding layer extent", 0, 0) msg(msg_body) arcpy.Clip_management(depth_raster, "#", clip_raster, input_source, "#", "#", "MAINTAIN_EXTENT") # TODO double check below # create IsNull to be used to check for NoData. if use_in_memory: is_null0 = "in_memory/is_null0" else: is_null0 = os.path.join( scratch_ws, "is_null0") if arcpy.Exists(is_null0): arcpy.Delete_management(is_null0) is_null_raster = arcpy.sa.IsNull(clip_raster) is_null_raster.save(is_null0) min_value = arcpy.GetRasterProperties_management( is_null0, "MINIMUM")[0] # all_nodata = arcpy.GetRasterProperties_management(clip_raster, "ALLNODATA")[0] if int(min_value) == 1: msg_body = create_msg_body( "Input rasters do not overlap.", 0, 0) msg(msg_body, WARNING) depth_raster = None else: org_depth_raster = depth_raster depth_raster = clip_raster no_initial_depth_raster = False # if depth_value > 0: # # grab set all values > 2 to default depth value # if use_in_memory: # depth_push = "in_memory/depth_push" # else: # depth_push = os.path.join(scratch_ws, "depth_push") # # if arcpy.Exists(depth_push): # arcpy.Delete_management(depth_push) # # msg_body = create_msg_body("Pushing depth > 2 to: " + str(depth_value), 0, 0) # msg(msg_body) # # depth_pushRaster = arcpy.sa.Con(clip_raster, depth_value, clip_raster, "VALUE > 2") # depth_pushRaster.save(depth_push) # # depth_raster = depth_push # else: # depth_raster = clip_raster else: depth_raster = None raise NoDepthRaster # if we don't have a depth raster: crate one based on the depth value if not depth_raster: if depth_value != 0: no_initial_depth_raster = True arcpy.AddMessage("Using default depth value of: " + str(depth_value)) # create raster from default depth value if use_in_memory: depth_raster = "in_memory/depth_value_raster" else: depth_raster = os.path.join( scratch_ws, "depth_value_raster") if arcpy.Exists(depth_raster): arcpy.Delete_management(depth_raster) # create raster from default depth value msg_body = create_msg_body( "Create depth raster from default depth value.", 0, 0) msg(msg_body) outConRaster = arcpy.sa.Con( is_null, depth_value, depth_value) outConRaster.save(depth_raster) else: bail = 1 msg_body = create_msg_body( "No depth raster and default depth value is 0. No point continuing.", 0, 0) msg(msg_body, WARNING) if bail == 0: # subtract depth raster from flood elevation raster cell_size_source = arcpy.GetRasterProperties_management( input_source, "CELLSIZEX") cell_size_depth = arcpy.GetRasterProperties_management( depth_raster, "CELLSIZEX") if cell_size_source.getOutput( 0) == cell_size_depth.getOutput(0): if arcpy.Exists(output_raster): arcpy.Delete_management(output_raster) # create raster from depth values # adjust values that are less than 0.2 if use_in_memory: depth_push = "in_memory/depth_boundary_push" depth_temp = "in_memory/depth_temp" else: depth_push = os.path.join( scratch_ws, "depth_boundary_push") if arcpy.Exists(depth_push): arcpy.Delete_management(depth_push) depth_temp = os.path.join( scratch_ws, "depth_temp") if arcpy.Exists(depth_temp): arcpy.Delete_management(depth_temp) msg_body = create_msg_body( "Adjusting boundary values by: " + str(boundary_offset), 0, 0) msg(msg_body) # add boundary offset to depth raster arcpy.Plus_3d(depth_raster, boundary_offset, depth_temp) depth_raster_object = arcpy.sa.Raster(depth_raster) # for values less than 0.2 -> grab adjusted depth raster. depth_push_Boundary_Raster = arcpy.sa.Con( depth_raster_object < 0.2, depth_temp, depth_raster) depth_push_Boundary_Raster.save(depth_push) depth_raster = depth_push if use_in_memory: clip_depth = "in_memory/clip_depth" else: clip_depth = os.path.join( scratch_ws, "clip_depth") if arcpy.Exists(clip_depth): arcpy.Delete_management(clip_depth) # create raster from default depth value msg_body = create_msg_body( "Create clip depth raster...", 0, 0) msg(msg_body) # grab depth elevation values where not null and null where is null (clip using flooding raster) outConRaster = arcpy.sa.Con( is_null, input_source, depth_raster) outConRaster.save(clip_depth) msg_body = create_msg_body( "Subtracting depth raster from input flooding raster.", 0, 0) msg(msg_body) if use_in_memory: minus_raster = "in_memory/minus_3D" else: minus_raster = os.path.join( scratch_ws, "minus_3D") if arcpy.Exists(minus_raster): arcpy.Delete_management(minus_raster) # actual subtract arcpy.Minus_3d(input_source, clip_depth, minus_raster) # now we want just the outside cells (1x cellsize) if use_in_memory: raster_polygons = "in_memory/raster_polygons" else: raster_polygons = os.path.join( scratch_ws, "raster_polygons") if arcpy.Exists(raster_polygons): arcpy.Delete_management(raster_polygons) out_geom = "POLYGON" # output geometry type arcpy.RasterDomain_3d(minus_raster, raster_polygons, out_geom) # buffer it outwards first if use_in_memory: polygons_outward = "in_memory/outward_buffer" else: polygons_outward = os.path.join( scratch_ws, "outward_buffer") if arcpy.Exists(polygons_outward): arcpy.Delete_management(polygons_outward) # x = cell_size_source.getOutput(0) x = float( re.sub("[,.]", ".", str(cell_size_source.getOutput(0)))) # x = float(str(cell_size_source.getOutput(0))) buffer_out = int(x) xy_unit = common_lib.get_xy_unit(minus_raster, 0) if xy_unit == "Feet": buffer_text = str(buffer_out) + " Feet" else: buffer_text = str(buffer_out) + " Meters" sideType = "FULL" arcpy.Buffer_analysis(raster_polygons, polygons_outward, buffer_text, sideType) # buffer it inwards so that we have a polygon only of the perimeter plus a 2 cells inward. if use_in_memory: polygons_inward = "in_memory/inward_buffer" else: polygons_inward = os.path.join( scratch_ws, "inward_buffer") if arcpy.Exists(polygons_inward): arcpy.Delete_management(polygons_inward) # x = cell_size_source.getOutput(0) x = float( re.sub("[,.]", ".", str(cell_size_source.getOutput(0)))) # x = float(str(cell_size_source.getOutput(0))) buffer_in = (boundary_size - 1) + int( 2 * x ) # boundary is always 2 cellsizes / user can't go lower than 2. xy_unit = common_lib.get_xy_unit(minus_raster, 0) if xy_unit == "Feet": buffer_text = "-" + str(buffer_in) + " Feet" else: buffer_text = "-" + str(buffer_in) + " Meters" sideType = "FULL" arcpy.Buffer_analysis(polygons_outward, polygons_inward, buffer_text, sideType) if use_in_memory: erase_polygons = "in_memory/erase" else: erase_polygons = os.path.join( scratch_ws, "erase") if arcpy.Exists(erase_polygons): arcpy.Delete_management(erase_polygons) xyTol = "1 Meters" arcpy.Erase_analysis(polygons_outward, polygons_inward, erase_polygons) msg_body = create_msg_body( "Buffering depth edges...", 0, 0) msg(msg_body) if use_in_memory: extract_mask_raster = "in_memory/extract_mask" else: extract_mask_raster = os.path.join( scratch_ws, "extract_mask") if arcpy.Exists(extract_mask_raster): arcpy.Delete_management( extract_mask_raster) extract_temp_raster = arcpy.sa.ExtractByMask( minus_raster, erase_polygons) extract_temp_raster.save(extract_mask_raster) if no_initial_depth_raster == True: if use_in_memory: plus_mask = "in_memory/plus_mask" else: plus_mask = os.path.join( scratch_ws, "plus_mask") if arcpy.Exists(plus_mask): arcpy.Delete_management(plus_mask) arcpy.Plus_3d(extract_mask_raster, (depth_value - 1), plus_mask) extract_mask_raster = plus_mask if use_in_memory: minus_raster2 = "in_memory/minus_3D2" else: minus_raster2 = os.path.join( scratch_ws, "minus_3D2") if arcpy.Exists(minus_raster2): arcpy.Delete_management(minus_raster2) # push depth elevation raster down by default depth value if depth_value > 0 and no_initial_depth_raster == False: msg_body = create_msg_body( "Pushing inner depth down by: " + str(depth_value) + " to prevent z-fighting.", 0, 0) msg(msg_body) arcpy.Minus_3d(minus_raster, depth_value, minus_raster2) else: minus_raster2 = minus_raster if 0: #use_in_memory: mosaic_raster = "in_memory/mosaic" else: mosaic_raster = os.path.join( scratch_ws, "mosaic") if arcpy.Exists(mosaic_raster): arcpy.Delete_management(mosaic_raster) listRasters = [] listRasters.append(extract_mask_raster) listRasters.append(minus_raster2) desc = arcpy.Describe(listRasters[0]) # grab the original outside cells and the pushed down depth elevation raster arcpy.MosaicToNewRaster_management( listRasters, os.path.dirname(mosaic_raster), os.path.basename(mosaic_raster), desc.spatialReference, "32_BIT_FLOAT", x, 1, "FIRST", "") # now we do an isnull on raster domain poly assignmentType = "CELL_CENTER" priorityField = "#" # Execute PolygonToRaster calc_field = "value_field" common_lib.delete_add_field( raster_polygons, calc_field, "DOUBLE") arcpy.CalculateField_management( raster_polygons, calc_field, 1, "PYTHON_9.3") if use_in_memory: poly_raster = "in_memory/poly_raster" else: poly_raster = os.path.join( scratch_ws, "poly_raster") if arcpy.Exists(poly_raster): arcpy.Delete_management(poly_raster) arcpy.PolygonToRaster_conversion( raster_polygons, calc_field, poly_raster, assignmentType, priorityField, x) # create isnull if use_in_memory: is_null2 = "in_memory/isnull_copy2" else: is_null2 = os.path.join( scratch_ws, "isnull_copy2") if arcpy.Exists(is_null2): arcpy.Delete_management(is_null2) is_Null_raster2 = arcpy.sa.IsNull(poly_raster) is_Null_raster2.save(is_null2) # con on mosaic finalRaster = arcpy.sa.Con(is_null2, poly_raster, mosaic_raster) finalRaster.save(output_raster) else: arcpy.AddWarning( "Cell size of " + common_lib.get_name_from_feature_class( input_source) + " is different than " + org_depth_raster + ". Exiting...") output_raster = None if use_in_memory: arcpy.Delete_management("in_memory") else: # use default depth value raise NoInputLayer end_time = time.clock() msg_body = create_msg_body( "Set Flood Elevation Value for Raster completed successfully.", start_time, end_time) msg(msg_body) arcpy.ClearWorkspaceCache_management() return output_raster else: raise LicenseErrorSpatial else: raise LicenseError3D arcpy.ClearWorkspaceCache_management() except MixOfSR: # The input has mixed SR # print(( 'Input data has mixed spatial references. Ensure all input is in the same spatial reference, including the same vertical units.' )) arcpy.AddError( 'Input data has mixed spatial references. Ensure all input is in the same spatial reference, including the same vertical units.' ) except NoInputLayer: print("Can't find Input layer. Exiting...") arcpy.AddError("Can't find Input layer. Exiting...") except NoDepthRaster: print("Can't find Depth raster. Exiting...") arcpy.AddError("Can't find depth raster. Exiting...") except NotProjected: print( "Input data needs to be in a projected coordinate system. Exiting..." ) arcpy.AddError( "Input data needs to be in a projected coordinate system. Exiting..." ) except NoLayerFile: print("Can't find Layer file. Exiting...") arcpy.AddError("Can't find Layer file. Exiting...") except LicenseError3D: print("3D Analyst license is unavailable") arcpy.AddError("3D Analyst license is unavailable") except LicenseErrorSpatial: print("Spatial Analyst license is unavailable") arcpy.AddError("Spatial Analyst license is unavailable") except NoNoDataError: print("Input raster does not have NODATA values") arcpy.AddError("Input raster does not have NODATA values") except NoUnits: print("No units detected on input data") arcpy.AddError("No units detected on input data") except NoPolygons: print("Input data can only be polygon features or raster datasets.") arcpy.AddError( "Input data can only be polygon features or raster datasets.") except ValueError: print("Input no flood value is not a number.") arcpy.AddError("Input no flood value is not a number.") except arcpy.ExecuteError: line, filename, synerror = trace() msg("Error on %s" % line, ERROR) msg("Error in file name: %s" % filename, ERROR) msg("With error message: %s" % synerror, ERROR) msg("ArcPy Error Message: %s" % arcpy.GetMessages(2), ERROR) except FunctionError as f_e: messages = f_e.args[0] msg("Error in function: %s" % messages["function"], ERROR) msg("Error on %s" % messages["line"], ERROR) msg("Error in file name: %s" % messages["filename"], ERROR) msg("With error message: %s" % messages["synerror"], ERROR) msg("ArcPy Error Message: %s" % messages["arc"], ERROR) except: line, filename, synerror = trace() msg("Error on %s" % line, ERROR) msg("Error in file name: %s" % filename, ERROR) msg("with error message: %s" % synerror, ERROR) finally: arcpy.CheckInExtension("3D") arcpy.CheckInExtension("Spatial")
def make_identical_tif_extents(tif_list, temp_dir="tmp"): if os.path.exists(temp_dir): shutil.rmtree(temp_dir) os.mkdir(temp_dir) start_time = time.time() print("Setting extents to be identical on {} GeoTIFFS...".format(len(tif_list))) # Temporary variables output_tif_list = [] tif_list_fixed_extents = [] tif_properties = {} cell_extentX = [] cell_extentY = [] coords = set() coords_str = set() coords_str_l = [] coords_l = [] all_top = [] all_bottom = [] all_left = [] all_right = [] outlines_unchanged = [] arcpy.env.overwriteOutput = True main_coord_s = arcpy.Describe(tif_list[0]).spatialReference # Load tif properties that we care about into dictionary for a_tif in tif_list: coord, csX, csY = (arcpy.Describe(a_tif).spatialReference, arcpy.GetRasterProperties_management(a_tif, "CELLSIZEX"), arcpy.GetRasterProperties_management(a_tif, "CELLSIZEY")) cell_extentX.append(float(str(csX))) cell_extentY.append(float(str(csY))) coords.add(coord) coords_str.add(coord.name) coords_str_l.append(coord.name) coords_l.append(coord) tif_properties[a_tif] = [coord.name] print("Step 1/4: Setting projections to be identical....") # Reproject rasters to one projection j = 0 for a_tif in tif_properties.keys(): desc = arcpy.Describe(a_tif).baseName if not os.path.isfile(os.path.join(temp_dir, desc + "_reproject.tif")): arcpy.ProjectRaster_management(a_tif, os.path.join(temp_dir, desc + "_reproject.tif"), main_coord_s) tif_properties[os.path.join(temp_dir, desc + "_reproject.tif")] = main_coord_s del tif_properties[a_tif] print("Step 2/4: Setting cell sizes to be identical....") # Set cell sizes to be identical if necessary if len(set(cell_extentX)) > 1 or len(set(cell_extentY)) > 1: x_freq = max(set(cell_extentX), key=cell_extentX.count) y_freq = max(set(cell_extentY), key=cell_extentY.count) tif_list = tif_properties.keys() for a_tif in tif_list: desc = arcpy.Describe(a_tif).baseName if float(str(arcpy.GetRasterProperties_management(a_tif, "CELLSIZEX"))) != x_freq or float(str(arcpy.GetRasterProperties_management(a_tif, "CELLSIZEY"))) != y_freq: if not os.path.isfile(os.path.join(temp_dir, desc + "_resample.tif")): arcpy.Resample_management(a_tif,os.path.join(temp_dir, desc + "_resample.tif"), x_freq) tif_properties[os.path.join(temp_dir, desc + "_resample.tif")] = main_coord_s try: del tif_properties[a_tif] except: pass # Generate polygon extent shape for each tif print("Step 3/4: Generating minimum bounding polygon....") for a_tif in tif_properties.keys(): desc = arcpy.Describe(a_tif).baseName arcpy.RasterDomain_3d(a_tif, os.path.join(temp_dir, desc + "_Outline_notproj.shp"), "POLYGON") arcpy.Project_management(os.path.join(temp_dir, desc + "_Outline_notproj.shp"),os.path.join(temp_dir, desc + "_Outline.shp"), main_coord_s) outlines_unchanged.append(os.path.join(temp_dir, desc + "_Outline.shp")) for x in range(0, len(outlines_unchanged)): this_outline = outlines_unchanged[x] if x == 0: outline_to_clip_to = outlines_unchanged[x + 1] outline_to_save_to = os.path.join(temp_dir, "final_cutout_tmp.shp") arcpy.Clip_analysis(this_outline, outline_to_clip_to, outline_to_save_to) arcpy.CopyFeatures_management(outline_to_save_to, os.path.join(temp_dir, "final_cutout.shp")) else: outline_to_clip_to = os.path.join(temp_dir, "final_cutout.shp") outline_to_save_to = os.path.join(temp_dir, "final_cutout_tmp.shp") arcpy.Clip_analysis(this_outline, outline_to_clip_to, outline_to_save_to) arcpy.CopyFeatures_management(outline_to_save_to, outline_to_clip_to) desc = arcpy.Describe(os.path.join(temp_dir, "final_cutout.shp")) bounding = "{} {} {} {}".format(desc.extent.XMin, desc.extent.YMin, desc.extent.XMax, desc.extent.YMax) print("Step 4/4: Clipping rasters to minimum bounding polygon....") for a_tif in tif_properties.keys(): desc = arcpy.Describe(a_tif).baseName ## Reproject the shapefile to be the same projection as the input raster coord_system = arcpy.Describe(a_tif).spatialReference arcpy.Project_management(os.path.join(temp_dir, "final_cutout.shp"), os.path.join(temp_dir, "final_cutout_proj.shp"), main_coord_s) if not os.path.isfile(os.path.join(temp_dir, desc + "_clip.tif")): outExtract = arcpy.sa.ExtractByMask(a_tif, os.path.join(temp_dir, "final_cutout_proj.shp")) outExtract.save(os.path.join(temp_dir, desc + "_clip.tif")) tif_list_fixed_extents.append(os.path.join(temp_dir, desc + "_clip.tif")) end_time = time.time() t = gt.readable_time(start_time, end_time) output_tif_list = tif_list_fixed_extents print("{} GeoTIFFs processed to have identical extents in {} days, {}:{}:{}".format(len(tif_list), t['dd'], t["hh"], t["mm"], t['ss'])) return output_tif_list
import os import arcgisscripting, os gp = arcgisscripting.create() import arcpy import os import glob arcpy.CheckOutExtension("3D") # Directory containing .tif files filepath = "I:\\APS2008Resource\\blockfiles\\scripts\\extentTest\\test" tifList = glob.glob(filepath + "/*.tif") print tifList for tifFile in tifList: print tifFile ##domain = filepath + '\\' + tifFile[:-4] + '_domain.shp' domain = tifFile[:-4] + '_domain.shp' bufferDomain = tifFile[:-4] + '_buffer.shp' clip = tifFile[:-4] + '_clip.tif' print 'working' arcpy.RasterDomain_3d(tifFile, domain, "POLYGON") print domain arcpy.Buffer_analysis(domain, bufferDomain, "-300 Meters", "FULL", "ROUND", "NONE", "") print bufferDomain arcpy.Clip_management(tifFile, "#", clip, bufferDomain, "256", "ClippingGeometry") print clip arcpy.Delete_management(domain) arcpy.Delete_management(bufferDomain) print 'Finished working on' + tifFile
def extrapolate_raster(lc_ws, lc_dsm, lc_cell_size, lc_log_dir, lc_debug, lc_memory_switch): try: if lc_memory_switch: raster_polygons = "memory/raster_polygons" else: raster_polygons = os.path.join(lc_ws, "raster_polygons") if arcpy.Exists(raster_polygons): arcpy.Delete_management(raster_polygons) out_geom = "POLYGON" # output geometry type arcpy.RasterDomain_3d(lc_dsm, raster_polygons, out_geom) # 2. buffer it inwards so that we have a polygon only of the perimeter plus a few cells inward?. if lc_memory_switch: polygons_inward = "memory/inward_buffer" else: polygons_inward = os.path.join(lc_ws, "inward_buffer") if arcpy.Exists(polygons_inward): arcpy.Delete_management(polygons_inward) if lc_memory_switch: polygons_outward = "memory/outward_buffer" else: polygons_outward = os.path.join(lc_ws, "outward_buffer") if arcpy.Exists(polygons_outward): arcpy.Delete_management(polygons_outward) x = float(lc_cell_size) if x < 0.1: arcpy.AddError( "Raster cell size is 0. Can't continue. Please check the raster properties." ) raise ValueError return None else: buffer_in = 6 * x xy_unit = common_lib.get_xy_unit(lc_dsm, 0) if xy_unit == "Feet": buffer_text = "-" + str(buffer_in) + " Feet" else: buffer_text = "-" + str(buffer_in) + " Meters" sideType = "OUTSIDE_ONLY" arcpy.Buffer_analysis(raster_polygons, polygons_inward, buffer_text, sideType) # create outside buffer for extent only. if xy_unit == "Feet": buffer_text = str(buffer_in) + " Feet" else: buffer_text = str(buffer_in) + " Meters" arcpy.Buffer_analysis(raster_polygons, polygons_outward, buffer_text, sideType) msg_body = create_msg_body("Buffering flood edges...", 0, 0) msg(msg_body) # 3. mask in ExtractByMask: gives just boundary raster with a few cells inwards if lc_memory_switch: extract_mask_raster = "in_memory/extract_mask" else: extract_mask_raster = os.path.join(lc_ws, "extract_mask") if arcpy.Exists(extract_mask_raster): arcpy.Delete_management(extract_mask_raster) extract_temp_raster = arcpy.sa.ExtractByMask( lc_dsm, polygons_inward) extract_temp_raster.save(extract_mask_raster) # 4. convert the output to points if lc_memory_switch: extract_mask_points = "in_memory/extract_points" else: extract_mask_points = os.path.join(lc_ws, "extract_points") if arcpy.Exists(extract_mask_points): arcpy.Delete_management(extract_mask_points) arcpy.RasterToPoint_conversion(extract_mask_raster, extract_mask_points, "VALUE") msg_body = create_msg_body("Create flood points...", 0, 0) msg(msg_body) # 5. Interpolate: this will also interpolate outside the bridge extent if lc_memory_switch: interpolated_raster = "in_memory/interpolate_raster" else: interpolated_raster = os.path.join(lc_ws, "interpolate_raster") if arcpy.Exists(interpolated_raster): arcpy.Delete_management(interpolated_raster) zField = "grid_code" power = 2 msg_body = create_msg_body("Interpolating bridge raster points...", 0, 0) msg(msg_body) arcpy.env.extent = polygons_outward # Execute IDW out_IDW = arcpy.sa.Idw(extract_mask_points, zField, lc_cell_size, power) # Save the output out_IDW.save(interpolated_raster) arcpy.ResetEnvironments() arcpy.env.workspace = lc_ws arcpy.env.overwriteOutput = True # extract the outer rim only extract_mask_raster2 = os.path.join(lc_ws, "extract_mask2") if arcpy.Exists(extract_mask_raster2): arcpy.Delete_management(extract_mask_raster2) extract_temp_raster = arcpy.sa.ExtractByMask( interpolated_raster, polygons_outward) extract_temp_raster.save(extract_mask_raster2) return extract_mask_raster2 except arcpy.ExecuteError: # Get the tool error messages msgs = arcpy.GetMessages(2) arcpy.AddError(msgs) except Exception: e = sys.exc_info()[1] arcpy.AddMessage("Unhandled exception: " + str(e.args[0]))
# for 92 reclass nlcd92 = arcpy.GetParameterAsText(7) # end variables # set environmental variables arcpy.CheckOutExtension("Spatial") arcpy.env.workspace = out_gdb arcpy.env.overwriteOutput=True # extent shp default if not extent_shp: arcpy.CheckOutExtension("3d") if mask: extent_shp = arcpy.RasterDomain_3d(mask, "nlcdprocextent", "POLYGON") else: extent_shp = arcpy.RasterDomain_3d(nlcd_classified, "nlcdprocextent", "POLYGON") else: # buffer extent feature extent_shp = arcpy.Buffer_analysis(in_features=extent_shp, out_feature_class="nlcdprocextent", buffer_distance_or_field="5000 Meters", dissolve_option="ALL") # mask default if mask: arcpy.AddMessage("Using specified mask") else: arcpy.Clip_management(nlcd_classified,"#","maskfinal", extent_shp, "#", "ClippingGeometry") mask=SetNull("maskfinal","maskfinal","Value = 0") mask.save("maskfinal") arcpy.env.snapRaster = mask
print "Data Processing Complete!" print " " print " " print "Performing Maximum Likelihood Classification..." print " " print " " arcpy.gp.MLClassify_sa(NDWI, NDWI_SignatureFile, MLClass_NDWI, "0.0", "EQUAL", "", Output_confidence_raster) print "Data Analysis Pt. 1 - Maximum Likelihood Classification Complete!" print " " print " " print "Getting Raster Domain..." arcpy.RasterDomain_3d(mosaic_b3, FloodMap_ExtentPolygon_shp, "POLYGON") print "Clipping Water Mask..." arcpy.gp.ExtractByMask_sa(WaterMask_USA_tif, FloodMap_ExtentPolygon_shp, WaterMaskClip_tif) print "Reclassifying NDWI Classification..." arcpy.gp.Reclassify_sa(MLClass_NDWI, "Value", "1 0;1 151 1", Reclass_MLClass_NDWI_tif, "DATA") print "Reclassifying Water Mask..." arcpy.gp.Reclassify_sa(path + "\\" + "WaterMaskClip.tif", "Value", "0 NODATA;1 2;2 1", Reclass_watermask_tif, "DATA") print "Subtracting Water Mask from NDWI Classification..." arcpy.gp.RasterCalculator_sa(
inRaster = "F:\\O3T2019_8_16_13.tif" inSQLClause = "" inMaskData = "D:\\cs\\res\\inshp\\cs.shp" # Check out the ArcGIS Spatial Analyst extension license arcpy.CheckOutExtension("Spatial") # Obtain a license for the ArcGIS 3D Analyst extension arcpy.CheckOutExtension("3D") # Execute ExtractByMask outExtractByMask = ExtractByMask(inRaster, inMaskData) #Get the geoprocessing result object elevMAXIMUMResult = arcpy.GetRasterProperties_management( outExtractByMask, "MAXIMUM") #Get the elevation standard deviation value from geoprocessing result object elevMAXIMUM = float(elevMAXIMUMResult.getOutput(0)) elevMINIMUMResult = arcpy.GetRasterProperties_management( outExtractByMask, "MINIMUM") #Get the elevation standard deviation value from geoprocessing result object elevMINIMUM = float(elevMINIMUMResult.getOutput(0)) inSQLClause = "VALUE > " + str(elevMAXIMUM - (elevMAXIMUM - elevMINIMUM) / 10) # Execute ExtractByAttributes attExtract = ExtractByAttributes(outExtractByMask, inSQLClause) # Save the output #attExtract.save("F:\\ree\\PM25T08.tif") outLine = "F:\\ree\\O3.shp" field = "VALUE" outGeom = "LINE" arcpy.RasterDomain_3d(attExtract, outLine, outGeom)
for name in files: if name[-6:] == "_c.jpg": getID = os.path.normpath(os.path.join(root,name)) try: year = getID.split('\\')[-2][-4:] except: continue try: ID = getID.split('\\')[-3] except: continue print getID.encode("utf-8") try: f.write(os.path.join(root,name).encode("utf-8")+'\t'+year.encode("utf-8")+'\t'+ID.encode("utf-8")+'\n') #ArcGIS App here print "Calculating raster domain for {0}.shp".format(ID.encode("utf-8")) #arcpy.RasterDomain_3d(in_raster=os.path.join(root,name), out_feature_class=ID.encode("utf-8")+".shp", out_geometry_type="LINE") arcpy.RasterDomain_3d(in_raster=os.path.join(root,name), out_feature_class=os.path.join(outpath,(ID+".shp")), out_geometry_type="POLYGON") except Exception as e: print e.message print ('Could not execute:\t'+os.path.join(root,name).encode("utf-8")+'\t'+year.encode("utf-8")+'\t'+ID.encode("utf-8")+'\n') logs.write('Could not execute:\t'+os.path.join(root,name).encode("utf-8")+'\t'+year.encode("utf-8")+'\t'+ID.encode("utf-8")+'\n') continue print "Dies ist ein Bild:{0}".format(os.path.join(root,name).encode("utf-8")) #print(os.path.join(root, name)) #for name in dirs: #print(os.path.join(root, name)) f.close() logs.close()
def footprints_DTM(path, pathdab, absolute_DTM_paths): ''' path = "C:/Users/nilscp/Desktop/testarcpy/DTM/" pathdab = "C:/Users/nilscp/Desktop/testarcpy/DTM/database.gdb/" footprints_DTM(path, pathdab) pathastra = "/net/astra/astra-01/nilscp/Moon/downloading/NAC_DTM_ALL/" absolute_DTM_path = ['Y:/nilscp/Moon/NAC_DTM/NAC_DTM_M102522406_M102529564_DEM.TIF', 'Y:/nilscp/Moon/NAC_DTM/NAC_DTM_M104877210_M104884367_DEM.TIF'] ''' # change to directory of interest os.chdir(path) # define paths and workspace (I need to create the gdb at some points) env.workspace = env.scratchWorkspace = pathdab # find all the DTMS #DTMs = glob.glob('*.tif') #absolute_DTM_path_now # Spatial reference (plate carree, global spatial reference) spatialReference_new = "PROJCS['Equirectangular_Moon',GEOGCS['GCS_Moon',DATUM['D_Moon',SPHEROID['Moon_localRadius',1737400.0,0.0]],PRIMEM['Reference_Meridian',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Plate_Carree'],PARAMETER['false_easting',0.0],PARAMETER['false_northing',0.0],PARAMETER['central_meridian',0.0],UNIT['Meter',1.0]]" # loop throgh DTMs for ix, pathDTM in enumerate(absolute_DTM_paths): # name of the DTM DTM = pathDTM.split("/")[-1] print(DTM) #path to the DTM path_dtm_folder = pathDTM.split(DTM)[0] # change directory to absolute path os.chdir(path_dtm_folder) # get the footprint arcpy.RasterDomain_3d(in_raster=path + DTM, out_feature_class="tmp_polygon_footprints", out_geometry_type="POLYGON") # projection of the polygon arcpy.Project_management("tmp_polygon_footprints", "tmp_polygon_footprints_proj", spatialReference_new) if arcpy.Exists("polygon_footprints_new"): # copy tmp all arcpy.CopyFeatures_management("polygon_footprints_new", "polygon_footprints_new_tmp") # add to main polygon and overwrite the main polygon arcpy.Merge_management( ["polygon_footprints_new_tmp", "tmp_polygon_footprints_proj"], "polygon_footprints_new") else: arcpy.CopyFeatures_management("tmp_polygon_footprints_proj", "polygon_footprints_new") # delete old raster and tmp polygon footprints (only one layer will be kept at a time) arcpy.Delete_management("tmp_polygon_footprints") arcpy.Delete_management("tmp_polygon_footprints_proj") arcpy.Delete_management("polygon_footprints_new_tmp")
# Process: Hillshade - works hshade = arcpy.HillShade_3d(Fill_DEM, Hillshade, "315", "45", "NO_SHADOWS", "1") print ("Hillshade complete.") # Process: Slope Raster slope = arcpy.Slope_3d(Fill_DEM, Slope_Percent, "PERCENT_RISE", 0.3048, "PLANAR", "") print ("Slope Raster complete.") # Process: Aspect Raster aspect = arcpy.sa.Aspect(Fill_DEM, "PLANAR", "") aspect.save (DEM_Aspect) print ("Aspect Raster complete.") # Process: Raster Domain - works arcpy.RasterDomain_3d(Fill_DEM, DEM_Domain, "POLYGON") print ("Raster Domain complete.") # Process: Clip - works tempEnvironment0 = arcpy.env.extent arcpy.Clip_analysis(Trail, DEM_Domain, Trail_Clip) arcpy.env.extent = tempEnvironment0 print ("Trail Clip complete.") # Process: Feature to Raster - works arcpy.FeatureToRaster_conversion(Trail_Clip, "OBJECTID", Trail_Raster, "1") print ("Feature to Raster complete.") # Process: Flow Accumulation - works flowacc = arcpy.sa.FlowAccumulation(DEM_FlowDir, "", "FLOAT", "D8") flowacc.save(FlowAcc_DEM)
os.chdir(dir_name) # List of image files in directory files = [] # Find image files in directory for file in glob.glob('*.tif')+glob.glob('*.jpg'): files.append(file) # List of files that will be added to mosaic mosaicFiles = [] # Check if each raster is in AOI, if so append to list of files to mosaic for file in files: #Create shapefile of raster arcpy.RasterDomain_3d(file, tempShp_name, 'POLYGON') #Check if shape file overlaps AOI arcpy.Intersect_analysis([tempShp_name, AOI], "in_memory/output") result = arcpy.GetCount_management('in_memory/output') overlap = int(result.getOutput(0)) if overlap == 1: mosaicFiles.append(file) # Clean up arcpy.Delete_management('in_memory/output') arcpy.Delete_management(tempShp_name) # Mosaic rasters in list arcpy.MosaicToNewRaster_management(mosaicFiles,dir_name, mosaic_name, "", "8_BIT_UNSIGNED",
def flood_from_raster(input_source, input_type, no_flood_value, baseline_elevation_raster, baseline_elevation_value, outward_buffer, output_polygons, smoothing, debug): try: # Get Attributes from User if debug == 0: # script variables aprx = arcpy.mp.ArcGISProject("CURRENT") home_directory = aprx.homeFolder tiff_directory = home_directory + "\\Tiffs" tin_directory = home_directory + "\\Tins" scripts_directory = aprx.homeFolder + "\\Scripts" rule_directory = aprx.homeFolder + "\\rule_packages" log_directory = aprx.homeFolder + "\\Logs" layer_directory = home_directory + "\\layer_files" project_ws = aprx.defaultGeodatabase enableLogging = True DeleteIntermediateData = True verbose = 0 use_in_memory = True else: # debug home_directory = r'D:\Gert\Work\Esri\Solutions\3DFloodImpact\work2.3\3DFloodImpact' tiff_directory = home_directory + "\\Tiffs" tin_directory = home_directory + "\\Tins" scripts_directory = home_directory + "\\Scripts" rule_directory = home_directory + "\\rule_packages" log_directory = home_directory + "\\Logs" layer_directory = home_directory + "\\layer_files" project_ws = home_directory + "\\Results.gdb" enableLogging = False DeleteIntermediateData = True verbose = 1 use_in_memory = False scratch_ws = common_lib.create_gdb(home_directory, "Intermediate.gdb") arcpy.env.workspace = scratch_ws arcpy.env.overwriteOutput = True # fail safe for Europese's comma's baseline_elevation_value = float( re.sub("[,.]", ".", baseline_elevation_value)) if not os.path.exists(tiff_directory): os.makedirs(tiff_directory) if not os.path.exists(tin_directory): os.makedirs(tin_directory) common_lib.set_up_logging(log_directory, TOOLNAME) start_time = time.clock() if arcpy.CheckExtension("3D") == "Available": arcpy.CheckOutExtension("3D") if arcpy.CheckExtension("Spatial") == "Available": arcpy.CheckOutExtension("Spatial") flood_level_layer_mp = None desc = arcpy.Describe(input_source) arcpy.AddMessage( "Processing input source: " + common_lib.get_name_from_feature_class(input_source)) spatial_ref = desc.spatialReference # create IsNull to be used to clip and check for NoData. if use_in_memory: is_null0 = "in_memory/is_null0" else: is_null0 = os.path.join(scratch_ws, "is_null0") if arcpy.Exists(is_null0): arcpy.Delete_management(is_null0) is_null_raster = arcpy.sa.IsNull(input_source) is_null_raster.save(is_null0) if spatial_ref.type == 'PROJECTED' or spatial_ref.type == 'Projected': # check input source type: projected rasters ONLY!!!! # check type, if polygon -> convert to raster if input_type == "RasterLayer" or input_type == "RasterDataset" or input_type == "raster": # prep raster # smooth result using focal stats if smoothing > 0: if use_in_memory: focal_raster = "in_memory/focal_raster" else: focal_raster = os.path.join( scratch_ws, "focal_raster") if arcpy.Exists(focal_raster): arcpy.Delete_management(focal_raster) if not (1 <= smoothing <= 100): smoothing = 30 neighborhood = arcpy.sa.NbrRectangle( smoothing, smoothing, "CELL") flood_elev_raster = arcpy.sa.FocalStatistics( input_source, neighborhood, "MEAN", "true") flood_elev_raster.save(focal_raster) # con if use_in_memory: smooth_input = "in_memory/smooth_input" else: smooth_input = os.path.join( scratch_ws, "smooth_input") if arcpy.Exists(smooth_input): arcpy.Delete_management(smooth_input) output = arcpy.sa.Con(is_null0, input_source, flood_elev_raster) output.save(smooth_input) input_raster = smooth_input else: input_raster = input_source else: raise NotSupported # use numeric value for determining non flooded areas: set these values to NoData. We need NoData for clippng later on if no_flood_value != "NoData": if common_lib.is_number(no_flood_value): msg_body = create_msg_body( "Setting no flood value: " + no_flood_value + " to NoData in copy of " + common_lib.get_name_from_feature_class( input_raster) + "...", 0, 0) msg(msg_body) if use_in_memory: null_for_no_flooded_areas_raster = "in_memory/null_for_flooded" else: null_for_no_flooded_areas_raster = os.path.join( scratch_ws, "null_for_flooded") if arcpy.Exists( null_for_no_flooded_areas_raster): arcpy.Delete_management( null_for_no_flooded_areas_raster) whereClause = "VALUE = " + no_flood_value # Execute SetNull outSetNull_temp = arcpy.sa.SetNull( input_raster, input_raster, whereClause) outSetNull_temp.save( null_for_no_flooded_areas_raster) input_raster = null_for_no_flooded_areas_raster else: raise ValueError else: pass msg_body = create_msg_body( "Checking for NoData values in raster: " + common_lib.get_name_from_feature_class(input_raster) + ". NoData values are considered to be non-flooded areas!", 0, 0) msg(msg_body) max_value = arcpy.GetRasterProperties_management( is_null0, "MAXIMUM")[0] # has_nodata = arcpy.GetRasterProperties_management(input_raster, "ANYNODATA")[0] ## fails on some rasters if int(max_value) == 1: # 1. get the outline of the raster as polygon via RasterDomain xy_unit = common_lib.get_xy_unit(input_raster, 0) if xy_unit: cell_size = arcpy.GetRasterProperties_management( input_raster, "CELLSIZEX") if baseline_elevation_raster: # check celll size cell_size_base = arcpy.GetRasterProperties_management( baseline_elevation_raster, "CELLSIZEX") if cell_size_base.getOutput( 0) == cell_size.getOutput(0): # Execute Plus if use_in_memory: flood_plus_base_raster = "in_memory/flooding_plus_base" else: flood_plus_base_raster = os.path.join( scratch_ws, "flooding_plus_base") if arcpy.Exists( flood_plus_base_raster): arcpy.Delete_management( flood_plus_base_raster) listRasters = [] listRasters.append(input_raster) listRasters.append( baseline_elevation_raster) desc = arcpy.Describe(listRasters[0]) arcpy.MosaicToNewRaster_management( listRasters, scratch_ws, "flooding_plus_base", desc.spatialReference, "32_BIT_FLOAT", cell_size, 1, "SUM", "") # check where there is IsNull and set the con values if use_in_memory: is_Null = "in_memory/is_Null" else: is_Null = os.path.join( scratch_ws, "is_Null") if arcpy.Exists(is_Null): arcpy.Delete_management(is_Null) is_Null_raster = arcpy.sa.IsNull( input_raster) is_Null_raster.save(is_Null) # Con if use_in_memory: flood_plus_base_raster_null = "in_memory/flooding_plus_base_null" else: flood_plus_base_raster_null = os.path.join( scratch_ws, "flooding_plus_base_null") if arcpy.Exists( flood_plus_base_raster_null): arcpy.Delete_management( flood_plus_base_raster_null) msg_body = create_msg_body( "Adding baseline elevation raster to input flood layer...", 0, 0) msg(msg_body) fpbrn = arcpy.sa.Con( is_Null, input_raster, flood_plus_base_raster) fpbrn.save(flood_plus_base_raster_null) input_raster = flood_plus_base_raster_null else: arcpy.AddWarning( "Cell size of " + input_raster + " is different than " + baseline_elevation_raster + ". Ignoring Base Elevation Raster.") else: if baseline_elevation_value > 0: if use_in_memory: flood_plus_base_raster = "in_memory/flood_plus_base" else: flood_plus_base_raster = os.path.join( scratch_ws, "flooding_plus_base") if arcpy.Exists( flood_plus_base_raster): arcpy.Delete_management( flood_plus_base_raster) arcpy.Plus_3d(input_raster, baseline_elevation_value, flood_plus_base_raster) input_raster = flood_plus_base_raster msg_body = create_msg_body( "Creating 3D polygons...", 0, 0) msg(msg_body) if use_in_memory: raster_polygons = "in_memory/raster_polygons" else: raster_polygons = os.path.join( scratch_ws, "raster_polygons") if arcpy.Exists(raster_polygons): arcpy.Delete_management(raster_polygons) out_geom = "POLYGON" # output geometry type arcpy.RasterDomain_3d(input_raster, raster_polygons, out_geom) # 2. buffer it inwards so that we have a polygon only of the perimeter plus a few ???????cells inward???????. if use_in_memory: polygons_inward = "in_memory/inward_buffer" else: polygons_inward = os.path.join( scratch_ws, "inward_buffer") if arcpy.Exists(polygons_inward): arcpy.Delete_management(polygons_inward) x = float( re.sub("[,.]", ".", str(cell_size.getOutput(0)))) # x = float(str(cell_size.getOutput(0))) if x < 0.1: arcpy.AddError( "Raster cell size is 0. Can't continue. Please check the raster properties." ) raise ValueError else: buffer_in = 3 * int(x) if xy_unit == "Feet": buffer_text = "-" + str( buffer_in) + " Feet" else: buffer_text = "-" + str( buffer_in) + " Meters" sideType = "OUTSIDE_ONLY" arcpy.Buffer_analysis(raster_polygons, polygons_inward, buffer_text, sideType) msg_body = create_msg_body( "Buffering flood edges...", 0, 0) msg(msg_body) # 3. mask in ExtractByMask: gives just boundary raster with a few cells inwards if use_in_memory: extract_mask_raster = "in_memory/extract_mask" else: extract_mask_raster = os.path.join( scratch_ws, "extract_mask") if arcpy.Exists(extract_mask_raster): arcpy.Delete_management( extract_mask_raster) extract_temp_raster = arcpy.sa.ExtractByMask( input_raster, polygons_inward) extract_temp_raster.save(extract_mask_raster) # 4. convert the output to points if use_in_memory: extract_mask_points = "in_memory/extract_points" else: extract_mask_points = os.path.join( scratch_ws, "extract_points") if arcpy.Exists(extract_mask_points): arcpy.Delete_management( extract_mask_points) arcpy.RasterToPoint_conversion( extract_mask_raster, extract_mask_points, "VALUE") msg_body = create_msg_body( "Create flood points...", 0, 0) msg(msg_body) # 5. Interpolate: this will also interpolate outside the flood boundary which is # what we need so we get a nice 3D poly that extends into the surrounding DEM if use_in_memory: interpolated_raster = "in_memory/interpolate_raster" else: interpolated_raster = os.path.join( scratch_ws, "interpolate_raster") if arcpy.Exists(interpolated_raster): arcpy.Delete_management( interpolated_raster) zField = "grid_code" power = 2 searchRadius = arcpy.sa.RadiusVariable( 12, 150000) msg_body = create_msg_body( "Interpolating flood points...", 0, 0) msg(msg_body) # Execute IDW out_IDW = arcpy.sa.Idw(extract_mask_points, zField, cell_size, power) # Save the output out_IDW.save(interpolated_raster) extent_poly = common_lib.get_extent_feature( scratch_ws, polygons_inward) msg_body = create_msg_body( "Clipping terrain...", 0, 0) msg(msg_body) # clip the input surface if use_in_memory: extent_clip_idwraster = "in_memory/extent_clip_idw" else: extent_clip_idwraster = os.path.join( scratch_ws, "extent_clip_idw") if arcpy.Exists(extent_clip_idwraster): arcpy.Delete_management( extent_clip_idwraster) # clip terrain to extent arcpy.Clip_management(interpolated_raster, "#", extent_clip_idwraster, extent_poly) # 6. clip the interpolated raster by (outward buffered) outline polygon if use_in_memory: polygons_outward = "in_memory/outward_buffer" else: polygons_outward = os.path.join( scratch_ws, "outward_buffer") if arcpy.Exists(polygons_outward): arcpy.Delete_management( polygons_outward) outward_buffer += 0.5 * int( x ) # we buffer out by half the raster cellsize if outward_buffer > 0: if xy_unit == "Feet": buffer_text = str( outward_buffer) + " Feet" else: buffer_text = str( outward_buffer) + " Meters" sideType = "FULL" arcpy.Buffer_analysis( raster_polygons, polygons_outward, buffer_text, sideType) raster_polygons = polygons_outward # clip the input surface if use_in_memory: flood_clip_raster = "in_memory/flood_clip_raster" else: flood_clip_raster = os.path.join( scratch_ws, "flood_clip_raster") if arcpy.Exists(flood_clip_raster): arcpy.Delete_management( flood_clip_raster) msg_body = create_msg_body( "Clipping flood raster...", 0, 0) msg(msg_body) # clip terrain to extent # arcpy.Clip_management(interpolated_raster, "#", flood_clip_raster, raster_polygons) Check again arcpy.Clip_management(interpolated_raster, "#", flood_clip_raster, raster_polygons) # 7. Isnull, and Con to grab values from flood_clip_raster for # create NUll mask if use_in_memory: is_Null = "in_memory/is_Null" else: is_Null = os.path.join( scratch_ws, "is_Null") if arcpy.Exists(is_Null): arcpy.Delete_management(is_Null) is_Null_raster = arcpy.sa.IsNull(input_raster) is_Null_raster.save(is_Null) # Con if use_in_memory: con_raster = "in_memory/con_raster" else: con_raster = os.path.join( scratch_ws, "con_raster") if arcpy.Exists(con_raster): arcpy.Delete_management(con_raster) temp_con_raster = arcpy.sa.Con( is_Null, interpolated_raster, input_raster) temp_con_raster.save(con_raster) msg_body = create_msg_body( "Merging rasters...", 0, 0) msg(msg_body) # 8. focal stats on raster to smooth? # 9. copy raster to geotiff if use_in_memory: con_raster_tif = "in_memory/con_raster_tif" else: con_raster_tif = os.path.join( tiff_directory, "con_raster.tif") if arcpy.Exists(con_raster_tif): arcpy.Delete_management(con_raster_tif) arcpy.CopyRaster_management( con_raster, con_raster_tif, "#", "#", "#", "#", "#", "32_BIT_FLOAT") msg_body = create_msg_body( "Copying to tiff...", 0, 0) msg(msg_body) # 10. raster to TIN zTol = 0.1 maxPts = 1500000 zFactor = 1 con_tin = os.path.join(tin_directory, "con_tin") if arcpy.Exists(con_tin): arcpy.Delete_management(con_tin) # Execute RasterTin arcpy.RasterTin_3d(con_raster_tif, con_tin, zTol, maxPts, zFactor) msg_body = create_msg_body( "Creating TIN...", 0, 0) msg(msg_body) # 11. TIN triangles if use_in_memory: con_triangles = "in_memory/con_triangles" else: con_triangles = os.path.join( scratch_ws, "con_triangles") if arcpy.Exists(con_triangles): arcpy.Delete_management(con_triangles) arcpy.TinTriangle_3d(con_tin, con_triangles) msg_body = create_msg_body( "Creating polygons...", 0, 0) msg(msg_body) # 12. make 2D polygons feature to feature class arcpy.FeatureClassToFeatureClass_conversion( con_triangles, scratch_ws, "con_triangles_2D") # 12. clip with smooth polygon smooth_polygons = os.path.join( scratch_ws, "smooth_raster_polygons") if arcpy.Exists(smooth_polygons): arcpy.Delete_management(smooth_polygons) msg_body = create_msg_body( "Smoothing edges...", 0, 0) msg(msg_body) CA.SmoothPolygon(os.path.join(raster_polygons), smooth_polygons, "PAEK", x, "", "FLAG_ERRORS") if use_in_memory: clip_smooth_triangles = "in_memory/clip_smooth_triangles" else: clip_smooth_triangles = os.path.join( scratch_ws, "clip_smooth_triangles") if arcpy.Exists(clip_smooth_triangles): arcpy.Delete_management( clip_smooth_triangles) msg_body = create_msg_body( "Clipping smooth edges...", 0, 0) msg(msg_body) # clip terrain to extent arcpy.Clip_analysis(con_triangles, smooth_polygons, clip_smooth_triangles) # clip to slightly lesser extent because of InterpolateShape fail. area_extent = common_lib.get_extent_feature( scratch_ws, clip_smooth_triangles) if use_in_memory: extent_inward = "in_memory/inward_extent_buffer" else: extent_inward = os.path.join( scratch_ws, "inward_extent_buffer") if arcpy.Exists(extent_inward): arcpy.Delete_management(extent_inward) buffer_in = 3 if xy_unit == "Feet": buffer_text = "-" + str( buffer_in) + " Feet" else: buffer_text = "-" + str( buffer_in) + " Meters" sideType = "FULL" arcpy.Buffer_analysis(area_extent, extent_inward, buffer_text, sideType) if use_in_memory: clip2_smooth_triangles = "in_memory/clip2_smooth_triangles" else: clip2_smooth_triangles = os.path.join( scratch_ws, "clip2_smooth_triangles") if arcpy.Exists(clip2_smooth_triangles): arcpy.Delete_management( clip2_smooth_triangles) msg_body = create_msg_body( "Clipping smooth edges a second time...", 0, 0) msg(msg_body) # clip terrain to extent arcpy.Clip_analysis(clip_smooth_triangles, extent_inward, clip2_smooth_triangles) # 13. interpolate on TIN if use_in_memory: clip_smooth_triangles3D = "in_memory/clip_smooth_traingles3D" else: clip_smooth_triangles3D = os.path.join( scratch_ws, "clip_smooth_triangles3D") if arcpy.Exists(clip_smooth_triangles3D): arcpy.Delete_management( clip_smooth_triangles3D) msg_body = create_msg_body( "Interpolating polygons on TIN", 0, 0) msg(msg_body) arcpy.InterpolateShape_3d( con_tin, clip2_smooth_triangles, clip_smooth_triangles3D, "#", 1, "LINEAR", "VERTICES_ONLY") # 13. to multipatch z_unit = common_lib.get_z_unit( clip_smooth_triangles3D, verbose) # temp layer flood_level_layer = "flood_level_layer" arcpy.MakeFeatureLayer_management( clip_smooth_triangles3D, flood_level_layer) # flood_level_mp = os.path.join(project_ws, common_lib.get_name_from_feature_class(input_raster) + "_3D") flood_level_mp = output_polygons + "_3D" if arcpy.Exists(flood_level_mp): arcpy.Delete_management(flood_level_mp) arcpy.Layer3DToFeatureClass_3d( flood_level_layer, flood_level_mp) # layer to be added to TOC flood_level_layer_mp = common_lib.get_name_from_feature_class( flood_level_mp) arcpy.MakeFeatureLayer_management( flood_level_mp, flood_level_layer_mp) # apply transparency here // checking if symbology layer is present if z_unit == "Feet": floodSymbologyLayer = layer_directory + "\\flood3Dfeet.lyrx" else: floodSymbologyLayer = layer_directory + "\\flood3Dmeter.lyrx" if not arcpy.Exists(floodSymbologyLayer): arcpy.AddWarning( "Can't find: " + floodSymbologyLayer + ". Symbolize features by error attribute to see data errors." ) arcpy.AddMessage("Results written to: " + output_polygons) if use_in_memory: arcpy.Delete_management("in_memory") if DeleteIntermediateData: fcs = common_lib.listFcsInGDB(scratch_ws) msg_prefix = "Deleting intermediate data..." msg_body = common_lib.create_msg_body( msg_prefix, 0, 0) common_lib.msg(msg_body) for fc in fcs: arcpy.Delete_management(fc) return flood_level_layer_mp # 14. adjust 3D Z feet to meters??? else: raise NoUnits else: raise NoNoDataError end_time = time.clock() msg_body = create_msg_body( "Create 3D Flood Leveles completed successfully.", start_time, end_time) else: raise NotProjected else: raise LicenseErrorSpatial else: raise LicenseError3D arcpy.ClearWorkspaceCache_management() msg(msg_body) except NotProjected: print( "Input data needs to be in a projected coordinate system. Exiting..." ) arcpy.AddError( "Input data needs to be in a projected coordinate system. Exiting..." ) except NoLayerFile: print("Can't find Layer file. Exiting...") arcpy.AddError("Can't find Layer file. Exiting...") except LicenseError3D: print("3D Analyst license is unavailable") arcpy.AddError("3D Analyst license is unavailable") except LicenseErrorSpatial: print("Spatial Analyst license is unavailable") arcpy.AddError("Spatial Analyst license is unavailable") except NoNoDataError: print("Input raster does not have NODATA values") arcpy.AddError("Input raster does not have NODATA values") except NoUnits: print("No units detected on input data") arcpy.AddError("No units detected on input data") except NoPolygons: print("Input data can only be polygon features or raster datasets.") arcpy.AddError( "Input data can only be polygon features or raster datasets.") except ValueError: print("Input no flood value is not a number.") arcpy.AddError("Input no flood value is not a number.") except arcpy.ExecuteError: line, filename, synerror = trace() msg("Error on %s" % line, ERROR) msg("Error in file name: %s" % filename, ERROR) msg("With error message: %s" % synerror, ERROR) msg("ArcPy Error Message: %s" % arcpy.GetMessages(2), ERROR) except FunctionError as f_e: messages = f_e.args[0] msg("Error in function: %s" % messages["function"], ERROR) msg("Error on %s" % messages["line"], ERROR) msg("Error in file name: %s" % messages["filename"], ERROR) msg("With error message: %s" % messages["synerror"], ERROR) msg("ArcPy Error Message: %s" % messages["arc"], ERROR) except: line, filename, synerror = trace() msg("Error on %s" % line, ERROR) msg("Error in file name: %s" % filename, ERROR) msg("with error message: %s" % synerror, ERROR) finally: arcpy.CheckInExtension("3D") arcpy.CheckInExtension("Spatial")