I’m pretty new in ArcGIS and Arcpy, but experienced in Python. At the moment, I am working on an existing Python-Code in VS Studio (not my Code). I have some trouble understanding ArcPy’s MakeFeatureLayer
, in order to edit FeatureClasses in ArcGIS by using SelectLayerByAttributes
. I struggle with some basics here. Here is a simple piece of code:
import arcpy
# Step 1: Import FeatureClass
inPath = arcpy.GetParameterAsText(0) # inPath will be a FeatureClass (Polygons)
# Step 2: MakeFeatureLayer (necessary for selection)
arcpy.management.MakeFeatureLayer(inPath, inPath)
# Step 3: Select and dissolve data
arcpy.management.SelectLayerByAttribute(inPath,"NEW_SELECTION","status = 'white'")
arcpy.analysis.PairwiseDissolve(inPath,inPath + "_diss", dissolve_field="status", statistics_fields="", multi_part="SINGLE_PART")
arcpy.management.SelectLayerByAttribute(inPath,"CLEAR_SELECTION")
# Step 4: Select and calcualte field
arcpy.management.SelectLayerByAttribute(inPath,"NEW_SELECTION","status = 'white'")
arcpy.management.CalculateField(inPath,"symbol", 1)
arcpy.management.SelectLayerByAttribute(inPath,"CLEAR_SELECTION")
# Step 5: Select and delete fields
arcpy.management.SelectLayerByAttribute(inPath,"NEW_SELECTION","status = 'red'")
arcpy.management.DeleteFeatures(inPath)
arcpy.management.SelectLayerByAttribute(inPath,"CLEAR_SELECTION")
Code steps:
-
Load in FeatureClass (Polygons)
-
MakeFeatureLayer
: Name of FeatureClass and FeatureLayer is the same in the code. -
SelectLayerByAttributes
: There is a field called “status” which contains several “colors” in form of a string (red, green, white, yellow, orange). I only want to select the white ones.Dissolve
all selected (white) polygons and write out to new FeatureClass -
CalculateField
: I want to calculate the field “symbol” for all selected “white” fields and write the number 1 in it. -
DeleteFeatures
: I want to select all “red” fields in “status” and delete them.
My understanding problems here:
-
In order to do the selection, I need to MakeFeatureLayer first and pass in the FeatureClass. As I understand it right, this step is necessary, because a Layer points to the data of the FeatureClass and make this data editable. Is it a problem or even necessary, that the FeatureLayer has the same name as the FeatureClass? Does it make any difference, when the name of the Layer is different to the name of the FeatureClass as long as I pass the right named Layer to the following commands?
-
Is there a kind of “invisible link” between Layer and FeatureClass? Does any change of the data in the Layer always automatically change the “linked” FeatureClass by itself? In my code example, after each step of editing (Dissolve, CalculateField, DeleteFeatures), I loaded the FeatureClass in ArcGIS and all the changes are done perfectly. There is no need to store or export the Layer sepperatly.
-
The Layer is only temporary. For how long does the Layer “stay active” within my script in VS Studio. Is it possible to edit Layer and therefore the FeatureClass later on again and again? In my script, the MakeFeatureLayer is done several times at different locations, always with the same FeatureClass as input. It seems, that in between the temporary Layer “dissappear”.
I hope my issues are described quiete clear. Thanks for help.