Friday, 27 January 2017

arcpy.mapping.MapDocument("CURRENT")


Can't share my script as geoprocesing service. The warning message: 00068 Script Script contains broken project data source: CURRENT Here is this part of the script, where the "CURRENT" mapping module is:


# get the map document
mxd = arcpy.mapping.MapDocument("CURRENT")
# get the data frame
df = arcpy.mapping.ListDataFrames(mxd,"*")[0]
# create a new layer
newlayer = arcpy.mapping.Layer("c:/temperature/result/atlagint.shp")
# add the layer to the map at the top of the TOC in data frame 0
arcpy.mapping.AddLayer(df, newlayer,"TOP")

sourceLayer = "c:/temperature/result/symbology.lyr"
layerSymb = arcpy.mapping.Layer(sourceLayer)
updateLayer = arcpy.mapping.ListLayers(mxd, "atlagint", df)[0]
arcpy.mapping.UpdateLayer(df, updateLayer, layerSymb, "TRUE")
arcpy.RefreshTOC()

Answer



You can't add a shapefile to the TOC of ArcMap with arcpy.mapping.AddLayer(). This function will only work with .lyr files or map layers already present in an mxd.


In your case, an alternative would be adding the .lyr file first (sourceLayer) and replacing its source with arcpy.mapping.replaceDataSource():


import os
# get the map document

mxd = arcpy.mapping.MapDocument("CURRENT")
# get the data frame
df = arcpy.mapping.ListDataFrames(mxd,"*")[0]

sourceLayer = arcpy.mapping.Layer("c:/temperature/result/symbology.lyr")
arcpy.mapping.AddLayer(df, sourceLayer,"TOP")

updateLayer = arcpy.mapping.ListLayers(mxd)[0]

newlayer = arcpy.mapping.Layer("c:/temperature/result/atlagint.shp")

newlayer_path = os.path.dirname(newlayer)
newlayer_name = os.path.basename(newlayer)

updateLayer.replaceDataSource(newlayer_path, "SHAPEFILE_WORKSPACE", newlayer_name)

arcpy.RefreshTOC()

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