I'm trying to run a loop based on a SearchCursor that selects layers from an input polygon and polyline. I want to select a feature from the polygon to use as a mask for running Euclidean Allocation on selected lines within the polygon feature. The goal is to have multiple areas of Euclidean Allocation run on a subset of polylines, each masked by a corresponding polygon.
Here's my original code. Note that this function is part of a much larger, otherwise functional script.
The input parameters for the three functions below are inline = a polyline feature class, inpoly = a polygon feature class, and valfield = a single field which corresponds to both inline and inpoly: i.e., each value of valfield = x in inline corresponds to lines that fall within inpoly features of the same value. All of these parameters are derived from in-memory feature classes fed in by earlier functions in the script--they're not located directly on disk.
def eucal_by_poly(inline, inpoly, valfield):
polylist = []
with arcpy.da.SearchCursor(inpoly, ("OID@", "SHAPE@", valfield)) as cursor:
vallist = sorted({row[2] for row in cursor})
for v in vallist:
lyr = "lyr"
selexp = "{0} = {1}".format(arcpy.AddFieldDelimiters(lyr, valfield), v)
mask_lyr = arcpy.MakeFeatureLayer_management(inpoly, lyr, selexp)
arcpy.env.mask = mask_lyr
line_lyr = arcpy.MakeFeatureLayer_management(inline, lyr, selexp)
eucalsub = arcpy.sa.EucAllocation(line_lyr, "", "", "", "LAT_ID")
eucalsubpoly = arcpy.RasterToPolygon_conversion(eucalsub, "eucalsubpoly", "SIMPLIFY", "Value")
polylist.append(eucalsubpoly)
polyeucal = arcpy.Merge_management(polylist, "polyeucal")
del cursor
return polyeucal
When I run this code I get this error:
Traceback (most recent call last):
File "G:\tgf_python\tools\py_scripts_working\transect_working_rasversion.py", line 406, in <module>
polyeucal = eucal_by_poly(cliptrans, eucal_poly, IDfield)
File "G:\tgf_python\tools\py_scripts_working\transect_working_rasversion.py", line 397, in eucal_by_poly
arcpy.env.mask = mask_lyr
File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\geoprocessing\_base.py", line 529, in set_
self[env] = val
File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\geoprocessing\_base.py", line 581, in __setitem__
ret_ = setattr(self._gp, item, value)
RuntimeError: Object: Error in accessing environment <mask>
I've tried several variations, including using arcpy.env.mask = "SHAPE@", to no avail. I also tried saving the poly layer to a real file on disk, both as a feature class and a rasterized version. In all cases I get the same "Error in accessing environment" message when trying to apply the mask.
Does anyone have a solution? I'm using ArcGIS 10.1 with an Advanced License and Python 2.7 on Windows 7. (Any other suggestions on improving code are welcome.)

inline,inpolyandvalfieldinto your function so just provide hardwired values for them and a description of the data that they represent so that we can use that to try and test. Our volunteers are usually happy to try and help debug code snippets but your code and its context is your responsibility. – PolyGeo Feb 01 '16 at 00:07defline and start your code snippet withimport arcpyand then three lines that setinline,inpolyandvalfield. Then run that code snippet, and present the result of running just that code snippet. – PolyGeo Feb 10 '16 at 02:27