Determining Nearest Neighbors

Liz Sanderson
Liz Sanderson
  • Updated

FME Version

  • FME 2022.2

Introduction

In this tutorial, you will learn how to find the nearest point from a point of interest based on the straight line distance between features. This is done in FME using the NeighborFinder transformer, which can be used to identify the nearest features, features within a specified distance, or the closest feature in a certain direction.

The NeighborFinder locates the nearest feature(s) and transfers its attributes to create a record of that neighbor. It also adds attributes to record the distance and bearing (angle) to that neighbor.

To learn how to find nearest neighbors with non-point features, such as polygons, see the Find Nearest Features article.
 

Step-by-Step Instructions

In this scenario, we have residential address points in an Esri File Geodatabase and cell phone tower locations in a CSV (Comma-Separated Value) file.

Screenshot 2022-12-16 at 9.08.23 AM.png

Screenshot 2022-12-16 at 9.08.43 AM.png

We want to find the nearest tower to each address and write that information into a new address dataset.

Download the attached workspace template and source data to follow along.

1. Generate a new workspace

Open FME Workbench and choose Generate Workspace. Set the reader and writer as follows:
 
  • Reader Format: Esri Geodatabase (File Geodb Open API)
  • Reader Dataset: <Path to tutorial download>/Addresses.gdb
  • Writer Format: Esri Geodatabase (File Geodb Open API)
  • Writer Dataset: <Path to tutorial download>/NewAddresses.gdb

Note we are writing to a new Geodatabase instead of updating the same one.

Screenshot 2022-12-16 at 8.26.50 AM.png

Click OK. When prompted, select only the PostalAddress table, not the PostcodeBoundaries table.

Screenshot 2022-12-16 at 8.27.44 AM.png
Click OK.

The generated workspace should include a Geodatabase reader and writer.

Screenshot 2022-12-16 at 8.31.28 AM.png

2. Add writer attributes

Double-click the writer feature type to open its Feature Type Properties dialog. Click the User Attributes tab and add two new attributes:
 
  • Name: STATIONID, Type: int
  • Name: STATIONDISTANCE, Type: double
 

Screenshot 2022-12-16 at 8.33.47 AM.png

3. Add a CSV reader

Click the Add Reader icon and add a reader with the following parameters:
 

  • Format: CSV (Comma Separated Value)
  • Dataset: <Path to tutorial download>/CellTowers.csv
  • Coord. System: LL84


Screenshot 2022-12-16 at 8.37.58 AM.png

If you click Parameters, you will notice the reader automatically sets the data type for the longitude attribute to x_coordinate, and the data type for the latitude attribute to y_coordinate.

Click OK. The workspace now includes two readers.

Screenshot 2022-12-16 at 8.42.06 AM.png

4. Add a Reprojector transformer

Before we use the NeighborFinder, we have to get the datasets into the same coordinate system. Add a Reprojector transformer and connect it after the CSV (cell tower) feature type. Open the parameters and configure it as follows to reproject the data to UTM83-10.

Screenshot 2022-12-16 at 8.46.17 AM.png

Click OK.

5. Add a NeighborFinder transformer

Add a NeighborFinder transformer. Connect the PostalAddress feature type to the Base port, and connect the Reprojector to the Candidate port. Connect the MatchedBase output port to the writer feature type.

Screenshot 2022-12-16 at 8.50.01 AM.png

Open the NeighborFinder parameters dialog.

  • We only need to find 1 nearest feature, so leave Number of Neighbors to Find set to 1
  • Set Maximum Distance to 500
  • Under Attribute Accumulation, check Merge Attributes


Screenshot 2022-12-16 at 8.55.16 AM.png

Click OK.

6. Map attributes

Expand the attributes on the NeighborFinder:MatchedBase output port and the writer feature type. Map StationID to STATIONID on the writer and _distance to STATIONDISTANCE.

Screenshot 2022-12-16 at 8.58.37 AM.png

Alternatively, you can use an AttributeManager transformer to map the attributes.

7. Run the workspace

Run the workspace and inspect the output. Each address should now include attributes that record the nearest cell phone tower (STATIONID) and the distance to that tower (STATIONDISTANCE).

Screenshot 2022-12-16 at 9.04.05 AM.png

Tips

You may want to attach an Inspector transformer to the NeighborFinder:UnmatchedBase port to catch any addresses that did not find a matching tower within 500 metres.

To find out how many towers are within 500 metres of an address, open the NeighborFinder parameters dialog. Delete the value of the Number of Neighbors to Find parameter so FME no longer stops at a single neighbor. Enter a value (such as "MyList") in the Close Candidate List Name parameter. A list of towers will now be created on the output and can be counted using the ListElementCounter transformer. This will take longer to run. To learn more about working with list attributes, see the Tutorial: Getting Started with List Attributes.
 

Data Attribution

The data used here originates from data made available by the City of Vancouver, British Columbia. It contains information licensed under the Open Government License - Vancouver.

Was this article helpful?

Comments

0 comments

Please sign in to leave a comment.