[GIS] Can’t Select and Export from Python Script

arcgis-10.1arcpyexportpythonselect

I am a novice in python programming but I am trying to learn by doing.

I got stuck with this function a while back and can't seem to find a solution or a workaround.

I am writing a script that is trying to select all data in an MXD within a 70m distance from a layer and then export that data to a newly created file geodatabase.
I can´t seem to get this to work, all I get is the error:

FDO error: -2147024894 [GDB_Items]

The table was not found. [Output name]

And I somehow manage to get some kind of output, in the form of empty geodatabase tables in the output file geodatabase with the “correct” output names.

I have a suspicion that the problem lies in the feature layers origins. They reside deep in our file tree (for exampel G:\KONSULTUPPDRAG VMN\SAMRÅD\2012\Uppsala\C 55 Örsundsbro – Kvarnbo\Kartor\Grundkartor\Fastighetskartan.gdb\vo) there is nothing I can do about it unfortunately.

Here is my code:

import arcpy, os, re
from unicodedata import normalize

Skapa_mapp = (unicode ("C:\\Users\dabr\\Desktop\KMZtest\\", 'UTF-8'))
Mxd_Natur = arcpy.mapping.MapDocument((unicode ("C:\\Users\\dabr\\Desktop\\KMZ test\\YAYYY_Test1.mxd", 'UTF-8')))
Vag = "c 801"

def Select_and_Export (Mxd_Natur, Skapa_mapp, Vag):
    Db = Skapa_mapp +"KMZ\\KMZ_Databas.gdb\\"                        #Define the output DB

for df in arcpy.mapping.ListDataFrames(Mxd_Natur):                  #Find the road from whitch the selection is made with. 
    for lyr in arcpy.mapping.ListLayers(Mxd_Natur, "", df):
        if lyr.name.lower() == unicode (Vag, 'UTF-8'):
            Utgangslager = lyr

for df in arcpy.mapping.ListDataFrames(Mxd_Natur):                  
    for lyr in arcpy.mapping.ListLayers(Mxd_Natur, "", df):         #Loop trough all the layers in the MXD to make 
                                                                    #a selection and exprt if annything got selected
        Sokvag = lyr.workspacePath                                  #Get the path to the specific layer
        SokvagNamn = lyr.datasetName                                #Get the name of the specific layer
        File_path = Sokvag + "\\" + SokvagNamn                      #Combine to create full path
        print File_path

        if not Sokvag.endswith(".gdb"):                             #layers in mxd can be both from GDB and .shp
            File_path = File_path + ".shp"

        Namn = lyr.name.lower()                                     #    
        Namn = re.sub('[ ]', '_', Namn)                             #create a output name bades on the layer name 
        Namn = normalize('NFKD', Namn).encode('ascii', 'ignore')    #in the mxd


        if arcpy.Exists("TEMP"):                                    #if temporary file exis´ts from previous run, remove it
            arcpy.Delete_management("TEMP")
            print "temp removed"


        arcpy.management.MakeFeatureLayer(File_path,"TEMP")         #Recreata a empty templayer
        arcpy.SelectLayerByLocation_management("TEMP", "WITHIN_A_DISTANCE", Utgangslager, "70 Meters", "NEW_SELECTION") #populate the templayer whit selected objekts?
        out = os.path.join(Db, Namn)                                #Create a path to the output location

        print str(arcpy.GetCount_management("TEMP").getOutput(0))+" Selected from " + Namn      #Show number of selected objekts.

        if int(arcpy.GetCount_management("TEMP").getOutput(0)) > 0: #check if anny objects were selected. if not dont export
            try:

                if "TEMP".endswith(".shp"):                         #test if the temporary layer is based on a shp file?
                    print Namn + " skall shp kopieras"
                    arcpy.CopyFeatures_management("TEMP", out.rstrip(".shp"))        #Coppy selected objekts to FDB
                    print Namn + " shp kopierad"
                else:
                    print Namn + " skall koppiers"
                    arcpy.CopyFeatures_management("TEMP", out)      #Coppy selected objekts to FDB
                    print Namn + " är kopierad"

            except:
               print arcpy.GetMessages()


        else:
            print lyr.name.lower() + " no objekts were selected"

Does anyone see a solution to my problem or perhaps a workaround?

Edit: After some tweaking I stumbled upon the problem and solution. I was using a File GDB as an output in the CopyFeatures_management function. I don’t know why but if I changed it to a personal GDB everything went fine. Can anyone explain why?

Best Answer

I wonder if this will help?

replace the following line,

if "TEMP".endswith(".shp"):

with this line

if arcpy.Describe("TEMP").featureClass.catalogPath.endswith(".shp"): 
Related Question