1

I have a script that I'm using to generate raster maps and automatically add them to the map display, but when they are added they are sourced to a temporary location. I use the same code for shapefiles and it links to the correct file.

Here's my code:

for fieldName in fieldList.split(';'):

    if IDW == 'true':

        # Run IDW
        outIDW = Idw(inPointFeatures, fieldName, cellSize, power, searchRadius)
        raster = fieldName + "b_idw.tif"
        outIDW.save(raster)

        # Convert raster to layer
        out_raster = fieldName + "_idw.lyr"
        display_raster = fieldName + "_idw.lyr"
        idw_raster = fieldName + "_idw.lyr"
        arcpy.MakeRasterLayer_management(raster, out_raster)
        arcpy.SaveToLayerFile_management(out_raster, display_raster, "ABSOLUTE")

        # Adjust symbology of IDW layer
        arcpy.ApplySymbologyFromLayer_management(display_raster, idw_symbology)
        arcpy.SaveToLayerFile_management(display_raster, idw_raster, "ABSOLUTE")

        # Add layer to map 
        mxd = arcpy.mapping.MapDocument("CURRENT")
        df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
        addLayer = arcpy.mapping.Layer(idw_raster)
        arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")

The code works properly and adds the map to the display, but it's sourced in this way...

Folder: C:\Users\User\AppData\Local\Temp 

Raster: x234bc54_d8c9_c34_by1c458

What I need is for the source to point to the original raster tiff or layer where the files are generated, not this temporary folder. Does anyone know how to accomplish this? I've tried using replaceDataSource and replaceWorkspacePaths, but have not succeeded.

1

If this is the entirety of your code it appears that you are not setting the workspace for the geo-processor.

When you create the raster output name with this line of code:

raster = fieldName + "b_idw.tif"

you are not specific where it should go. Therefore it appears to be defaulting to a default temp location. So I recommend you adjust this to include the full path name for the tif file, something like:

raster = "C:/myfolder/" + fieldName + "b_idw.tif"
  • It's not the full code. I have the workspace set using these lines: workspace = arcpy.GetParameterAsText(2) arcpy.env.workspace = workspace – cartoscience Nov 2 '14 at 16:23
  • I've also tried using the direct path instead of a parameter and I get the same result. I have a shapefile in the same script and it doesn't have this problem. Also, the raster and layer files appear in the correct folder, but this temporary location is used as the source when it's displayed. – cartoscience Nov 2 '14 at 16:24
  • Try setting this line to a full path: addLayer = arcpy.mapping.Layer(idw_raster)? – Hornbydd Nov 2 '14 at 19:49
  • Like this? addLayer = arcpy.mapping.Layer("C:/output/" + fieldName + "_idw.lyr") I get this error: <type 'exceptions.ValueError'>: Object: CreateObject Layer invalid data source – cartoscience Nov 2 '14 at 22:47
  • @bleegp Would you be able to edit your question to expand your code snippet to one that works up to where you are stuck, please? – PolyGeo Sep 1 '15 at 1:48
0

Try replacing the raster layer data source after you apply symbology:

obj_lyr.replaceDataSource(os.path.dirname(sPath_fc_grid), "FILEGDB_WORKSPACE", os.path.basename(sPath_fc_grid))  
  • 1
    Welcome to GIS SE! As a new user be sure to take the Tour. Your answer here references a layer object (obj_lyr) and library (os) that the asker is not using. I recommend that you edit your answer to more closely address the question. – PolyGeo Sep 1 '15 at 1:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.