ArcPy assistance please

220
1
10-24-2021 09:30 PM
scott_taylor_he360
New Contributor

Good Morning,

 

I was wondering if someone could help me out with some ArcPy stuff.  I am no programmer by any means and I usually just brute force my way through code to make it do what I want.  Not a great approach, I recognize that.  But if it works it works right?  Well unfortunately, this doesn't quite work the way I want or even need it to.  It gets the job done but it is still pretty manual.  For instance putting in the path every time.   

Oh and to make matters worse I am pretty new to ArcGis in general.

Quick rundown before I post the code.

I have a lot of layers in the database (.gdb) I need moved into their respective group folders.  Those group folders also have subfolders.  I do this process manually right now, every week and it takes me a very long time. 

Anyway here is the complete code.  If anyone has any ideas on how to clean this up please let me know.  Right now it moves the layers into the right place but leaves a copy in the contents pane that I remove manually.  This is the small group I need to move so I will adapt what ever help I get into a very large project.  So if you could show me a small snip-it of code that would help me clean this up I would really appreciate it...

I searched but didn't find a way to use code tags.  So if I screwed that up too please let me know. 

import arcpy

blank_group_layer = arcpy.mp.LayerFile(r"C:\Users\Stak1\Documents\ArcGIS\Projects\Layer_Symbology_Reference\RFGeo_Folder.lyrx") #created previously in ArcPro and exported as lyrx file
print(blank_group_layer)
aprx = arcpy.mp.ArcGISProject('CURRENT')
m = aprx.listMaps('*')[0]

 


m.addLayer(blank_group_layer, "TOP")
model_output_group1 = m.listLayers("UHF")[0]
model_output_group2 = m.listLayers("VHF")[0]
model_output_group3 = m.listLayers("Lband")[0]
model_output_group4 = m.listLayers("Xband")[0]
print(model_output_group1)
print(model_output_group2)
print(model_output_group3)
print(model_output_group4)

#UHF

fc_output1 = r'C:\Users\Stak1\Documents\ArcGIS\Projects\20211015-20211022\20211015-20211022.gdb\UHF_SINGLE'
print(fc_output1)
fl_output1 = 'UHF_SINGLE'
print(fl_output1)

fc_output2 = r'C:\Users\Stak1\Documents\ArcGIS\Projects\20211015-20211022\20211015-20211022.gdb\UHF_MULTI'
print(fc_output2)
fl_output2 = 'UHF_MULTI'
print(fl_output2)

fc_output3 = r'C:\Users\Stak1\Documents\ArcGIS\Projects\20211015-20211022\20211015-20211022.gdb\UHF_ELLIPSES_SINGLE'
print(fc_output3)
fl_output3 = 'UHF_ELLIPSES_SINGLE'
print(fl_output3)

fc_output4 = r'C:\Users\Stak1\Documents\ArcGIS\Projects\20211015-20211022\20211015-20211022.gdb\UHF_ELLIPSES_MULTI'
print(fc_output4)
fl_output4 = 'UHF_ELLIPSES_MULTI'
print(fl_output4)

#VHF
fc_output5 = r'C:\Users\Stak1\Documents\ArcGIS\Projects\20211015-20211022\20211015-20211022.gdb\VHF_SINGLE'
print(fc_output5)
fl_output5 = 'VHF_SINGLE'
print(fl_output5)

fc_output6 = r'C:\Users\Stak1\Documents\ArcGIS\Projects\20211015-20211022\20211015-20211022.gdb\VHF_MULTI'
print(fc_output6)
fl_output6 = 'VHF_MULTI'
print(fl_output6)

fc_output7 = r'C:\Users\Stak1\Documents\ArcGIS\Projects\20211015-20211022\20211015-20211022.gdb\VHF_ELLIPSES_SINGLE'
print(fc_output7)
fl_output7 = 'VHF_ELLIPSES_SINGLE'
print(fl_output7)

fc_output8 = r'C:\Users\Stak1\Documents\ArcGIS\Projects\20211015-20211022\20211015-20211022.gdb\VHF_ELLIPSES_MULTI'
print(fc_output8)
fl_output8 = 'VHF_ELLIPSES_MULTI'
print(fl_output8)

#Lband
fc_output9 = r'C:\Users\Stak1\Documents\ArcGIS\Projects\20211015-20211022\20211015-20211022.gdb\Lband_SINGLE'
print(fc_output9)
fl_output9 = 'Lband_SINGLE'
print(fl_output9)

