How do I display geocode results within messages section of script results?

752
7
Jump to solution
02-17-2020 12:29 PM
KyleCrawford2
New Contributor III

I have a script that accepts the input of four tables and then geocodes these tables. My goal is to display the name of the output feature class and the number of unmatched records for each iteration of geocoding. This information is displayed while the tool is running but to better the user experience I would like this info displayed after the script is finished. 

0 Kudos
1 Solution

Accepted Solutions
KyleCrawford2
New Contributor III

So I figured out a few things that I was getting wrong but in the end, this is the simple code block:

arcpy.MakeFeatureLayer_management(geocode_result1, "geocode_result_1", whereClause)
result = arcpy.GetCount_management("geocode_result_1")
count = int(result[0])
if count > 0:
   arcpy.AddMessage("Weekly Crime - Unmatched: " + str(count))
else:
   arcpy.AddMessage("Weekly Crime: All Matched")

This will allow the end-user to quickly see if they need to rematch any unmatched records. 

View solution in original post

0 Kudos
7 Replies
DanPatterson_Retired
MVP Esteemed Contributor

AddMessage—ArcPy Functions | Documentation 

But you will only see the messages when you use "View Details" on the tool after it runs.  Messages don't display in a popup or anything like that

0 Kudos
KyleCrawford2
New Contributor III

Hey Dan I understand that the AddMessage function will work, but I don't know the correct syntax to receive the results from arcpy.GeocodeAddresses_geocoding to get the information I'm looking for to display under "View Details". For each file being geocoded I would like the name of the ouput feature class and the number of unmatched records to be shown. 

0 Kudos
JoeBorgione
MVP Esteemed Contributor

You might want to consider adding the message at the completion of geocoding: make a feature layer of your geocoded results, select where status = 'U' and use get count to see how many are unmatched; return that count in your message.

That should just about do it....
0 Kudos
KyleCrawford2
New Contributor III

Joe thank you for your suggestion. This is the code block that I came up with. Unfortunately, when I place it multiple times in the script to handle the number of inputs I get undesired results. I could probably set up a loop that runs through this code block, but my python skills are limited.

whereClause = "Status = 'U'"
arcpy.MakeFeatureLayer_management(geocode_result1, "geocode_result1", whereClause)
result = arcpy.GetCount_management("geocode_result1")
count = int(result.getOutput(0))
if count > 0:
   try:
      arcpy.AddMessage("Weekly Crime: Unmatched:" + count)
   except:
      arcpy.AddMessage("Weekly Crime: All Matched")‍‍‍‍‍‍‍‍‍

Below is the common result I receive. The two issues are for one, not all the messages are displayed. The second issue is that the outputs are wrong, "Weekly Crime 4 Weeks" should return the unmatched response not an all matched response.

Messages:

Running script Geocoding Weekly Crimes...
Weekly Crime 4 Weeks: All Matched
Weekly Crime 4 Weeks Night: All Matched
Completed script Geocoding Weekly Crimes...

0 Kudos
JoeBorgione
MVP Esteemed Contributor

line 4 is suspect: 

count = int(result.getOutput(0))

count = int(result[0])‍‍‍)

My line 3 just worked for me....

That should just about do it....
0 Kudos
KyleCrawford2
New Contributor III

So I figured out a few things that I was getting wrong but in the end, this is the simple code block:

arcpy.MakeFeatureLayer_management(geocode_result1, "geocode_result_1", whereClause)
result = arcpy.GetCount_management("geocode_result_1")
count = int(result[0])
if count > 0:
   arcpy.AddMessage("Weekly Crime - Unmatched: " + str(count))
else:
   arcpy.AddMessage("Weekly Crime: All Matched")

This will allow the end-user to quickly see if they need to rematch any unmatched records. 

0 Kudos
JoeBorgione
MVP Esteemed Contributor

Here's a cooler way to format your message in line 5:

arcpy.Addmessage(f'Weekly Crime - Unmatched {count}')

see:  https://realpython.com/python-f-strings/ 

That should just about do it....