# spatially joining points within same polygons

549
1
12-19-2013 12:29 PM New Contributor
I have point file "A" that I want to spatially join to point file "B" in order to get distance from the A points to the closest B point, but only when the A and B points at within the same polygon feature (e.g., county). What are the ways I can do this?

I do not want to select each polygon one-at-a-time.
Tags (3) by MVP Honored Contributor
I have point file "A" that I want to spatially join to point file "B" in order to get distance from the A points to the closest B point, but only when the A and B points at within the same polygon feature (e.g., county). What are the ways I can do this?

I do not want to select each polygon one-at-a-time.

Multistep processing is how you solve all problems like this.

1.  After precalulating the X/Y coordinates of all points, spatially join both sets of points to the polygon file (two operations).  I assume no two polygons overlap so each should have a single point for each original point (unless they touch the polygon boundary).
2.  Spatially Join those outputs to each other within the tolerance you want using the One To Many option and get all matches (since the closest might not be in the same polygon).  The tolerance should be no bigger than the widest distance across the widest polygon.
3.  Select by Attributes where the joined points have the same polygon FIDs and export that point set.
4.  Calculate the Euclidean distance of the points from the X/Y values of both points.
5.  Summarize with the PointAFID values as the case field and get the Min Euclidean Distance.
6.  Join the summary table back to the point pair join set and select where the distance is equal to the MIN distance.
7.  Break the join.
7.  Export the result and you have the set of points in A that were within the tolerance of point B that were in the same polygon.

Geoprocessing is always supposed to be multistep for anything worth doing.  Also, I never worry about creating more data than I need and throwing away what I don't want if no optimized direct tool is available.  Time used to think up a more direct approach costs more than throwing away useless data and I find the direct approach often takes about as much processing time in the end anyway.  Even if you wrote a more direct processing script, it pretty much has to do most of these steps anyway. 