0

I want to symbolize a layer in an active session and add to display if some kind of value is right.

I found a way to symbolize in an active session here

So i tried to extend it, but unfortunately it doesn't work that way

myMXD = arcpy.mapping.MapDocument("Current")
lstDataFrames = arcpy.mapping.ListDataFrames(myMXD)
dlrLayer = arcpy.mapping.ListLayers(myMXD,"layer_name")
allFrames = arcpy.mapping.ListDataFrames(myMXD)
dlr_rows = arcpy.SearchCursor(path)

for dlr_row in dlr_rows:
    if dlr_row.getValue("field_name") == "Value":
        for dataFrame in allFrames:
            myMXD.activeView = dataFrame
        for mapLayer in dlrLayer:
            arcpy.ApplySymbologyFromLayer_management(mapLayer, path)
    else:
        arcpy.AddMessage("Error")

How could i do that?

4
  • Could you explain what happens when you try and extend the principle. Is there an error? If so what is it and where does it happen?
    – TSJ
    Commented Dec 9, 2013 at 14:35
  • Just found it out myself. I used the searchCursor before. I created a new one and then it worked
    – Simmal
    Commented Dec 9, 2013 at 15:07
  • Great, well done finding it yourself.
    – TSJ
    Commented Dec 9, 2013 at 15:13
  • 2
    Glad that you found a solution to the problem. For the benefit of others who may be trying to do something similar, it would be helpful if you were to write up you did, including the solution, and enter it as an answer to this question. Commented Dec 9, 2013 at 16:06

2 Answers 2

0

So the problem was that the searchCursor "dlr_rows" has been used in another function. I created a new searchCursor "dlr_checks" and it worked. At least for one Layer.

I could simplify the script a bit, but i encountered a problem when using it for 2 or more layers. The first layer in the "dlr_checks"-for-loop is getting symbolized, but the script won't do it for the next layers in the next for-loops.

Does anyone maybe have an idea about that? Is it maybe the lyr-files that won't fit, because i only used the dlr-layer to create them?

dlr_checks = arcpy.SearchCursor(dlr)
fwr_checks = arcpy.SearchCursor(fwr)

myMXD = arcpy.mapping.MapDocument("Current")

dlrLayer = arcpy.mapping.ListLayers(myMXD, "Layer_1")
fwrLayer = arcpy.mapping.ListLayers(myMXD, "Layer_2")


for dlr_check in dlr_checks:
    if dlr_check.getValue("field_name") == "value_1":
        for dlr_mapLayer in dlrLayer:
            arcpy.ApplySymbologyFromLayer_management(dlr_mapLayer, "value_1.lyr")
    elif dlr_check.getValue("field_name") == "value_2":
        for dlr_mapLayer in dlrLayer:
            arcpy.ApplySymbologyFromLayer_management(dlr_mapLayer, "value_2.lyr")


for fwr_check in fwr_checks:
    if fwr_check.getValue("field_name") == "value_1":
        for fwr_mapLayer in fwrLayer:
            arcpy.ApplySymbologyFromLayer_management(fwr_mapLayer, "value_1.lyr")
    elif fwr_check.getValue("field_name") == "value_2":
        for fwr_mapLayer in fwrLayer:
            arcpy.ApplySymbologyFromLayer_management(fwr_mapLayer, "value_2.lyr")
4
  • How about a definition query limiting the layers to "value_1" and "value_2", then apply symbology from ".lyr-file", where symbology is set for those values?
    – gm70560
    Commented Dec 10, 2013 at 17:59
  • Where should i place that definition query? So you think it might be a problem with the searchCursor-objects?
    – Simmal
    Commented Dec 10, 2013 at 21:45
  • I think I wasn't clear on your intent. What does ".lyr-file" do when applied? If you want to only display and symbolize "value_1 & 2", look at def. query
    – gm70560
    Commented Dec 10, 2013 at 23:18
  • Ok. I think that might be kind of my problem. Because these are 2 different lyr-files. I just edited it in the code. So I'm trying to find a specific value in a field of the first feature class and symbolize it. If it is another value then symbolize the way i want it for the other value. that works for the first feature class (first for loop). But it doesn't work for the next feature classes (next for loop)
    – Simmal
    Commented Dec 11, 2013 at 9:09
0

Now i finally got that going for me.

I just tried different things and left out the "ListLayers"-Function

This script symbolizes the desired layers in an active ArcMap session.

dlr_checks = arcpy.SearchCursor(dlr)
fwr_checks = arcpy.SearchCursor(fwr)


for dlr_check in dlr_checks:
    if dlr_check.getValue("field_name") == "value_1":
        arcpy.ApplySymbologyFromLayer_management("Layer_1", "value_1.lyr")
    elif dlr_check.getValue("field_name") == "value_2":
        arcpy.ApplySymbologyFromLayer_management("Layer_2", "value_2.lyr")


for fwr_check in fwr_checks:
    if fwr_check.getValue("field_name") == "value_1":
        arcpy.ApplySymbologyFromLayer_management("Layer_1", "value_1.lyr")
    elif fwr_check.getValue("field_name") == "value_2":
        arcpy.ApplySymbologyFromLayer_management("Layer_2", "value_2.lyr")

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.