def getDownstreamGridCodes(gridcode, catchFC, flownet, flownetFC): '''Traces downstream from the project's catchment and lists all downstream catchment GRIDCODES''' #Get the catchment corresponding to the gridcode theCatchLyr = arcpy.MakeFeatureLayer_management( catchFC, "theCatch", "GRIDCODE = {}".format(gridcode)) #Clip the flowine within the catchment and get its lowest point theFlowline = arcpy.Clip_analysis(flowlineFC, theCatchLyr, "in_memory/theFlowline") theFlowpoint = arcpy.FeatureVerticesToPoints_management( theFlowline, "in_memory/thePoint", "END") #Trace from this point downstream to the end of the HUC8 geometric network theTraceLyr = arcpy.TraceGeometricNetwork_management( flownet, "DownStrmLyr", theFlowpoint, "TRACE_DOWNSTREAM") #Extract the line feature theTraceLine = arcpy.SelectData_management(theTraceLyr, os.path.basename(flownetFC)) #Make a new feature layer of catchments and select those that intersect the downstream trace theCatchLyr = arcpy.MakeFeatureLayer_management(catchFC, "theCatch") theCatchments = arcpy.SelectLayerByLocation_management( theCatchLyr, "INTERSECT", theTraceLine) #Create a list of catchment GRIDCODEs in the selected catchments outGridcodes = [] with arcpy.da.SearchCursor(theCatchments, "GRIDCODE") as cur: for rec in cur: outGridcodes.append(rec[0]) return outGridcodes
def recheck_orientation(self, geometry_name, geometry_folder, networkLine): arcpy.env.overwriteOutput = True geodatabaseTopage = geodatabase() geodatabasePath = geodatabaseTopage.path(geometry_folder) print("Recréation du réseau géométrique") if arcpy.Exists(geodatabasePath + "/" + networkLine + "_BUILDERR"): arcpy.Delete_management(geodatabasePath + "/" + networkLine + "_BUILDERR") arcpy.Delete_management(geometry_folder + geometry_name) arcpy.CreateGeometricNetwork_management( geometry_folder, geometry_name, networkLine + " SIMPLE_EDGE NO;" + networkLine + "_summit SIMPLE_JUNCTION YES", "0.001", "#", "#", "#", "PRESERVE_ENABLED") print("Ajout de l'exutoire") arcpy.MakeFeatureLayer_management( geometry_folder + networkLine + "_summit", 'lyr') arcpy.SelectLayerByLocation_management( 'lyr', 'INTERSECT', geometry_folder + networkLine + "_Exutoire") arcpy.CalculateField_management('lyr', 'AncillaryRole', "2", "PYTHON_9.3", "#") arcpy.SelectLayerByAttribute_management('lyr', "CLEAR_SELECTION") arcpy.Delete_management("lyr") print("Calcul du sens d'écoulement") arcpy.SetFlowDirection_management(geometry_folder + geometry_name, "WITH_DIGITIZED_DIRECTION") arcpy.TraceGeometricNetwork_management( geometry_folder + "/" + geometry_name, geometry_name, geometry_folder + networkLine + "_Exutoire", "TRACE_UPSTREAM") arcpy.MakeFeatureLayer_management( geometry_folder + networkLine + "_notUpstream", 'lyr_notUpstream') return arcpy.GetCount_management('lyr_notUpstream').__str__()
# Select 10 hectare lake junctions that don't intersect the target lake. arcpy.SelectLayerByLocation_management(tenhajunction_lyr, "INTERSECT", fc, '', "NEW_SELECTION") arcpy.SelectLayerByLocation_management(tenhajunction_lyr, "INTERSECT", fc, '', "SWITCH_SELECTION") # Select 4 hectare lake junctions that do intersect it. arcpy.SelectLayerByLocation_management(fourhajunction_lyr, "INTERSECT", fc, '', "NEW_SELECTION") # Copy junctions arcpy.CopyFeatures_management(fourhajunction_lyr, os.path.join(lakes, "junct" + name)) lakejunction = os.path.join(lakes, "junct" + name) try: # Trace the network upstream from the junctions from above, stopping at ten hectare junctions. arcpy.TraceGeometricNetwork_management( network, os.path.join(lakes, "im" + name + "tracelyr"), lakejunction, "TRACE_UPSTREAM", tenhajunction_lyr) trace = os.path.join(lakes, "im" + name + "tracelyr", "NHDFlowline") # Write the trace arcpy.CopyFeatures_management( trace, os.path.join(lakes, "im" + name + "trace")) traceshp = os.path.join(lakes, "im" + name + "trace") # Make a layer from the trace arcpy.MakeFeatureLayer_management( traceshp, os.path.join(lakes, "im" + name + "tracesellyr")) tracesel = os.path.join(lakes, "im" + name + "tracesellyr") # Select from the trace lines those that don't have their midpoint in the lake arcpy.SelectLayerByLocation_management(tracesel, "HAVE_THEIR_CENTER_IN", fc, '', "NEW_SELECTION") arcpy.SelectLayerByLocation_management(tracesel,
arcpy.Merge_management([non10junctions, non10vertices], os.path.join(outfolder, "non10merged.shp")) non10merged = os.path.join(outfolder, "non10merged.shp") arcpy.MakeFeatureLayer_management(non10merged, os.path.join(outfolder, "non10merged.lyr")) non10merged_lyr = os.path.join(outfolder, "non10merged.lyr") # For tracing barriers, select non10merged points that don't intersect a 10 ha lake. arcpy.SelectLayerByLocation_management(non10merged_lyr, "INTERSECT", csiwaterbody10ha, "", "NEW_SELECTION") arcpy.CopyFeatures_management(non10merged_lyr, os.path.join(outfolder, "non10barriers.shp")) non10barriers = os.path.join(outfolder, "non10barriers.shp") # Trace1-Trace downstream to first non10hajunction starting from csijunction10ha flag points. arcpy.TraceGeometricNetwork_management(network, "trace1", csijunction10ha,\ "TRACE_DOWNSTREAM", non10barriers) # Save trace1 flowlines and junctions to layers on disk. arcpy.SaveToLayerFile_management( "trace1\HYDRO_NET_Junctions", os.path.join(outfolder, "trace1junctions.lyr")) trace1junctions_lyr = os.path.join(outfolder, "trace1junctions.lyr") arcpy.SaveToLayerFile_management("trace1\NHDFlowline", os.path.join(outfolder, "trace1flowline.lyr")) trace1flowline_lyr = os.path.join(outfolder, "trace1flowline.lyr") # Select vertice midpoints that intersect trace1 flowlines selection for new flags for trace2. arcpy.SelectLayerByLocation_management(vertices_lyr, "INTERSECT", trace1flowline_lyr, "", "NEW_SELECTION") arcpy.CopyFeatures_management(vertices_lyr, os.path.join(outfolder, "trace2flags.shp"))
def upstream_lakes(nhd_gdb, output_table): #define paths in nhd geodatabase nhd_waterbody = os.path.join(nhd_gdb, 'NHDWaterbody') nhd_flowline = os.path.join(nhd_gdb, 'NHDFlowline') nhd_junctions = os.path.join(nhd_gdb, 'HYDRO_NET_Junctions') hydro_net = os.path.join(nhd_gdb, 'Hydrography', 'HYDRO_NET') arcpy.env.workspace = 'in_memory' arcpy.env.outputCoordinateSystem = arcpy.SpatialReference(102039) arcpy.MakeFeatureLayer_management(nhd_junctions, 'junctions') cu.multi_msg('Preparing layers and fields for calculations....') # select only lakes as defined in our project: waterbodies with one of these # types and greater than 4ha, and certain reservoirs greater than 10ha fcodes = (39000, 39004, 39009, 39010, 39011, 39012, 43600, 43613, 43615, 43617, 43618, 43619, 43621) gte_4ha_lakes_query = '''("AreaSqKm" >=0.04 AND "FCode" IN %s) OR ("FCode" = 43601 AND "AreaSqKm" >= 0.1)''' % ( fcodes, ) gte_10ha_lakes_query = '''("AreaSqKm" >=0.1 AND "FCode" IN %s) OR ("FCode" = 43601 AND "AreaSqKm" >= 0.1)''' % ( fcodes, ) arcpy.MakeFeatureLayer_management(nhd_waterbody, 'gte_4ha_lakes', gte_4ha_lakes_query) arcpy.MakeFeatureLayer_management(nhd_waterbody, 'gte_10ha_lakes', gte_10ha_lakes_query) arcpy.CopyRows_management('gte_4ha_lakes', 'output_table') # (need this for line 61 make feature layer to work right!) arcpy.CopyFeatures_management('gte_4ha_lakes', 'gte_4ha_lakes_DISK') # add the new fields we'll calculate: count and area count_fields = ['Upstream_Lakes_4ha_Count', 'Upstream_Lakes_10ha_Count'] area_fields = ['Upstream_Lakes_4ha_Area_ha', 'Upstream_Lakes_10ha_Area_ha'] for cf in count_fields: arcpy.AddField_management('output_table', cf, 'LONG') for af in area_fields: arcpy.AddField_management('output_table', af, 'DOUBLE') new_fields = count_fields + area_fields # for each lake, use its junctions as input flags to the upstream trace, then # evalute the traced network for how many lakes are in it cu.multi_msg( "Calculating upstream network for each lake. This may take a few minutes, or a few hours..." ) with arcpy.da.UpdateCursor('output_table', ['Permanent_Identifier'] + new_fields) as cursor: for row in cursor: id = row[0] # get the junction points on top of this lake, can be 0 or several where_clause = """"{0}" = '{1}'""".format('Permanent_Identifier', id) arcpy.MakeFeatureLayer_management('gte_4ha_lakes_DISK', "this_lake", where_clause) arcpy.SelectLayerByLocation_management('junctions', "INTERSECT", "this_lake", "1 Meters") count_jxns = int( arcpy.GetCount_management('junctions').getOutput(0)) # if the lake has no upstream connectivity whatsoever, it will have # no junctions on top of it. assign 0s # to the counts of upstream lakes. This can either be an "isolated" # lake or a "headwater" lake if count_jxns <= 0: row[1:] = [0, 0, 0, 0] # if there are any junctions, go ahead and trace upstream else: arcpy.CopyFeatures_management("junctions", 'this_lake_jxns') arcpy.TraceGeometricNetwork_management(hydro_net, "upstream", 'this_lake_jxns', "TRACE_UPSTREAM") arcpy.SelectLayerByLocation_management('gte_4ha_lakes', "INTERSECT", "upstream/NHDFlowline", '1 Meters', 'NEW_SELECTION') # Remove this lake from the selection!! arcpy.SelectLayerByAttribute_management( 'gte_4ha_lakes', 'REMOVE_FROM_SELECTION', where_clause) count_4ha_lakes = int( arcpy.GetCount_management('gte_4ha_lakes').getOutput(0)) # if there is upstream connectivity but no countable lakes # assign 0s for all the counts/areas and do not try to copy rows if count_4ha_lakes <= 0: row[1:] = [0, 0, 0, 0] # if there are any lakes upstream, copy rows before doing # counts so we can use a search cursor on just these lakes else: arcpy.CopyRows_management('gte_4ha_lakes', 'these_4ha_lakes') # row[1] is 4ha_Upstream_Lakes_Count # row [3] is 4ha_Upstream_Lakes_Area row[1] = int( arcpy.GetCount_management('these_4ha_lakes').getOutput( 0)) total_area = 0 with arcpy.da.SearchCursor('these_4ha_lakes', ['AreaSqKm']) as area4_cursor: for area4_row in area4_cursor: total_area += area4_row[0] * 100 row[3] = total_area # same but for 10ha arcpy.SelectLayerByLocation_management( 'gte_10ha_lakes', "INTERSECT", "upstream/NHDFlowline", '1 Meters', 'NEW_SELECTION') arcpy.SelectLayerByAttribute_management( 'gte_10ha_lakes', 'REMOVE_FROM_SELECTION', where_clause) count_10ha_lakes = int( arcpy.GetCount_management('gte_10ha_lakes').getOutput( 0)) # don't try to copy rows if selection is empty if count_10ha_lakes <= 0: row[2] = 0 row[4] = 0 # if there are features selected, copy rows so we # can use a search cursor else: arcpy.CopyRows_management('gte_10ha_lakes', 'these_10ha_lakes') row[2] = int( arcpy.GetCount_management( 'these_10ha_lakes').getOutput(0)) total_area = 0 with arcpy.da.SearchCursor( 'these_4ha_lakes', ['AreaSqKm']) as area10_cursor: for area10_row in area10_cursor: total_area += area10_row[0] * 100 row[4] = total_area cursor.updateRow(row) # delete intermediates before next iteration for item in [ 'this_lake', 'this_lake_jxns', 'upstream', 'these_4ha_lakes', 'these_10ha_lakes' ]: try: arcpy.Delete_management(item) except: continue # clean up the output table all_fields = [f.name for f in arcpy.ListFields('output_table')] for f in all_fields: if f not in ['Permanent_Identifier'] + new_fields: try: arcpy.DeleteField_management('output_table', f) except: continue # write out the final file and clean up intermediates arcpy.CopyRows_management('output_table', output_table) for item in [ 'junctions', 'gte_4ha_lakes', 'gte_10ha_lakes', 'output_table' ]: arcpy.Delete_management(item)
def classify_lake_connectivity(nhd, out_feature_class, exclude_intermit_flowlines=False, debug_mode=False): if debug_mode: arcpy.env.overwriteOutput = True temp_gdb = cu.create_temp_GDB('classify_lake_connectivity') arcpy.env.workspace = temp_gdb arcpy.AddMessage('Debugging workspace located at {}'.format(temp_gdb)) else: arcpy.env.workspace = 'in_memory' layers_list = [] temp_feature_class = "in_memory/temp_fc" # Local variables: nhdflowline = os.path.join(nhd, "Hydrography", "NHDFLowline") nhdjunction = os.path.join(nhd, "Hydrography", "HYDRO_NET_Junctions") nhdwaterbody = os.path.join(nhd, "Hydrography", "NHDWaterbody") network = os.path.join(nhd, "Hydrography", "HYDRO_NET") # Get lakes, ponds and reservoirs over a hectare. csi_population_filter = '''"AreaSqKm" >=0.01 AND\ "FCode" IN (39000,39004,39009,39010,39011,39012,43600,43613,43615,43617,43618,43619,43621)''' all_lakes_reservoirs_filter = '''"FType" IN (390, 436)''' # Can't see why we shouldn't just attribute all lakes and reservoirs # arcpy.Select_analysis(nhdwaterbody, "csiwaterbody", lake_population_filter) arcpy.AddMessage("Initializing output.") if exclude_intermit_flowlines: arcpy.CopyFeatures_management(out_feature_class, temp_feature_class) else: arcpy.Select_analysis(nhdwaterbody, temp_feature_class, all_lakes_reservoirs_filter) # Get lakes, ponds and reservoirs over 10 hectares. lakes_10ha_filter = '''"AreaSqKm" >= 0.1 AND "FType" IN (390, 436)''' arcpy.Select_analysis(nhdwaterbody, "csiwaterbody_10ha", lakes_10ha_filter) # Exclude intermittent flowlines, if requested if exclude_intermit_flowlines: flowline_where_clause = '''"FCode" NOT IN (46003,46007)''' nhdflowline = arcpy.Select_analysis(nhdflowline, "nhdflowline_filtered", flowline_where_clause) # Make dangle points at end of nhdflowline arcpy.FeatureVerticesToPoints_management(nhdflowline, "dangles", "DANGLE") layers_list.append( arcpy.MakeFeatureLayer_management("dangles", "dangles_lyr")) # Isolate start dangles from end dangles. arcpy.FeatureVerticesToPoints_management(nhdflowline, "start", "START") arcpy.FeatureVerticesToPoints_management(nhdflowline, "end", "END") arcpy.SelectLayerByLocation_management("dangles_lyr", "ARE_IDENTICAL_TO", "start") arcpy.CopyFeatures_management("dangles_lyr", "startdangles") arcpy.SelectLayerByLocation_management("dangles_lyr", "ARE_IDENTICAL_TO", "end") arcpy.CopyFeatures_management("dangles_lyr", "enddangles") # Special handling for lakes that have some intermittent flow in and some permanent if exclude_intermit_flowlines: layers_list.append( arcpy.MakeFeatureLayer_management(nhdflowline, "nhdflowline_lyr")) arcpy.SelectLayerByAttribute_management( "nhdflowline_lyr", "NEW_SELECTION", '''"WBArea_Permanent_Identifier" is null''') arcpy.FeatureVerticesToPoints_management("nhdflowline_lyr", "non_artificial_end", "END") arcpy.SelectLayerByAttribute_management("nhdflowline_lyr", "CLEAR_SELECTION") arcpy.AddMessage("Found source area nodes.") # Get junctions from lakes >= 10 hectares. layers_list.append( arcpy.MakeFeatureLayer_management(nhdjunction, "junction_lyr")) arcpy.SelectLayerByLocation_management("junction_lyr", "INTERSECT", "csiwaterbody_10ha", XY_TOLERANCE, "NEW_SELECTION") arcpy.CopyFeatures_management("junction_lyr", "flags_10ha_lake_junctions") arcpy.AddMessage("Found lakes >= 10 ha.") # Make points shapefile and layer at flowline vertices to act as potential flags and/or barriers. arcpy.AddMessage("Tracing...") arcpy.FeatureVerticesToPoints_management(nhdflowline, "midvertices", "MID") layers_list.append( arcpy.MakeFeatureLayer_management("midvertices", "midvertices_lyr")) # Get vertices that are not coincident with 10 hectare lake junctions. arcpy.SelectLayerByLocation_management("midvertices_lyr", "INTERSECT", "flags_10ha_lake_junctions", "", "NEW_SELECTION") arcpy.SelectLayerByLocation_management("midvertices_lyr", "INTERSECT", "flags_10ha_lake_junctions", "", "SWITCH_SELECTION") arcpy.CopyFeatures_management("midvertices_lyr", "non10vertices") # Get junctions that are not coincident with 10 hectare lake junctions. arcpy.SelectLayerByLocation_management("junction_lyr", "INTERSECT", "flags_10ha_lake_junctions", "", "NEW_SELECTION") arcpy.SelectLayerByLocation_management("junction_lyr", "INTERSECT", "flags_10ha_lake_junctions", "", "SWITCH_SELECTION") arcpy.CopyFeatures_management("junction_lyr", "non10junctions") # Merge non10vertices with non10junctions arcpy.Merge_management( ["non10junctions", "non10vertices"], "all_non_flag_points") # inputs both point fc in_memory layers_list.append( arcpy.MakeFeatureLayer_management("all_non_flag_points", "all_non_flag_points_lyr")) # Tests the counts...for some reason I'm not getting stable behavior from the merge. mid_n = int(arcpy.GetCount_management("non10vertices").getOutput(0)) jxn_n = int(arcpy.GetCount_management("non10junctions").getOutput(0)) merge_n = int( arcpy.GetCount_management("all_non_flag_points").getOutput(0)) if merge_n < mid_n + jxn_n: arcpy.AddWarning( "The total number of flags ({0}) is less than the sum of the input junctions ({1}) " "and input midpoints ({2})".format(merge_n, jxn_n, mid_n)) # For tracing barriers, select all_non_flag_points points that intersect a 10 ha lake. arcpy.SelectLayerByLocation_management("all_non_flag_points_lyr", "INTERSECT", "csiwaterbody_10ha", XY_TOLERANCE, "NEW_SELECTION") arcpy.CopyFeatures_management("all_non_flag_points_lyr", "barriers") # Trace1-Trace downstream to first barrier (junctions+midvertices in 10 ha lake) starting from flags_10ha_lake_junctions flag points. arcpy.TraceGeometricNetwork_management(network, "trace1", "flags_10ha_lake_junctions", "TRACE_DOWNSTREAM", "barriers") # Save trace1 flowlines and junctions to layers on disk. arcpy.CopyFeatures_management("trace1\HYDRO_NET_Junctions", "trace1_junctions") # extra for debugging arcpy.CopyFeatures_management("trace1\NHDFlowline", "trace1_flowline") # Select vertice midpoints that intersect trace1 flowlines selection for new flags for trace2. layers_list.append( arcpy.MakeFeatureLayer_management("non10vertices", "non10vertices_lyr")) arcpy.SelectLayerByLocation_management("non10vertices_lyr", "INTERSECT", "trace1_flowline", "", "NEW_SELECTION") # Trace2-Trace downstream from midpoints of flowlines that intersect the selected flowlines from trace1. arcpy.TraceGeometricNetwork_management(network, "trace2", "non10vertices_lyr", "TRACE_DOWNSTREAM") # Save trace1 flowlines and junctions to layers and then shapes on disk. arcpy.CopyFeatures_management("trace2\HYDRO_NET_Junctions", "trace2junctions") arcpy.CopyFeatures_management("trace2\NHDFlowline", "trace2_flowline") # extra for debugging arcpy.AddMessage("Done tracing.") # Make shapefile for seepage lakes. (Ones that don't intersect flowlines) if exclude_intermit_flowlines: class_field_name = "Permanent_Lake_Connectivity" else: class_field_name = "Maximum_Lake_Connectivity" arcpy.AddField_management(temp_feature_class, class_field_name, "TEXT", field_length=13) layers_list.append( arcpy.MakeFeatureLayer_management(temp_feature_class, "out_fc_lyr")) arcpy.SelectLayerByLocation_management("out_fc_lyr", "INTERSECT", nhdflowline, XY_TOLERANCE, "NEW_SELECTION") arcpy.SelectLayerByLocation_management("out_fc_lyr", "INTERSECT", nhdflowline, "", "SWITCH_SELECTION") arcpy.CalculateField_management("out_fc_lyr", class_field_name, """'Isolated'""", "PYTHON") # New type of "Isolated" classification, mostly for "permanent" but there were some oddballs in "maximum" too arcpy.SelectLayerByLocation_management("out_fc_lyr", "INTERSECT", "startdangles", XY_TOLERANCE, "NEW_SELECTION") arcpy.SelectLayerByLocation_management("out_fc_lyr", "INTERSECT", "enddangles", XY_TOLERANCE, "SUBSET_SELECTION") arcpy.CalculateField_management("out_fc_lyr", class_field_name, """'Isolated'""", "PYTHON") # Get headwater lakes. arcpy.SelectLayerByLocation_management("out_fc_lyr", "INTERSECT", "startdangles", XY_TOLERANCE, "NEW_SELECTION") arcpy.SelectLayerByAttribute_management( "out_fc_lyr", "REMOVE_FROM_SELECTION", '''"{}" = 'Isolated' '''.format(class_field_name)) arcpy.CalculateField_management("out_fc_lyr", class_field_name, """'Headwater'""", "PYTHON") # Select csiwaterbody that intersect trace2junctions arcpy.AddMessage("Beginning connectivity attribution...") arcpy.SelectLayerByLocation_management("out_fc_lyr", "INTERSECT", "trace2junctions", XY_TOLERANCE, "NEW_SELECTION") arcpy.CalculateField_management("out_fc_lyr", class_field_name, """'DR_LakeStream'""", "PYTHON") # Get stream drainage lakes. Either unassigned so far or convert "Headwater" if a permanent stream flows into it, # which is detected with "non_artificial_end" arcpy.SelectLayerByAttribute_management( "out_fc_lyr", "NEW_SELECTION", '''"{}" IS NULL'''.format(class_field_name)) arcpy.CalculateField_management("out_fc_lyr", class_field_name, """'DR_Stream'""", "PYTHON") if exclude_intermit_flowlines: arcpy.SelectLayerByAttribute_management( "out_fc_lyr", "NEW_SELECTION", '''"{}" = 'Headwater' '''.format(class_field_name)) arcpy.SelectLayerByLocation_management("out_fc_lyr", "INTERSECT", "non_artificial_end", XY_TOLERANCE, "SUBSET_SELECTION") arcpy.CalculateField_management("out_fc_lyr", class_field_name, """'DR_Stream'""", "PYTHON") # Prevent 'upgrades' due to very odd flow situations, better to stick with calling both classes 'Headwater' arcpy.SelectLayerByAttribute_management( "out_fc_lyr", "NEW_SELECTION", '''"Maximum_Lake_Connectivity" = 'Headwater' AND "Permanent_Lake_Connectivity" = 'DR_Stream' ''' ) arcpy.CalculateField_management("out_fc_lyr", class_field_name, """'Headwater'""", "PYTHON") # Project output once done with both. Switching CRS earlier causes trace problems. if not exclude_intermit_flowlines: arcpy.CopyFeatures_management(temp_feature_class, out_feature_class) else: arcpy.Project_management(temp_feature_class, out_feature_class, arcpy.SpatialReference(102039)) # Clean up for layer in layers_list: arcpy.Delete_management(layer) else: arcpy.Delete_management("in_memory") arcpy.AddMessage("{} classification is complete.".format(class_field_name))
def aggregate_watersheds(watersheds_fc, nhd_gdb, eligible_lakes, output_fc, mode=['interlake', 'cumulative']): """Creates a feature class with all the aggregated upstream watersheds for all eligible lakes (>4ha and certain FCodes) in this subregion.""" arcpy.env.workspace = 'in_memory' # names huc4_code = re.search('\d{4}', os.path.basename(nhd_gdb)).group() nhd_waterbody = os.path.join(nhd_gdb, 'NHDWaterbody') hydro_net_junctions = os.path.join(nhd_gdb, 'Hydrography', 'HYDRO_NET_Junctions') hydro_net = os.path.join(nhd_gdb, 'Hydrography', 'HYDRO_NET') # get this hu4 wbd_hu4 = os.path.join(nhd_gdb, "WBD_HU4") field_name = (arcpy.ListFields(wbd_hu4, "HU*4"))[0].name whereClause4 = """{0} = '{1}'""".format( arcpy.AddFieldDelimiters(nhd_gdb, field_name), huc4_code) arcpy.Select_analysis(wbd_hu4, "hu4", whereClause4) # make layers for upcoming spatial selections # and fcs in memory arcpy.MakeFeatureLayer_management(hydro_net_junctions, "junctions") arcpy.MakeFeatureLayer_management(watersheds_fc, 'watersheds') all_lakes = eligible_lakes arcpy.MakeFeatureLayer_management(all_lakes, "all_lakes_lyr") ## arcpy.SelectLayerByLocation_management("all_lakes_lyr", "INTERSECT", "hu8") arcpy.CopyFeatures_management("all_lakes_lyr", 'eligible_lakes') # ten ha lakes and junctions if mode == 'interlake': tenha_where_clause = """"AreaSqKm" >= .1""" arcpy.Select_analysis("eligible_lakes", 'tenha_lakes', tenha_where_clause) arcpy.MakeFeatureLayer_management('tenha_lakes', 'tenha_lakes_lyr') arcpy.SelectLayerByLocation_management('junctions', 'INTERSECT', 'tenha_lakes', search_distance="1 Meters") arcpy.CopyFeatures_management('junctions', 'tenha_junctions') arcpy.MakeFeatureLayer_management('tenha_junctions', 'tenha_junctions_lyr') # for each lake, calculate its interlake watershed in the upcoming block prog_count = int(arcpy.GetCount_management('eligible_lakes').getOutput(0)) counter = 0 with arcpy.da.SearchCursor('eligible_lakes', ["Permanent_Identifier"]) as cursor: for row in cursor: counter += 1 if counter % 50 == 0: print("{0} out of {1} lakes completed.".format( counter, prog_count)) id = row[0] where_clause = """"{0}" = '{1}'""".format("Permanent_Identifier", id) arcpy.MakeFeatureLayer_management('eligible_lakes', "this_lake", where_clause) arcpy.SelectLayerByLocation_management("junctions", "INTERSECT", "this_lake", search_distance="1 Meters") count_jxns = int( arcpy.GetCount_management('junctions').getOutput(0)) if count_jxns == 0: arcpy.SelectLayerByLocation_management('watersheds', 'CONTAINS', 'this_lake') else: arcpy.CopyFeatures_management("junctions", 'this_lake_jxns') if mode == 'interlake': arcpy.SelectLayerByLocation_management( 'tenha_junctions_lyr', 'ARE_IDENTICAL_TO', 'this_lake_jxns') arcpy.SelectLayerByAttribute_management( 'tenha_junctions_lyr', 'SWITCH_SELECTION') arcpy.CopyFeatures_management('tenha_junctions_lyr', 'other_tenha_junctions') arcpy.SelectLayerByLocation_management( 'tenha_lakes_lyr', 'INTERSECT', 'other_tenha_junctions', search_distance='1 Meters') arcpy.TraceGeometricNetwork_management( hydro_net, "upstream", 'this_lake_jxns', "TRACE_UPSTREAM", in_barriers='other_tenha_junctions') elif mode == 'cumulative': arcpy.TraceGeometricNetwork_management( hydro_net, "upstream", 'this_lake_jxns', "TRACE_UPSTREAM") arcpy.SelectLayerByLocation_management("watersheds", "CONTAINS", "upstream/NHDFlowline") arcpy.SelectLayerByLocation_management( "watersheds", 'CROSSED_BY_THE_OUTLINE_OF', 'upstream/NHDFLowline', selection_type="ADD_TO_SELECTION") watersheds_count = int( arcpy.GetCount_management("watersheds").getOutput(0)) if watersheds_count == 0: arcpy.SelectLayerByLocation_management( 'watersheds', 'CONTAINS', 'this_lake') # Sometimes when the trace stops at 10-ha lake, that shed(s) # gets selected. Remove them with the tenha_lakes_lyr # that already has only OTHER lakes selected # using other_tenha_junctions causes some stuff to be picked up # that shouldn't be when junctions are right on boundaries if mode == 'interlake': arcpy.SelectLayerByLocation_management( "watersheds", "CONTAINS", "tenha_lakes_lyr", selection_type="REMOVE_FROM_SELECTION") arcpy.Dissolve_management("watersheds", "this_watershed") arcpy.AddField_management("this_watershed", 'Permanent_Identifier', 'TEXT', field_length=255) arcpy.CalculateField_management("this_watershed", "Permanent_Identifier", """'{}'""".format(id), "PYTHON") arcpy.Erase_analysis('this_watershed', 'this_lake', 'lakeless_watershed') if not arcpy.Exists("output_fc"): arcpy.CopyFeatures_management('lakeless_watershed', "output_fc") # to avoid append mismatch due to permanent_identifier cu.lengthen_field("output_fc", 'Permanent_Identifier', 255) else: arcpy.Append_management('lakeless_watershed', "output_fc", 'NO_TEST') for item in [ 'this_lake', 'this_watershed', 'this_lake_jxns', 'upstream', 'lakeless_watershed', 'other_tenha_junctions' ]: try: arcpy.Delete_management(item) except: continue arcpy.EliminatePolygonPart_management("output_fc", "output_hole_remove", "AREA", "3.9 Hectares", "0", "CONTAINED_ONLY") arcpy.Clip_analysis("output_hole_remove", "hu4", output_fc) arcpy.Delete_management('output_fc') arcpy.ResetEnvironments()
def cumulative_watersheds(nhd, watersheds, topoutfolder, filterlakes): # Naming Convention subregion_number = os.path.basename(nhd) subregion = subregion_number[4:8] outfolder = os.path.join(topoutfolder, subregion + "CWS") if not os.path.exists(outfolder): os.mkdir(outfolder) # Projections: nad83 = arcpy.SpatialReference(4269) albers = arcpy.SpatialReference(102039) # NHD variables: flowline = os.path.join(nhd, "Hydrography", "NHDFlowline") waterbody = os.path.join(nhd, "Hydrography", "NHDWaterbody") network = os.path.join(nhd, "Hydrography", "HYDRO_NET") junction = os.path.join(nhd, "Hydrography", "HYDRO_NET_Junctions") arcpy.env.extent = waterbody # Make shapefiles for one hectare and ten hectare lakes that intersect flowlines. arcpy.FeatureClassToShapefile_conversion(waterbody, outfolder) waterbodyshp = os.path.join(outfolder, "NHDWaterbody.shp") waterbody_lyr = os.path.join(outfolder, "waterbody.lyr") arcpy.MakeFeatureLayer_management(waterbodyshp, waterbody_lyr) arcpy.SelectLayerByAttribute_management(waterbody_lyr, "NEW_SELECTION", '''"AreaSqKm">=0.04''') fcodes = (39000, 39004, 39009, 39010, 39011, 39012, 43600, 43613, 43615, 43617, 43618, 43619, 43621) whereClause = '''("AreaSqKm" >=0.04 AND "FCode" IN %s) OR ("FCode" = 43601 AND "AreqSqKm" >= 0.1)''' % ( fcodes, ) ## whereClause = '''"AreaSqKm" >=0.04 AND ("FCode" = 39000 OR "FCode" = 39004 OR\ ## "FCode" = 39009 OR "FCode" = 39010 OR "FCode" = 39011 OR "FCode" = 39012 OR "FCode" = 43600 OR "FCode" = 43613 OR\ ## "FCode" = 43615 OR "FCode" = 43617 OR "FCode" = 43618 OR "FCode" = 43619 OR "FCode" = 43621 OR ("FCode" = 43601 AND "AreaSqKm" >=0.1 ))''' arcpy.SelectLayerByAttribute_management(waterbody_lyr, "SUBSET_SELECTION", whereClause) all4ha = os.path.join(outfolder, "all4ha.shp") arcpy.CopyFeatures_management(waterbody_lyr, all4ha) arcpy.SelectLayerByLocation_management(waterbody_lyr, "INTERSECT", flowline, "", "SUBSET_SELECTION") try: filtershp = os.path.join(outfolder, "filter.shp") arcpy.Project_management(filterlakes, filtershp, nad83, '', albers) arcpy.SelectLayerByLocation_management(waterbody_lyr, "INTERSECT", filtershp, '', "SUBSET_SELECTION") except: pass fourha = os.path.join(outfolder, "fourha.shp") arcpy.CopyFeatures_management(waterbody_lyr, fourha) fourha_lyr = os.path.join(outfolder, "fourha.lyr") arcpy.MakeFeatureLayer_management(fourha, fourha_lyr) # Make shapefiles of junctions that intersect one hectare and ten hectare lakes. junction_lyr = os.path.join(outfolder, "junction.lyr") arcpy.MakeFeatureLayer_management(junction, junction_lyr) arcpy.SelectLayerByLocation_management(junction_lyr, "INTERSECT", fourha, '', "NEW_SELECTION") fourhajunction = os.path.join(outfolder, "fourhajunction.shp") arcpy.CopyFeatures_management(junction_lyr, fourhajunction) # Split lakes. arcpy.AddField_management(fourha, "ID", "TEXT") arcpy.CalculateField_management(fourha, "ID", '''"%s" % (!FID!)''', "PYTHON") arcpy.AddField_management(all4ha, "ID", "TEXT") arcpy.CalculateField_management(all4ha, "ID", '''"%s" % (!FID!)''', "PYTHON") lakes = os.path.join(outfolder, "lakes") if not os.path.exists(lakes): os.mkdir(lakes) arcpy.Split_analysis(all4ha, all4ha, "ID", lakes) # Iterate tracing. arcpy.env.workspace = lakes watersheds_lyr = os.path.join(outfolder, "watersheds.lyr") arcpy.MakeFeatureLayer_management(watersheds, watersheds_lyr) fcs = arcpy.ListFeatureClasses() fourhajunction_lyr = os.path.join(outfolder, "fourhajunction.lyr") arcpy.MakeFeatureLayer_management(fourhajunction, fourhajunction_lyr) # Create folder for final output cws = os.path.join(outfolder, "CWS") if not os.path.exists(cws): os.mkdir(cws) arcpy.AddMessage("Starting iteration.") for fc in fcs: arcpy.RefreshCatalog(outfolder) name = os.path.splitext(fc)[0] arcpy.AddMessage("Processing " + name + ".") # Sets the output to in memory: lakes = "in_memory" # Repair the lake geometery if needed. arcpy.RepairGeometry_management(fc) # Make sure the lake's own watershed gets added (merged) back in to the final aggregated watershed: # Make a centroid for the lake, then intersect it with watersheds, then merge it with the previous sheds made above. center = os.path.join(lakes, "center" + name) arcpy.FeatureToPoint_management(fc, center, "INSIDE") arcpy.SelectLayerByLocation_management(watersheds_lyr, "INTERSECT", center, '', "NEW_SELECTION") ownshed = os.path.join(lakes, "ownshed" + name) arcpy.CopyFeatures_management(watersheds_lyr, ownshed) # Select 4 hectare lake junctions that do intersect it. arcpy.SelectLayerByLocation_management(fourhajunction_lyr, "INTERSECT", fc, '', "NEW_SELECTION") # Copy junctions lakejunction = os.path.join(lakes, "junct" + name) arcpy.CopyFeatures_management(fourhajunction_lyr, lakejunction) try: # Trace the network upstream from the junctions from above. arcpy.TraceGeometricNetwork_management( network, os.path.join(lakes, "im" + name + "tracelyr"), lakejunction, "TRACE_UPSTREAM") trace = os.path.join(lakes, "im" + name + "tracelyr", "NHDFlowline") # Write the trace traceshp = os.path.join(lakes, "im" + name + "trace") arcpy.CopyFeatures_management(trace, traceshp) # Make a layer from the trace tracesel = os.path.join(lakes, "im" + name + "tracesellyr") arcpy.MakeFeatureLayer_management(traceshp, tracesel) # Select from the trace lines those that don't have their midpoint in the lake arcpy.SelectLayerByLocation_management(tracesel, "HAVE_THEIR_CENTER_IN", fc, '', "NEW_SELECTION") arcpy.SelectLayerByLocation_management(tracesel, "HAVE_THEIR_CENTER_IN", fc, '', "SWITCH_SELECTION") # Select watersheds that intersect the trace arcpy.SelectLayerByLocation_management(watersheds_lyr, "INTERSECT", tracesel, '', "NEW_SELECTION") sheds = os.path.join(lakes, "im" + name + "sheds") arcpy.CopyFeatures_management(watersheds_lyr, sheds) sheds_lyr = os.path.join(lakes, "im" + name + "shedslyr") arcpy.MakeFeatureLayer_management(sheds, sheds_lyr) except: arcpy.AddMessage("Isolated shed.") sheds3 = os.path.join(lakes, "sheds3" + name) try: arcpy.Merge_management([sheds, ownshed], sheds3) except: arcpy.CopyFeatures_management(ownshed, sheds3) # Dissolve the aggregate watershed if it has more than one polygon polynumber = int(arcpy.GetCount_management(sheds3).getOutput(0)) pre = os.path.join(lakes, "pre" + name) if polynumber > 1: arcpy.AddField_management(sheds3, "Dissolve", "TEXT") arcpy.CalculateField_management(sheds3, "Dissolve", "1", "PYTHON") arcpy.Dissolve_management(sheds3, pre) elif polynumber < 2: arcpy.CopyFeatures_management(sheds3, pre) # Get the permanent id from the feature and add it to output shed field = "Permanent_" cursor = arcpy.SearchCursor(fc) for row in cursor: id = row.getValue(field) arcpy.AddField_management(pre, "NHD_ID", "TEXT") arcpy.CalculateField_management(pre, "NHD_ID", '"{0}"'.format(id), "PYTHON") # Erase the lakes own geometry from its watershed arcpy.Erase_analysis(pre, fc, os.path.join(CWS, "CWS" + name + ".shp")) # Delete intermediate in_memory fcs and variables temp_items = [ lakejunction, trace, traceshp, tracesel, sheds, sheds_lyr, center, sheds2, sheds3, pre, fc, ownshed ] cu.cleanup(temp_items)
def transport_spatial_validation(estamap_version, with_patch=False): logging.info('environment') em = gis.ESTAMAP(estamap_version) arcpy.env.XYTolerance = '0.01 Meters' fgdb_name = 'TransportSpatialValidation_{}.gdb'.format('withPatch' if with_patch else 'exclPatch') fgdb = os.path.join(em.path, 'Routing', fgdb_name) if not os.path.exists(os.path.join(em.path, 'Routing')): logging.info('creating Routing folder: {}'.format(os.path.join(em.path, 'Routing'))) os.makedirs(os.path.join(em.path, 'Routing')) logging.info('creating fgdb: {}'.format(os.path.join(em.path, 'Routing', fgdb_name))) if arcpy.Exists(os.path.join(em.path, 'Routing', fgdb_name)): arcpy.Delete_management(os.path.join(em.path, 'Routing', fgdb_name)) arcpy.CreateFileGDB_management(out_folder_path=os.path.join(em.path, 'Routing'), out_name=fgdb_name) logging.info('creating feature dataset') arcpy.CreateFeatureDataset_management(out_dataset_path=os.path.join(em.path, 'Routing', fgdb_name), out_name='FD', spatial_reference=arcpy.SpatialReference(3111)) logging.info('importing ROAD') arcpy.FeatureClassToFeatureClass_conversion(in_features=os.path.join(em.sde, 'ROAD'), out_path=os.path.join(em.path, 'Routing', fgdb_name, 'FD'), out_name='ROAD') road_count = arcpy.GetCount_management(os.path.join(em.path, 'Routing', fgdb_name, 'FD', 'ROAD')).getOutput(0) logging.info('ROAD records: {}'.format(road_count)) logging.info('importing ROAD_INFRASTRUCTURE') arcpy.FeatureClassToFeatureClass_conversion(in_features=os.path.join(em.sde, 'ROAD_INFRASTRUCTURE'), out_path=os.path.join(em.path, 'Routing', fgdb_name, 'FD'), out_name='ROAD_INFRASTRUCTURE') road_infrastructure_count = arcpy.GetCount_management(os.path.join(em.path, 'Routing', fgdb_name, 'FD', 'ROAD_INFRASTRUCTURE')).getOutput(0) logging.info('ROAD_INFRASTRUCTURE records: {}'.format(road_infrastructure_count)) in_source_feature_classes = 'ROAD SIMPLE_EDGE NO;ROAD_INFRASTRUCTURE SIMPLE_JUNCTION NO' if with_patch: in_source_feature_classes = in_source_feature_classes + ';ROAD_PATCH SIMPLE_EDGE NO' logging.info('importing ROAD_PATCH') arcpy.FeatureClassToFeatureClass_conversion(in_features=os.path.join(em.sde, 'ROAD_PATCH'), out_path=os.path.join(em.path, 'Routing', fgdb_name, 'FD'), out_name='ROAD_PATCH', where_clause='"MERGE_TRANSPORT" = 1') road_patch_count = arcpy.GetCount_management(os.path.join(em.path, 'Routing', fgdb_name, 'FD', 'ROAD_PATCH')).getOutput(0) logging.info('ROAD_PATCH records: {}'.format(road_patch_count)) ## logging.info('importing ROAD_INFRASTRUCTURE_PATCH') ## arcpy.MakeFeatureLayer_management(in_features=os.path.join(em.sde, 'ROAD_INFRASTRUCTURE_PATCH'), ## out_layer='in_memory\\road_infrastructure_patch_layer', ## where_clause='"MERGE_TRANSPORT" = 1') ## arcpy.FeatureClassToFeatureClass_conversion(in_features='in_memory\\road_infrastructure_patch_layer', ## out_path=os.path.join(em.path, 'Routing', fgdb_name, 'FD'), ## out_name='ROAD_INFRASTRUCTURE_PATCH') logging.info('creating geometric network: {}'.format(os.path.join(em.path, 'Routing', fgdb_name, 'FD', 'geonet'))) arcpy.CreateGeometricNetwork_management(in_feature_dataset=os.path.join(em.path, 'Routing', fgdb_name, 'FD'), out_name='geonet', in_source_feature_classes=in_source_feature_classes, preserve_enabled_values='PRESERVE_ENABLED') ## arcpy.AddEdgeEdgeConnectivityRuleToGeometricNetwork_management(in_geometric_network=os.path.join(em.path, 'Routing', fgdb_name, 'FD', 'geonet'), ## in_from_edge_feature_class="ROAD", ## from_edge_subtype="ROAD", ## in_to_edge_feature_class="ROAD", ## to_edge_subtype="ROAD", ## in_junction_subtypes="'ROAD_INFRASTRUCTURE : ROAD_INFRASTRUCTURE'", ## default_junction_subtype="ROAD_INFRASTRUCTURE : ROAD_INFRASTRUCTURE") ## ## arcpy.AddEdgeJunctionConnectivityRuleToGeometricNetwork_management(in_geometric_network=os.path.join(em.path, 'Routing', fgdb_name, 'FD', 'geonet'), ## in_edge_feature_class="ROAD", ## edge_subtype="ROAD", ## in_junction_feature_class="ROAD_INFRASTRUCTURE", ## junction_subtype="ROAD_INFRASTRUCTURE", ## default_junction="DEFAULT", ## edge_min="", ## edge_max="", ## junction_min="", ## junction_max="") ## ## # DC by: find dc function ## ## logging.info('finding disconnected: ROAD') ## arcpy.FindDisconnectedFeaturesInGeometricNetwork_management(in_layer=os.path.join(em.path, 'Routing', fgdb_name, 'FD', 'ROAD'), ## out_layer='in_memory\\ROAD_disconnected') ## if arcpy.Exists('in_memory\\ROAD_disconnected'): ## fdc_road_count = arcpy.GetCount_management('in_memory\\ROAD_disconnected').getOutput(0) ## logging.info('found ROAD disconnected: {}'.format(fdc_road_count)) ## ## arcpy.FeatureClassToFeatureClass_conversion(in_features='in_memory\\ROAD_disconnected', ## out_path=os.path.join(em.path, 'Routing', fgdb_name), ## out_name='ROAD_disconnected') ## ## logging.info('finding disconnected: ROAD_INFRASTRUCTURE') ## arcpy.FindDisconnectedFeaturesInGeometricNetwork_management(in_layer=os.path.join(em.path, 'Routing', fgdb_name, 'FD', 'ROAD_INFRASTRUCTURE'), ## out_layer='in_memory\\ROAD_INFRASTRUCTURE_disconnected') ## if arcpy.Exists('in_memory\\ROAD_INFRASTRUCTURE_disconnected'): ## fdc_road_infrastructure_count = arcpy.GetCount_management('in_memory\\ROAD_INFRASTRUCTURE_disconnected').getOutput(0) ## logging.info('found ROAD_INFRASTRUCTURE disconnected: {}'.format(fdc_road_infrastructure_count)) ## ## arcpy.FeatureClassToFeatureClass_conversion(in_features='in_memory\\ROAD_INFRASTRUCTURE_disconnected', ## out_path=os.path.join(em.path, 'Routing', fgdb_name), ## out_name='ROAD_INFRASTRUCTURE_disconnected') # DC by: tracing logging.info('creating starting point') arcpy.CreateFeatureclass_management(out_path=os.path.join(em.path, 'Routing', fgdb_name), out_name='StartingPoint', geometry_type='POINT', spatial_reference=arcpy.SpatialReference(3111)) with arcpy.da.InsertCursor(in_table=os.path.join(em.path, 'Routing', fgdb_name, 'StartingPoint'), field_names=['SHAPE@']) as ic: pt = arcpy.Point(2497133.064, 2409284.931) ic.insertRow((pt,)) logging.info('Tracing Geometric Network...') arcpy.TraceGeometricNetwork_management(in_geometric_network=os.path.join(em.path, 'Routing', fgdb_name, 'FD', 'geonet'), out_network_layer='in_memory\\geonet_trace_output', in_flags=os.path.join(em.path, 'Routing', fgdb_name, 'StartingPoint'), in_trace_task_type='FIND_DISCONNECTED', in_trace_ends='NO_TRACE_ENDS', in_trace_indeterminate_flow='NO_TRACE_INDETERMINATE_FLOW', in_junction_weight_range_not='AS_IS', in_edge_weight_range_not='AS_IS') logging.info('Trace Complete. Checking counts and export.') group_layer = arcpy.mapping.Layer('in_memory\\geonet_trace_output') for layer in arcpy.mapping.ListLayers(group_layer): if not layer.isGroupLayer: layer_count = arcpy.GetCount_management(layer).getOutput(0) if int(layer_count) > 0: logging.info('found disconnected in layer: {}'.format(layer.name)) logging.info('count disconnected: {}'.format(layer_count)) arcpy.FeatureClassToFeatureClass_conversion(in_features=layer, out_path=os.path.join(em.path, 'Routing', fgdb_name), out_name=layer.name + '_disconnected_ALL') if arcpy.Exists(os.path.join(em.path, 'Routing', fgdb_name, 'ROAD_disconnected_ALL')): logging.info('Export ROAD disconnected UNNAMED') arcpy.FeatureClassToFeatureClass_conversion(in_features=os.path.join(em.path, 'Routing', fgdb_name, 'ROAD_disconnected_ALL'), out_path=os.path.join(em.path, 'Routing', fgdb_name), out_name='ROAD_disconnected_UNNAMED', where_clause="ROAD_NAME = 'UNNAMED'") logging.info('Export ROAD disconnected NAMED Victoria') arcpy.FeatureClassToFeatureClass_conversion(in_features=os.path.join(em.path, 'Routing', fgdb_name, 'ROAD_disconnected_ALL'), out_path=os.path.join(em.path, 'Routing', fgdb_name), out_name='ROAD_disconnected_NAMED_VIC', where_clause='''ROAD_NAME <> 'UNNAMED' and ( (LEFT_LOCALITY not like '%(NSW)%' or RIGHT_LOCALITY not like '%(NSW)%') AND (LEFT_LOCALITY not like '%(SA)%' or RIGHT_LOCALITY not like '%(SA)%') ) ''') logging.info('Export ROAD disconnected NAMED Interstate') arcpy.FeatureClassToFeatureClass_conversion(in_features=os.path.join(em.path, 'Routing', fgdb_name, 'ROAD_disconnected_ALL'), out_path=os.path.join(em.path, 'Routing', fgdb_name), out_name='ROAD_disconnected_NAMED_INTERSTATE', where_clause='''ROAD_NAME <> 'UNNAMED' and ( (LEFT_LOCALITY like '%(NSW)%' or RIGHT_LOCALITY like '%(NSW)%') OR (LEFT_LOCALITY like '%(SA)%' or RIGHT_LOCALITY like '%(SA)%') ) ''') logging.info('clean up trace group layer') arcpy.Delete_management('in_memory\\geonet_trace_output')
Flag_Locations = pointflag arcpy.AddMessage(" snap is done ") try: gnVersionFDS_1 = "in_memory\\Storm_Net" SemnanDB_SDE_elec_Net = "address of you elec_Net" arcpy.TraceGeometricNetwork_management(SemnanDB_SDE_elec_Net, gnVersionFDS_1, Flag_Locations, Trace_Task_Type,"", "", "", "", "", "NO_TRACE_ENDS", "", "", "", "AS_IS", "", "", "", "AS_IS") network = arcpy.mapping.Layer(gnVersionFDS_1) count=0 for sublayer in network: count=count+1 arcpy.AddMessage("count layer {} ".format(count)) spal=None arcpy.AddMessage(" {} ".format("input In Script")) arcpy.AddMessage(" workspace is = " + arcpy.env.workspace) #FC = arcpy.CreateFeatureclass_management("in_memory",FeatureClass,"POLYLINE","","DISABLED","DISABLED",spatialRefCode)
flagFields = ['OID@', 'FlowTime'] with arcpy.da.UpdateCursor(flags, flagFields) as rows: for row in rows: # Create temporary variables for functions oid = row[0] newNet = 'SDN_Net' + str(oid) flag = 'flag' + str(oid) exp = '"OBJECTID" = ' + str(oid) # Create in memory feature layer of one catch basin arcpy.MakeFeatureLayer_management(flags, flag, exp) # Trace the network downstream arcpy.TraceGeometricNetwork_management( sdnNet, newNet, flag, "TRACE_DOWNSTREAM", "#", "#", "#", "#", "#", "NO_TRACE_ENDS", "NO_TRACE_INDETERMINATE_FLOW", "#", "#", "AS_IS", "#", "#", "#", "AS_IS") gMain = "GravityMain" latLine = "LateralLine" oChann = "OpenChannel" culvert = "Culvert" pseudoLine = "PseudoLine" # Create array of temporary paths for enumeration pathList = [gMain, latLine, oChann, culvert] # Create an empty array to be populated with paths to be merged later mergeList = [] # Create variable for total time
#For each segment, identify connected reaches with dams as barriers #Get start point of each reach arcpy.FeatureVerticesToPoints_management(outsnsplit, netp, point_location='START') segdic = defaultdict(list) #dictionary to contain segment ID as key and SUBREACH_ID of corresponding reaches as values #Iterate over each reach print('Find connected reaches...') #################### FUTURE EDIT TO DO: WRITE DIRECTLY DIC WITH KEYS AS SUBREACH_id AND VALUE AS SEGID TO MAKE IT FASTER - FINE FOR NOW with arcpy.da.SearchCursor(netp, ['SUBREACH_ID', 'SHAPE@']) as cursor: i=0 for row in cursor: if row[0] not in set(itertools.chain.from_iterable(segdic.values())): #If reach not already part of a segment print(i) #Select all connected reaches using dams a barriers (this includes stopping reaches where dam is though) arcpy.TraceGeometricNetwork_management(in_geometric_network= geonetout, out_network_layer = "connectlyr", in_flags = row[1], in_barriers= damsnap_editclean, in_trace_task_type= "FIND_CONNECTED", in_trace_ends = 'NO_TRACE_ENDS') connect_tr = "connectlyr/{}".format(os.path.split(outsnsplit)[1]) #Add reaches with barriers on them that are connected arcpy.TraceGeometricNetwork_management(in_geometric_network= geonetout, out_network_layer = "connectstoplyr", in_flags = row[1], in_barriers=damsnap_editclean, in_trace_task_type= "FIND_CONNECTED", in_trace_ends = 'TRACE_ENDS') connectstop_tr = "connectstoplyr/{}".format(os.path.split(outsnsplit)[1]) #Populate dictionary with SUBREACH_ID for that segment ID with arcpy.da.SearchCursor(connect_tr, ["SUBREACH_ID"]) as cursor_connect: for row_connect in cursor_connect: