def CombineShorelinePolygons(bndMTL: str, bndMHW: str, inletLines: str, ShorelinePts: str, bndpoly: str, SA_bounds: str='', verbose: bool=True): """ Use MTL and MHW contour polygons to create shoreline polygon. 'Shoreline' = MHW on oceanside and MTL on bayside """ start = time.clock() # Inlet lines must intersect the MHW polygon symdiff = os.path.join(arcpy.env.scratchGDB, 'shore_1symdiff') split = os.path.join(arcpy.env.scratchGDB, 'shore_2split') join = os.path.join(arcpy.env.scratchGDB, 'shore_3_oceanMTL') erase = os.path.join(arcpy.env.scratchGDB, 'shore_4_bayMTL') union_2 = os.path.join(arcpy.env.scratchGDB, 'shore_5union') # Create layer (symdiff) of land between MTL and MHW and split by inlets print("...delineating land between MTL and MHW elevations...") arcpy.Delete_management(symdiff) # delete if already exists arcpy.SymDiff_analysis(bndMTL, bndMHW, symdiff) # Split symdiff at inlets (and SA_bounds) print("...removing the MHW-MTL areas on the oceanside...") if len(SA_bounds) > 0: arcpy.FeatureToPolygon_management([symdiff, inletLines, SA_bounds], split) # Split MTL features at inlets and study area bounds else: arcpy.FeatureToPolygon_management([symdiff, inletLines], split) # Split MTL features at inlets # Isolate polygons touching shoreline points and erase from symdiff arcpy.SpatialJoin_analysis(split, ShorelinePts, split+'_join', "#","KEEP_COMMON", match_option="COMPLETELY_CONTAINS") arcpy.Erase_analysis(symdiff, split+'_join', erase) # Merge bayside MHW-MTL with above-MHW polygon arcpy.Union_analysis([erase, bndMHW], union_2) arcpy.Dissolve_management(union_2, bndpoly, multi_part='SINGLE_PART') # Dissolve all features in union_2 to single part polygons print('''User input required! Select extra features in {} for deletion.\nRecommended technique: select the polygon/s to keep and then Switch Selection.\n'''.format(os.path.basename(bndpoly))) return(bndpoly)
def remove_points_near_intersection(self): itm_fc_name = 'all_gps_pnts' arcpy.Intersect_analysis([self.network_links_gis], 'links_intersections', output_type='POINT') arcpy.Buffer_analysis('links_intersections', 'buffer_intersection', 30) arcpy.management.XYTableToPoint(os.path.join(self.workspace_csv_progress, 'stamp_time.csv'), itm_fc_name, "POINT_X", "POINT_Y", coordinate_system=arcpy.SpatialReference(2039)) arcpy.Intersect_analysis(['buffer_intersection', itm_fc_name], 'point_in_buffer', output_type='POINT') arcpy.SymDiff_analysis(itm_fc_name, 'point_in_buffer', "symdiff")
def __remove_points_near_intersection(self, buffer_file_path): """ Remove GPS points near intersection from :param buffer_file_path :param buffer_file_path: :return: """ print('remove points near intersection') gps_with_speed_shp = 'gps_with_speed.shp' Gps_trajectories.delete_file( [gps_with_speed_shp, 'point_in_buffer.shp', "symdiff.shp"]) arcpy.management.XYTableToPoint( in_table='stamp_time.csv', out_feature_class=gps_with_speed_shp, x_field="POINT_X", y_field="POINT_Y", coordinate_system=arcpy.SpatialReference(2039)) arcpy.Intersect_analysis([buffer_file_path, gps_with_speed_shp], 'point_in_buffer.shp', output_type='POINT') arcpy.SymDiff_analysis(gps_with_speed_shp, 'point_in_buffer.shp', "symdiff.shp")
def createRasterBoundaryAndFootprints(fgdb_path, target_path, project_ID, project_path, project_UID): a = datetime.datetime.now() stat_out_folder = os.path.join(target_path, STAT_LAS_FOLDER) b_file_list = [] c_file_list = [] for f_name in [f for f in os.listdir(stat_out_folder) if (f.startswith('B_') and f.endswith('.shp'))]: b_path = os.path.join(stat_out_folder, f_name) c_path = os.path.join(stat_out_folder, "C{}".format(f_name[1:])) try: if not os.path.exists(b_path): arcpy.AddWarning("Failed to find B boundary file {}".format(b_path)) else: b_file_list.append(b_path) except: pass try: if not os.path.exists(c_path): arcpy.AddWarning("Failed to find C boundary file {}".format(c_path)) else: c_file_list.append(c_path) deleteFields(c_path) except: pass a = doTime(a, "Found {} footprints".format(len(b_file_list))) try: for file_type in [r'C_*.shp', r'B_*.shp']: bad_shape_type_list = list(filter(lambda x: arcpy.Describe(x).shapeType != 'Polygon',glob.glob(os.path.join(stat_out_folder, file_type)))) if len(bad_shape_type_list) > 0: for bad_type in bad_shape_type_list: arcpy.AddMessage("ERROR: Bad shape type in file '{}'".format(bad_type)) except: pass las_footprint = getLasFootprintPath(fgdb_path) lasd_boundary = getLasdBoundaryPath(fgdb_path) if arcpy.Exists(las_footprint): arcpy.AddMessage("Footprints exist: {}".format(las_footprint)) else: # Delete the boundary if the footprints don't exist (have to recreate anyway) deleteFileIfExists(lasd_boundary, True) lasd_boundary_B = "{}B".format(lasd_boundary) deleteFileIfExists(lasd_boundary_B, True) las_footprint_1 = os.path.join(fgdb_path, "{}B1".format(las_footprint)) las_footprint_2 = os.path.join(fgdb_path, "{}B2".format(las_footprint)) las_footprint_CP = os.path.join(fgdb_path, "{}B_CP".format(las_footprint)) a = doTime(a, "\tMerging B footprints to {}".format(las_footprint_2)) deleteFileIfExists(las_footprint_1, True) deleteFileIfExists(las_footprint_2, True) deleteFileIfExists(las_footprint_CP, True) ##Old Code arcpy.Merge_management(inputs=b_file_list, output=las_footprint_2) ##New Code #BJN: Reduce memory usage by appending blocks of tiles instead of merging # all of the tiles at once. out_path, out_name = os.path.split(las_footprint_2) cfs_template = b_file_list[0] arcpy.CreateFeatureclass_management(out_path, out_name, "POLYGON", template = cfs_template, spatial_reference = cfs_template) append_group_size = 1024 #appending x tiles at a time append_group_start = 0 #list offset for the current file group while append_group_start < len(b_file_list): #set the new file group endpoint append_group_end = append_group_start + append_group_size #Use the next append_group_size tiles, or up to the end of the list, #whichever is smaller. append_msg = '{}Adding {} tiles'.format(' '*8, append_group_size) if append_group_end < len(b_file_list): arcpy.Append_management(b_file_list[append_group_start:append_group_end], las_footprint_2) else: append_msg = '{}Adding {} tiles'.format(' '*8, len(b_file_list[append_group_start:])) arcpy.Append_management(b_file_list[append_group_start:], las_footprint_2) arcpy.AddMessage(append_msg) #Set the new start point append_group_start = append_group_end arcpy.Compact_management(out_path) ##End New Code #Utility.addToolMessages() deleteFields(las_footprint_2) arcpy.RepairGeometry_management(in_features=las_footprint_2, delete_null="DELETE_NULL") #Utility.addToolMessages() a = doTime(a, "\tMerged B and repaired footprints to {}".format(las_footprint_2)) #arcpy.CreateCartographicPartitions_cartography(in_features=las_footprint_2, out_features=las_footprint_CP, feature_count=PARTITION_COUNT) #a = doTime(a, "\tCreated B carto parts") #arcpy.env.cartographicPartitions = las_footprint_CP #a = doTime(a, "\tSet B cartographic partitions to {}".format(las_footprint_CP)) #arcpy.SimplifyPolygon_cartography(in_features=las_footprint_2, out_feature_class=las_footprint_1, algorithm="POINT_REMOVE", tolerance="0.5 Meters", minimum_area="0 Unknown", error_option="RESOLVE_ERRORS", collapsed_point_option="NO_KEEP", in_barriers="") #Utility.addToolMessages() #arcpy.env.cartographicPartitions = None #a = doTime(a, "\tSimplified B las footprints to {}".format(las_footprint_1)) #deleteFileIfExists(las_footprint_2, True) #arcpy.RepairGeometry_management(in_features=las_footprint_1, delete_null="DELETE_NULL") #Utility.addToolMessages() #a = doTime(a, "Merged B las footprints {}".format(las_footprint_1)) createBoundaryFeatureClass(las_footprint_2, lasd_boundary_B) deleteFileIfExists(las_footprint_2, True) a = datetime.datetime.now() # Merge the other footprints before clipping las_footprint_1 = os.path.join(fgdb_path, "{}C1".format(las_footprint)) las_footprint_2 = os.path.join(fgdb_path, "{}C2".format(las_footprint)) las_footprint_CP = os.path.join(fgdb_path, "{}C_CP".format(las_footprint)) deleteFileIfExists(las_footprint_1, True) deleteFileIfExists(las_footprint_2, True) deleteFileIfExists(las_footprint_CP, True) a = doTime(a, "\tMerging C las footprints to {}".format(las_footprint_2)) arcpy.Merge_management(inputs=c_file_list, output=las_footprint_2) #Utility.addToolMessages() deleteFields(las_footprint_2) arcpy.RepairGeometry_management(in_features=las_footprint_2, delete_null="DELETE_NULL") #Utility.addToolMessages() a = doTime(a, "\tMerged C las footprints to {}".format(las_footprint_2)) #arcpy.CreateCartographicPartitions_cartography(in_features=las_footprint_2, out_features=las_footprint_CP, feature_count=PARTITION_COUNT) #a = doTime(a, "\tCreated C carto parts") #arcpy.env.cartographicPartitions = las_footprint_CP #a = doTime(a, "\tSet C cartographic partitions to {}".format(las_footprint_CP)) #arcpy.SimplifyPolygon_cartography(in_features=las_footprint_2, out_feature_class=las_footprint_1, algorithm="POINT_REMOVE", tolerance="0.5 Meters", minimum_area="0 Unknown", error_option="RESOLVE_ERRORS", collapsed_point_option="NO_KEEP", in_barriers="") #Utility.addToolMessages() #arcpy.env.cartographicPartitions = None #a = doTime(a, "\tSimplified C las footprints to {}".format(las_footprint_1)) #deleteFileIfExists(las_footprint_2, True) #arcpy.RepairGeometry_management(in_features=las_footprint_1, delete_null="DELETE_NULL") #Utility.addToolMessages() #a = doTime(a, "Merged C las footprints {}".format(las_footprint_1)) lasd_boundary_C = "{}C".format(lasd_boundary) deleteFileIfExists(lasd_boundary_C, True) createBoundaryFeatureClass(las_footprint_2, lasd_boundary_C) lasd_boundary_SD = "{}_SD".format(lasd_boundary) lasd_boundary_SD1 = "{}_SD1".format(lasd_boundary) lasd_boundary_SD2 = "{}_SD2".format(lasd_boundary) lasd_boundary_SD3 = "{}_SD3".format(lasd_boundary) deleteFileIfExists(lasd_boundary_SD, True) deleteFileIfExists(lasd_boundary_SD1, True) deleteFileIfExists(lasd_boundary_SD2, True) deleteFileIfExists(lasd_boundary_SD3, True) # Replace a layer/table view name with a path to a dataset (which can be a layer file) or create the layer/table view within the script arcpy.SymDiff_analysis(in_features=lasd_boundary_B, update_features=lasd_boundary_C, out_feature_class=lasd_boundary_SD1, join_attributes="ONLY_FID", cluster_tolerance="3 Meters") arcpy.Buffer_analysis(in_features=lasd_boundary_SD1, out_feature_class=lasd_boundary_SD2, buffer_distance_or_field="-3 Meters", line_side="FULL", line_end_type="ROUND", dissolve_option="NONE", dissolve_field="", method="PLANAR") arcpy.MultipartToSinglepart_management(in_features=lasd_boundary_SD2, out_feature_class=lasd_boundary_SD3) arcpy.Buffer_analysis(in_features=lasd_boundary_SD3, out_feature_class=lasd_boundary_SD, buffer_distance_or_field="3 Meters", line_side="FULL", line_end_type="ROUND", dissolve_option="NONE", dissolve_field="", method="PLANAR") arcpy.DeleteField_management(in_table=lasd_boundary_SD, drop_field="FID_BoundaryLASDatasetB;FID_BoundaryLASDatasetC;BUFF_DIST;ORIG_FID") deleteFileIfExists(lasd_boundary_C, True) deleteFileIfExists(lasd_boundary_SD1, True) deleteFileIfExists(lasd_boundary_SD2, True) deleteFileIfExists(lasd_boundary_SD3, True) a = doTime(a, "Created symetrical difference in boundaries {}".format(lasd_boundary_SD)) checkNullFields(las_footprint_2) a = datetime.datetime.now() deleteFileIfExists(las_footprint, True) arcpy.Clip_analysis(in_features=las_footprint_2, clip_features=lasd_boundary_B, out_feature_class=las_footprint, cluster_tolerance="") #deleteFileIfExists(las_footprint_1, True) deleteFileIfExists(lasd_boundary_B, True) #deleteFileIfExists(lasd_boundary, True) #deleteFileIfExists(las_footprint_CP, True) #arcpy.env.cartographicPartitions = None deleteFileIfExists(las_footprint_2, True) a = doTime(a, "Clipped las footprints to dataset boundary {} ".format(las_footprint)) deleteFields(las_footprint) try: arcpy.RepairGeometry_management(in_features=las_footprint, delete_null="KEEP_NULL") except: pass if arcpy.Exists(lasd_boundary): arcpy.AddMessage("Boundary exists: {}".format(lasd_boundary)) else: deleteFileIfExists(lasd_boundary, True) summary_string, field_alter = getStatsFields(las_footprint) createBoundaryFeatureClass(las_footprint, lasd_boundary, summary_string, field_alter) addProjectInfo(las_footprint, lasd_boundary, project_ID, project_path, project_UID) deleteFields(lasd_boundary) try: arcpy.RepairGeometry_management(in_features=lasd_boundary, delete_null="KEEP_NULL") except: pass return lasd_boundary, las_footprint
def checkCoverage(maskA, maskB, inputFile=None): # Checks the coverage between polygons mask A and mask B # Set temporary variables prefix = os.path.join(arcpy.env.scratchGDB, "cover_") maskACopy = prefix + "maskACopy" maskBCopy = prefix + "maskBCopy" maskACoverage = prefix + "maskACoverage" # Copy maskA and maskBCopy to temporary variables arcpy.CopyFeatures_management(maskA, maskACopy) arcpy.CopyFeatures_management(maskB, maskBCopy) percOut = 0 if checkLicenceLevel('Advanced') or arcpy.ProductInfo() == "ArcServer": # Check if the fields Area_ha exist in maskACopy and maskBCopy # If they do, delete them maskAFields = arcpy.ListFields(maskACopy) for field in maskAFields: if field.name == "Area_ha": arcpy.DeleteField_management(maskACopy, "Area_ha") maskBFields = arcpy.ListFields(maskBCopy) for field in maskBFields: if field.name == "Area_ha": arcpy.DeleteField_management(maskBCopy, "Area_ha") # Delete fields area_ha in in maskACopy and maskBCopy # Calculate total area of the coverage (study area or contributing area) arcpy.AddField_management(maskBCopy, "Area_ha", "DOUBLE") exp = "!SHAPE.AREA@HECTARES!" arcpy.CalculateField_management(maskBCopy, "Area_ha", exp, "PYTHON_9.3") maskBCopyVal = 0 for row in arcpy.da.SearchCursor(maskBCopy, ["Area_ha"]): maskBCopyVal += float(row[0]) # Use symmetrical difference to find which areas in maskA and maskBCopyCopy do not overlap arcpy.SymDiff_analysis(maskACopy, maskBCopy, maskACoverage) # Check if there are non-overlapping areas in the land use and coverage rows = [row for row in arcpy.da.SearchCursor(maskACoverage, "*")] numFeatures = len(rows) if numFeatures != 0: # There is a discrepancy between mask A and mask B # Calculate the area of the polygons in the discrepancy arcpy.AddField_management(maskACoverage, "Sliver", "DOUBLE") exp = "!SHAPE.AREA@HECTARES!" arcpy.CalculateField_management(maskACoverage, "Sliver", exp, "PYTHON_9.3") area = 0 for row in arcpy.da.SearchCursor(maskACoverage, ["Sliver"]): area += float(row[0]) # Calculate percentage of the coverage area that the discrepancy covers percOut = float(area) / float(maskBCopyVal) * 100.0 if percOut > 2.5: warning = 'Input data coverage is less tha 97.5 percent of the study area' log.warning(warning) warning = 'This may cause discrepancies in later calculations' log.warning(warning) warning = 'Please check this input: ' + str(inputFile) log.warning(warning) # Delete temporary masks arcpy.Delete_management(maskACopy) arcpy.Delete_management(maskBCopy) arcpy.Delete_management(maskACoverage) else: log.warning( 'Coverage discrepancies between soil, land use, and coverage extent not checked as advanced license not present.' ) log.warning( 'Please ensure the soil and land use shapefile cover at least 97.5 percent of the coverage extent' )
"%s Meters"%analysis_distance) env.extent = buff_geom_list[0].extent # Extract the applicable hydro layer for this feature, only containing ocean arcpy.Clip_analysis(hydro_fc,buff_geom_list,"tempHydro") print "\t finished clip oceans" # DEBUG # Try using intersect instead of clip # DEBUG ## arcpy.Intersect_analysis([hydro_fc,buff_geom_list],"tempHydro") # DEBUG ## print "\t finished intersecting oceans" # DEBUG arcpy.FeatureClassToFeatureClass_conversion("tempHydro",r"C:\ExposureDetection\scratch", "Hydro.shp") # DEBUG print "\t finished dumping to temp" # DEBUG # Extract the nearby properties arcpy.Clip_analysis(gnaf_fc,buff_geom_list,"tempNearby") print "\t finished clip properties" # Remove intersecting features arcpy.SymDiff_analysis("tempTarget", "tempNearby","tempNearbyNoIntersect") print "\t finished SymDiff" # Set their elev to 10 arcpy.CalculateField_management("tempNearbyNoIntersect",offset_fieldname,10,"PYTHON_9.3") print "\t finished Calc Field" # Do the processing stats = exposure("tempTarget","tempNearbyNoIntersect","tempHydro") # A little feedback to check it's working... if max(stats) > 0: print " Exposure detected! ".center(79,"*") else: print "\t...row not exposed..." # Write the results to our out CSV
num_fail = 0 for city in city_ls: cnt = 0 #计数器 for key in dict: inFeatures = inWorkspace + '//' + city+ dict[key] print inFeatures updateFileName = city + ""+dict[key]#"_OUTLINE" updateFeatures = updateWorkspace + '//' + updateFileName print updateFeatures outFeatureClass = os.path.join(outWorkspace,version_no) + city_dict2[city] + key + u'差分量.shp'#【修改版本号使输出成果命名正确】 print outFeatureClass clusterTolerance = ".0001 Meters" # Execute SymDiff try: #注意!差分前后数据坐标必须一致!!! arcpy.SymDiff_analysis(inFeatures, updateFeatures, outFeatureClass, "ALL", clusterTolerance) print(city + key + u'差分成功!') cnt += 1 except: print(city + key + u'差分失败!请确认!') num_fail += 1 if cnt==2: num_ok += 1 print(u'差分完成!共{0}城市成功,{1}差分失败项,请确认!'.format(num_ok,num_fail)) time_end=time.time() last = round((time_end-time_start)/60,2) print u'绿地水系差分处理耗时:{}min'.format(last)
def RunSymmetricalDifference(): print("Running symmetrical difference to look for changes...") for feature_class in feature_class_list: arcpy.SymDiff_analysis( feature_class + "_Prev", feature_class, str(changes_fgdb) + "/SymDiff_" + str(feature_class), "ALL")
def save_changes_to_gdb(input_gdb, input_tfl, bcgw_connection): """ Takes input database. Finds line changes using last edited date stamp and saves those as separate dataset finds area changes using symmetrical difference tool comparison to previous final and saves those (if any)""" arcpy.env.workspace = input_gdb table_object = geobc.TableInfo() check_out_date = table_object.get_last_date( input_gdb + os.sep + input_tfl + '_Change_History', 'Date_Extracted') check_out_date = check_out_date.strftime( "%b %d %Y %H:%M:%S") #format to string for use in query arcpy.AddMessage('Checked out on ' + check_out_date) tfl_lines = current_tfl_layer_name + '_Line' schedule_a = current_tfl_layer_name + '_Schedule_A' #make a feature layer of the boundary poly not including any deletion areas for the comparison boundary = current_tfl_layer_name + '_Boundary' arcpy.MakeFeatureLayer_management(boundary, 'Boundary_fl', "Poly_Type <> 'Deletion'") boundary_final = final_tfl_layer_name + '_Boundary' schedule_a_final = final_tfl_layer_name + '_Schedule_A' #make a feature layer of the changed lines - if there are any - save to database if arcpy.Exists(input_gdb + os.sep + input_tfl + '_Line_Changes'): arcpy.Delete_management(input_gdb + os.sep + input_tfl + '_Line_Changes') changed_lines = arcpy.MakeFeatureLayer_management( tfl_lines, 'lines_fl', "last_edited_date > date '" + check_out_date + "'") arcpy.CopyFeatures_management( changed_lines, input_gdb + os.sep + input_tfl + '_Line_Changes') arcpy.Delete_management(changed_lines) #Get a difference layer for the TFL Boundary (compare to TFL Final) final_boundary_diff = join(input_gdb, input_tfl + '_Boundary_Difference') if arcpy.Exists(final_boundary_diff): arcpy.Delete_management(final_boundary_diff) arcpy.SymDiff_analysis(boundary, 'Boundary_fl', final_boundary_diff) #Difference layer TFL Schedule A from Final final_sched_a_diff = join(input_gdb, input_tfl + '_Schedule_A_Difference') if arcpy.Exists(final_sched_a_diff): arcpy.Delete_management(final_sched_a_diff) arcpy.SymDiff_analysis(schedule_a, schedule_a_final, final_sched_a_diff) #set up parameters for #Schedule A and Current boundary Difference layers from BCGW to final tfl_whse = bcgw_connection + '\\WHSE_ADMIN_BOUNDARIES.FADM_TFL_ALL_SP' sched_a_whse = bcgw_connection + '\\WHSE_ADMIN_BOUNDARIES.FADM_TFL_SCHED_A' #get the format for the forest file ID for query if '_0' in input_tfl: ffid = input_tfl.replace('_0', '') else: ffid = input_tfl.replace('_', '') bcgw_sched_a_diff = join(input_gdb, input_tfl + '_Sched_A_BCGW_Difference') bcgw_sched_a_fl = arcpy.MakeFeatureLayer_management( sched_a_whse, 'bcgw_sched_a_fl', "FOREST_FILE_ID = '" + ffid + "' AND RETIREMENT_DATE IS NULL") if arcpy.Exists(bcgw_sched_a_diff): arcpy.Delete_management(bcgw_sched_a_diff) arcpy.SymDiff_analysis(bcgw_sched_a_fl, schedule_a_final, bcgw_sched_a_diff) arcpy.Delete_management(bcgw_sched_a_fl) #make a difference layer between previous final and BCGW bcgw_boundary_diff = join(input_gdb, input_tfl + '_Boundary_BCGW_Difference') bcgw_boundary_fl = arcpy.MakeFeatureLayer_management( tfl_whse, 'bcgw_boundary_fl', "FOREST_FILE_ID = '" + ffid + "'") if arcpy.Exists(bcgw_boundary_diff): arcpy.Delete_management(bcgw_boundary_diff) arcpy.SymDiff_analysis(bcgw_boundary_fl, 'boundary_fl', bcgw_boundary_diff) arcpy.Delete_management(bcgw_boundary_fl) arcpy.Delete_management('boundary_fl') #Check the difference layers - if there are any differences notify the editor - otherwise - delete them #NOTE: this is pretty repetetive and should probably be within a function if int(arcpy.GetCount_management(bcgw_sched_a_diff)[0]) > 0: arcpy.AddMessage( 'Differences found between final Schedule A and BCGW Schedule A -- Saving difference layer' ) else: arcpy.AddMessage( 'No differences found between final Schedule A and BCGW Schedule A' ) arcpy.Delete_management(bcgw_sched_a_diff) if int(arcpy.GetCount_management(bcgw_boundary_diff)[0]) > 0: arcpy.AddMessage( 'Differences found between final boundary and BCGW boundary -- Saving difference layer' ) else: arcpy.AddMessage( 'No differences between final Boundary and BCGW boundary') arcpy.Delete_management(bcgw_boundary_diff) if int(arcpy.GetCount_management(final_sched_a_diff)[0]) > 0: arcpy.AddMessage( 'Difference found between working and final Schedule A -- Saving difference' ) else: arcpy.AddMessage( 'No differences found between working and final Schedule A') arcpy.Delete_management(final_sched_a_diff) if int(arcpy.GetCount_management(final_boundary_diff)[0]) > 0: arcpy.AddMessage( 'Differences found between working and final boundary - Saving difference layer\n' ) else: arcpy.AddMessage( 'No differences found between working and final boundary\n') arcpy.Delete_management(final_boundary_diff)
def check_bcgw_to_final_differences( tfl_basename, input_gdb, BCGWConnection): #IAN - use standard variable names here """takes input folder and bcgw connection - compares the TFL Final with BCGW for schedule A and boundary and saves any differences between them to the edit database.""" final_tfl_data_folder = join(TFL_FINAL_FOLDER, folder_basename, 'data', 'FADM_' + folder_basename + '.gdb', 'TFL_Data') #This function is also implemented in TFL_Prepare_Review_Package.py boundary_final = join(final_tfl_data_folder, folder_basename + '_Boundary') schedule_a_final = join(final_tfl_data_folder, folder_basename + '_Schedule_A') arcpy.AddMessage('Checking differences between BCGW and final TFLs...') tfl_whse = BCGWConnection + '\\WHSE_ADMIN_BOUNDARIES.FADM_TFL_ALL_SP' sched_a_whse = BCGWConnection + '\\WHSE_ADMIN_BOUNDARIES.FADM_TFL_SCHED_A' #IAN - this may change when we standardize TFL Current View ForestFileID if '_0' in folder_basename: sched_a_ffid = folder_basename.replace('_0', '') else: sched_a_ffid = folder_basename.replace('_', '') #Compare Schedule A sources sched_a_diff = join(input_gdb, folder_basename + '_Sched_A_BCGW_Difference') bcgw_sched_a_fl = arcpy.MakeFeatureLayer_management( sched_a_whse, 'bcgw_sched_a_fl', "FOREST_FILE_ID = '" + sched_a_ffid + "' AND RETIREMENT_DATE IS NULL") if arcpy.Exists(sched_a_diff): arcpy.Delete_management(sched_a_diff) arcpy.SymDiff_analysis(bcgw_sched_a_fl, schedule_a_final, sched_a_diff) #Compare TFL Current View sources boundary_diff = join(input_gdb, folder_basename + '_Boundary_BCGW_Difference') bcgw_boundary_fl = arcpy.MakeFeatureLayer_management( tfl_whse, 'bcgw_boundary_fl', "FOREST_FILE_ID = '" + folder_basename.replace('_', ' ') + "'") if arcpy.Exists(boundary_diff): arcpy.Delete_management(boundary_diff) arcpy.SymDiff_analysis(bcgw_boundary_fl, boundary_final, boundary_diff) #Check the difference layers - if there are any differences warn the editor - otherwise - delete them if int(arcpy.GetCount_management(sched_a_diff)[0]) > 0: arcpy.AddWarning( '===== Warning: differences found in Schedule A: TFL Final and BCGW are not equal' ) arcpy.AddWarning( 'Make sure to check the _Sched_A_BCGW_Difference dataset in the working folder to review and resolve differences' ) else: arcpy.AddMessage( 'No differences found between TFL Final Schedule A and BCGW Schedule A' ) arcpy.Delete_management(sched_a_diff) if int(arcpy.GetCount_management(boundary_diff)[0]) > 0: arcpy.AddWarning( '===== Warning: differences found in Boundary: TFL Final and BCGW are not equal' ) arcpy.AddWarning( 'Make sure to check the _Boundary_BCGW_Differenc dataset in the working folder to review and resolve differences\n' ) else: arcpy.AddMessage( 'No differences found between Final TFL Boundary and BCGW TFL Boundary\n' ) arcpy.Delete_management(boundary_diff)
(round(end.X, 4), round(end.Y, 4))] if branch[0] == branch[1]: cursor.deleteRow() Count += 1 if Count > 0: arcpy.AddWarning('Deleted %s loops' % (Count)) del cursor, row arcpy.AddMessage('Creating output files') arcpy.Dissolve_management( "in_memory\\templines", output, field, "", "SINGLE_PART", "DISSOLVE_LINES" ) #remove overlaps and dissolve based on specified fields if output2: arcpy.SymDiff_analysis(output, infc, output2, "ONLY_FID") except Exception, e: arcpy.AddError('%s' % (e)) finally: del_files = ["in_memory\\templines", "in_memory\\temptable"] for fname in del_files: try: arcpy.DeleteFeatures_management(fname) except Exception, e: continue if __name__ == "__main__":
inFeatures = arcpy.ListFeatureClasses("Land_Management_NoSymetricDiff*") # Execute SymDiff newList = [] delFields = [ "FID_RGB_BO", "Id", "Area", "FID_RGB_Ba", "MAJ_BAS", "Shape_Le_1", "Shape_Ar_1" ] name = "NotReported" for fc in inFeatures: for clip in clipped_features: temp = os.path.split(clip)[-1] if temp.startswith("RGB_Basin") and fc.endswith("_bas"): output = out_gdb + "\\" + name + "_bas" arcpy.SymDiff_analysis(fc, clip, output) if temp.startswith("RGB_Ses") and fc.endswith("_ses"): output = out_gdb + "\\" + name + "_ses" arcpy.SymDiff_analysis(fc, clip, output) cur = arcpy.UpdateCursor(output) for row in cur: row.setValue("MANGTYPE_2", 'Private or Not Reported') row.setValue("ADMIN_AGENCY_CODE", 'PVT') row.setValue("ADMIN_DEPT_CODE", 'PVT') row.setValue("MANGTYPE_1", 'Private or Not Reported') row.setValue("ADMIN_UNIT_TYPE", 'Not Applicable') cur.updateRow(row) arcpy.DeleteField_management(output, delFields) print "Step 10 completed at", datetime.datetime.now().strftime("%I:%M:%S%p")
temp_bounding_box_path = os.path.join(arcgis_temp_dir, "bounding_box.shp") temp_bounding_box_small_path = os.path.join( arcgis_temp_dir, "bounding_box_small.shp") temp_frame_path = os.path.join(arcgis_temp_dir, "frame.shp") ## Create difference of tile bounding box and inwards buffered tile bounding box arcpy.MakeFeatureLayer_management( segments_shp_path, "seg_lyr") ## make a layer from the feature class arcpy.MinimumBoundingGeometry_management( "seg_lyr", temp_bounding_box_path, "ENVELOPE", "ALL") arcpy.Buffer_analysis(temp_bounding_box_path, temp_bounding_box_small_path, -2) arcpy.SymDiff_analysis(temp_bounding_box_path, temp_bounding_box_small_path, temp_frame_path) ## Select polygons intersecting this frame to remove boundary segments arcpy.SelectLayerByLocation_management( in_layer="seg_lyr", overlap_type="INTERSECT", select_features=temp_frame_path) ## Use a SQL query to select segments larger than a certain threshold to remove background polygon ## and segments associated with trees smaller than a threshold arcpy.SelectLayerByAttribute_management( in_layer_or_view="seg_lyr", selection_type="ADD_TO_SELECTION", where_clause=' "Crown_dm" >= %d OR "Tree_ht" < %d ' % (PARAMS['crown_dm_thresh'], PARAMS['tree_ht_thresh']))
def matchPolygon(sourcePolygon, retrievalPolygon): # 正对角线长度 s_len, sourcePartList = mu.polygonCatercorner(sourcePolygon) # 正对角线长度 r_len, retrievalPartList = mu.polygonCatercorner(retrievalPolygon) scale = r_len / s_len for part in sourcePartList: for pnt in part: pnt.x *= scale pnt.y *= scale # 构建多边形求交 arcpy.env.workspace = dataPath arcpy.env.overwriteOutput = True workspace = "in_memory\\" sourceInFeature = workspace + sourcePolygon.oid + "_in" sourceFeatures = [] for part in sourcePartList: array = arcpy.Array() for pnt in part: point = arcpy.Point() point.X = pnt.x point.Y = pnt.y array.append(point) poly = arcpy.Polygon(array) sourceFeatures.append(poly) arcpy.CopyFeatures_management(sourceFeatures, sourceInFeature) retrievalInFeature = workspace + retrievalPolygon.oid + "_in" retrievalFeatures = [] for part in retrievalPartList: array = arcpy.Array() for pnt in part: point = arcpy.Point() point.X = pnt.x point.Y = pnt.y array.append(point) poly = arcpy.Polygon(array) retrievalFeatures.append(poly) arcpy.CopyFeatures_management(retrievalFeatures, retrievalInFeature) # 相交取反 diffOutFeature = workspace + retrievalPolygon.oid + "_diff" arcpy.SymDiff_analysis(sourceInFeature, retrievalInFeature, diffOutFeature) # 求取差异面积,求差异度 sArea = 0 rArea = 0 dArea = 0 with arcpy.da.SearchCursor(diffOutFeature, ["SHAPE@"]) as cursor: for row in cursor: dArea += row[0].area with arcpy.da.SearchCursor(sourceInFeature, ["SHAPE@"]) as cursor: for row in cursor: sArea += row[0].area with arcpy.da.SearchCursor(retrievalInFeature, ["SHAPE@"]) as cursor: for row in cursor: rArea += row[0].area # 删除中间数据 arcpy.Delete_management(diffOutFeature) arcpy.Delete_management(sourceInFeature) arcpy.Delete_management(retrievalInFeature) d = dArea / (sArea + rArea) return 1 - d, scale
arcpy.SelectLayerByAttribute_management("orig_polys_lyr", "NEW_SELECTION", sql_newer_poly) arcpy.CopyFeatures_management("orig_polys_lyr", "in_memory/newer_fc") # Intersect older and newer fire polygons to get overlapping sections (reburn areas) arcpy.Intersect_analysis(["in_memory/older_fc", "in_memory/newer_fc"], "in_memory/reburn_poly", "ALL") # Intersect older fire and reburn area to get perimeter of older fire within newer burn shared_line = os.path.join(ws, "shared_line" + str(n_poly) + ".shp") arcpy.Intersect_analysis(["in_memory/older_fc", "in_memory/reburn_poly"], shared_line, "ALL", "#", "LINE") # Convert reburn polygon to polyline arcpy.PolygonToLine_management("in_memory/reburn_poly", "in_memory/reburn_line") # Get section of reburn perimeter that excludes previously id'd shared line inverse_line = os.path.join(ws, "inverse_line" + str(n_poly) + ".shp") arcpy.SymDiff_analysis("in_memory/reburn_line", shared_line, inverse_line) # Generate points along the inverse intersection line print 'generating points' inverse_pts = os.path.join(ws, "inverse_pts" + str(n_poly) + ".shp") arcpy.GeneratePointsAlongLines_management(inverse_line, inverse_pts, 'DISTANCE', Distance='500 meters') # Get distance from each point on the line to the intersected line. This is the distance of newer fire within older print 'getting distance' arcpy.Near_analysis(inverse_pts, shared_line, "#", "LOCATION", "ANGLE") # Add id fields (older, newer fire IDs) to each point. Otherwise no way to tell # which points are associated with which fires arcpy.AddField_management(inverse_pts, "parentid1", "LONG") arcpy.AddField_management(inverse_pts, "parentid2", "LONG")