Select to view content in your preferred language

Creating locator using ArcPy throws ERROR 002815: A table is required for each role.

542
2
10-24-2024 11:36 AM
Labels (1)
bbauman
New Contributor

I have been trying to create a locator using ArcPy, but am constantly getting the following error:
Failed to execute. Parameters are not valid.
ERROR 002815: A table is required for each role.
Failed to execute (CreateLocator)

The code I'm using is copied directly from the geoprocessing history tab in the Catalog pane after creating the locator manually via ArcGIS Pro, via right-clicking on the tool result and selecting copy as Python command

For demonstration purposes, I've hardcoded the layers for ease of readability (and to help troubleshoot). Code is as follows:


import arcpy
import os

project_dir = r"path\to\GeocodingProject"
project_name = "GeocodingProject"

aprx = arcpy.mp.ArcGISProject(os.path.join(project_dir, f"{project_name}.aprx"))

map = aprx.listMaps()[0]

try:
    # Create Locator
    country_code = 'USA'
    language_code = "ENG"
    precision_type = "GLOBAL_HIGH"
    out_locator = os.path.join(project_dir, f"geocoding_locator_state_11")
    primary_reference_data = "tl_2024_11_addrfeat_featnames StreetAddress"

    field_mapping = [
            f"StreetAddress.FEATURE_ID 'tl_2024_11_addrfeat_featnames.shp'.FID",\
            f"StreetAddress.HOUSE_NUMBER_FROM_LEFT 'tl_2024_11_addrfeat_featnames.shp'.LFROMHN",\
            f"StreetAddress.HOUSE_NUMBER_TO_LEFT 'tl_2024_11_addrfeat_featnames.shp'.LTOHN",\
            f"StreetAddress.HOUSE_NUMBER_FROM_RIGHT 'tl_2024_11_addrfeat_featnames.shp'.RFROMHN",\
            f"StreetAddress.HOUSE_NUMBER_TO_RIGHT 'tl_2024_11_addrfeat_featnames.shp'.RTOHN",\
            f"StreetAddress.PARITY_LEFT 'tl_2024_11_addrfeat_featnames.shp'.PARITYL",\
            f"StreetAddress.PARITY_RIGHT 'tl_2024_11_addrfeat_featnames.shp'.PARITYR",\
            f"StreetAddress.STREET_PREFIX_DIR 'tl_2024_11_addrfeat_featnames.shp'.PREDIR",\
            f"StreetAddress.STREET_PREFIX_TYPE 'tl_2024_11_addrfeat_featnames.shp'.PRETYP",\
            f"StreetAddress.STREET_NAME 'tl_2024_11_addrfeat_featnames.shp'.NAME",\
            f"StreetAddress.STREET_SUFFIX_TYPE 'tl_2024_11_addrfeat_featnames.shp'.SUFTYP",\
            f"StreetAddress.STREET_SUFFIX_DIR 'tl_2024_11_addrfeat_featnames.shp'.SUFDIR",\
            f"StreetAddress.FULL_STREET_NAME 'tl_2024_11_addrfeat_featnames.shp'.FULLNAME",\
            f"StreetAddress.POSTAL_LEFT 'tl_2024_11_addrfeat_featnames.shp'.ZIPL",\
            f"StreetAddress.POSTAL_RIGHT 'tl_2024_11_addrfeat_featnames.shp'.ZIPR",\
            f"StreetAddress.POSTAL_EXT_LEFT 'tl_2024_11_addrfeat_featnames.shp'.PLUS4L",\
            f"StreetAddress.POSTAL_EXT_RIGHT 'tl_2024_11_addrfeat_featnames.shp'.PLUS4R"
            ]
   
    arcpy.geocoding.CreateLocator(
        country_code=country_code,
        primary_reference_data=primary_reference_data,
        field_mapping=field_mapping,
        out_locator=out_locator,
        language_code=language_code,
        alternatename_tables=None,
        alternate_field_mapping=None,
        custom_output_fields=None,
        precision_type="GLOBAL_HIGH"
    )
    print("Locator created successfully.")

    aprx.save()
except Exception as e:
    print(f"Error creating locator: {e}")



2 Replies
ShanaBritt
Esri Regular Contributor

@bbauman 

1. Is the shapefile referenced as the primary_reference_data variable in the path listed below?

"project_dir = r"path\to\GeocodingProject"

2. If the shapefile is in that path, you should use "arcpy.env.workspace " set to the path to the  shapefile. This is needed when running the script as a standalone script outside of ArcGIS Pro. There is a standalone script example in the tool help that you can use as a reference (Python tab), https://pro.arcgis.com/en/pro-app/latest/tool-reference/geocoding/create-locator.htm#GUID-61AE3A4C-2....

3. You can also try putting the full  path and file extension to the shapefile in the primary_reference_data variable.

4. If you open the project and copy the Python from the History and paste it into the Python window in ArcGIS Pro to run the script does the error occur?

5. When building the locator it is important not to map the Feature_ID field when you do not have duplicate features with different attributes in the primary data. It looks like you are using TIGER line data in the example you  provided and mapping FID to Feature_ID for this data is not necessary and can impact the performance and quality of the locator and results.

Here are some helpful resources:

https://pro.arcgis.com/en/pro-app/latest/help/data/geocoding/create-a-locator-that-restores-missing-...

https://pro.arcgis.com/en/pro-app/latest/help/data/geocoding/collapse-duplicate-features-in-the-data...

 

0 Kudos
DanielFox1
Esri Regular Contributor
0 Kudos