This article is part of the Tool Mastery Series, a compilation of Knowledge Base contributions to introduce diverse working examples for Designer Tools. Here we’ll delve into uses of the Find Nearest Tool on our way to mastering the Alteryx Designer:
Imagine this – you’re on vacation. You’re on a sandy beach where the sun has been relentless all day. It’s hot and you need something to cool you off. Ice cream would be perfect! But there’s no cell phone service here! How can you find the closest ice cream shop without Google? Well, with Alteryx of course!
The Find Nearest tool in the Spatial category can save the day. This tool identifies the shortest distance between points or polygons in one file and the points, polygons, or lines in a second file.
This tool accepts up to 2 inputs and has 2 outputs.
- T (Mandatory)
- U (Optional)
- When deciding which data source will be the target and which will be the universe, make sure to keep your question/objective in mind. Some like to think of the Target as the known and the Universe as the unknown.
There are 3 main sections when configuring the Find Nearest tool:
- Targets (T Input): Select the spatial object field you want to use as the target
- Any type of spatial object can be used here, but if it is not a point, the centroid will be used
- Universe: The spatial object field that will be used as the universe
- If a tool is connected to this input, the ‘Use Records from U Input’ will automatically be selected. In this case, a spatial field from the connected tool will be used as the universe
- If no tool is connected to the U input, you must select a file where the spatial object field is stored. You can either type the path in the textbox or click the down arrow to browse for the file
- Whichever radio button is selected, you must choose the spatial object field to use from that source
- Selection Criteria
- How many nearest points to find?
- Defaulted to 1, this is the number of nearest Universe objects that will be returned for each Target
- Maximum Distance
- How far away you are willing to search for the nearest object
- Ignore 0 Distance matches
- If this checkbox is checked, records matched with themselves will not be returned
Below these three sections is a select table where you are able to select, deselect, reorder columns; modify data type and size; and rename a column or add a description (much like a Select tool).
If any of the objects from the Target are found within the maximum distance from the universe, they will be returned out of the M output. The number of matches returned depends on the total objects matched along with the threshold you set for how many nearest points to find. Remember M for matched objects.
The Target objects that did not find a nearest point with an object from the Universe will come out the U output. Remember U for unmatched objects.
Point to point - Find the nearest Dairy Queen to your location
Luckily, your Grandma had just sent you a listing of all of the Dairy Queen locations before you left for vacation. Looking around, you find the address of your current location and plug it into a Text Input tool. Follow it up with a CASS and a Street Geocoder tool to get the spatial object for where you are. Now, you’re almost there. You just have to configure the Find Nearest tool.
We know our current location, but we do not know which Dairy Queen is closest to us. Therefore we connect our location to the Target input and configure Grandma’s file of DQ locations to the Universe input. There is only one spatial object field in each, so we select those. We are only concerned about the Dairy Queen closest to us, so we keep the nearest points to the default of 1. We are willing to drive a while to find a location, so we will set the maximum distance to 20 and select Drive Distance (Mi). We don’t see any Dairy Queen locations around, so there shouldn’t be any 0 distance matches, but we will leave this unchecked anyway.
After running the workflow, we see that two fields were added that were not present in the Target or the Universe inputs.
FindNearestRank is an integer representing the rank of nearest locations from the Universe objects. If you only selected to return 1 nearest point, all of the ranks will be 1. However, if two Universe objects were found within the maximum distance, the closest one will have a rank of 1 while the other will have a rank of 2. In the rare instance that two universe objects are the same distance away, they will have the same rank.
DriveDistanceMiles will return the miles it would take to drive from the Target location to the Universe object.
*Other fields may be present depending on the configuration of the Find Nearest tool.*
Looking at the results, you see that there is a Dairy Queen close by! Only 2.52 miles away to drive there! Feeling joyful, you hop in your car and head to the nearest DQ that you found without the use of your phone.
Now on to some additional, potentially more practical, use cases…
Point to Line – Find the nearest US coastline to each US state capital
Not only can the Find Nearest tool find the closest point to a point, it can find the closest line to a point (and vice versa). In this example, we are wanting to find the nearest US coastline to each US state’s capital.
Here, our question is “Which US coastline is nearest to each capital city?” This means we know where each capital is, but we are trying to figure out where the closest coastline is. Therefore, our known (Target) is the capital city and the unknown (Universe) is the coastline.
We select the spatial object fields to be used from each input. We only want the nearest coastline returned, so we leave the number of nearest points to find at 1. Let’s say that we want to find the nearest coastline but want to ignore the ones that are really far. In this case, we will choose 300 Kilometers as the maximum distance to search for a match.
After running the workflow, we see that 19 records come out the U output. This means that 19 capital cities do not have a coastline within 300 Kilometers.
33 records come out the M output meaning 33 capitals do have a coastline within 300 Kilometers. But wait. There were only 50 Target inputs, and 19 + 33 is 52. Taking a closer look at the output we see that two states (California and Nevada) matched with two spatial objects from the Universe that were the same distance away, and both received a rank of 1. This happened because two segments of the Pacific coastline were equidistant from each of the capitals. A Unique tool was added so that we only have one record per city.
Unlike our point to point drive distance output, we had 3 additional fields in our matched output here instead of 2.
FindNearestRank is the same as before.
DistanceKilometers returns the distance, in Kilometers, of a straight path from the point spatial object (capital city) to the centroid of the line spatial object (coastline)
Direction tells you the cardinal direction the Universe (coastline) is relative to the Target (city).
The Browse tool will help you visualize your results.
If you want every capital city to match with a coastline, increase the maximum distance until no records come out the U output. With this dataset, the maximum distance must be at least 1302 in order to find a match for every capital city.
Polygon to Line – Find the three nearest falls to select zip codes
In this last example, say we are a travel agency located in various zip codes in the Northeastern US. We have several people calling in and asking about which falls they should visit, out of Niagara and Great Falls. We want to find the falls that are closest to each of our zip codes so that we can recommend the correct destination to our customers.
We know which zip codes we are located in, so we use that as our Target input. We are trying to figure out which falls are closest to each of the zip codes, so this will be the Universe input. Let’s return the closest two points within 500 miles so that we can see the distance of both falls in our matched output, given they are both within 500 miles.
Similar to example two, because our spatial objects are not points, the centroid of each polygon (zip code) and line (fall) will be used to determine distance.
As we can see in our M output, nearly all of the zip codes are within 500 miles of both falls. One zip code, 49411, was within 500 miles of Niagara Falls but not Great Falls and returned only one record in the M output. One zip code, 53706, was not within 500 miles of either of the falls and came out the U side.
That’s it! Pretty powerful and not very hard to use once you’ve got the hang of it. If you’re looking for even more content on the Find Nearest tool, the Alteryx Community has lots of resources currently available:
These resources reveal other use cases for this tool including:
- Discovering patterns of customer visits to particular retail locations
- Selecting site closest to public transportation hubs
- Determining number of competitors within a range of a business
- Calculating total shipped miles per item from the closest warehouse to each store
By now, you should have expert-level proficiency with theFind NearestTool! If you can think of a use case we left out, feel free to use the comments section below! Consider yourself a Tool Master already? Let us know email@example.com you’d like your creative tool uses to be featured in the Tool Mastery Series.
Stay tuned with our latest posts everyTool Tuesdayby followingAlteryxon Twitter! If you want to master all the Designer tools, considersubscribingfor email notifications.