Thursday 16 May 2019

arcgis desktop - Adding shapefile as layer in ArcMap via Python scripting?



I am trying to automate various tasks in ArcGIS Desktop (using ArcMap generally) with Python, and I keep needing a way to add a shapefile to the current map. (And then do stuff to it, but that's another story).


The best I can do so far is to add a layer file to the current map, using the following ("addLayer" is a layer file object):


def AddLayerFromLayerFile(addLayer): 
import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
arcpy.mapping.AddLayer(df, addLayer, "AUTO_ARRANGE")
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
del mxd, df, addLayer


However, my raw data is always going be shapefiles, so I need to be able to open them. (Equivantly: convert a shapefile to a layer file without opening it, but I'd prefer not to do that).




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...