3

I'm doing a toolbox that one shapefile intersects with many others shapefiles in folders, subfolders. The code works fine, but only for the first folder (in this case the "input_folder" but the subfolders, appears this error when intersects the first shapefile from subfolder:

ERROR 000732: Input Features: Dataset curvas_de_nivel.shp does not exist or is not supported
Failed to execute (Intersect).

but the shape is on the folder.

This is my code:

import os
import arcpy
inter = arcpy.GetParameterAsText(0)
input_folder = arcpy.GetParameterAsText(1)
output_folder = arcpy.GetParameterAsText(2)
arcpy.env.overwriteOutput=True
arcpy.env.workspace = input_folder
list_fcs = arcpy.ListFeatureClasses()
for root, directory, files in os.walk(input_folder):
 for fc in files:
 if fc.endswith('.shp'):
 rename_shp = "intersected_{}".format(fc)
 rename_excel = "calculated_{}.xls".format(os.path.splitext(fc)[0])
 newfile = os.path.join(output_folder,rename_shp)
 newtable = os.path.join(output_folder,rename_excel)
 
 arcpy.analysis.Intersect([fc, inter], newfile,"","","INPUT")
 desc = arcpy.Describe(fc)
 geometry = desc.shapeType
 if geometry == 'Polygon':
 fieldName = "NEW_AREA"
 expr ='!shape.area@hectares!'
 arcpy.management.AddField(newfile,fieldName,"DOUBLE")
 arcpy.CalculateField_management(newfile,fieldName,expr,'PYTHON')
 arcpy.TableToExcel_conversion(newfile,newtable)
Kadir Şahbaz
78.6k57 gold badges260 silver badges407 bronze badges
asked Apr 5, 2023 at 2:25
1
  • 2
    Don't use os.walk and arcpy.ListFeatureClasses, use arcpy.da.Walk that combines the functionality of both. Commented Apr 5, 2023 at 13:43

2 Answers 2

5

Your fc variable's value is the name of the file (shapefile), but does not include the path to the file, so the system does not know where the feature class is. This may be fine for feature classes that are directly within your working directory, but for any sub-directories, it is not.

You will need to prefix the path of the enclosing folder.

Eg, use the os.path.join() function to produce an operating-system-safe file path prefixing the root (the files parent directory path from the os.walk()) to the fileName (one of the files from the os.walk().

for root, directory, files in os.walk(input_folder):
 for fileName in files:
 if fileName.endswith('.shp'):
 fc = os.path.join(root, fileName)

So your script would become:

import os
import arcpy
inter = arcpy.GetParameterAsText(0)
input_folder = arcpy.GetParameterAsText(1)
output_folder = arcpy.GetParameterAsText(2)
arcpy.env.overwriteOutput=True
arcpy.env.workspace = input_folder
list_fcs = arcpy.ListFeatureClasses()
for root, directories, files in os.walk(input_folder):
 for fileName in files:
 if fileName.endswith('.shp'):
 fc = os.path.join(root, fileName)
 rename_shp = "intersected_{}".format(fc)
 rename_excel = "calculated_{}.xls".format(os.path.splitext(fc)[0])
 newfile = os.path.join(output_folder,rename_shp)
 newtable = os.path.join(output_folder,rename_excel)
 
 arcpy.analysis.Intersect([fc, inter], newfile,"","","INPUT")
 desc = arcpy.Describe(fc)
 geometry = desc.shapeType
 if geometry == 'Polygon':
 fieldName = "NEW_AREA"
 expr ='!shape.area@hectares!'
 arcpy.management.AddField(newfile,fieldName,"DOUBLE")
 arcpy.CalculateField_management(newfile,fieldName,expr,'PYTHON')
 arcpy.TableToExcel_conversion(newfile,newtable)

UPDATE:

I do agree with @bixb0012 where he commented, "Don't use os.walk and arcpy.ListFeatureClasses, use arcpy.da.Walk that combines the functionality of both". And to add to this comment, not only does it provide the functionality of both, but it would also remove the need to check if a file ended in ".shp", as it will only return actual spatial data sets, not all other files. However, I have not included this in the updated version of the script above, which merely answers your actual question.

A further update to incorporate this additional change might look like this:

import os
import arcpy
inter = arcpy.GetParameterAsText(0)
input_folder = arcpy.GetParameterAsText(1)
output_folder = arcpy.GetParameterAsText(2)
arcpy.env.overwriteOutput=True
arcpy.env.workspace = input_folder
list_fcs = arcpy.ListFeatureClasses()
for root, directories, featureClasses in arcpy.da.Walk(input_folder, datatype="FeatureClass"):
 for fcName in featureClasses :
 fc = os.path.join(root, fcName)
 rename_shp = "intersected_{}".format(fc)
 rename_excel = "calculated_{}.xls".format(os.path.splitext(fc)[0])
 newfile = os.path.join(output_folder,rename_shp)
 newtable = os.path.join(output_folder,rename_excel)
 
 arcpy.analysis.Intersect([fc, inter], newfile,"","","INPUT")
 desc = arcpy.Describe(fc)
 geometry = desc.shapeType
 if geometry == 'Polygon':
 fieldName = "NEW_AREA"
 expr ='!shape.area@hectares!'
 arcpy.management.AddField(newfile,fieldName,"DOUBLE")
 arcpy.CalculateField_management(newfile,fieldName,expr,'PYTHON')
 arcpy.TableToExcel_conversion(newfile,newtable)
answered Apr 5, 2023 at 2:57
0
0

This code worked for me, thanks @son-of-a-beach and @bixb0012 for the help.

import os
import arcpy
inter = arcpy.GetParameterAsText(0)
input_folder = arcpy.GetParameterAsText(1)
output_folder = arcpy.GetParameterAsText(2)
arcpy.env.overwriteOutput = True
arcpy.env.workspace = input_folder
list_fcs = arcpy.ListFeatureClasses()
for root, dirs, files in os.walk(input_folder):
 for dir in dirs:
 arcpy.env.workspace = os.path.join(root,dir)
 for fc in arcpy.ListFeatureClasses():
 if fc.endswith('.shp'):
 rename_shp = "intersected_{}".format(fc)
 rename_excel = "calculated_{}.xls".format(os.path.splitext(fc)[0])
 newfile = os.path.join(output_folder, rename_shp)
 newtable = os.path.join(output_folder, rename_excel)
 arcpy.analysis.Intersect([fc, inter], newfile, "ALL", None, "INPUT")
 desc = arcpy.Describe(fc)
 geometry = desc.shapeType
 if geometry == 'Polygon':
 fieldName = "NEW_AREA"
 expr = '!shape.area@hectares!'
 arcpy.management.AddField(newfile, fieldName, "DOUBLE")
 arcpy.CalculateField_management(newfile, fieldName, expr, 'PYTHON')
 arcpy.TableToExcel_conversion(newfile, newtable)
answered Apr 30, 2023 at 23:12

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.