1

I started trying to teach myself Python/ArcPy a couple of weeks ago in an attempt to someday be able to convert my plethora of models to more shareable and user-friendly formats (script tools, add-ins, etc.) and it's been going super well so far, but a general topic I am still hung up on is receiving user-defined parameters in my scripts.

Specific example I'm seeking help for: I need a way for the end-user to define stop points based on network junction attributes (my ND has custom junctions) and use those inputs to solve the route. I have scoured the internet for answers and I'm still coming up just short. Here's some sample code of what I think needs to happen. Remember, I'm still super new to this, so there may be some rookie mistakes in this code. Also, I did not complete the code for finding the routes, but included enough to get my point across.

import arcpy, os, datetime, time

#check out NA extension
arcpy.CheckOutExtension("Network")

#set environments - not a real path here, obviously
arcpy.env.workspace = arcpy.GetParameterAsText(0) #user selects gdb where network junctions are located
Dir = arcpy.env.workspace
arcpy.env.overwriteOutput = True

#get parameters for stop location selection
networkJunctions = arcpy.GetParameterAsText(1) #network junctions feature class
stop1 = arcpy.GetParameterAsText(2) #first stop
stop2 = arcpy.GetParameterAsText(3) #second stop

#define variables
expression = " NAME IN ('stop1', 'stop2') " #selection expression
date = time.strftime("%d%m%y")
outputFCname = Dir + "\\RouteStops" + date

#create layer from network junctions FC
arcpy.MakeFeatureLayer_management(networkJunctions, "networkJunctions_lyr")

#Select the two stops used to solve the route
arcpy.SelectLayerByAttribute_management("networkJunctions_lyr", "NEW_SELECTION", expression)

#new FC of selected stops
arcpy.CopyFeatures_management("networkJunctions_lyr", outputFCname)

#solve routes - incomplete
arcpy.na.FindRoutes(will worry about this part later)

This code successfully runs up until the FindRoutes part, but there is no output. I have a hunch that I may be making my feature layer at the wrong point in the code, but I've reordered all of this and I still get a new feature class with nothing in it.

  • Welcome to GIS SE! Thank you for taking the new user tour. What happens when you run that code? Is there a specific part that isn't working for you? Any errors? Please edit your question to add more information around what actually happens when you try it. – Midavalo May 15 '17 at 17:09
  • Sorry for the trouble! I edited the code to only show it up to the point where it's failing and explained it in the question. Code runs successfully, but my output fc is empty. – MammothTarantula May 15 '17 at 20:41
1

The problem is with how you create a SQL expression.

expression = " NAME IN ('stop1', 'stop2') " this one will always try to create features that have stop1 and stop2 as their NAME attribute value. I am pretty sure you don't have any. You probably want to use the string value from the variables stop1 and stop2. You need to do the string formatting:

stop1 = 'Hospital'
stop2 = 'Centre'
print "NAME IN ( '{0}', '{1}' )".format(stop1, stop2)
#"NAME IN ( 'Hospital', 'Centre' )"
  • Thanks Alex! This worked like a charm and also helped me learn more about casting in Python, which I'm sure will help me tremendously down the line. Side note: stumbled across your Wordpress site and have enjoyed learning from that as well. Appreciate the assistance! – MammothTarantula May 26 '17 at 16:23
  • No worries, glad it works now! Also pretty happy to hear that some of my posts are helpful! – Alex Tereshenkov May 26 '17 at 19:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.