fc_output10 = r'C:\Users\Stak1\Documents\ArcGIS\Projects\20211015-20211022\20211015-20211022.gdb\Lband_MULTI'
print(fc_output10)
fl_output10 = 'Lband_MULTI'
print(fl_output10)

fc_output11 = r'C:\Users\Stak1\Documents\ArcGIS\Projects\20211015-20211022\20211015-20211022.gdb\Lband_ELLIPSES_SINGLE'
print(fc_output11)
fl_output11 = 'Lband_ELLIPSES_SINGLE'
print(fl_output11)

fc_output12 = r'C:\Users\Stak1\Documents\ArcGIS\Projects\20211015-20211022\20211015-20211022.gdb\Lband_ELLIPSES_MULTI'
print(fc_output12)
fl_output12 = 'Lband_ELLIPSES_MULTI'
print(fl_output12)

#Xband
fc_output13 = r'C:\Users\Stak1\Documents\ArcGIS\Projects\20211015-20211022\20211015-20211022.gdb\Xband_SINGLE'
print(fc_output13)
fl_output13 = 'Xband_SINGLE'
print(fl_output13)

fc_output14 = r'C:\Users\Stak1\Documents\ArcGIS\Projects\20211015-20211022\20211015-20211022.gdb\Xband_MULTI'
print(fc_output14)
fl_output14 = 'Xband_MULTI'
print(fl_output14)

fc_output15 = r'C:\Users\Stak1\Documents\ArcGIS\Projects\20211015-20211022\20211015-20211022.gdb\Xband_ELLIPSES_SINGLE'
print(fc_output15)
fl_output15 = 'Xband_ELLIPSES_SINGLE'
print(fl_output15)

fc_output16 = r'C:\Users\Stak1\Documents\ArcGIS\Projects\20211015-20211022\20211015-20211022.gdb\Xband_ELLIPSES_MULTI'
print(fc_output16)
fl_output16 = 'Xband_ELLIPSES_MULTI'
print(fl_output16)

#UHF
fl_output1 = arcpy.MakeFeatureLayer_management(fc_output1, fl_output1).getOutput(0)
fl_output2 = arcpy.MakeFeatureLayer_management(fc_output2, fl_output2).getOutput(0)
fl_output3 = arcpy.MakeFeatureLayer_management(fc_output3, fl_output3).getOutput(0)
fl_output4 = arcpy.MakeFeatureLayer_management(fc_output4, fl_output4).getOutput(0)
print(fl_output1)
print(fl_output2)
print(fl_output3)
print(fl_output4)
m.addLayerToGroup(model_output_group1, fl_output1)
m.addLayerToGroup(model_output_group1, fl_output2)
m.addLayerToGroup(model_output_group1, fl_output3)
m.addLayerToGroup(model_output_group1, fl_output4)

#VHF
fl_output5 = arcpy.MakeFeatureLayer_management(fc_output5, fl_output5).getOutput(0)
fl_output6 = arcpy.MakeFeatureLayer_management(fc_output6, fl_output6).getOutput(0)
fl_output7 = arcpy.MakeFeatureLayer_management(fc_output7, fl_output7).getOutput(0)
fl_output8 = arcpy.MakeFeatureLayer_management(fc_output8, fl_output8).getOutput(0)
print(fl_output5)
print(fl_output6)
print(fl_output7)
print(fl_output8)
m.addLayerToGroup(model_output_group2, fl_output5)
m.addLayerToGroup(model_output_group2, fl_output6)
m.addLayerToGroup(model_output_group2, fl_output7)
m.addLayerToGroup(model_output_group2, fl_output8)

#Lband
fl_output9 = arcpy.MakeFeatureLayer_management(fc_output9, fl_output9).getOutput(0)
fl_output10 = arcpy.MakeFeatureLayer_management(fc_output10, fl_output10).getOutput(0)
fl_output11 = arcpy.MakeFeatureLayer_management(fc_output11, fl_output11).getOutput(0)
fl_output12 = arcpy.MakeFeatureLayer_management(fc_output12, fl_output12).getOutput(0)
print(fl_output9)
print(fl_output10)
print(fl_output11)
print(fl_output12)
m.addLayerToGroup(model_output_group3, fl_output9)
m.addLayerToGroup(model_output_group3, fl_output10)
m.addLayerToGroup(model_output_group3, fl_output11)
m.addLayerToGroup(model_output_group3, fl_output12)

