3

I would like to achieve the following functionality through arcpy:

  1. Querying an existing baselayer on time and create a new layer
  2. Apply symbology from an existing layer and add the new layer to the current session's data frame on top of the basemap

Have given my code below which gives me an Assertion Error when I try to add the layer returned from arcpy.MakeFeature. I am missing something trying to impact the current session. If I run the arcpy.MakeFeatureLayer_management command on the python console it works fine. But I am not able to get it work through the scripts in the toolbox. Please advice me on this.

import arcpy
arcpy.env.overwriteOutput = True
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
arcpy.MakeFeatureLayer_management ("base_layer", "new_layer_1","DATE_TIME" = TO_DATE('13-JUL-2010 12:40:00','DD-MON-YYYY HH24:MI:SS'))
arcpy.ApplySymbologyFromLayer_management("out_time",r"C:\WIP\Shape files\KARTHIK.TEST_TABLE.lyr")
arcpy.mapping.AddLayer(df,out_time)
arcpy.RefreshActiveView()
2
  • Could you post the actual error message that you're seeing?
    – Roy
    Commented Jun 13, 2012 at 11:29
  • 1
    First note is that MapDocument("CURRENT") only applies when in ArcMap, to the currently open map document - otherwise it requires a file path.
    – om_henners
    Commented Jun 13, 2012 at 11:41

2 Answers 2

4

I suspect the issue you're having with the MakeFeatureLayer_management command is that the double quotes aren't escaped correctly in your where string.

arcpy.MakeFeatureLayer_management ("base_layer", "new_layer_1","\"DATE_TIME\" = TO_DATE('13-JUL-2010 12:40:00','DD-MON-YYYY HH24:MI:SS')")

This will make you a Python Object of the Layer class. You then need to save the lyr to disk before it can be used in ArcMap;

Layers created in ArcCatalog cannot be used in ArcMap unless they are saved to a layer file using the Save_To_Layer_File tool (Make Feature Layer documentation)

This appears to hold true if your run the tool from a Python script or a model outside ArcMap also.

As such you need to do the following (which is a bit convoluted to say the least!):

  1. Save the layer to disk
  2. Create a layer object from the file on disk
  3. Add the object to the map
  4. Apply symbology to the object in the map

And your code should look something like:

arcpy.MakeFeatureLayer_management ("base_layer", "new_layer_1","\"DATE_TIME\" = TO_DATE('13-JUL-2010 12:40:00','DD-MON-YYYY HH24:MI:SS')")
arcpy.SaveToLayerFile_management("new_layer_1", "file_path.lyr", "ABSOLUTE")
lyr = arcpy.mapping.Layer("file_path.lyr")
arcpy.mapping.AddLayer(df, lyr)
src_lyr = arcpy.mapping.Layer(r"C:\WIP\Shape files\KARTHIK.TEST_TABLE.lyr")
arcpy.mapping.UpdateLayer(df, arcpy.mapping.ListLayers(mxd, "new_layer_1", df)[0], src_lyr, True)
3

The problem is that Feature Classes reside as layers in memory. If you want the layer to persist after your script / ArcMap session you have to actually save it to disk.

I think what you're looking for is the Select tool -- much like the MakeFeatureLayerTool, but it creates a permanent feature class (or shapefile). It takes a feature class as an input, allows you to set the output location and optionally takes a where clause to limit features in the selection.

Select_analysis (in_features, out_feature_class, {where_clause})

Use the same ApplySymbologyFromLayer that you've already got based on the output of the Select gp tool.

Your Answer

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

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