Last active
June 19, 2024 16:53
-
-
Save OterLabb/f014bdb1e5c254cc01884d626d61d2ce to your computer and use it in GitHub Desktop.
ArcGIS distanceTo script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import arcpy | |
# Variables | |
pointData = 'points' # Point dataset | |
distanceData = 'other_fc' # Distance to this dataset | |
arcpy.env.workspace = r'C:/Users/myUser/Documents/GIS/new_data.gdb' | |
# Create a new field name, and add it to pointData | |
distanceField = 'dis_' + distanceData[:4] # Gets first 4 letters of distanceData | |
arcpy.AddField_management(pointData, distanceField, 'FLOAT') | |
# Get geometry of distanceData | |
lCursor = arcpy.da.SearchCursor(distanceData, ['SHAPE@']) | |
for lines in lCursor: | |
lineGeometry = lCursor[0] | |
# Gets total number in pointdata for status updates | |
totalNumber = int(arcpy.GetCount_management(pointData).getOutput(0)) | |
countNumber = 0 | |
with arcpy.da.UpdateCursor(pointData, ['SHAPE@', distanceField]) as bCursor: | |
for points in bCursor: | |
pointGeometry = points[0] | |
distance = pointGeometry.distanceTo(lineGeometry) # Actual calculation of distance | |
# Calculate and print status | |
countNumber = countNumber + 1 | |
percentNumber = float(countNumber) / totalNumber * 100 | |
print(round(percentNumber, 2), "% completed. Distance is", distance, "meters.") | |
# Update new field with distance | |
bCursor.updateRow((pointGeometry, distance)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is the distance unit controlled by the spatial reference? I am testing this out and it seems like the returned distance values is not what I had initially expected.