Select to view content in your preferred language

Help with Select by Select by Attribute and Get Parameter as Text

642
4
02-08-2022 11:14 AM
ColeNelson
Emerging Contributor

Hi I am trying to develop a custom tool that will create a network analysis layer/road network based on new fire hydrants in an area. 

Hydrants are assigned a WaterSysID based on the community/water system they are a part of.  As subdivisions are built new fire hydrant feature classes are created then appended to our main hydrant feature class.  We then use those new hydrants to generate a road network that will eventually be used to create buffers, however that is another tool.

import arcpy
arcpy.env.overwriteOutput = True

arcpy.env.workspace =  r"C:\UpdateDevelopmentPython\ForDeveloping\ExistingHydrants.gdb"
Hydrant_FC = "Hydrants"
Streetmap = r"C:\UpdateDevelopmentPython\Streetmap Premium\FGDB\StreetMap_Data\NorthAmerica.gdb\Routing\Routing_ND"
EnterWaterSysID = arcpy.GetParameterAsText(0)
NewHydrants = arcpy.GetParameterAsText(1)

qry = "{0} = {1}".format(arcpy.AddFieldDelimiters(datasource= Hydrant_FC, field = 'WaterSysID'),EnterWaterSysID)

arcpy.SelectLayerByAttribute_management(in_layer_or_view=Hydrant_FC, where_clause=qry)
arcpy.SelectLayerByLocation_management(Hydrant_FC,"Within",NewHydrants,None,"SUBSET_SELECTION","NOT_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="")
 
This is the code that I have so far, and it works great when I enter in one WaterSysID number.  However, I'd like to be able to enter in multiple WaterSysIDs and generate a road network from those.  Like what's shown in the screenshot.
tool.PNG
However when I do this I am then given this error:
 
Traceback (most recent call last):
File "C:\UpdateDevelopmentPython\DevelopmentScripts\ExtractStreets.py", line 13, in <module>
arcpy.SelectLayerByAttribute_management(in_layer_or_view=Hydrant_FC, where_clause=qry)
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 10296, in SelectLayerByAttribute
 
If anyone has any suggestions that would be very much appreciated, still pretty new/green to Python and Arcpy.  Thanks for the help!!!
 
 
 
 










 

    

0 Kudos
4 Replies
DonMorrison1
Frequent Contributor

I would start by printing out the parameters as your code receives them and also the where clause so you can inspect what they really look like - then run the commands interactively using those as inputs to see if they work.  The AddMessage function can be used for writing to the custom tool message log.

0 Kudos
AlfredBaldenweck
MVP Regular Contributor

So, what's happening is that you aren't telling it what to do if you have more than one input.

That is, it's expecting one value (In your example, "4"), and it's getting "4;152" as a string, which I assume is not a value in your dataset.

You should convert it to a list, then iterate whatever you're doing over the list.

EnterWaterSysID = arcpy.GetParameterAsText(0) # outputs "4;159" as string
'''versus'''
EnterWaterSysID = arcpy.GetParameterAsText(0).split(";") # outputs: ["4", "159"] as a list

'''code'''
for WaterSys in EnterWaterSysID:
    '''code'''

 

I apologize, I'm not familiar with your sort of workflow so I'm not sure how everything would fit together after that, but that should be a start.

0 Kudos
by Anonymous User
Not applicable

Your where clause will need to updated to select the multiple SysId's as a set as well, after they are split into a list.  This can be done by using SQL's IN and some join formatting.

qry = "{0} = {1}".format(arcpy.AddFieldDelimiters(datasource= Hydrant_FC, field = 'WaterSysID'),EnterWaterSysID)

# currently looks like: qry = 'WaterSysID = ["4", "159"]'

# adding the () and join method:
qry = "{0} IN ({1})".format(arcpy.AddFieldDelimiters(datasource= Hydrant_FC, field = 'WaterSysID'), ', '.join(EnterWaterSysID))

# will create the query to select each passed in SysID as a set.  This is expandable so it will work if there is one SysID or many.

qry = "WaterSysID IN (4, 159, 345, 234, 23)"

# if the SysIds datatype is strings:
qry = """{0} IN ('{1}')""".format(arcpy.AddFieldDelimiters(datasource= Hydrant_FC, field = 'WaterSysID'), "', '".join(EnterWaterSysID))
# looks like: 
qry = "WaterSysID IN ('12', '23', '123', '435', '65', '213')"

 

0 Kudos
ColeNelson
Emerging Contributor

Thank you so much for the help, that totally worked and I can now select multiple attributes!!!

0 Kudos