# Faster equivalent of arcpy distanceTo method

Unfortunately arcpy distanceTo method is painfully slow. When speed is crucial, I am still using Avenue to work around it, but it is getting harder to transfer ArcView 3 to newer computers.

As requested by @PolyGeo code snippets tested on polygon made of 3250 vertices.

# ------------------

Avenue:

``````theView = av.GetActiveDoc
Catchments=theView.GetActiveThemes.Get(0)
aTab=Catchments.getFtab
shpFld=aTab.findField("Shape")
feat=aTab.ReturnValue(shpFld,0)
outLine=feat.AsPolyLine
pC=feat.ReturnCenter

aTime0=date.now
for each i in (0..10000)
d=outLine.Distance(pC)
end
msgbox.info ((Date.Now-aTime0).AsSeconds.asstring,"")
``````

# -------------------

Python:

``````import arcpy, time
infc=r'C:\Temp\theme1.shp'
with arcpy.da.SearchCursor(infc,("FID","Shape@")) as cursor:
for i,feat in cursor:
break
outline=feat.boundary()
pC=feat.centroid

start = time.time()
for i in range(10000):
d=outline.distanceTo(pC)

end = time.time()
``````
• Executing: Script3 centres Start Time: Fri Jul 28 19:32:08 2017
• Running script Script3...
• 83.7339999675751
• Completed script Script3...

# ---------------------

Note that last 5 lines are doing exactly the same thing, calculatiog distance from polygon centroid to it's boundary. As one can see arcpy function is far behind out of date Avenue.

So, does anybody aware of python module that works significantly faster, please

• @FelixIP, I feel your pain. The `arcpy.Geometry` objects are very expensive to create and operate. I'd go for the `Near` GP tool for calculating the matrix of plain distances between two feature classes (which you can pre-create if needed using other GP tools). Or do you need to operate on each vertex of the polygon as if it is an individual geometry object? The thing is that running GP tools will be faster in general than using `arcpy.Geometry` objects. – Alex Tereshenkov Jul 28 '17 at 7:53
• @AlexTereshenkov it is about this nice idea blog.mapbox.com/… that I implemented in both languages, but slow handling of geometries in arcpy makes ArcGIS version almost useless. I am not sure that using tools can help in this case – FelixIP Jul 28 '17 at 8:08
• @FelixIP, I see. I'd still recommend prototyping getting the centroids of your features (through a GP tool) into an `in_memory` feature class and then run the `Near` GP tool. I promise it will be much faster than `arcpy.Geometry`, but I am not sure whether it will beat Avenue ;) – Alex Tereshenkov Jul 28 '17 at 8:24
• Alex's suggestion is what I would try first. If you need much more speed I'd suggest using PostGIS for these sorts of distance queries. – Alexander Jul 28 '17 at 9:22
• @Alex Tereshenko I did a quick test: replaced distance calculation inside loop by spatial join (closest) between list of point geometries to outline, output to in memory feature class. It took 7 seconnds to complete. Massive acceleration. If you post your suggestion as an answer, I'll gladly accept it – FelixIP Jul 30 '17 at 9:59