Solved

Reading Attributes from a File Geodatabase and create Features via Python

  • 2 August 2016
  • 2 replies
  • 3 views

Hi,

 

I want to read only one Attribute(FGDB_ID) from a File Geodatabase with Python.The geometry is not required. The readout of the File Geodatebase works well, but no features are generated. 

The Attributes will be printed only in the log window, but no Features will be generated.

How can I create features from the individual values?

import fmeobjects
import arcpy
# Template Class
class FeatureCreator(object):
    def __init__(self):
        # Name der FC
        fc = "\\\\path\\gdb.gdb\\GK_ANNO"
        # Ergebnisliste
        FGDB_ID = []

        # ArcPy-Suchcursor erstellen und auf die entsprechende Spalte der FC
        # ansetzen.
        cursor = arcpy.SearchCursor(fc, "", "", "FGDB_ID")

        # Spalte zeilenweise auslesen...
        for row in cursor:

        # ... und FGDB_ID an Ergebnisliste anhängen.
            FGDB_ID.append(row.FGDB_ID)

        #pprint(FGDB_ID)
        for id in FGDB_ID:
            print(id)
        pass
    def close(self):
        feature = fmeobjects.FMEFeature()
        self.pyoutput(feature)

And sorry for my bad english ;)

icon

Best answer by david_r 2 August 2016, 17:16

View original

2 replies

Userlevel 4

Two things:

First, you are currently creating one feature in the close() method, but you do not set any attributes. 

Secondly, you create a local list object FGDB_ID in the __init__() method, but it is not visible outside that method. You can convert your local list object to a class instance object by prefixing it with "self."

Try something like this (untested) where we loop over the list in the close() method, and for each ID we create a new feature with an attribute "FGDB_ID":

import fmeobjects
import arcpy
# Template Class
class FeatureCreator(object):
    def __init__(self):
        # Name der FC
        fc = "\\\\path\\gdb.gdb\\GK_ANNO"
        # Ergebnisliste
        self.FGDB_ID = []
 
        # ArcPy-Suchcursor erstellen und auf die entsprechende Spalte der FC
        # ansetzen.
        cursor = arcpy.SearchCursor(fc, "", "", "FGDB_ID")
 
        # Spalte zeilenweise auslesen...
        for row in cursor:
 
        # ... und FGDB_ID an Ergebnisliste anhängen.
            self.FGDB_ID.append(row.FGDB_ID)

    def close(self):
        for id in self.FGDB_ID:
            feature = fmeobjects.FMEFeature()
            feature.setAttribute('FGDB_ID', id)
            self.pyoutput(feature)

Thanks. It Works.

Reply