I'm having trouble with my Python code to automate an ArcMap process using ArcPy.
I have a shapefile of all the census sectors of a country, let's say census.shp. And I have 20 or 30 shapefiles of polygons that intersect some of these sectors, I'm naming them polygon01.shp to polygonXX.shp. This polygon shapes are updated every month so I'm writing this code to iterate through them and generate the intersection between each one of the polygon shapefiles with the census.shp and store it in a specific folder that is accessed by another applications.
I have two problems:
- I could manage to do all the processing and generate the intersect shapefile, polygon01_intersect.shp, but it doesn't work every time. Sometimes I have to reboot my computer to run again, otherwise arcpy returns "WARNING 000117: Warning empty output generated". I've tried to use arcpy.RepairGeometry_management before using the intersect, but I wasn't able to remove this unpredictability. Sometimes it goes smoothly, sometimes it doesn't. With the same files;
- Even if the first polygon shapefile intersects ok, the next one always results in the same warning 000117. So I couldn't pass the first file because the second fails.
This is my code. This is my first code using ArcPy but there are not many resources on Google to help. By the way, I have ArcMap 10.3, I'm coding the Python script with Visual Studio Code using the native ArcGIS Python compiler in C:\Python27\ArcGIS10.3\python.exe to run the script.
census = "census.shp" dir_poly = "polygons\\" dir_results = "results\\" files = glob.glob(dir_poly + "*.shp") for poly in files: result = dir_results + poly[poly.rfind("\\")+1:-4] + "_intersect.shp" print("Processing file " + poly) arcpy.Intersect_analysis([census, poly], result) print("Intersect completed: " + arcpy.GetMessages()) #this is where the warning is printed and the next lines, when I try to add a field, throw exceptions