5

I am using ArcView Version 10.1 and I need to perform a point distance analysis like:

What is the distance from A to B-Z, B to A-Z, C to A-Z, etc?

What are the steps that I take to do this?

  • I would recommend starting with Add XY Coordinates and then using an arcpy.da SearchCursor with an InsertCursor to write out the same table that Point Distance would - an existing answer to an earlier question has the formula to use gis.stackexchange.com/a/54640/115 – PolyGeo Sep 11 '14 at 3:26
  • 1
    Or points file to line then calculate geometry - not as technical as the answer PolyGeo indicated but can be done interactively without any python knowledge... One pair of points at a time - the order doesn't matter as the distance A-B is the same as B-A (if it's not then there's trouble!) – Michael Stimson Sep 11 '14 at 3:34
  • Thank you both for your recommendations. I don't have too much experience with python so I'll give the second option a try. Thanks again! – Jennifer Lopez Sep 11 '14 at 3:42
  • @MichaelMiles-Stimson Making lots of single segment lines sounds like it could work well as long as the number of points is not too many. – PolyGeo Sep 11 '14 at 3:43
  • If you've got a lot of points consider point distance tool, use the points as input and near features and you'll have every distance from every point to another point you'll just need to sort through with definition queries like INPUT_FID = X and NEAR_FID = Y to find the distance X to Y. – Michael Stimson Sep 11 '14 at 3:55
6

The following code is not polished but should work to create the same output table as the Point Distance tool but requires ArcGIS 10.1 (or later) for Desktop and only a Basic level license:

import arcpy,math

# Set variables for input point feature classes and output table
ptFC1 = "C:/temp/test.gdb/PointFC1"
ptFC2 = "C:/temp/test.gdb/PointFC2"
outGDB = "C:/temp/test.gdb"
outTableName = "outTable"
outTable = outGDB + "/" + outTableName

arcpy.env.overwriteOutput = True

# Create empty output table
arcpy.CreateTable_management(outGDB,outTableName)
arcpy.AddField_management(outTable,"INPUT_FID","LONG")
arcpy.AddField_management(outTable,"NEAR_FID","LONG")
arcpy.AddField_management(outTable,"DISTANCE","DOUBLE")

# Create and populate two dictionaries with X and Y coordinates for each
# OBJECTID in second feature class using a SearchCursor
ptFC2XCoordDict = {}
ptFC2YCoordDict = {}
with arcpy.da.SearchCursor(ptFC2,["OBJECTID","SHAPE@XY"]) as cursor:
    for row in cursor:
        ptFC2XCoordDict[row[0]] = row[1][0]
        ptFC2YCoordDict[row[0]] = row[1][1]

# Open an InsertCursor ready to have rows written for each pair of OBJECTIDs
iCursor = arcpy.da.InsertCursor(outTable,["INPUT_FID","NEAR_FID","DISTANCE"])
# Use a SearchCursor to read the rows (and X,Y coordinates) of the first
# feature class
with arcpy.da.SearchCursor(ptFC1,["OBJECTID","SHAPE@XY"]) as cursor:
    for row in cursor:
        x1 = row[1][0]
        y1 = row[1][1]
        for i in range(len(ptFC2XCoordDict)):
            x2 = ptFC2XCoordDict[i+1]
            y2 = ptFC2YCoordDict[i+1]
            # Prepare and insert the InsertCursor row
            iRow = [row[0],i+1,math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1))]
            iCursor.insertRow(iRow)
del iCursor

print "Done!"
  • PolyGeo, if you supplied the same feature class for input and output would it produce a table like the point distance tool? From the brief read of what you have produced it seems so. That could be quite handy if the hard-coded paths were changed to arguments. – Michael Stimson Sep 11 '14 at 21:19
  • Like the Point Distance tool "Both Input Features and Near Features can be the same dataset" but in that case, I have included no code to ensure that "when the input and near features are the same record, that result will be skipped so as not to report that each feature is 0 units from itself". It is just quick code to show that the main part of Point Distance is easily done with arcpy.da cursors, rather than to write a polished tool with the same arguments and options. – PolyGeo Sep 11 '14 at 21:42
  • I like it! It wouldn't take very much to make this into a polished tool... change inputs into args (either sys.argv or arcpy.GetParameterAsText) and make the temp os.environ.get("Temp")... – Michael Stimson Sep 11 '14 at 22:07
  • How would the table work inside of a Python Toolbox? I have three inputs. Two are feature classes, one is a geodatabase where the table goes. It gets down to this line ' x2 = ptFC2XCoordDict[i+1]' and fails with this message: Traceback (most recent call last): File "<string>", line 123, in execute KeyError: 1. Any ideas why it's doing that? – MjonesGEO Nov 14 '14 at 17:58
  • @joebob So that you can access much better formatting options to show your code snippet and the error it produces I recommend a new question. – PolyGeo Nov 14 '14 at 19:19
1

Using arcpy geometry objects is a good way to determine distances between features. Use data access cursors to access a feature's geometry and the method angleAndDistanceTo to determine distances.

From my blog:

import os
import arcpy

#inFeat is the path to your input feature class
#nearFeat is the path to your near feature class
#outTable is the path to the table that will be created by the function
def PointDistance (inFeat, nearFeat, outTable):
    #create table
    tabPath, tabName = os.path.split (outTable)
    arcpy.CreateTable_management (tabPath, tabName)
    #add fields
    fldDi = {"INPUT_FID" : "LONG",
             "NEAR_FID" : "LONG",
             "DISTANCE" : "DOUBLE"}
    for fld in ["INPUT_FID", "NEAR_FID", "DISTANCE"]:
        arcpy.AddField_management (outTable, fld, fldDi [fld])
    with arcpy.da.InsertCursor (outTable, ["INPUT_FID",
                                           "NEAR_FID",
                                           "DISTANCE"
                                           ]) as iCurs:
        with arcpy.da.SearchCursor (inFeat,
                                    ["OID@",
                                     "SHAPE@"
                                     ]) as inCurs:
            with arcpy.da.SearchCursor (nearFeat,
                                        ["OID@",
                                         "SHAPE@"]
                                        ) as nearCurs:
                for inOid, inGeom in inCurs:
                    for nearOid, nearGeom in nearCurs:
                        row = (inOid, nearOid,
                               inGeom.distanceTo (nearGeom))
                        iCurs.insertRow (row)
  • This is a good answer Emil, do you have a link to the help docs for angleAndDistanceTo? I can only find queryPointAndDistance which possibly does the same thing. – Michael Stimson Jun 6 '18 at 22:45
  • 1
    @MichaelStimson check methods for a geometry object. – Emil Brundage Jun 6 '18 at 22:49
  • Ah, I see now, angleAndDistanceTo is new in 10.3, I'm using 10.2 reference (to match my install). – Michael Stimson Jun 6 '18 at 23:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.