Select to view content in your preferred language

Loop Array and Set Variables

710
3
Jump to solution
03-01-2023 03:35 PM
kapalczynski
Occasional Contributor III

I probably am going about this completely wrong.. but stuck... I HOPE this is going to make sense ... my mind is spinning... 

I am trying to set a few variables at the top of the script.  This script is used over and over again with minor tweaks by someone that does not understand python that well (including me)

The purpose is for someone to modify the script and add or Remove Feature Classes that will be read later in the script for COPY etc. 

  • As you can see below I have LayerA_DEV and Layer B_DEV in a DEV DB. 
  • I am defining their new location in a TEST DB with LayerA_Tst and LayerBTst

Users can add a Layer C or Layer D if they choose....

They would then have to create a NEW layersToCopy variables like the two variables layersToCopy1 and layersToCopy2 which points to the To and FROM

I then packaged those into 1 array that I read later.

I am trying to read the MAIN array which gives me 2 elements per read....

As the amount of Layers being read (LayerA_DEV, LayerB_DEV, LayerC_DEV) can increase or decrease I dont know how may variables I would need and dont want to the users to have to comb through the script to add them layer.... 

If they wanted to add a layer for processing they would only have to add:

LayerC_DEV = "Database Connections\\xxx.sde\\GIS_DATA.aaa_NEWFC"

LayerC_Tst = "Database Connections\\yyy.sde\\GIS_DATA.aaa_NEWFC"

layersToCopy3 = [LayerC_DEV, LayerC_Tst]

layersToCopy = [layersToCopy1,layersToCopy2, layersToCopy3 ]

 

All the code below would read this and there would be no need by the user to modify anything else...

How can I cycle through the layersToCopy Variable and create the required variables to populate  to below 

If I can get those written I could cycle through the layersToCopy Array and set the parameters for the Copy_Management 

 

 

 #  THIS IS WHAT I AM TRYING TO DO IN THE FOR LOOP

for layers in layersToCopy:
    # Read the First Value in layersToCopy [LayerA_DEV, LayerA_Tst]z
    # Break apart into 2 varaibles run copy
    arcpy.Copy_management(LayerA_DEV, LayerA_Tst)

    # Read the Second Value in layersToCopy [LayerB_DEV, LayerB_Tst]
    # Break apart into 2 varaibles run copy
    # arcpy.Copy_management(LayerB_DEV, LayerB_Tst)

    # Resulting in 2 new Feature Classes copied to a new DB

 

 

 

LayerA_DEV = "Database Connections\\xxx.sde\\GIS_DATA.aaa_LOGSHEET"
LayerB_DEV = "Database Connections\\xxx.sde\\GIS_DATA.aaa_PERMIT_I"

LayerA_Tst = "Database Connections\\yyy.sde\\GIS_DATA.aaa_LOGSHEET"
LayerB_Tst = "Database Connections\\yyy.sde\\GIS_DATA.aaa_PERMIT_I"

layersToCopy1 = [LayerA_DEV, LayerA_Tst]
layersToCopy2 = [LayerB_DEV, LayerB_Tst]
layersToCopy = [layersToCopy1,layersToCopy2]

# I want to loop thorugh layersToCopy and create 4 variables that I can use to Copy Features
for layers in layersToCopy:
    for copyLayers in layers:
        print(copyLayers)

 

 

 

 

RESULT:

Database Connections\xxx.sde\GIS_DATA.aaa_LOGSHEET
Database Connections\xxx.sde\GIS_DATA.aaa_LOGSHEET
Database Connections\yyy.sde\GIS_DATA.aaa_PERMIT_I
Database Connections\yyy.sde\GIS_DATA.aaa_PERMIT_I

0 Kudos
2 Solutions

Accepted Solutions
kapalczynski
Occasional Contributor III

Think I got it.....

The one thing I can count on is there will only be a From and To so I could use something like this:

one,two = layers 

 

I think this is how its split?

Any thoughts?

# Local variables:
LayerA_DEV = "Database Connections\\xxx.sde\\GIS_DATA.aaa_LOGSHEET"
LayerB_DEV = "Database Connections\\yyy.sde\\GIS_DATA.aaa_PERMIT_I"
LayerC_DEV = "Database Connections\\zzz.sde\\GIS_DATA.aaa_PERMIT_C"

LayerA_Tst = "Database Connections\\xxx.sde\\GIS_DATA.aaa_LOGSHEET"
LayerB_Tst = "Database Connections\\yyy.sde\\GIS_DATA.aaa_PERMIT_I"
LayerC_Tst = "Database Connections\\zzz.sde\\GIS_DATA.aaa_PERMIT_C"

