ArcGIS Pro | ArcPy - How To Refresh the Map

22085
30
02-16-2017 06:15 PM
TD
by
New Contributor III

Hi ArcGIS Community,

I have a Python script that simply add points to a feature class in file GDB every second. The corresponding layer is in an ArcGIS Pro map, and I would like to visualise the point as their are being added, without manually interacting with the map.

When running the script as a standalone script, panning or zooming will force the map to refresh and display points newly added.

How can I programatically refresh the map to see the update displayed dynamically, without the need for user interaction ?

Thanks

  • Windows 8.1
  • ArcGIS Pro 1.4.1

import arcpy, time, random, datetime

aprx = arcpy.mp.ArcGISProject('D:\workspaces\sandbox\sandbox\sandbox.aprx')


fields = ['SHAPE@XY', 'DATETIME']
iteration = 10
second = 1
# bouding box: east, west, south, north (define your own extent)
boundingBox = [x1,x2,y1,y2]

fc = r"D:\workspace\mygdb.gdb\Points"


for i in range(iteration):
     
     x = random.uniform(boundingBox[0],boundingBox[1])
     y = random.uniform(boundingBox[2],boundingBox[3])
     
     xy = (x,y)
     
     cursor = arcpy.da.InsertCursor(fc, fields)
     cursor.insertRow((xy, datetime.datetime.now()))
     del cursor
     
     print('Done! ... i=' + str(i))
     time.sleep(second)


print('QUIT')‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
30 Replies
DanPatterson_Retired
MVP Emeritus

From the help topic on changes if you are using arcpy.mp (formerly arcpy.mapping) changes are supposed to reflected automatically only if 'Current' is being used. Also the changes mean that camera, layout and map replace the old... and I can't find any refresh-anything in the class list

TD
by
New Contributor III

Hi Dan,

The problem with the Python window is that the map refreshes only once the script has completed. Even manually interacting with the map does not refresh it. I'd like to update the display while the script is running.

And I'd like to be able to run the script as a standalone script.

0 Kudos
DanPatterson_Retired
MVP Emeritus

This Alphabetical list of ArcPy functions—ArcPy Functions | ArcGIS Desktop  and Alphabetical list of ArcPy classes—ArcPy Classes | ArcGIS Desktop  and Alphabetical list of ArcPy functions—ArcPy Functions | ArcGIS Desktop  is pretty well all you have

The only thing I can think of is if you had one script call another within a loop rather than have the whole process in one.  I can't remember if passing back a value from the called script would trigger something in arcmap or PRO

JoshuaBixby
MVP Esteemed Contributor

I think Dan's first comment is getting to the root of your issue.  Just so I understand, you want to run this as a stand-alone script and not in the currently open ArcGIS Pro session?  If the former, do you want to modify the display of an APRX file while another user has it open?

Also, recreating the insert cursor every time within your loop will be a performance killer.

0 Kudos
TD
by
New Contributor III

Hi Joshua,

Let me rephrase the expected workflow.

  • I'd like to open an ArcGIS Pro project and visualise a layer of a point feature class.
  • While viewing this layer, I would like to run a script that is going to create additional points in this feature class.
  • Everytime a new point is added by the script to the feature class, I want the display of the layer or map of the ArcGIS Pro project to refresh in oder to visualise the new point.

It doesn't matter if the script runs as standalone or in the Python Pro window, I was just describing what I have tested.

I hope it helps.

Thanks

DanPatterson_Retired
MVP Emeritus

Well, since 'Current' project (*aprx) doesn't require a refresh of the active map according to the help then either the help is somewhat misleading or the script isn't finishing.  As an final idea, perhaps attaching the script to a tool in arctoolbox wil enable the expected behaviour, I am not convinced that an external script is going to perform the task properly or at all

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

From the Migrating from arcpy.mapping to ArcGIS Pro documentation:

When using the CURRENT keyword in the Python window with ArcGIS Desktop, you sometimes had to call refreshActiveView to force a screen refresh. This is no longer the case with ArcGIS Pro. All arcpy.mp API changes will directly update the application.

It is the last sentence that I believe is relevant here.  I may be interpreting the documentation too literally, but in your case you are not making arcpy.mp API changes, you are adding data to feature class.

Without an explicit refresh call or a zoom or pan action, ArcGIS Pro would constantly have to be polling the data source to see if any data has changed.  Doing so introduces several questions and challenges.  For example, how frequently do you do the polling, every second, half-second?  What kind of additional and extraneous network or disk traffic is created by the constant polling?

If you were changing arcpy.mp elements (symbology, map frame properties, changing data source for layer), I am sure the changes would happen automagically.  In this case, you are updating data in a data source, and I don't believe that is covered by the automatic refresh.

In terms of refresh, I am not sure if there is some automatic refresh at a designated time interval, e.g., every 5 minutes the screen refreshes no matter what.

by Anonymous User
Not applicable

Hi,

I also have this problem, but I only need to refresh the scene at the end of my python script. The script adds records into a feature class, and then I want to see those on screen when the script is done. 

Chris

KjetilSverdrup-Thygeson
New Contributor III

Hi,

I struggle with the same problem, having a script tool which inserts new points to an existing feature class in ArcSDE using an Insert Cursor.
The new points do not automatically appear on the map.

One interactive workaround for me is to choose "List by data source", selecting the ArcSDE connection and on the Versioning tab click "Refresh". This is not very user friendly though.

Another workaround is to "change" the version of the Feature layer to the same version it is in (see code below).

This works, but is way too slow (30 sec).

def refreshMap(layerName):
   p = arcpy.mp.ArcGISProject('current')
   m = p.listMaps("MainMap")[0]
   for l in m.listLayers("*"):
      if l.longName == layerName:
         conProp = l.connectionProperties
         arcpy.ChangeVersion_management(l, 'TRANSACTIONAL', conProp['connection_info']['version'])

Please give us a way to refresh the map!