feature class list and display results in dataframe

3753
4
Jump to solution
12-15-2014 09:26 AM
GustavoCordero
New Contributor III

I'm using arcgis 10.2.2

 

 

I am trying to create a line of commands with python

list, every feature class in a feature dataset

and then display me in my data frame so that they can see on my map

 

example

 

# Set the workspace. List all of the polygon feature classes that
# start with 'G'
#
arcpy.env.workspace = "D:/St_Johns/data.gdb"
fcs = arcpy.ListFeatureClasses("G*", "polygon")

 

 

Now I need to have this result in my dataframe

  all polygons polygons starting with "G"

 

 

I have to do in my scrip to have the polygons in my dataframe?

 

0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

I assume based on your original question that you want to obtain all feature classes that start with "G" and add them to the current mxd. In that case you can do this:

#import modules
import os
import arcpy

# input workspace
ws = r'C:\Users\Infomacion.gdb'
arcpy.env.workspace = ws

# feature dataset list
datasets = arcpy.ListDatasets(feature_type='feature')
datasets.append('')

# create a list with the featureclasses
lst_fcs = []
for ds in datasets:
    for fc in arcpy.ListFeatureClasses(wild_card="G*", feature_type="polygon", feature_dataset=ds):
        path = os.path.join(ws, ds, fc)
        lst_fcs.append(path)

# create MXD and dataframe objects
mxd = arcpy.mapping.MapDocument ("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]

# loop through feature class list
for fc in lst_fcs:
    lyr = arcpy.mapping.Layer(fc)
    arcpy.mapping.AddLayer(df, lyr, "BOTTOM")

Basically, when you have the path to the featureclass, you can create a layer object (line 26) which can be added to the dataframe.

View solution in original post

4 Replies
XanderBakker
Esri Esteemed Contributor

When you have the list of featureclasses you can loop through the featureclass names and add them to you TOC.

Before the loop you should get your mxd object, the dataframe where you want to add the layers.

To get you going, have a look at the AddLayer method:ArcGIS Help (10.2, 10.2.1, and 10.2.2)

... and maybe you are interested in learning a little more on the arcpy.mapping module:ArcGIS Help (10.2, 10.2.1, and 10.2.2)

GustavoCordero
New Contributor III

I'm new to programming, you Might help me

I try to follow your advice

#import modules

import os

import arcpy

#create MXD project to know where to send the Feature classs

mxd = arcpy.mapping.MapDocument ("CURRENT")

df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]

#Route information

arcpy.env.workspace = r'C:\Users\Infomacion.gdb'

#feature class list

datasets = arcpy.ListDatasets(feature_type='feature')

datasets = [''] + datasets if datasets is not None else []

for ds in datasets:

    for fc in arcpy.ListFeatureClasses(feature_dataset=ds):

        path = os.path.join(arcpy.env.workspace, ds, fc)

        print path

#feature class list

arcpy.mapping.AddLayer(df, , "BOTTOM") #  

as should be the code in addLayer?

0 Kudos
XanderBakker
Esri Esteemed Contributor

I assume based on your original question that you want to obtain all feature classes that start with "G" and add them to the current mxd. In that case you can do this:

#import modules
import os
import arcpy

# input workspace
ws = r'C:\Users\Infomacion.gdb'
arcpy.env.workspace = ws

# feature dataset list
datasets = arcpy.ListDatasets(feature_type='feature')
datasets.append('')

# create a list with the featureclasses
lst_fcs = []
for ds in datasets:
    for fc in arcpy.ListFeatureClasses(wild_card="G*", feature_type="polygon", feature_dataset=ds):
        path = os.path.join(ws, ds, fc)
        lst_fcs.append(path)

# create MXD and dataframe objects
mxd = arcpy.mapping.MapDocument ("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]

# loop through feature class list
for fc in lst_fcs:
    lyr = arcpy.mapping.Layer(fc)
    arcpy.mapping.AddLayer(df, lyr, "BOTTOM")

Basically, when you have the path to the featureclass, you can create a layer object (line 26) which can be added to the dataframe.

GustavoCordero
New Contributor III

thank you very much everything works perfect

0 Kudos