Copying from a list of feature layers using python

1486
8
07-24-2014 10:22 AM
MehrshadNourani
New Contributor III

I am trying to copy feature layers using the following code, but get no results. Can anyone help me understand why my code does not work?

import os

import arcpy

arcpy.env.workspace = ws = 'C:\\WorkSpace\\sahie_saipe\\sahie_saipe.gdb'

dictionary_Layer = {'co06_Layer':'co06', 'co07_Layer':'co07', 'cnty08_Layer':'co08', 'co09_Layer':'co09', 'co10_Layer':'co10', 'co11_Layer':'co11', 'co12_Layer':'co12', 'st_Select':'state'}

for layer in arcpy.mapping.ListLayers(mxd):

    if layer in dictionary_Layer:

        old = os.path.join(ws, layer)

        new = os.path.join(ws, dictionary_Layer[layer])

        try:

            arcpy.CopyFeatures_management(old,new,"")

            print 'copy %s to %s' %(layer, dictionary_Layer[layer])

        except:

            print 'Could not copy %s'

0 Kudos
8 Replies
KerryAlley
Occasional Contributor

Hello,

The variable "layer" represents the layer object, not the name of the layer (a string).  You therefore need to look for layer.name in the dictionary, and more specifically look in the list of the dictionary keys.  I've copied your code and edited lines 6-12 below for clarification... hopefully I didn't introduce typos!

Kerry

import os

import arcpy

arcpy.env.workspace = ws = 'C:\\WorkSpace\\sahie_saipe\\sahie_saipe.gdb'

dictionary_Layer = {'co06_Layer':'co06', 'co07_Layer':'co07', 'cnty08_Layer':'co08', 'co09_Layer':'co09', 'co10_Layer':'co10', 'co11_Layer':'co11', 'co12_Layer':'co12', 'st_Select':'state'}

for layer in arcpy.mapping.ListLayers(mxd):

    if layer.name in dictionary_Layer.keys():

        old = os.path.join(ws, layer.name)

        new = os.path.join(ws, dictionary_Layer[layer.name])

        try:

            arcpy.CopyFeatures_management(old,new,"")

            print 'copy %s to %s' %(layer.name, dictionary_Layer[layer.name])

        except:

            print 'Could not copy %s'(layer.name)

            print 'Could not copy %s'

IanMurray
Frequent Contributor

Hi Mehrshad,

First off, you never define what your mxd variable is.  To use the arcpy.mapping.ListLayers, you need to make a map document object which you can call the ListLayers on.    Use mxd = arcpy.mapping.MapDocument(r"Filepathtoyourmapdocumenthere") to create your MapDocument object first.

Secondly, you are copying feature layers and making feature classes it seems, which is fine, but you shouldn't need the old = os.path.join(ws, layer), since feature layers are stored in memory and not on the hard drive.  If you are referencing existing feature classes, then its fine as is.  If not though I would take out the old = , and put  in this CopyFeatures_management(layer.name, new) for your copy features statement.  Also you would need to use that layer.name for checking agaisnt your dictionary.

Hope this helps you out.

MehrshadNourani
New Contributor III

Kerry, Ian,

Thank you very much for  your help. I incorporated your suggestions (below) but no results!

@

import os 

import arcpy  

arcpy.env.workspace = ws = 'C:\\WorkSpace\\sahie_saipe\\sahie_saipe.gdb' 

dictionary_Layer = {'co06_Layer':'co06', 'co07_Layer':'co07', 'cnty08_Layer':'co08', 'co09_Layer':'co09', 'co10_Layer':'co10', 'co11_Layer':'co11', 'co12_Layer':'co12', 'st_Select':'state'}

mxd = arcpy.mapping.MapDocument('C:\\WorkSpace\\sahie_saipe\\sahie_saipe.mxd')

for layer in arcpy.mapping.ListLayers(mxd): 

    if layer.name in dictionary_Layer.keys(): 

#      old = os.path.join(ws, layer.name) 

       new = os.path.join(ws, dictionary_Layer[layer.name]) 

       try: 

          arcpy.CopyFeatures_management(layer.name, new,"") 

          print 'copy %s to %s' %(layer.name, dictionary_Layer[layer.name]) 

       except: 

          print 'Could not copy %s'(layer.name) 

          print 'Could not copy %s'

0 Kudos
KerryAlley
Occasional Contributor

Since Ian brought it up, I'll add that you don't need specify the full path for the output feature class either, as long as you want it to be saved in the already-specified workspace.

As with most geoprocessing tools, CopyFeatures_management honors selections and definition queries, so make sure you know what you're copying.

If the code below doesn't work, try commenting out the try and except lines (and de-indenting the arcpy.CopyFeatures_management and print lines) to see what error occurs.

Also, I found typos in my previous post.  There should only be one line (that looks like this) in the except block:

     print 'Could not copy %s'%layer.name

import os 

import arcpy  

arcpy.env.workspace = ws = 'C:\\WorkSpace\\sahie_saipe\\sahie_saipe.gdb' 

dictionary_Layer = {'co06_Layer':'co06', 'co07_Layer':'co07', 'cnty08_Layer':'co08', 'co09_Layer':'co09', 'co10_Layer':'co10', 'co11_Layer':'co11', 'co12_Layer':'co12', 'st_Select':'state'}

mxd = arcpy.mapping.MapDocument('C:\\WorkSpace\\sahie_saipe\\sahie_saipe.mxd')

for layer in arcpy.mapping.ListLayers(mxd): 

    if layer.name in dictionary_Layer.keys(): 

       try: 

          arcpy.CopyFeatures_management(layer.name, dictionary_Layer[layer.name]) #only 2 required parameters, full path not necessary to output to workspace

          print 'copy %s to %s' %(layer.name, dictionary_Layer[layer.name]) 

       except: 

          print 'Could not copy %s'%layer.name

0 Kudos
MehrshadNourani
New Contributor III

Kerry,

Thank you verey much for your help.  I'll try your suggestions and see if the code would work.

Best

RhettZufelt
MVP Frequent Contributor

Not anywhere I can test, but think it might be related to this line:

if layer.name in dictionary_Layer.keys():

In the past, to check if a value is in a dictionary, I used syntax similar to this to get the results:

if (dictionary_Layer.has_key(layer.name)):

     do stuff

R_

0 Kudos
KerryAlley
Occasional Contributor

Our methods of searching for keys have the same functionality, and they're both described here.

0 Kudos
XanderBakker
Esri Esteemed Contributor

actually..

if layer.name in dictionary_Layer:

... is enough, since it automatically checks for the keys...

0 Kudos