Hi all, I'm needing your help once again with a code/script tool that I have been working on.
import arcpy
import os
arcpy.env.overwriteOutput = True
arcpy.env.workspace = r"C:\UpdateDevelopmentPython\ForDeveloping\ForDeveloping.gdb"
EnterWaterSysID = arcpy.GetParameterAsText(0).split(";")
Hydrant_FC = "Hydrants"
WaterSystems = "WaterSystems"
Streetmap = r"C:\UpdateDevelopmentPython\Streetmap Premium\FGDB\StreetMap_Data\NorthAmerica.gdb\Routing\Routing_ND"
qry = "{0} IN ({1})".format(arcpy.AddFieldDelimiters(datasource= Hydrant_FC, field = 'WaterSysID'), ', '.join(EnterWaterSysID))
arcpy.SelectLayerByAttribute_management(in_layer_or_view=Hydrant_FC, where_clause=qry)
arcpy.SelectLayerByLocation_management(Hydrant_FC,"Within",WaterSystems,None,"SUBSET_SELECTION","INVERT")
arcpy.env.workspace = r"C:\UpdateDevelopmentPython\ForDeveloping\ForDeveloping.gdb"
Service_Area = arcpy.na.MakeServiceAreaAnalysisLayer(network_data_source=Streetmap,
layer_name="Service Area",
travel_mode="Driving Distance",
travel_direction="FROM_FACILITIES",
cutoffs=[0.189394],
time_of_day="",
time_zone="LOCAL_TIME_AT_LOCATIONS",
output_type="LINES",
polygon_detail="STANDARD",
geometry_at_overlaps="SPLIT",
geometry_at_cutoffs="RINGS",
polygon_trim_distance="100 Meters",
exclude_sources_from_polygon_generation=[],
accumulate_attributes=["Miles"],
ignore_invalid_locations="SKIP")[0]
Updated_Service_Area = arcpy.na.AddLocations(in_network_analysis_layer=Service_Area,
sub_layer="Facilities",
in_table=Hydrant_FC,
field_mappings="",
search_tolerance="5000 Meters",
sort_field="",
search_criteria=[["Routing_Streets", "SHAPE"],
["Routing_Streets_Override", "NONE"],
["Routing_ND_Junctions", "NONE"]],
match_type="MATCH_TO_CLOSEST",
append="APPEND",
snap_to_position_along_network="NO_SNAP",
snap_offset="5 Meters",
exclude_restricted_elements="EXCLUDE",
search_query=[])[0]
Solve_Succeeded = arcpy.na.Solve(in_network_analysis_layer=Updated_Service_Area,
ignore_invalids="SKIP",
terminate_on_solve_error="TERMINATE",
simplification_tolerance="",
overrides="")
Lines = arcpy.Select_analysis("Lines","Lines_Selected")
arcpy.SelectLayerByAttribute_management(Hydrant_FC, "CLEAR_SELECTION")
Lines_Extracted = r"C:\UpdateDevelopmentPython\ForDeveloping\ForDeveloping.gdb\Lines_Selected"
p = arcpy.mp.ArcGISProject("CURRENT")
m = p.listMaps("Map")[0]
m.addDataFromPath(Lines_Extracted)
NewHydrantLines = arcpy.MakeFeatureLayer_management(Lines_Extracted,"HydrantLines")
outgdb = r"C:\UpdateDevelopmentPython\ForDeveloping\ForDeveloping.gdb\Lines_Selected_Joined"
arcpy.analysis.SpatialJoin("Lines_Selected",
"Hydrants",
outgdb,
"JOIN_ONE_TO_ONE",
"KEEP_COMMON",
'WaterSysID "WaterSysID" true true false 4 Long 0 0,First,#,Hydrants,WaterSysID,-1,-1',
"WITHIN_A_DISTANCE",
"1000 Feet", '')
Streets_Joined = r"C:\UpdateDevelopmentPython\ForDeveloping\ForDeveloping.gdb\Lines_Selected_Joined"
fieldName = "WaterSysID"
delimfield = arcpy.AddFieldDelimiters(Streets_Joined,fieldName)
sql = delimfield = EnterWaterSysID
with arcpy.da.SearchCursor(Streets_Joined,fieldName) as cursor:
for row in cursor:
arcpy.SelectLayerByAttribute_management(row,"NEW_SELECTION",sql)
After I perform the spatial join which is line 69 of my code I get an feature class called Lines_Selected_Joined with an attribute table that looks like the following.
This works great but now what I'm trying to do is generate individual feature classes based on the unique values found in the WaterSysID column.
The WaterSysID values that I want to extract from this one feature class should be the same ones that I enter in the tool using the GetParameterAsText function, found on line 7 of my code. In this case it would be for WaterSysIDs 4 and 159.
Any suggestions on how to go about this? Thanks all in advance for the help, I do appreciate it as I'm pretty green when it comes to Python.
Why don't you use
Split By Attributes (Analysis)—ArcGIS Pro | Documentation
to produce the featureclasses based on your unique classes in the field
Hi Don! That worked great, thank you so much for the help!!!
The only question I have left is how do I rename the new feature classes?
For example, instead of the feature class being called T4, I'd like to call it 4WS_Streets, or something along those lines. This was the code I used for the SplitByAttributes.
finalLines = r"C:\UpdateDevelopmentPython\ForDeveloping\NewHydrants.gdb"
fields = ["WaterSysID"]
arcpy.SplitByAttributes_analysis(Lines_Selected,finalLines,fields)
Rename (Data Management)—ArcGIS Pro | Documentation
or modify/emulate the code in the actual script located in....
C:\...Your_install_folder ...\Resources\ArcToolBox\Scripts\splitbyattribute.py