Adding all GIS Layers from a folder

1658
10
10-06-2011 08:17 AM
Ryan_Galbraith
New Contributor III
Anyone know how to create a looping statement that adds all the GIS layers from a folder?  Using arcpy.mapping.

Here is what I got:

arcpy.env.workspace = '#whatever the folder is'
mxd = arcpy.mapping.MapDocument("Current")
fclist = arcpy.ListFeatureClasses()
for fc in fclist:
     .....#something to add to your feature classes
0 Kudos
10 Replies
by Anonymous User
Not applicable
Original User: jscheirer

arcpy.MakeFeatureLayer_management(fc, fc)
0 Kudos
JeffBarrette
Esri Regular Contributor
You are on the right track.  Check out the samples for MakeFeatureLayer.  Also check out arcpy.mapping.AddLayer.

Jeff
0 Kudos
by Anonymous User
Not applicable
Original User: rdg129

arcpy.MakeFeatureLayer_management(fc, fc)


No, that, didn't work, the two fcs can't be the same.
0 Kudos
JasonScheirer
Occasional Contributor III
The second one is the name of the layer in the TOC. If that's not working, something like this:


arcpy.env.workspace = '#whatever the folder is'
mxd = arcpy.mapping.MapDocument("Current")
fclist = arcpy.ListFeatureClasses()
for i, fc in enumerate(fclist):
    arcpy.CreateFeatureLayer_management(fc, "layer_%i" % i)
0 Kudos
by Anonymous User
Not applicable
Original User: rdg129

That doesn't work either.  Not sure why not it always brings up an error.
0 Kudos
RichardFairhurst
MVP Honored Contributor
That doesn't work either.  Not sure why not it always brings up an error.


Perhaps a dumb question, but what are you actually putting in the first line to specify the workspace folder?  If the path is unspecified or invalid (such as incorrect slashes, which is common with path strings), that could be the source of the error.

arcpy.env.workspace = 'C:\\GIS_DATA\\' # or r 'C:\GIS_DATA\'
mxd = arcpy.mapping.MapDocument("Current")
fclist = arcpy.ListFeatureClasses()
for i, fc in enumerate(fclist):
    arcpy.CreateFeatureLayer_management(fc, "layer_%i" % i)
0 Kudos
by Anonymous User
Not applicable
Original User: rdg129

I am using shapefiles, does that matter?
0 Kudos
RichardFairhurst
MVP Honored Contributor
I am using shapefiles, does that matter?


I think the problem is in the fact that the fc variable is a numeric value and not a feature class because it has been converted to an enumerated value.  The CreateFeatureLayer expects fc to be an actual feature class and not a number.  Separte the enumeration variable i from the fc variable.  Try the code below:

arcpy.env.workspace = 'C:\\GIS_DATA\\' # or r 'C:\GIS_DATA\'
mxd = arcpy.mapping.MapDocument("Current")
fclist = arcpy.ListFeatureClasses()
i = 0
for fc in fclist:
    i += 1
    arcpy.CreateFeatureLayer_management(fc, "layer_%i" % i)
0 Kudos
by Anonymous User
Not applicable
Original User: rdg129

GRRR.  Nothing is working.  thanks for all your help though.
0 Kudos