Finding the nearest feature with the same attributes

One of the key analysis functions of ArcGIS is the ability to determine distances between geographic features. One toolset in the Analysis toolbox, the Proximity toolset, contains several geoprocessing tools dedicated to calculating distances between or around feature locations. One of the tools in this toolset, the Near tool, is used to find which feature is the closest to each feature in an input feature class. Examples of using the Near tool include: finding the nearest high-school for each census block in the school district (closest facility problem); calculating the distance to the central business district as part of a site selection/suitability analysis; or determining the average distance to the closest park for different city districts, as a measure of quality of life.

The Near tool provides a simple but effective way for determining from a set of locations which is the closest. But what if the goal is to find the closest feature that has the same attributes as an input feature? Out of the box, the Near tool alone cannot answer this question unless your data is structured so that feature classes only contain features with like attributes (if this is the case, then you can simply run the Near tool once for each feature class, where the Input Features and the Near Features are both that same feature class). However, it is more likely that a feature class will represent similar features with some differences in attributes. For example, say you are analyzing data that represents samples of tree locations (a feature class where each point represents the location of a tree). This tree locations feature class will have an attribute field that specifies the exact species of tree that occurs at that location. Since the Near tool alone cannot find the closest feature within some attribute group, some work must be done to answer the question: How can I find the nearest feature with the same attributes?

Near By Group

Conceptually, this question can be answered by the Near tool (what is the nearest feature?). However, the question also contains a constraint (with the same attributes?) that is not directly supported by the Near tool. To answer the full question additional ModelBuilder techniques must be used, along with the Near tool, to create a custom workflow (this could also be done with scripting, but this entry focuses on a ModelBuilder solution). In ArcGIS 10.0, new iterator functions were added to ModelBuilder to allow for iterative or looping logic to be added to geoprocessing models. One of these model iterators, Iterate Feature Selection, allows for attribute groups to be designated and iteratively selected. Using selection sets output by the iterator allows for features belonging to the same attribute group to be iteratively fed into the Near tool – this will answer the question of which is the nearest feature with the same attributes.

In the graphic below, the Near By Group model is used to find the closest point of the same color. Since the Input Feature is red, the second red point is returned as the Nearest Feature, despite there being green and purple points closer than that second red point.


The Near By Group tool is structured as a nested model tool where the main model, Near By Group, calls a submodel, Iterate Near. It is necessary to include the Iterate Feature Selection iterator and Near tool in a submodel since those are the only components of the model that should be run multiple times (every part of a model that contains an iterator will be run multiple times; you can separate the parts that should be run iteratively from those that should not by using this kind of submodel structure).

Main model (Near By Group)

1. Copy the Input Features to a new Output Feature Class

2. Add a Near ID field (this field will contain the ID of the nearest feature in the same attribute group)

3. Add a Distance field (this field will contain the distance from an input feature to the nearest feature in the same attribute group)

4. Run the submodel, Iterate Near, to execute the Near tool once for each attribute group (see the submodel steps below)

5. Delete the NEAR_FID and NEAR_DIST fields that were added by the Near tool (this information is contained in the new fields that were added in steps 2 and 3)

Submodel (Iterate Near)

1. Run an iteration to select all features that belong to an attribute group

2. Run the Near tool to determine the distance from each Input Feature in that attribute group to the nearest feature in that attribute group

3. Calculate the Near ID field (added in the main model, step 2) equal to the value of the field NEAR_FID added by the Near tool

4. Calculate the Distance field (added in the main model, step 3) equal to the value of the field NEAR_DIST added by the Near tool

More Information

The Near By Group tool can be downloaded from here. The download contains the model tool, sample data, and the contents of this entry.

About the author

Drew Flater is a group lead product engineer at Esri, working on the spatial analysis and geoprocessing capabilities of ArcGIS.

Next Article

FCC Form 477 Broadband Layer Updated to June 2021

Read this article