I am using ArcView (ArcGIS Desktop Basic) 10.1 and I need to perform a point distance analysis.
What are the steps to determine the distance from A to B-Z, B to A-Z, C to A-Z, etc?
I am using ArcView (ArcGIS Desktop Basic) 10.1 and I need to perform a point distance analysis.
What are the steps to determine the distance from A to B-Z, B to A-Z, C to A-Z, etc?
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!"
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)