Skip to main content
3 of 7
deleted 1 characters in body

Can´t select and export from python script

I am a novice in python programing but I am trying ot learn by doing.

I got stuck whit 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 whit in a mxd within a 70m distance from a layer and then export that data to a newly created FDB. 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 form of empty geodatabase tables in the output FDB whit 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 outpot location
                                          
        print str(arcpy.GetCount_management("TEMP").getOutput(0))+" Selected from " + Namn      #Show number of selecter objekts.
        
        if int(arcpy.GetCount_management("TEMP").getOutput(0)) > 0: #check if anny objects weref 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 layers were selected"

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