Question about updating the map display from script

628
4
08-12-2011 09:36 AM
RalphFinch
New Contributor III
Iâ??m just starting to learn ArcGIS scripting with python.

I have a table of locations and their x-y coordinates (DSM2OutputLocations.lyr). I have a small script (which will be part of a larger one) that selects a single desired location, and writes that one location to a .shp fileâ?¦see below. Eventually I will loop this over many locations, displaying each location one-by-one.

I can run this successfully in a Toolbox script. The problem is my ArcMap display doesnâ??t automatically update when the .shp file changes. Instead I have to manually tell ArcMap to re-read the .shp file so the map updates with the new output location display.

My question is, how to get ArcMap to automatically show the new output location. Is there a way to tell ArcMap to re-read the new .shp file? Or should I use another method entirely?

# Import arcpy module
import arcpy, os
from arcpy import env

env.workspace = "Z:/DSM2-GIS/"

# Local variables:
Output_Loc = "oldr_midr"

inLayer = "DSM2OutputLocations.lyr"
outFeature = "DSM2_Out_Loc.shp"
Select_Expr = "`NAME` = " + "'" + Output_Loc + "'"

# Process: Select
arcpy.Select_analysis(inLayer, outFeature, Select_Expr)
Tags (2)
0 Kudos
4 Replies
LoganPugh
Occasional Contributor III
Assuming DSM2_Out_Loc.shp is already being displayed by one of the layers in your map, try adding arcpy.RefreshActiveView().

Otherwise try creating a layer from the output shapefile with MakeFeatureLayer_management().

You may also want to look into parameterizing your script tool instead of hardcoding file paths and layer names, to make it more generic. See Understanding Script Tool Parameters.

If you intend on overwriting the output shapefile if it already exists, you should add arcpy.env.overwriteOutput = True near the top of your script. See arcpy.env.
0 Kudos
RalphFinch
New Contributor III
Assuming DSM2_Out_Loc.shp is already being displayed by one of the layers in your map, try adding arcpy.RefreshActiveView().


Thanks! This does refresh the display (manually, F5) but it doesn't force ArcMap to re-read the new contents of DSM2_Out_Loc.shp (which is already being displayed). To force ArcMap to re-read the new contents I have to manually right-click the layer and Data->Repair Data Source->select DSM2_Out_Loc.shp.  Is there a way to automate this last step?  Is there a better way to do this than re-writing DSM2_Out_Loc.shp and re-reading it?

You may also want to look into parameterizing your script tool instead of hardcoding file paths and layer names, to make it more generic. See Understanding Script Tool Parameters.


Yes, you are right, at the moment I am just experimenting and keep it simple with the hardcoded names.

If you intend on overwriting the output shapefile if it already exists, you should add arcpy.env.overwriteOutput = True near the top of your script. See arcpy.env.


Excellent, I was wondering how to do this too, so it works outside of a custom Tool.
0 Kudos
LoganPugh
Occasional Contributor III
My guess is the script tool expects there to be some kind of output -- it has to be told what that output is. The most common way to do this is to have input and output parameters. Since you are not using any input or explicit output parameters, the way you do this is by creating a derived parameter in your script tool, and set its value with SetParameterAsText() at the end of your script.

E.g.
arcpy.SetParameterAsText(0, outFeature)


As for whether what you are currently doing is the best approach I couldn't say as I don't know what you are doing with the created shapefile. If it's simply for display purposes, you could avoid creating the shapefile by using MakeFeatureLayer instead of Select -- this doesn't create any new data, just a feature layer referencing the queried features.

Also for your inLayer variable, you don't have to use a .lyr file -- you could simply use the name of a layer in your map. When you start using parameters, if you set up an input parameter of type FeatureLayer, it can take anything that looks like a Feature Layer, including names of layers in the map, .lyr files, geodatabase feature classes and shapefiles.
0 Kudos
RalphFinch
New Contributor III
Well, I found a much better way to accomplish what I want (recall I am just starting to learn arcpy).  I found I can update Definition Queries within arcpy and am using those (my real code has a lot more than shown below):

workspace = "D:/ArcGIS"
overwriteOutput = True

DSM2ChansLyr = "DSM2 Channels"
mxd = arcpy.mapping.MapDocument("DSM2_Network.mxd")
lyrDSM2Chans = ListLayers(mxd,DSM2ChansLyr)[0]
# query to get channel sensitivity results for target loc
queryExpr = "Perturbed = " + "'" + perturbed + "'" + " AND " + \
                      "Parameter = " + "'" + outParam + "'" + " AND " + \
                      "Output_Location = " + "'" + Loc + "'"
lyrDSM2Chans.definitionQuery = queryExpr


Elementary stuff for the experienced arcpy programmer but new and fun for me.
0 Kudos