#Xband
fl_output13 = arcpy.MakeFeatureLayer_management(fc_output13, fl_output13).getOutput(0)
fl_output14 = arcpy.MakeFeatureLayer_management(fc_output14, fl_output14).getOutput(0)
fl_output15 = arcpy.MakeFeatureLayer_management(fc_output15, fl_output15).getOutput(0)
fl_output16 = arcpy.MakeFeatureLayer_management(fc_output16, fl_output16).getOutput(0)
print(fl_output13)
print(fl_output14)
print(fl_output15)
print(fl_output16)
m.addLayerToGroup(model_output_group4, fl_output13)
m.addLayerToGroup(model_output_group4, fl_output14)
m.addLayerToGroup(model_output_group4, fl_output15)
m.addLayerToGroup(model_output_group4, fl_output16)

 

Told ya it was long.  Thank you for any help you can give me.

Oh ArcGIS Pro 2.8.3

0 Kudos
1 Reply
JohannesLindner
MVP Regular Contributor

Sadly, the code-button is pretty well hidden...

JohannesLindner_0-1635145294766.png

JohannesLindner_1-1635145320787.png

 

You're repeating a lot of stuff. Your code can be considerably shortened by separating data (your paths) and logic (what to do with them). Also, your GDB path is the same for all layers, so you should just concatenate the feature class names to that path.

You weren't doing anything with the blank group layer, so I omitted that part. If you tell me what you want to do with it, I can insert it again. Nevermind, I'm stupid.

 

 

 

import arcpy
import os

# Change your path here, only once
gdb_path = r'C:\Users\Stak1\Documents\ArcGIS\Projects\20211015-20211022\20211015-20211022.gdb'

# Define your output groups
# {group_layer_name: [[fc_name, layer_name]]}
# You name the layers exactly as the feature classes, so this is a little
# overkill/too abstacted, but this way, you have the ability to name your
# layers independently from the fc in the future.
model_output_groups = {
    "UHF": [
        ["UHF_SINGLE", "UHF_SINGLE"],
        ["UHF_MULTI", "UHF_MULTI"],
        ["UHF_ELLIPSES_SINGLE", "UHF_ELLIPSES_SINGLE"],
        ["UHF_ELLIPSES_MULTI", "UHF_ELLIPSES_MULTI"],
        ],
    "VHF": [
        ["VHF_SINGLE", "VHF_SINGLE"],
        ["VHF_MULTI", "VHF_MULTI"],
        ["VHF_ELLIPSES_SINGLE", "VHF_ELLIPSES_SINGLE"],
        ["VHF_ELLIPSES_MULTI", "VHF_ELLIPSES_MULTI"],
        ],
    "LBand": [
        ["Lband_SINGLE", "Lband_SINGLE"],
        ["Lband_MULTI", "Lband_MULTI"],
        ["Lband_ELLIPSES_SINGLE", "Lband_ELLIPSES_SINGLE"],
        ["Lband_ELLIPSES_MULTI", "Lband_ELLIPSES_MULTI"],
        ],
    "XBand": [
        ["Xband_SINGLE", "Xband_SINGLE"],
        ["Xband_MULTI", "Xband_MULTI"],
        ["Xband_ELLIPSES_SINGLE", "Xband_ELLIPSES_SINGLE"],
        ["Xband_ELLIPSES_MULTI", "Xband_ELLIPSES_MULTI"],
        ]
    }


aprx = arcpy.mp.ArcGISProject('CURRENT')
m = aprx.listMaps('*')[0]
blank_group_layer = arcpy.mp.LayerFile(r"C:\Users\Stak1\Documents\ArcGIS\Projects\Layer_Symbology_Reference\RFGeo_Folder.lyrx") #created previously in ArcPro and exported as lyrx file
m.addLayer(blank_group_layer, "TOP")

for group in model_output_groups:
    group_layer = m.listLayers(group)[0]
    print(group_layer)
    for model_output in model_output_groups[group]:
        fc = os.path.join(gdb_path, model_output[0])
        layer = arcpy.management.MakeFeatureLayer(fc, model_output[1]).getOutput(0)
        print(layer)
        m.addLayerToGroup(group_layer, layer)

 

 

 

 


Have a great day!
Johannes