layersToCopy1 = [LayerA_DEV, LayerA_Tst]
layersToCopy2 = [LayerB_DEV, LayerB_Tst]
layersToCopy3 = [LayerC_DEV, LayerC_Tst]

layersToCopy = [layersToCopy1,layersToCopy2,layersToCopy3]

def copyFeatures():
    for layers in layersToCopy:
        one, two = layers
        print("Variables as assigned are : ")
        fromFC = (str(one))
        toFC = (str(two))
        print("Going from" + fromFC)
        print("Going to" + toFC)
        #arcpy.Copy_management(fromFC, toFC)

if __name__ == '__main__':
    copyFeatures()

 

View solution in original post

0 Kudos
DanielMiranda2
Occasional Contributor

A possible alternative is to use a Python dictionary. Then you only need to update the dictionary and it could be of any size without having to add/remove additional variables.

# Note the format of the dictionary. You pair your source and destination separated by a colon. Then add a comma and repeat for each source/destination pair. The whole thing is wrapped in curly brackets.

layers_to_copy = {
"Database Connections\\xxx.sde\\GIS_DATA.aaa_LOGSHEET": "Database Connections\\yyy.sde\\GIS_DATA.aaa_LOGSHEET",
"Database Connections\\xxx.sde\\GIS_DATA.aaa_PERMIT_I": "Database Connections\\yyy.sde\\GIS_DATA.aaa_PERMIT_I"
}
			
# You could put this portion in a function as you did.
for layer in layers_to_copy.keys():
   fromFC = layer
   toFC = layers_to_copy[layer]
   print(f'Going from {fromFC}')
   print(f'Going to {toFC}')
   arcpy.Copy_management(fromFC, toFC)

 

View solution in original post

3 Replies
kapalczynski
Occasional Contributor III

Think I got it.....

The one thing I can count on is there will only be a From and To so I could use something like this:

one,two = layers 

 

I think this is how its split?

Any thoughts?

# Local variables:
LayerA_DEV = "Database Connections\\xxx.sde\\GIS_DATA.aaa_LOGSHEET"
LayerB_DEV = "Database Connections\\yyy.sde\\GIS_DATA.aaa_PERMIT_I"
LayerC_DEV = "Database Connections\\zzz.sde\\GIS_DATA.aaa_PERMIT_C"

LayerA_Tst = "Database Connections\\xxx.sde\\GIS_DATA.aaa_LOGSHEET"
LayerB_Tst = "Database Connections\\yyy.sde\\GIS_DATA.aaa_PERMIT_I"
LayerC_Tst = "Database Connections\\zzz.sde\\GIS_DATA.aaa_PERMIT_C"

layersToCopy1 = [LayerA_DEV, LayerA_Tst]
layersToCopy2 = [LayerB_DEV, LayerB_Tst]
layersToCopy3 = [LayerC_DEV, LayerC_Tst]

layersToCopy = [layersToCopy1,layersToCopy2,layersToCopy3]

def copyFeatures():
    for layers in layersToCopy:
        one, two = layers
        print("Variables as assigned are : ")
        fromFC = (str(one))
        toFC = (str(two))
        print("Going from" + fromFC)
        print("Going to" + toFC)
        #arcpy.Copy_management(fromFC, toFC)

if __name__ == '__main__':
    copyFeatures()

 

0 Kudos
DanielMiranda2
Occasional Contributor

A possible alternative is to use a Python dictionary. Then you only need to update the dictionary and it could be of any size without having to add/remove additional variables.

# Note the format of the dictionary. You pair your source and destination separated by a colon. Then add a comma and repeat for each source/destination pair. The whole thing is wrapped in curly brackets.

layers_to_copy = {
"Database Connections\\xxx.sde\\GIS_DATA.aaa_LOGSHEET": "Database Connections\\yyy.sde\\GIS_DATA.aaa_LOGSHEET",
"Database Connections\\xxx.sde\\GIS_DATA.aaa_PERMIT_I": "Database Connections\\yyy.sde\\GIS_DATA.aaa_PERMIT_I"
}
			
# You could put this portion in a function as you did.
for layer in layers_to_copy.keys():
   fromFC = layer
   toFC = layers_to_copy[layer]
   print(f'Going from {fromFC}')
   print(f'Going to {toFC}')
   arcpy.Copy_management(fromFC, toFC)

 

kapalczynski
Occasional Contributor III

@DanielMiranda2   Thanks so much... yea this is what I had envisioned ... much more simple.... I am going to mark this as the solution ... Cheers.. and thank you