Select to view content in your preferred language

Geocode takes a crazy amount of time to respond

134
3
Jump to solution
Tuesday
RogerAsbury
Frequent Contributor

I've got a Azure function app that processes data from a non-Survey123 form. On of the questions on that form allows a person to enter an address, just a street, or a landmark. I'm using geocode() to get x:y coordinates of that location. It works, but it seem to take nearly a half-hour to complete, which is crazy. Below is the code and I'm wondering what I may have done wrong here. This is my first time using the geocode() functionality.

# Given a location and location type (not currently used), attempt to geocode the location
# and return the X,Y coordinates. If no coordinates can be found, return (0,0).
# NOTE: THIS IS CURRENTLY REALLY SLOW. I don't know if it's an ESRI thing or a code thing,
# but it takes nearly a half hour to geocode locations.
def locateIssue(location, locationType):
    logging.error(f"Locating issue for location: {location}")
    gis = GIS(url, user, passwd)
    try:
        logging.error(f"Attempting to geocode location: {location} of type: {locationType}")
        extent_env = Envelope({
            "xmin": -148.633333,
            "ymin": 64.25, 
            "xmax": -143.883333, 
            "ymax": 65.45, 
            "spatialReference": {"wkid": 4326}
        })
        result = geocode(address=location, search_extent=extent_env, max_locations=1)
        logging.error(f"Geocode result for location '{location}': {result}")
    except Exception as e:
        logging.error(f"Failed to geocode location: {location}. Error: {e}")
        return (0, 0)
    if result:
       for item in result:
            x = item['location']['x']
            y = item['location']['y']
            return (x, y)
           
    logging.error(f"Failed to locate issue for location: {location}")
    return (0, 0)

 

EDIT: If run from my development machine, this code executes almost instantaneously. So possibly a communication issue between Azure and ArcGIS Online?

--
Roger Asbury
Analyst/Programmer - Fairbanks North Star Borough
0 Kudos
1 Solution

Accepted Solutions
RogerAsbury
Frequent Contributor

Ok, after spending all day on this, here is what I found and how I fixed it:
First, logic to get timing on what, exactly, was going on when trying to get geocode, resulting in the following text:

2026-07-23T00:01:05   [Information]   locateIssue geocoder resolution time: 1621.485s, geocoder: https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer
2026-07-23T00:01:05   [Information]   locateIssue geocode call time: 0.395s

Which indicated the bulk of the time was spent determining the geocoder. Once found, the call to it was super fast.

Set that URL as the default geocoder, and everything went quickly. 

My assumptions:

  1. When setting gis = GIS(url, username, password) the url variable points to our segment of ArcGIS Online (entity.maps.arcgis.com), which doesn't have(?) a default geocoder, or doesn't point to one, or something...
  2. By providing a good url, there is no need to look one up, saving time.

I don't really see anything in the geocode() documentation mentioning any of this (arcgis.geocoding module | ArcGIS API for Python | Esri Developer) but maybe I missed it.

--
Roger Asbury
Analyst/Programmer - Fairbanks North Star Borough

View solution in original post

3 Replies
HaydenWelch
MVP Regular Contributor

I have a spot where I do something similar (using reverse_geocode) and it's not too slow for me:

...

    def update_addresses(self, demand_feats: FeatureClass[PointGeometry, Demand_Point]) -> dict[int, str]:
        updates: dict[int, str] = {}
        with demand_feats.where("ADDRESS_FULL IS NULL OR ADDRESS = '0 UNKNOWN ADDRESS'"):
            need_address = list(demand_feats)

        if need_address:
            SetProgressorLabel('Getting Addresses from AGOL...')
            _ = GIS()
            updates = {
                d['OID@']: (
                    reverse_geocode(
                        json.loads(d['SHAPE@'].JSON),  # type: ignore
                        distance=30.0,
                        location_top='street',
                    )['address']['LongLabel'] or '0 UNKNOWN ADDRESS').upper()
                for d in need_address
            }
        return updates

 

I believe that having an initialized GIS object is important. Since you need to have creds to use the geocoding service. Your dev machine is probably using your creds, but maybe Azure isn't?

0 Kudos
RogerAsbury
Frequent Contributor

My apologies, a GIS() line was in the code, but got trimmed by my overzealous trimming of potential security ids. 🙂 I've re-added it to the code.

--
Roger Asbury
Analyst/Programmer - Fairbanks North Star Borough
RogerAsbury
Frequent Contributor

Ok, after spending all day on this, here is what I found and how I fixed it:
First, logic to get timing on what, exactly, was going on when trying to get geocode, resulting in the following text:

2026-07-23T00:01:05   [Information]   locateIssue geocoder resolution time: 1621.485s, geocoder: https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer
2026-07-23T00:01:05   [Information]   locateIssue geocode call time: 0.395s

Which indicated the bulk of the time was spent determining the geocoder. Once found, the call to it was super fast.

Set that URL as the default geocoder, and everything went quickly. 

My assumptions:

  1. When setting gis = GIS(url, username, password) the url variable points to our segment of ArcGIS Online (entity.maps.arcgis.com), which doesn't have(?) a default geocoder, or doesn't point to one, or something...
  2. By providing a good url, there is no need to look one up, saving time.

I don't really see anything in the geocode() documentation mentioning any of this (arcgis.geocoding module | ArcGIS API for Python | Esri Developer) but maybe I missed it.

--
Roger Asbury
Analyst/Programmer - Fairbanks North Star Borough