This is starting to seem like an insurmountable task, so I am hoping I can get a few hints and pointers on how to do this. I need to automate the process of geocoding a db table and unfortunately our ArgGIS Enterprise server is still using ArcMap 10.4, so ArcPro is not an option. I have tried all of the obvious tricks like creating a model, running it to make sure it runs successfully, then exporting to a python script file. This doesn't work because the I need to use the esri World Geocode Service, and the url is different depending on whether you are geocoding from within ArcMap, or from a standalone script. I know the service url is "https://geocode.arcgis.com/arcgis/rest/services/ " and this seems to be consistent no matter the source doc (and the fact I was given this url by esri support).
Basically I have 2 issues:
1. finding the correct locator url (the second part of the url), for instance:
"World/GeocodeServer/ArcGIS World Geocoding Service"
"World.GeocodeServer"
"World/GeocodeServer"
2. Authenticating the request. So far I have tried creating a connection file, or signing in to ArcGIS online via arcpy.
Below are some examples of what I have tried. Of note, I spent a an hour yesterday doing a screenshare with esri support with no luck.
Here is the attempt with no connection file:
import arcpy
cwd = sys.path[0]
output_gdb_name = "Temp.gdb"
output_gdb = os.path.join(cwd, output_gdb_name)
arcpy.env.overwriteOutput = True
field_input = {"Address": "MailingAddressStreet",
"City": "MailingAddressCityText",
"Postal": "MailingAddressZipID"
}
fieldMap = arcpy.FieldInfo()
for field in field_input:
fieldMap.addField(field, field_input[field], "VISIBLE", "NONE")
addr_table = os.path.join(output_gdb, "WildlifeOperatorInformationTable")
geocodeServer = "https://geocode.arcgis.com/arcgis/rest/services"
addr_locator = os.path.join(geocodeServer, "World.GeocodeServer")
geocode_result = os.path.join(output_gdb,"geocodeResult")
arcpy.GeocodeAddresses_geocoding(addr_table, addr_locator, fieldMap, geocode_result, 'STATIC')
Attempted to create a connection file and get no error, but no connection file exists for output directory:
import arcpy, os, sys
cwd = sys.path[0]
output_gdb_name = "Temp.gdb"
output_gdb = os.path.join(cwd, output_gdb_name)
arcpy.env.overwriteOutput = True
outPath = r"...\NuisanceWildlifeTrappers"
server_url = "https://geocode.arcgis.com/arcgis/rest/services"
conn_file_name = "arcGIS_geocoding.ags"
conn_file = os.path.join(outPath, conn_file_name)
user = "John.Smith"
pwd = "xxxxxxx"
arcpy.mapping.CreateGISServerConnectionFile("USE_GIS_SERVICES",
outPath,
conn_file_name,
server_url,
"ARCGIS_SERVER",
username=user,
password=pwd,
save_username_password=True)
print "completed"
print arcpy.GetMessages()
I then created a GIS server connection using ArcCatalog for testing to see if I could get it to work.
locator = "GIS Servers\arcgis on geocode.arcgis.com (user)"
field_input = {"Address": "MailingAddressStreet",
"City": "MailingAddressCityText",
"Postal": "MailingAddressZipID"
}
fieldMap = arcpy.FieldInfo()
for field in field_input:
fieldMap.addField(field, field_input[field], "VISIBLE", "NONE")
addr_table = os.path.join(output_gdb, "WildlifeOperatorInformationTable")
geocode_result = os.path.join(output_gdb,"geocodeResult_fromScript")
arcpy.GeocodeAddresses_geocoding(addr_table, locator, fieldMap, geocode_result, 'STATIC')
....and got the following error:
I did try signing in to the portal by placing this code in the python script just before the geocode function but it makes no difference:
cwd = sys.path[0]
output_gdb_name = "Temp.gdb"
output_gdb = os.path.join(cwd, output_gdb_name)
field_input = {"Address": "MailingAddressStreet",
"City": "MailingAddressCityText",
"Postal": "MailingAddressZipID"
}
fieldMap = arcpy.FieldInfo()
for field in field_input:
fieldMap.addField(field, field_input[field], "VISIBLE", "NONE")
addr_table = os.path.join(output_gdb, "WildlifeOperatorInformationTable")
geocodeServer = "https://geocode.arcgis.com/arcgis/rest/services"
addr_locator = os.path.join(geocodeServer, "World.GeocodeServer")
geocode_result = os.path.join(output_gdb,"geocodeResult")
portalUrl = arcpy.GetActivePortalURL()
print portalUrl
arcpy.SignInToPortal_server('John.Smith', 'xxxxxx')
arcpy.GeocodeAddresses_geocoding(addr_table, addr_locator, fieldMap, geocode_result, 'STATIC')
arcpy.SignOutFromPortal_server()
print "completed"
print arcpy.GetMessages()
Got this error:
I also read the documentation about authenticating a request by generating a token and followed the steps by creating an application at developers.arcgis.com, but am not sure how generated the token and use it from a python script. There isn't any documentation I can find on how to do this.
Sorry for the long post, but I have been at this for a while. Thanks for letting me know what I am doing wrong and for any suggestions.