Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a ArcPy script to calculate Moran's I several times (50 to be precise). However, I am not interested in the Moran's I value, what I need is the z-score returned by the tool. Even though the Geoprocessing results window dispalays the z-score, I am not able to get the score through the script.

I have been through the documentation on ArcGIS resource center and the example given by them is:

arcpy.SpatialAutocorrelation_stats("olsResults.shp", "Residual","NO_REPORT", "GET_SPATIAL_WEIGHTS_FROM_FILE","EUCLIDEAN DISTANCE", "NONE", "#","euclidean6Neighs.swm")

The result returned by this script is the Moran's I index value.

Is there any way I can get the z-score besides looking at the Geoprocessing results window or the Reports?

share|improve this question

This is a bit of a hack, but I blame ESRI for that. You can create a variable to store the messages and then do some string manipulation to get the z-score like this:

messages = arcpy.GetMessages()
zIndex = messaging.find('z-score')
zScore = messaging[zIndex + 17 : zIndex + 25]

This could probably be done more efficiently with regex, but I need to get back to work. This could break if the space between the z-score text in the message string and the value changes for some reason or the number of significant digits (i.e. length of the z-score value) changes. I looked for an object that points to the actual z-score in the debugger, but I could not find it. It might be there though.

share|improve this answer
Great work around. But I wonder if there is a better solution. The primary problem I am having with this approach is that I have ran the tool several times manually. I do not want to extract the z-values that were reported from those runs in the code. – Dipto Apr 12 '13 at 18:01
So where do you want to see the z-score from the manual runs? It is hard to tell because your original question seemed like you wanted access to it in the code, but now it seems like you want it in some other form. Maybe I am just misunderstanding. – TankofVines Apr 12 '13 at 18:10
I still want he z-scores from the code. But sometimes it happens that in the same session I run the Moran's I tool manually also for some experiments. I am not able to separate the results returned from this run and the results obtained from the runs from the code. – Dipto Apr 13 '13 at 3:49

The Moran's I tool itself is a python script ( If you examine the script, at line 86, it creates a GlobalI object using your input parameters. If you create a new version of the script instead of using that object for reporting, you could return that object as your output and then grab its .zi attribute.

(I'll try to add some code ideas later.)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.