Jump to content
Sign in to follow this  
Reyalino

Add Multiple Features Arcpy

Recommended Posts

Hi All,

Using arcpy mapping module I am trying to add multiple shapefiles

import arcpy
arcpy.env.workspace = "D:/Directory/Map"

mxd = arcpy.mapping.MapDocument("D:/Directory/Map/layout.mxd")
listFC = arcpy.ListFeatureClasses()

for fc in listFC:
  df = arcpy.mapping.ListDataFrames(mxd)[0]
  layer = arcpy.mapping.Layer(fc)
  arcpy.mapping.AddLayer(df,layer,"TOP")
  
                        

When I run those script via jupyter notebook only 1 shapefile is added to the arcgis. But when I run it through Python Window on ArcGIS it adds all the shapefiles but also sometimes only add 1 shapefile. Not sure why this happen. Could someone give me a clue? thanks ( I am using ArcGIS 10.5 Not the original :-D) 

 

regards

 

 

Share this post


Link to post
Share on other sites

dear Reyalino,

put

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

 

before "for loop",

import arcpy
arcpy.env.workspace = "D:/Directory/Map"

mxd = arcpy.mapping.MapDocument("D:/Directory/Map/layout.mxd")
listFC = arcpy.ListFeatureClasses()
df = arcpy.mapping.ListDataFrames(mxd)[0]
for fc in listFC:
  layer = arcpy.mapping.Layer(fc)
  arcpy.mapping.AddLayer(df,layer,"TOP")
  • Like 2

Share this post


Link to post
Share on other sites

thanks for your fast response yousef233. I have put the dataframelist before the loop it works but after the second trial it only added 1 single feature. I have made several trials by run the code via python window and jupyter notebook. I am just wondering, why the code sometime running properly but not in the other day. 

 

Share this post


Link to post
Share on other sites

do you work with layout.mxd or "current" ?

do you save it after adding layers?

 

works fine here

import arcpy
arcpy.env.workspace = r"C:\Users\Default.gdb"

mxd = arcpy.mapping.MapDocument(r"D:\Untitled.mxd")
listFC = arcpy.ListFeatureClasses()
df = arcpy.mapping.ListDataFrames(mxd)[0]
for fc in listFC:
  layer = arcpy.mapping.Layer(fc)
  arcpy.mapping.AddLayer(df,layer,"TOP")

mxd.save()
del mxd

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.