I found an inconsistency between arcpy in Python 2.7 and in Python 3.4 (ArcGIS 10.4.1 and ArcGIS Pro 1.3.1 Patch 1).
The task is simple. Take an input feature class and a zone feature class, clip the input FC using the zone FC to produce a clipped FC. Then, use the statistics tool to summarize areas (or whatever) by a category field in the clipped FC.
The inconsistency occurs when the the zone feature(s) does not intersect the input feature(s):
- In Python 2.7, result of the clip tool is an empty feature class with all the columns defined as if there was a feature. Warning 000117 ('empty output generated') is issued but the output feature class does exist. The statistics tool produces an empty output table with the expected fields included. As far as I know, this has been the usual behaviour and that's what my scripts expected.
- In Python 3.4, result of the clip tool does not produce any output. Warning 000117 is issued but the output feature class does not exist. Calling .getOutput(0) on the clip result object returns the path where the feature class should be, but it is not actually there. Consequently, the statistics tool fails because it requires the result of the clip tool to exist.
I need my script to work in both pythons and empty results is a valuable result too. I dealt with it by checking whether the warning was issued. In my case, the task was inside a for-loop and I could simply skip to the next iteration (using the 'continue' statement) when the warning was raised. The code snippet below shows how one can check if the warning was issued and to act accordingly.
geology = r'C:\temp\geology.shp'
catchment = r'C:\temp\catchment.shp'
arcpy.env.workspace = r'C:\temp\wdb.gdb'
stats_table = None
clipped = arcpy.analysis.Clip(geology, catchment, 'clppd')
warnings = str(clipped.getMessages(1)).lower()
if 'empty output generated' in warnings:
clipped_fc = clipped.getOutput(0)
stats = arcpy.analysis.Statistics(clipped_fc, 'outtbl', [["Shape_Area", "SUM"]], "LEX")
stats_table = stats.getOutput(0)
The same thing happened when I tried it in ArcGIS Pro user interface so it is probably not a Python problem, but I needed to solve it in Python.
I hope this will help someone or that someone will tell me if there is a better way to deal with it.