# Selecting Features and Averaging Attributes

I currently have two point shapefiles, SHP A with approximately 75 points and SHP B with approximately 2000 points. What I would like to do is:

1) For each point in SHP A I would like to find the closest 10-15 points in SHP B

2) Find the average of Field1 for the selected SHP B points

3) Place the average Field1 for the closest 10-15 points into attribute table for each point in SHP A

I can easily comprehend and complete the selection process of this task, and I'm sure I could complete it in a very long way, but I am wondering if there is any tools or processes to do this in an easier manner.

• Can you expand on "closest 10-15 points"? How do you decide? Also, since this answer will likely involve arcpy cursors, which version of ArcGIS are you using? Jan 29 '15 at 0:46
• Deciding the points is not overly crucial, I am probably simply going to do a 4.5m circle buffer around each point in SHP A, and select all points from SHP B that falls within the buffer. However, I would like a way to tell the software that for each individual point in SHP A, take the points from SHP B that fall within the buffer, average the Field1 values of the SHP B points, and put that value into the attribute table of the SHP A point. Jan 29 '15 at 18:11

And your licensing level. The "Generate Near table" tool in the ArcGIS analysis toolbox sounds like the one you need but this is "advanced" level. These tools also specify a maximum distance around which to search for near features, rather than a number of near features. If you specify no distance, all features will be compared. Bear in mind that this will generate a quite large table, ie 75 X 2000 records.

If you haven't got an advanced license, then you could do a bit of schlepping in Access (or other db) by adding the coords to each table, then make a query with both tables, but with no link. This generates the same list of everything to everything. Then calculate a Euclidian distance on each pair of coords. Then do some more querying to get the number of near things you require.

• I do have an advanced license with access to all extensions. I am not at my computer with Arc on it right this moment so I cant play around with the tool you are referring to. I take it that this creates a new table showing the closest points in one layer to each point in another layer? Jan 29 '15 at 18:12
• Yes. See the tool box. Analysis Tools / Proximity / Generate Near Table Jan 29 '15 at 18:20
• I can see how that would allow me to see the nearest points in SHP B and the distance they are from each point in SHP A but not sure how from this I can average attributes from the original SHP B attribute tables? Jan 29 '15 at 19:27
• @GISgoose It won't be a one-step process. Related: gis.stackexchange.com/questions/126639 and gis.stackexchange.com/questions/124063 You give two methods of selecting B points - distance or count. GNT can accommodate both (and doesn't have to create as large a table as this answer currently suggests). Once you've run it, you'll have to join the result back to the B shapefile based on FIDs to pull in your Field1. Then you'll run Summary Statistics on the joined table, with A's FID as a CASE field and B Field1 as a statistic field type AVG. Then join that result back to A. Jan 29 '15 at 22:51

Per your clarifying comment on the question, if you do choose to go the buffer route (and note then that you may or may not have anywhere near the same number of B points averaged to get the value for each A - this does seem like a pretty significant factor in the analysis, if it's going to be count or distance based, but if you say it isn't crucial...), another alternative would be to:

1. create your A buffers (do not dissolve)
2. intersect them with your B points, which will give your B points an A identity attribute
3. run Summary Statistics on the intersected B result with the A identity attribute as a CASE field and B Field1 as a STATISTIC field of type AVG (as noted in my comment on Neil's answer)
4. join that SS table result back to A points (or use Join Field to pull the attribute in rather than join and export)
• Both this and your comment on Neil's response will help tremendously, thank you! Jan 30 '15 at 0:52

This code will work in field calculator (Python) on numeric field

``````def getAver(shp,n):
fc=r'D:\Scratch\points2000.shp'
lst=[]
p=shp.lastpoint; x=p.X;y=p.Y
with arcpy.da.SearchCursor(fc, ["Shape@","d2river"]) as cursor:
for row in cursor:
p2=row[0].lastPoint
dX=x-p2.X; dY=y-p2.Y
d=dX*dX+dY*dY
lst.append([d,row[1]])
lst.sort()
avr=[]
for i in range(n):
p=lst[i]
avr.append(p[1])
import numpy
return numpy.mean(avr)
``````

adjust path to shapefile with 2000 points (line 2 of the block) and field name to summarise in line 5. Use

``````getAver( !Shape!,15)
``````

to call function. Change numpy.mean(avr) in the last line to other statistics if required. P.S. For the life of me I cannot figure out why shp.distanceTo(p2) didn't work.