ArcGIS Pro | ArcPy - How To Refresh the Map

22589
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
DarrylKlassen1
Occasional Contributor II

I am also having this problem.  I have have a script that allows the user to select an Address from our Property layer, and it zooms to this property, but.... It only works if I close and open the map up again.  I would like it to automatically zoom to the property, which it doesn't.  Code below.  I would like the have Refresh object at the end.

import arcpy,sys,string,os

dbconn = r"S:\GIS\Scripts\Python\Darryl_Working\Zoom_Address_Pro\Data.gdb"

inputlayer = dbconn + "\\GIS_Property"

inputaddress = arcpy.GetParameter(0)
mapname = arcpy.GetParameter(1)

cur,row = None, None
cur = arcpy.SearchCursor(inputlayer)
for row in cur:
    if str(row.ADDRESS).upper() == inputaddress.upper():
        ex = row.SHAPE

aprx = arcpy.mp.ArcGISProject("CURRENT")
map = aprx.listMaps(mapname)[0]

map.defaultCamera.setExtent(ex.extent)

DanPatterson_Retired
MVP Emeritus

add a tool to a toolbox, associate the parameters with it, it might do the query and when it is done, maybe it will zoom to it.  

A standalone script isn't going to do any zooming I am afraid, but a script in a toolbox just might behave as we want it to

0 Kudos
DarrylKlassen1
Occasional Contributor II

Sorry, I should have mentioned this.  This stand-alone script is attached to a toolbox with a combo box that has a list of all the addresses in it.  When I execute the tool, it works fine, but doesn't refresh the extent unless I close and open the map again.

0 Kudos
DanPatterson_Retired
MVP Emeritus

hmmm camera uses a derived property

Extent is not a direct property of the Camera object because it is not an explicit property but rather a derived property. Camera positions do not store extent properties, but from an X, Y location and a scale, extent is derived. The getExtent method will return a derived extent.

Camera—ArcPy | ArcGIS Desktop 

have you tried messing with the extent object?

Extent (Environment setting)—Geoprocessing | ArcGIS Desktop 

I don't know if setting the extent to the selection will be enough for the camera to read it.

I would be much simpler if they put a refresh option on the tool dialog if desired

And doesn't the refresh button on the map work? or do you still have to close it?

0 Kudos
DarrylKlassen1
Occasional Contributor II

Clicking the refresh button doesn't work, you have to actually close the map and open it again.  The arcpy.env.extent returns a NoneType value, and when I set it to an extent, nothing happens.  Will keep trying this route.

DanPatterson_Retired
MVP Emeritus

set it before the work is processed, then set it after the selection and before the camera.  camera is probably reading the None thing since it isn't set

DarrylKlassen1
Occasional Contributor II

I can't even manually (using the Python window) change the extent of the map.

curtvprice
MVP Esteemed Contributor

Unlike ArcMap you can't set the extent directly (as Dan quoted above):

Extent is not a direct property of the Camera object

0 Kudos
FredericPoliart_EsriAU
Occasional Contributor II

is your map.defaultCamera.setExtent(ex.extent) actually working? 
I am trying to do something similar in pure-pyton arcPy ( no ArcGIS Pro.exe running )  
I want to open a "blank.aprx" , add a layer to it (ok) , then set zoom scale or change the extents ,
and finally render export to a PNG file. 

The .setExtent either causes runtime error or does nothing 

0 Kudos
NoelPeterson
New Contributor III

Not sure why this is marked as "assumed answered". I don't see a solution anywhere in here.