Jump to content

Calculating distance to median point for a group of points in Model Builder In ArcGIS


Recommended Posts

I have two shapefiles. First of them, there is about 115 000 points (clients and their field size) All together there is about 16 226 different clients. In the other shapefile I have calculated the median point for all the clients. Now, I want to calculate the distance from each client field to its corresponding median point. Doing it one by one takes a lot of time.

I tried to use Generate Near Table tool but it takes account other clients mean centers too, because some are near to each other. I need to classify source points by client number, but no distance tool have case field option to do this.

How this process should look like in Model Builder? I know that i must use iterators but witch ones? I tried to do different things but I still get wrong results.

 

Can someone help me? :)

Link to comment
Share on other sites

Try this python script: ------

import arcpy
import math

# Change these first four variables to suit your data
# 1. points is the raw point file
# 2. medianPoints is a point file of median points using ArcMap's 'Median Center' tool
# 3. sharedNameField is the name of the field that both points and medianPoints share
# 4. distanceFieldName is the name of the field that will be created to store distances

points = r'C:\give your file path\your data.shp'
medianPoints = r'C:\give output file path\points_median_center.shp'
sharedNameField = 'name of the field'
distanceFieldName = "pointDist"

# add field to point dataset that will store distances corresponding to median point
arcpy.AddField_management(points, distanceFieldName, "DOUBLE")

# loop through median points, get x,y coords and field value
medianPointCursor = arcpy.SearchCursor(medianPoints)
for medianPoint in medianPointCursor:
medPointXY = medianPoint.getValue('Shape').getPart()
x1,y1 = medPointXY.X, medPointXY.Y
fieldValue = medianPoint.getValue(sharedNameField)

# query the point file for only points that match the sharedNameField value
query = "{0} = '{1}'".format(sharedNameField, fieldValue)
pointCursor = arcpy.UpdateCursor(points, query)
for point in pointCursor:
pointXY = point.getValue('Shape').getPart()
x2,y2 = pointXY.X, pointXY.Y
distanceToMedianPt = math.hypot(x2 - x1, y2 - y1)
point.setValue(distanceFieldName, distanceToMedianPt)
pointCursor.updateRow(point)

del point, pointCursor
del medianPoint, medianPointCursor
  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.

Disable-Adblock.png

 

If you enjoy our contents, support us by Disable ads Blocker or add GIS-area to your ads blocker whitelist