Find Nearest Features

Liz Sanderson
Liz Sanderson
  • Updated

FME Version

  • FME 2022.0


In this tutorial, you will learn how to find the nearest features using point and polygon datasets. The purpose of this operation is to find the nearest feature from an object of interest based on the straight line distance between features (i.e. starting from a point of interest, such as a school in Vancouver, find the nearest park polygon feature). In order to accomplish this, you need to use the NeighborFinder transformer. The NeighborFinder is typically used to identify the nearest features, features within a specified distance, or by finding the closest feature in a certain direction.


Step-by-step Instructions

In this scenario, you are interested in determining where the nearest park with washroom facilities is from the starting point, Alexander Academy. In this exercise, you will learn how to query your datasets based on attribute values and perform a simple spatial analysis using a point and polygon dataset. For an in-depth tutorial on finding the nearest neighbors and calculating both the distance and bearing for multiple features, see the Determining Nearest Neighbors article.

The first source dataset is a feature class from the Community geodatabase containing schools in the City of Vancouver. SchoolsSource.png
The second source is a MapInfo TAB dataset containing parks and park amenities in the City of Vancouver. ParkSource.png 

1. Create a New Workspace
Open FME Workbench and create a blank workspace. 
2. Add an Esri Geodatabase (File Geodb Open API) Reader
Add an Esri Geodatabase (File Geodb Open API) reader to the canvas by clicking on the Reader button on the top menu bar or by going to Readers > Add Reader. In the Add Reader dialog, select Esri Geodatabase (File Geodb Open API) as the Format, then for Dataset browse to the CommunityMap.gdb dataset which is available for download from the Files section on this article. Then open the Parameters. 
In the Parameters, click on the ellipsis next to Table and select both the Parks and Schools datasets. Click OK three times to finish adding the reader. 
3. Filter for Dog Parks and Washrooms
We only want the parks that have washrooms but are not dog parks, so we will use a Tester to filter the data. Click on the Parks reader feature type to select it. Then add a Tester to the canvas by typing “Tester” to bring up the list of FME Transformers in the Quick Add Search. Select the Tester from the list of Transformers by double-clicking or by using the arrow keys and the Enter key to add it. 
Double-click on the Tester to open the Parameters. In the parameters, click on the empty box below Left Value to show the drop-down arrow. Click on the drop-down arrow, expand Attribute Values, then select Washrooms. Set the Operator to =, then type in Y for the Right Value. 

Washrooms = Y

Then on the next line, set the test to Dog Parks = N. Change the Logic between the two tests from OR to AND. 
4. Filter for Alexander Academy
Next, we want to filter for school. To filter for the school name more accurately, we can cache the features. Run the Workspace with Feature Caching Enabled. To do this, click on the drop-down arrow next to the Run button, then select Enable Feature Caching. Click on the Run button to run the workspace. Now the features from the Schools dataset will be cached. 

Add another Tester to the canvas and connect the Tester_2 to the Schools reader feature type. In the Tester_2 parameters, set the Left Value to school_name, the Operator to =, then for the Right Value, click on the drop-down arrow and select Cached Values. Since there are multiple schools, this will open a Select Value dialog. 
In the Select Value dialog, click on Alexander Academy, then click OK twice to confirm the test. 
5. Find Neighboring Parks
With the data filtered, we can now find which park is the closest to Alexander Academy. Add a NeighborFinder transformer to the canvas and connect the Base input port to the Tester_2 (from the Schools reader feature type). Then connect the Candidate input port to the Tester (from the Parks reader feature type). Notice how the connection lines are crossed; this can be fixed by right-clicking on the Base port and selecting Move Down. 

Open the NeighborFinder parameters and enable Merge Attributes, the other parameters can be left as the default. The default parameters will return only the closest park, but if we wanted a list of parks, we could increase the Number of Neighbors to Find parameter. 
6. Keep Attributes
The NeighborFinder transformer creates multiple attributes, as well; since we merged attributes we now have both sets of attributes from the Parks and Schools datasets. To clean up the output, let’s only keep the attributes we want. 
Add an AttributeKeeper transformer to the canvas and connect it to the MatchedBase output port on the NeighborFinder. 

In the parameters, keep only the following attributes:

  • school_name
  • ParkName
  • _distance

7. Run Workspace
Connect an Inspector transformer to the AttributeKeeper Output port. 
Run the workspace by clicking on the Run button on the top toolbar, or by using Run > Run Workspace on the top menu bar. 
After running the workspace, only the park with a washroom that is the closest to Alexander Academy will be listed, which is Andy Livingstone Park. This park is 838.25 map units away from the school, which in this case is meters. 


Data Attribution

Data used in this tutorial originates from open data made available by the City of Vancouver, British Columbia. It contains information licensed under the Open Government License - Vancouver.

Was this article helpful?



Please sign in to leave a comment.