1

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()
asked Jan 8, 2015 at 15:36

1 Answer 1

2

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()
answered Jan 9, 2015 at 12:59

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.