Wednesday, 6 December 2017

Adding Shapefile with Symbology from Layer File to ArcMap MXD using ArcPy?


I have a Shapefile:




  • C:\DATA\points.shp


A layer file:



  • C:\SYMBOLOGY\symbology.lyr


And a map document:



  • C:\MAPS\map.mxd



How can I add points.shp to map.mxd with the properties of symbology.lyr all using python?



Answer



I just tested this and it worked!


The layer file that I added, and then changed the data source of, was saved from a layer called dummy that pointed at a dummy.shp file.


import arcpy

mxd = arcpy.mapping.MapDocument("C:\\MAPS\\map.mxd")
df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]


addLayer = arcpy.mapping.Layer("C:\\SYMBOLOGY\\symbology.lyr")
arcpy.mapping.AddLayer(df, addLayer)
lyr = arcpy.mapping.ListLayers(mxd,"dummy",df)[0]

print lyr.name
print lyr.dataSource

lyr.replaceDataSource("C:\\DATA\\", "SHAPEFILE_WORKSPACE", "points")
lyr.name = "points"


print lyr.dataSource
print lyr.name

mxd.saveACopy("C:\\MAPS\\map2.mxd")

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...