0

I'm somewhat new to Python and am working on automating some of the daily processes we do at my company. In this code example, I'm trying to automate pulling in data from a CSV file, creating buffers and drive time around that data, and then appending spatial data to those created trade areas.

I was having no errors with my code when I set the workspace myself. However, I'd like to make the workspace a GetParameterAsText variable so that other people in my department can use this code and save the data/output to wherever they want to.

When I write the code with the workspace set as GetParameter, I get the following error:

Traceback (most recent call last):
  File "X:\Users\Andy.Rowlands\AHR Data - New Comp\Individual Rocks-Projects\Python Implementation - July2016\InternalModel_PythonScript.py", line 46, in <module>
    for fcBuffer in featureClasses:
TypeError: 'NoneType' object is not iterable*

Any thoughts? Can you set the workspace as a GetParameter? Here is my current code.

#import arcpy library
import arcpy
import arcview

#set workspace to GetParameter field
arcpy.env.workspace = arcpy.GetParameterAsText(0)

#acquire extensions for Business Analyst and Network Analyst for use
arcpy.CheckOutExtension("Business")
arcpy.CheckOutExtension("Network")

#in order to use its tools, add Business Analyst toolbox to script
arcpy.AddToolbox("C:\Program Files (x86)\ArcGIS\Desktop10.3\Business Analyst\ArcToolbox\Toolboxes\Business Analyst Tools.tbx")

#create GetParameter for input csv table that has store location and lat/long
inputTable = arcpy.GetParameterAsText(1)

#set variables for lat/long table
location = "Location"
xCoord = "Long"
yCoord = "Lat"

#set the spatial reference; 4269 is code for GCS_North_American_1983
spatialRef = arcpy.SpatialReference(4269)

#create XY feature class for each location in csv table
for row in inputTable:
    arcpy.MakeXYEventLayer_management(inputTable, xCoord, yCoord, "Location_{0}".format(location), spatialRef)

#create variable of feature classes created by previous MakeXYLayer function (only point files)
featureClasses = arcpy.ListFeatureClasses("", "Point")

#for each XY point layer created, run a 0.5, 1, and 3 mile buffer
for fcBuffer in featureClasses:
    arcpy.MultipleRingBuffer_analysis(featureClasses, "Buffer_{0}".format(fcBuffer), [0.5, 1, 3], "Miles")

#for ecah XY point layer created, run a 2.5, 5, and 10 minute drive time trade area
for fcDriveTime in featureClasses:
    arcpy.DriveTime_ba(featureClasses, location, "All", "2.5,5,10", "Minutes", "DriveTime_{0}".format(fcDriveTime))

#Create variable of feature classes created by previous rings and drive time functions
BufferClasses = arcpy.ListFeatureClasses("Buffer")
DriveTimeClasses = arcpy.ListFeatureClasses("DriveTime")

#Create variables for spatial overlay function
InputLayer = "C:\ArcGIS\Business Analyst\US_2015\Data\Demographic Data\esri_bg.bds"
DemogFields = "INDMGMT_CY; A25I25_CY; A35I150_CY; A45I75_CY; MEDIA55_CY; \
                MEDDIA35CY; A15NW250CY; AVGNWA65CY; MEDVAL_CY; POPGRWCYFY; \
                N14_EMP; N17_BUS; N18_BUS; N34_EMP"

#Run spatial overlay to append data to buffers and drive time
for BufferAppend in BufferClasses:
    arcpy.SpatialOverlay_ba(InputLayer, BufferClasses, DemogFields, "BufferAppend_{0}".format(BufferAppend))

for DriveTimeAppend in DriveTimeClasses:
    arcpy.SpatialOverlay_ba(InputLayer, DriveTimeClasses, DemogFields, "DriveTimeAppend_{0}".format(DriveTimeAppend))

#Create variable of feature classes created by spatial overlay functions
BufferAppendClasses = arcpy.ListFeatureClasses("BufferAppend")
DriveTimeAppendClasses = arcpy.ListFeatureClasses("DriveTimeAppend")

#Create function for exporting output from spatial overlay to CSV file
#Do this for both buffers and drive time
for BufferTable in BufferAppendClasses:
    arcpy.TableToExcel_conversion(BufferAppendClasses, "BufferAppendTable_{0}.".format(BufferTable))

for DriveTimeTable in DriveTimeAppendClasses:
    arcpy.TableToExcel_conversion(DriveTimeAppendClasses, "DriveTimeAppendTable_{0}".format(DriveTimeTable))

print ("DONE")
  • 2
    arcpy.env.workspace = arcpy.GetParameterAsText(0) should work. How are you trying to execute this code? Do you have the script linked to a Model Builder scripting tool, are you copying and pasting the code into the Python window in ArcMap, or running it in an IDE like IDLE? – GBG Aug 23 '16 at 17:25
  • 1
    If you are executing the above code as a script tool, what you have should be fine just set the type as workspace when setting up the parameters of the script tool – GeoJohn Aug 23 '16 at 18:11
  • 1
    As @GeoJohn says, that sounds like you have not configured your first parameter on the tool Properties. Or maybe you are trying to run the code from your IDE instead of its tool dialog. – PolyGeo Aug 23 '16 at 18:16
  • 1
    @arowl24 Yes, definitely. The GetParameterAsText() method won't do anything unless you set it up as a script tool. In ArcCatalog, create a new toolbox, add the script and follow the dialog to setup the parameters. – GeoJohn Aug 23 '16 at 18:54
  • 1
    I would use the Catalog window of ArcMap to Add Script to a toolbox. – PolyGeo Aug 23 '16 at 19:05
3

Yes, using GetParameterAsText() for the environment workspace is fine, just remember to pass a value to it.

You can set up your script to offer a default value if none is received through GetParameterAsText() which can be useful if you are testing through your IDE rather than through ArcGIS Desktop:

# Get my workspace - if it's blank then use default value
myWS = arcpy.GetParameterAsText(0)
if myWS == '#' or not myWS:
    myWS = r"D:\Temp\GISSE" # Default value if none passed to script

arcpy.env.workspace = myWS

And to test:

print arcpy.env.workspace

Which outputs

D:\Temp\GISSE
2

Traditionally one does not supply global environment settings as parameters, they set them via the environment button on the tool interface. The geo-processor would then inherit them. So below is a simple script but note it is reporting the cell size, I set that by going to Environments > Raster Analysis > cell size on the tool script interface before clicking on the OK button to run the script.

import arcpy
arcpy.env.addOutputsToMap = False
txt_file = arcpy.GetParameterAsText(0)
f = open(txt_file, "w")
f.write("test")
f.close()
arcpy.AddMessage(str(arcpy.env.cellSize))

If you want your users to have the same experience as using any other geo-processing tool I would recommend that approach. At this point it's more about you documenting the tool and educating your users than you desperately trying to code all eventualities.

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.