1

I am trying to understand arcpy/python, and am confused about where a layer file is located during the process.

This is my code:

import os, sys
import arcpy

gdb  = "C:\\Example.gdb"
featureClass = "C:\\Example.gdb\feature"
arcpy.MakeFeatureLayer_management(featureClass, "OutLayer", "\"Field_Name\" = 'Name One'")
arpy.CopyFeatures_management("Outlayer", "C:\\Example.gdb\\test")

This seems to work, but I don't understand where "Outlayer" exists. If I run the code again, I get the message that I can't because "Outlayer" already exists. It is not in the .gdb, or the default.gdb. When I specify a path (e.g. C:\GIS\OutLayer) it isn't there. Finally, when I search for it using Windows Explorer, I can't find the file. It is not important, but just trying to figure out what is going on.

  • When you're finished with the layer use arcpy.Delete_management("OutLayer") to clear it from memory or use arcpy.env.overwriteOutput = True to avoid getting this problem recurring. – Michael Stimson Jun 9 '15 at 22:24
8

The "OutLayer" is sitting in memory. It is a feature layer representation of your feature class, and in your case a subset of features as you provided a where clause. Think of it as like a layer in ArcMap with a Definition Query.

If you set the arcpy.env.overwriteOutput = True or explicitly delete the reference to the feature layer you shouldn't have any issues overwriting it.

Here is an example to illustrate, you can see the feature layer has the same data source and I added a way to show how you can see any layers that are sitting in memory:

>>> import arcpy
>>> shp = r'C:\TEMP\UofM_cc.shp'
>>> lyr = arcpy.management.MakeFeatureLayer(shp, 'temp_layer').getOutput(0)
>>> print lyr.dataSource
C:\TEMP\UofM_cc.shp
>>> # see layers in memory
>>> layers = [globals()[v] for v in dir() if isinstance(globals()[v], arcpy.mapping.Layer)]
>>> layers
[<map layer u'temp_layer'>]
>>> # get variable names for layers in memory
>>> layer_vars = [v for v in dir() if isinstance(globals()[v], arcpy.mapping.Layer)]
>>> layer_vars
['lyr']
>>> 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.