Can't figure out create locator tool for arcpy

279
3
Jump to solution
a month ago
Myles
by
New Contributor

Hello everyone, 
I'm running Pro 3.1.2 and i'm working on a python script that automates the creation of an mmpk for use in field maps. Currently, the script pulls data from a server and adds that to a map. I hope to use those layers directly from the contents pane to create the mmpk. 

Haven't been able to get passed the create locator tool for the mmpk. I'm looking to set up a locator with one of the layers (a road layer) but whenever I run the script, I get this error: 
Error creating locator: Failed to execute. Parameters are not valid.
ERROR 002790: Every Table requires a Role.

I'd appreciate any insights I can get on this. Thank you!

The code looks like this: 

# List of data source URLs
serverData = [r"https://serverdata",
]

# Open Pro Project
aprx = arcpy.mp.ArcGISProject(r"pathToProject")

# Create a new disposable map
map = aprx.createMap("MMPK", "MAP")
print(map.name, "has been added")

# Get rid of basemap layer from map
basemap = map.listLayers()
for i in basemap:
    print(i.name, "is an active layer")
    if i.name == "Topographic":
        map.removeLayer(i)

# Add Data Layers from Server to map
layers = []
for data in serverData:
    try:
        layer = map.addDataFromPath(data)
        layers.append(layer)
        print(f"Added layer from {data}")
    except Exception as e:
        print(f"Error adding layer from {data}: {e}")

# Create Locator
lang = "ENG"
output = r"saveLocatorPath"
road_layer = layers[1]
fieldMap = "StreetAddress.HOUSE_NUMBER_FROM_LEFT",road_layer,".L_LADD",\
            "StreetAddress.HOUSE_NUMBER_TO_LEFT",road_layer,".L_HADD",\
            "StreetAddress.HOUSE_NUMBER_FROM_RIGHT",road_layer,".R_LADD",\
            "StreetAddress.HOUSE_NUMBER_TO_RIGHT",road_layer,".R_HADD",\
            "StreetAddress.HOUSE_NUMBER_FROM_RIGHT",road_layer,".R_LADD",\
            "StreetAddress.PARITY_LEFT",road_layer,".L_PARITY",\
            "StreetAddress.PARITY_RIGHT",road_layer,".R_PARITY",\
            "StreetAddress.STREET_NAME",road_layer,".NAME",\
            "StreetAddress.STREET_SUFFIX_TYPE",road_layer,".SFX",\
            "StreetAddress.STREET_SUFFIX_DIR",road_layer,".DIR",\
            "StreetAddress.FULL_STREET_NAME",road_layer,".FULL_NAME"


refData = road_layer, "StreetAddress"

try:
    arcpy.geocoding.CreateLocator(country_code="CAN", primary_reference_data=refData, field_mapping=fieldMap,  out_locator=output, language_code=lang)
    print("Locator created successfully.")
except Exception as e:
    print(f"Error creating locator: {e}")
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
dgiersz_cuyahoga
Occasional Contributor

Try refData = road_layer + " StreetAddress"

#CLE #sloth

View solution in original post

3 Replies
dgiersz_cuyahoga
Occasional Contributor

Try refData = road_layer + " StreetAddress"

#CLE #sloth
Myles
by
New Contributor

Thank you very much. This seemed to ease up my error, paired with Blake's response. It could not register the layer and role with my initial setup. 

0 Kudos
BlakeTerhune
MVP Regular Contributor

I think your fieldMap is incorrect. The error: "Every table requires a role" refers to the role that is also configured within the field_mapping. If you run the tool manually in ArcGIS Pro, then go to the geoprocessing history tab in the Catalog pane, you can right click on the tool result and copy as Python command. Then paste that into your code editor so you can see the formatting of everything. That's what I did here and this was what I got.

 

fieldMap = [
    "StreetAddress.HOUSE_NUMBER_FROM_LEFT 'road_layer'.L_LADD",
    "StreetAddress.HOUSE_NUMBER_TO_LEFT 'road_layer'.L_HADD",
    "StreetAddress.HOUSE_NUMBER_FROM_RIGHT 'road_layer'.R_LADD",
    "StreetAddress.HOUSE_NUMBER_TO_RIGHT 'road_layer'.R_HADD",
    "StreetAddress.HOUSE_NUMBER_FROM_RIGHT 'road_layer'.R_LADD",
    "StreetAddress.PARITY_LEFT 'road_layer'.L_PARITY",
    "StreetAddress.PARITY_RIGHT 'road_layer'.R_PARITY",
    "StreetAddress.STREET_NAME 'road_layer'.NAME",
    "StreetAddress.STREET_SUFFIX_TYPE 'road_layer'.SFX",
    "StreetAddress.STREET_SUFFIX_DIR 'road_layer'.DIR",
    "StreetAddress.FULL_STREET_NAME 'road_layer'.FULL_NAME"
]