Select to view content in your preferred language

ArcGIS Pro | ArcPy - How To Refresh the Map

25074
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
JeffHiggins
New Contributor

I ran into a similar problem where a Dynamic Text Element in a Layout needed to be refreshed during a Python script.  Basically the table values that fed the dynamic text were updated in a loop before exporting an image of the map.  The Text Element would not update after the table value changed so every image exported from the loop would should the initial value.  I solved this by turning on and off the text element after the table value was changed and before the image was exported in the loop.

 

Perhaps that is a workaround here - turn the element on and off to induce a refresh.

TommyLuksha
New Contributor II

I like JeffHiggins Idea here. I am trying to find a way to have my attribute table refresh as well though. 

YinghongLi1
Occasional Contributor

Hello Tommy,

I am having the same problem that newly inserted rows in a table did not display unless i use the refresh command on my editing version.  I'd like to know if you have found a solution or workaround to this issue.

Thanks

ArapahoeAdmin
New Contributor II

So yeah... four years and no real solution here?

I'm in a similar boat.... a Python-based Geoprocessing tool I've written moves address points to building centroids.  I would've never known it actually worked until I shutdown ArcGIS Pro and restarted it after running the tool and seeing my points as updated.  There's no easy way yo just refresh the screen after the script is done.  A normal redraw shows the points in their old locations.  I'm using arcpy.da.Editor and start & stopOperation to enclose the edit itself.  Like I said... it works, but the point locations on the map don't update without a complete shutdown and restart of ArcGIS Pro.  What are we all missing here?

 

0 Kudos
wOwen
by
New Contributor II

@ArapahoeAdmin I think what we are all missing here is the RefreshTOC, RefreshActiveView, and RefreshCatalog options that are available in ArcMap! In my case, I'm adding and deleting attachments to a hosted feature service using match tables. So far, arcpy.management.RemoveAttachments() works only the first time I run it after I open the Project. Subsequent calls fail. I've tried programmatically removing and re-adding the hosted feature layer and deleting and rebuilding all variable references, but it still fails. I'm guessing this is a refresh issue b/c if I close down and re-open the project, then run the function from the interactive python window or the script using the same match table, it works ... but only once. Grrr.

NeilFordyce
New Contributor III

I am automating map iteration over 40 fields in a table and are refreshing the map by setting the reference scale each iteration e.g.

map.referenceScale = 80000

This refreshes the map and legend in particular and the pdf contains the updated result.

0 Kudos
NSCHLAGE
New Contributor II

Howdy,
I had a similar issue converting a code from a Notebook (Jupyter Notebook) into a script tool. I had issues where in the Notebook everything worked fine, but layers and tables created in the code and referenced in the Project/Map Table of Contents would not get added to the TOC when run in a script tool or in the Python window when debugging line-by-line. I had this issue back in gradschool using ModelBuilder in ArcMap v9.x, where intermediate outputs were not added to the map document and broke the model because layers were 'inaccessible' to it.

Another user commented on not needing a refresh in Pro any more because the arcpy.mp functions all include a refresh when working in the "CURRENT" project. The problem we're running into in this thread seems to be running tools that are not in arcpy.mp and thus don't include a refresh. So, we can use the .mp functions to force/workaround and apply a refresh manually. After whatever portion of your code makes a change you need to see (or programmatically call updated data from that isn't accessible) we'll need to run a manual refresh by re-declaring the project, and map layers like so:
_________________________
import arcpy
project = arcpy.mp.ArcGISProject("CURRENT") #establishes initial workspace
mapdoc = project.listMaps("map-name")[0] #establishes initial workspace
#
#your change(s) performed that need viewed or referenced by further functions
#
project = arcpy.mp.ArcGISProject("CURRENT") #force a refresh
mapdoc = project.listMaps("map-name")[0] #force a refresh
#
#Obersvations and additional tools on updated data from the first run of tools
#
_____________________
This seems to have fixed my issues. I'm guessing because once a variable is declared by ArcGISProject, listMaps(), and listLayers() are static, so changes made later in the code need refresh by declaring those variables again. Hope it is helpful to others.

0 Kudos
BRENNEJM
New Contributor III

An example of some of the suggestions here. You can update the map scale by a small amount, so that it's not noticeable on screen but causes the map view to refresh.

# Set the active project to the current aprx open (used in the Refresh() function to refresh the map view).
aprx = arcpy.mp.ArcGISProject('current')

# Function to refresh the map view.
def refresh():
    # Refresh the map view by changing the map scale.
    # The map view changes by such a small amount that it isn't
    # noticeable, but still causes the map view to refresh.
    mv = aprx.activeView
    mv.camera.scale = mv.camera.scale + 0.1
RyanBoyd
New Contributor

Just a note, this method works well for me with a File GDB but not with an Enterprise geodatabase

melisahansen
New Contributor III

Has there been any updates to this?

0 Kudos