Cant get selected feature to clear?

749
5
07-10-2013 09:06 AM
ja3
by
New Contributor
So I am fairly new to python scripting, but I have what seems like it should be a fairly simple script that selects an attribute from a layer, zooms to it, and then clears the selection and exports a png of the map. However, the CLEAR_SELECTION function doesn't actually seem to be doing anything, and all the maps the script outputs still have the selected feature highlighted in blue when it is outputted to the png.

# Import arcpy module
import arcpy



# Set Geoprocessing environments
arcpy.env.workspace = "*****"

##overwrites output
arcpy.env.overwriteOutput = True


#mapDocument
mxd = arcpy.mapping.MapDocument(*******.mxd")

#setting layer variable
addLayer = arcpy.mapping.Layer(********.lyr")


##setting dataset variable
df = arcpy.mapping.ListDataFrames(mxd)[0]

#select layer 
arcpy.SelectLayerByAttribute_management(addLayer, "NEW_SELECTION", ' *********** ')

#set scale
df.scale = 7000000

##adds layer variable to mxd
arcpy.mapping.AddLayer(df, addLayer)


##zooms to layer
df.zoomToSelectedFeatures()


####unselect point
arcpy.SelectLayerByAttribute_management(addLayer,"CLEAR_SELECTION")


##exports current map extent to png
arcpy.mapping.ExportToPNG(mxd,*************,df,df_export_width=550,df_export_height=480)
0 Kudos
5 Replies
markdenil
Occasional Contributor III
You could try a new selection with an expression you know will return no records.
something like OBJECTID = 0
0 Kudos
ja3
by
New Contributor
Unfortunately that didn't seem to work either
0 Kudos
markdenil
Occasional Contributor III
Using   arcpy.RefreshActiveView()
after    arcpy.SelectLayerByAttribute_management(addLayer,"CLEAR_SELECTION")
would help.

Didn't notice it wasn't there earlier....
0 Kudos
ja3
by
New Contributor
Still no luck.


Interestingly, the RemoveLayer command won't work once the layer is added either. There seems to be something wrong with the way I am adding the layer to the mxd

I finally got it to work, but in a really round-about way.

# Import arcpy module
import arcpy



# Set Geoprocessing environments
arcpy.env.workspace = "**************"

##overwrites output
arcpy.env.overwriteOutput = True


#mapDocument
mxd = arcpy.mapping.MapDocument(r"***********************.mxd")

#setting layer variable
nl = arcpy.mapping.Layer(r"*****************************.lyr")

##setting dataset variable
df = arcpy.mapping.ListDataFrames(mxd)[0]

#select layer 
arcpy.SelectLayerByAttribute_management(nl, "NEW_SELECTION", ' "***************" ')

#set scale
df.scale = 7000000

##adds layer variable to mxd
arcpy.mapping.AddLayer(df, nl)

##zooms to layer
df.zoomToSelectedFeatures()

extent=df.extent




for nl in arcpy.mapping.ListLayers(mxd, "", df):
        arcpy.mapping.RemoveLayer(df, nl)

arcpy.RefreshActiveView() 

#mapDocument
mxd = arcpy.mapping.MapDocument(r"***************************.mxd")


nl2= arcpy.mapping.Layer(r"**********************************")

##setting dataset variable
df = arcpy.mapping.ListDataFrames(mxd)[0]

df.extent=extent

##adds layer variable to mxd
arcpy.mapping.AddLayer(df, nl2)


##exports current map extent to png
arcpy.mapping.ExportToPNG(mxd,r"************************",df,df_export_width=550,df_export_height=480)

0 Kudos
markdenil
Occasional Contributor III
Oh, by the way, there is a bug in setting dataframe extents.
You sometimes have to do it twice, so it is a good practice to do it twice in your code.
0 Kudos