0

I want to loop through shapefiles in a folder, calculate Euclidean distance and then save the files in an output folder with an appended name in arcpy. I want the name to be the filename with "KYU_" at the beginning and "_dist" at the end. Using @BERA code:

import arcpy, os

arcpy.env.workspace = "E:\\test_input" #There are two shapefiles in here 
OutputFolder = "E:\\test_output"
arcpy.env.extent = "415637.469400001 2988625.7212 796637.469400001 3843125.7212"
arcpy.env.mask = "E:\\boundary\\boundary.tif"
for file in arcpy.ListFeatureClasses():
    newfile = os.path.join(OutputFolder, 'KYU_'+file.replace('.shp','')+'_dist.tif')
    print file
    print newfile
    arcpy.gp.EucDistance_sa(file, newfile, "550000", "250", Output_direction_raster, "PLANAR", "", Output_back_direction_raster)

Getting the error:

Runtime error 
Traceback (most recent call last):
  File "<string>", line 7, in <module>
TypeError: 'NoneType' object is not iterable
2
  • 1
    What is the full error message that you are receiving? Please provide it as formatted text rather than a picture. Are you using ArcPy with ArcGIS Pro or ArcMap? – PolyGeo May 20 at 7:19
  • I tried removing that but still getting the same error – hughbk May 22 at 2:22
2

I dont have Spatial Analyst but:

To list feature classes you need to use ListFeatureClasses, arcpy.env.workspace is just one folder

import arcpy, os

arcpy.env.workspace = r'C:\GIS\data\testdata' #Here's the 2018.shp file
OutputFolder = r'C:\data'
for file in arcpy.ListFeatureClasses():
    newfile = os.path.join(OutputFolder, 'KYU_'+file.replace('.shp','')+'_dist.tif')
    print file
    print newfile
    #arcpy.gp.EucDistance_sa(...
    
#2018.shp
#C:\data\KYU_2018_dist.tif
4
  • Ah sorry, yes that should be the output. I want it to be the same filename but with "KYU_" at the beginning and "_dist" at the end appended so KYU_someraster_dist.tif. Euclidean distance will calculate a raster – hughbk May 20 at 7:47
  • With your code BERA I'm getting the errors: Runtime error Traceback (most recent call last): File "<string>", line 5, in <module> TypeError: 'NoneType' object is not iterable – hughbk May 20 at 8:15
  • Can you edit your question and update with the code you are using? Are you sure there are shapefiles in your workspace? – BERA May 20 at 8:30
  • Okay I edited the original question, yes there are two shapefiles in the workspace – hughbk May 20 at 9:57
0

If you are getting error with feature input, you could always try to convert the feature to raster first and use that as input to the EucDist since the tool converts it internally anyway. Make sure the input feature has correct spatial reference information.

Note: the most efficient (current) way to run EucDist is to use the Spatial Analyst Extension python module.

from arcpy.sa import *

# Set environment settings
env.workspace = "E:\\test_input"
OutputFolder = "E:\\test_output"

# Set local variables
maxDistance = ""
cellSize = 10

for file in arcpy.ListFeatureClasses():
    newfile = os.path.join(OutputFolder, 'KYU_'+file.replace('.shp','')+'_dist.tif')
    # convert
    result = arcpy.FeatureToRaster_conversion(file, "OID", "outraster", cellSize)
    # Execute EucDistance
    outEucDistance = EucDistance(result, maxDistance, cellSize)

# Save the output 
outEucDistance.save(newfile)

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.