Jump to content

Our forum made possible by displaying online advertisements to our visitors.

Please consider supporting us by disabling your ads blocker.

  • Announcements

    • EmperoR

      Dear Guests   12/24/2016

      Welcome to the GIS-AREA. like most online communities you must register to view or post in our community, but don't worry this is a simple free process that requires minimal information for you to signup. Be a part of GIS-AREA by signing in or creating an account here. Once you have created your account and have introduced yourself to our community, you can start new topics, reply to others, subscribe interesting threads, get your customize profiles, make news friends, and others nice stuff. To make your stay as pleasant and constructive as possible, please don't forget to read through our pinned forum rules and board guidelines in each section before you do anything else.
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

×

Important Information

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