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")