Location Reference Arcpy libraries

2427
12
02-21-2019 06:11 AM

I'm developing some python scripts using the location referencing tools - I found I can include the location referencing tools if I add Program Files (x86)\ArcGIS\LocationReferencing\Desktop10.6\Bin to my python path and import functions from the file locref.py - is this the correct way to use the locating referencing tools in python?  The functions are named a little differently in this file than they are when you run them from the toolbox, so I just want to be sure I've got the right tools.

for example if I import this:

from locref import GenerateRoutes

and use GenerateRoutes in a python script, am I correct to assume this is the exact same thing as runnning the generate routes toolbox tool, which results in a function from arcpy.GenerateRoutes_locref?

I can't import GenerateRoutes_locref in my python script, unless it's hiding in another python lib other than the one I found.

Kyle

12 Replies
ScottFierro2
Occasional Contributor III

Kyle,

I use a different path:

C:\Program Files (x86)\ArcGIS\LocationReferencing\Desktop10.5\ArcToolBox\Toolboxes

I can say it's worked for all my scripts and only caused 1 hiccup that I never pinpointed the root cause of. The script was fairly involved and I was working to automate it via Windows Task Scheduler from a server and when run in WingIDE, IDLE or Arc it worked fine but called via Task Scheduler something tripped up. Worked through support over it for a bit because of it being on server to vet through environment variables and 32 vs 64 bit python installs but had to abandon the case because of getting swamped elsewhere and I worked around it by simply publishing it as a GP then automating via Task Scheduler and a batch file to call the GP.

I think I made a mistake, I think that is the path to the locfef library I mean to specify:

So same question, with this as the correct path.  Sounds like at least Scott and I are on the same page here.  

Kyle

I'm just suspicuous because the tools are called with different names in arcmap vs python, and I have fell subject to calling tools from a wrong library in an esri product after a software update before.

0 Kudos

I had similar problems after os upgrades on some servers, part of the problem for me was running tools as admin user made it difficult to troubleshoot running something as that user.  

AmitHazra
Esri Contributor

Hello Kyle

I think you guys are absolutely on the right track. As stated in our documentationPython is initially only aware of tools stored in ArcGIS system toolboxes like the Data Management Tools, Conversion Tools, and Analysis Tools toolboxes. The most straightforward method for importing your Roads and Highways Location Referencing toolbox into a script is to simply use arcpy.ImportToolbox(). Once you've done that you can access any of the Location Referencing tools as arcpy.<toolname>_<alias>.

A super simple example:

# Import arcpy module
import arcpy
# Check out any necessary licenses
arcpy.ImportToolbox("C:\Program Files (x86)\ArcGIS\LocationReferencing\Desktop10.6\ArcToolbox\Toolboxes\Location Referencing Tools.tbx")
arcpy.CheckOutExtension("Highways")
# Local variables
network = r"C:\myRHData\ALRS.gdb\LRSN_Routes"
output_table = r"C:\myRHData\ALRS.gdb\Concurrencies_Output"
# Execute the tool
arcpy.CalculateRouteConcurrencies_locref(network, output_table, "", "FIND_DOMINANCE")
arcpy.CheckInExtension("Highways")

 

If in doubt on the naming for a tool, a quick and easy way to expose the python reference is to use the ArcMap Python window. Simply drag and drop the Location Referencing GP you are unsure of into the window:

 

Hoping that helps answer your original question or at least gets other folks up and running using Python and Roads and Highways,

Amit @ Esri

p.s. For anyone interested in learning more about the Location Referencing toolbox discussed throughout this thread please visit:

An overview of the Location Referencing toolbox—Location Referencing for Roads and Highways | ArcGIS... 

soniadickerson1
Occasional Contributor

I'm working on the same type of process using python to call the RelocateEvents_locref from the locref toolbox but, I can't get it to work due to not being able to get a highway license, which we have 4 available.  Also, I'm getting: TypeError: RelocateEvents() takes no arguments (9 given).  But since I'm unable to check out a license, the "no arguments" error may just be a repercussion.

0 Kudos
NathanEasley
Esri Regular Contributor

Hi Sonia,

You can follow the same format as the RelocateEvents example 2 stand alone python script at the bottom of this help topic http://desktop.arcgis.com/en/arcmap/latest/tools/location-referencing-roads-toolbox/relocate-events.... for checking out the Roads and Highways license for the tool.

Nathan @ Esri

soniadickerson1
Occasional Contributor

I was using a 10.4 python version which could not access the license.  Now that I'm using the 10.5 version, I can.  HOWEVER, I get the same error when importing from a toolbox.  When I use the example2 script, it also fails, but with the following error:  AttributeError: 'module' object has no attribute 'RelocateEvents_locref'

Any ideas on that one?  Thanks so much for getting back with me on this. 

0 Kudos

My original post here was a sort of comment about the python functions being called/named differently than the toolbox tools or the documented names.  You might try importing the locref lib, then try:

from locref import RelocateEvents

RelocateEvents(in_network, event_name, last_invoked_time, lrs_time, last_lrs_time, output_format, export_file, out_features, include_event_geometry)