Saturday, 7 December 2019

python - Arcpy: MakeFeatureLayer_management: Where is the file?


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.



Answer




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
[]
>>> # get variable names for layers in memory
>>> layer_vars = [v for v in dir() if isinstance(globals()[v], arcpy.mapping.Layer)]
>>> layer_vars
['lyr']
>>>

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...