Changing Symbology in Python

1889
2
Jump to solution
02-08-2017 10:12 PM
Leith_JohnHAWKINS
New Contributor III
Hi So I am working a project to create these maps and I am trying to get symbology to change but I cant 

import arcpy
from arcpy import env
arcpy.env.overwriteOutput = True
#Set File workspace
file_workspace = 'N:\\GIS\\Projects\\AA_Leith_Hawkins_TestBed\\Search_Cursor\\Search_Cursor.gdb'
env.workspace = file_workspace

#buffer set up
Holdings = 'N:\\GIS\\Projects\\AA_Leith_Hawkins_TestBed\\Search_Cursor\\Search_Cursor.gdb\\Data\\Holdings'
distances = [1000, 4000]
unit = "Meters"
#Make a feature layer
arcpy.MakeFeatureLayer_management(Holdings,'Holdings_Layer')

#searchcursor for evey row in dataset
with arcpy.da.SearchCursor(Holdings, ['Holding_Reference_Number'])as Holdings_Ref_cursor:
    for row in Holdings_Ref_cursor:
        print row[0]
        query = "Holding_Reference_Number = " + str(row[0])
        print query
        File_output = file_workspace+ '\\' 'Buffer_'+str(row[0])
        #print File_output

        #Select Feature using the reference number from holdings layer
        arcpy.SelectLayerByAttribute_management('Holdings_Layer', 'NEW_SELECTION',"Holding_Reference_Number = " + str(row[0]))

        # Export holding to geodatabase
        Holding_Boundary = file_workspace+ '\\' 'Holding_'+str(row[0])
        arcpy.management.CopyFeatures('Holdings_Layer', Holding_Boundary)

        #Mutliple ring Buffer using Selected Features
        arcpy.MultipleRingBuffer_analysis('Holdings_Layer', File_output, distances, unit, "", "ALL")
        arcpy.MakeFeatureLayer_management(File_output, 'Buffer_Layer')
       #arcpy.Buffer_analysis("Holdings_Layer", ofc, var_Buffer, "FULL", "ROUND", "ALL", "")
        print 'Buffer complete'

        #Intersect Features
        Intersect_out_features = file_workspace+ '\\' 'Intersect_'+str(row[0])
        arcpy.Intersect_analysis([Holdings,File_output],Intersect_out_features, "", "", "INPUT")
        print "intersect Complete"


        #delete Fields
        Drop_Fields = ['FID_Holdings','Holding_Reference_Number','Holding_Location_Address','Local_Land_Services_Region_Id',
                      'Local_Land_Services_Region_Name', 'RLPB_Board_Name', 'Property_Identification_Code','Occupier_Id',
                      'Occupier_Full_Name','Occupier_Mailing_Address','Occupier_Home_Phone','Occupier_Mobile_Phone',
                       'Occupier_Email_Address', 'Total_Area', 'Is_Rateable_Indicator','Rateable_Area','Nominal_Notional_Carrying_Cap',
                       'FID_Buffer_108050022']
        arcpy.DeleteField_management(Intersect_out_features,Drop_Fields)




        #Export to Excel
        arcpy.MakeFeatureLayer_management(Intersect_out_features, 'Intersect_Layer')
        Intersect_Selection_Excel = 'distance = 1000'
        print Intersect_Selection_Excel
        arcpy.SelectLayerByAttribute_management('Intersect_Layer', 'NEW_SELECTION', Intersect_Selection_Excel)
        Excel_Output = 'N:\\GIS\\Projects\\AA_Leith_Hawkins_TestBed\\Search_Cursor\\'
        Excel_Location = Excel_Output + '\\' +str(row[0])+'.xls'
        print Excel_Location
        arcpy.TableToExcel_conversion('Intersect_Layer', Excel_Location)
        #arcpy.SelectLayerByAttribute_management('Intersect_Layer', 'CLEAR_SELECTION')

        #print Png_output


        #test = arcpy.MakeFeatureLayer_management(File_output, 'Buffer_Layer')
        #arcpy.ApplySymbologyFromLayer_management('Buffer_Layer', 'C:\\Database_connections\\assesment_Model_lyr\\Buffer.lyr')


        #arcpy.ApplySymbologyFromLayer_management('Holdings_Layer',
         #                                        'C:\\Database_connections\\assesment_Model_lyr\\Holding.lyr')
        #arcpy.ApplySymbologyFromLayer_management('Intersect_Layer',
         #                                        'C:\\Database_connections\\assesment_Model_lyr\\Intersect.lyr')


        #add Layers to the Map
        mxd = arcpy.mapping.MapDocument('N:\\GIS\Projects\\AA_Leith_Hawkins_TestBed\\Search_Cursor\\Search_Cursor_mxd.mxd')
        df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
        addLayer = arcpy.mapping.Layer(File_output)
        addLayer2 = arcpy.mapping.Layer(Holding_Boundary)
        addLayer3 = arcpy.mapping.Layer(Intersect_out_features)



        arcpy.mapping.AddLayer(df, addLayer, "TOP")
        print addLayer
        arcpy.mapping.AddLayer(df, addLayer2, "TOP")
        print addLayer2
        arcpy.mapping.AddLayer(df, addLayer3, "TOP")
        print addLayer3
        arcpy.RefreshActiveView()


        #change Symbology
        arcpy.ApplySymbologyFromLayer_management('Buffer_Layer', 'C:\\Database_connections\\assesment_Model_lyr\\Buffer.lyr')
        arcpy.ApplySymbologyFromLayer_management('Holdings_Layer', 'C:\\Database_connections\\assesment_Model_lyr\\Holding.lyr')
        arcpy.ApplySymbologyFromLayer_management('Intersect_Layer', 'C:\\Database_connections\\assesment_Model_lyr\\Intersect.lyr')
        arcpy.RefreshActiveView()

        #zoom to layer

        print df
        lyr = arcpy.mapping.ListLayers(mxd, '', df)[2]
        print lyr.name
        extent = lyr.getExtent()
        print extent
        df.extent = extent

        #Export Map to PNG File
        Png_output = "N:\\GIS\Projects\\AA_Leith_Hawkins_TestBed\\" + str(row[0]) + '.png'
        arcpy.mapping.ExportToPNG(mxd,Png_output)
        print 'Map Created'
        del mxd

I cant work out where I need to call the Change symbology from ive had a look through the syntax guide but I think I must be a bit slow

Should I be calling the layer file or the layers I am adding to the Dataframe ??



Tags (1)
0 Kudos
1 Solution

Accepted Solutions
LukeWebb
Occasional Contributor III

Hi, Your script:

1) Creates the layer with default symbology

2) Adds it to the map.

3) Changes the symbology of the original layer. (Not the one that is now in the map)

4) Exports the map.

You need to either:

a) find reference to the layer in the map after you have added it and change the symbology here.

OR 

b) Change the symnbology of the layer, and then add to the map.

View solution in original post

2 Replies
DanPatterson_Retired
MVP Esteemed Contributor

without wading through the code are you saving and reloading the mxd to see if the updates have been made? are you working in interactive mode? (which would require a 'refresh') You might be advised to replace the code screen grab with one that uses the python syntax highlighter (follow the dots on the toolbar when editing or creating a post)

LukeWebb
Occasional Contributor III

Hi, Your script:

1) Creates the layer with default symbology

2) Adds it to the map.

3) Changes the symbology of the original layer. (Not the one that is now in the map)

4) Exports the map.

You need to either:

a) find reference to the layer in the map after you have added it and change the symbology here.

OR 

b) Change the symnbology of the layer, and then add to the map.