Hi,
I am developing (slowly) workflows to script geoprocessing tasks using Python and Arcpy (along with the other common suspects - pandas, numpy, etc.). I want to operate using the 'in_memory' workspace OR (situation-dependent) use `arcpy.da.SearchCursor...` to access rows and return objects, perform geoprocessing tasks like merge, union, append, etc. to avoid creating intermediate data.
ArcMap 10.6.1
Yesterday I created a function that basically does this:
def extract_cursor(fcs_list, target_field_list, target_val_list, fname):
for i in range(len(fcs_list)):
fcs = fcs_list[i]
target_field = target_field_list[i]
target_val = target_val_list[i]
with arcpy.da.SearchCursor(fcs, [target_field, 'SHAPE@']) as cursor:
for row in cursor:
# select rows with matching vals
if target_val in row:
# initiate new feature
if 'shapes_union' not in locals():
# get geometry object - Polyline in my case
shapes_union = row[1]
else:
# merge new geometry objects to build one containing
# all features/rows matching selection criteria
shapes_union = shapes_union.union(row[1])
arcpy.CopyFeatures_management(shapes_union, fname)
Then I run the function as such (pretend it's housed here: 'path/to/zachs_arc_scripts/zachs_geo_script.py')
import sys
sys.path.append('path/to/zachs_arc_scripts')
from zachs_geo_script import extract_cursor
fcs_in ['kentucky','missouri','kansas']
target_field = ['rivers','streams','rivers']
target_val = ['rural','rural','seasonal']
fname = 'merged_rivers_ky_mo_ks'
extract_cursor(fcs_in, target_field, target_val, fname)
To be honest I originally had this functioning the exact same nested in a class, and instead of a list I saved variables in a csv. Then passed the csv and extracted the list of variables via a pandas dataframe. But I figured this was easier to see for feedback purposes.
PROBLEM! the feature class merged_rivers_ky_mo_ks will be created, but it contains just one val or is empty. So to troubleshoot I decomposed the function and ran it directly in the Python Window and it works! The problem is that when run in the Python Window the geometry object is this:
row[1]
<Polyline object at xyz>
but within the function run through the Window (as described above) - I get this:
row[1]
>>>geoprocessing describe geometry object object at xyz>
Furthermore, if I
return(row[1])
within the function I return the proper Polyline object not the geoprocessing describe geometry...
So the problem is arising from this object type which I can only find sparse info on.
Please help with knowledge on this specific object and issue or offer suggestions for alternate workflows. I really want to get some decent geoprocessing scripts going, but I keep running into inexplicable issues like this. Thanks! Zach