Skip to main content
spelling and grammar corrections
Source Link
Fezter
  • 22k
  • 11
  • 71
  • 126

Can´t select Can't Select and exportExport from python scriptPython Script

I am a novice in python programingprogramming but I am trying otto 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 writinggot stuck with this function a script that is tryingwhile back and can't seem to select all data whit in a mxd within a 70m distance fromfind a layer and then export that data tosolution or a newly created FDBworkaround. I can´t seem to get this to work, all I get is the error:

FDOI 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: -2147024894 [GDB_Items]

The table was not found. [Output name]

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 FDB whitfile geodatabase with the “correct” output names.

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"

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

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.

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"

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

Can't Select and Export from Python Script

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.

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?

added 255 characters in body
Source Link

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?

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?

added 8 characters in body
Source Link
import arcpy, os, re
 from unicodedata import normalize

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

def Select_and_Export (Mxd_Natur, Skapa_mapp, Vag):
    Db = Skapa_mapp +"KMZ\KMZ_Databas+"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 layersobjekts were selected"
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 layers were selected"
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"
deleted 1 characters in body
Source Link
Loading
deleted 1 characters in body
Source Link
Loading
added 71 characters in body
Source Link
Loading
Source Link
Loading