This site uses different types of cookies, including analytics and functional cookies (its own and from other sites). To change your cookie settings or find out more, click here. If you continue browsing our website, you accept these cookies.
We have extended our Early Bird Tickets for Inspire 2023! Discounted pricing goes until February 24th. Save your spot!
on 07-17-2018 09:26 AM - edited on 08-03-2021 11:22 AM by csalgado5
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.
There are 3 main sections when configuring the Find Nearest tool:
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:
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 atcommunity@alteryx.comif 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.
Great explanations and examples, @Kenda! Now, off to locate my nearest ice cream shop!
Thank you so much ^^
@Kenda in your point to line example, you use the line as the universe; in a use case I'm trying to solve, i found that if i used the line as the target I had no matches even though from a logical perspective (how many points are within 3 miles of this line) the line should be the target.
Once I flipped this it worked as desired.
Should non-point objects always be configured as the Universe?
@davidhe that might not always necessarily be the case. When a non-point object is used as the target, Alteryx will use the centroid. It is possible that you may need to increase your maximum distance a bit to account for this?
@Kenda BOOM "When a non-point object is used as the target, Alteryx will use the centroid"
Golden light bulb moment thank you! The line is 600 miles long, the centroid doesn't have any points near it. Of course!
Ah ha! That'll do it @davidhe
You could mess around with using the Poly-Split tool to break up your line into points before feeding it into the Find Nearest tool. This would enable you to use the whole line, not just the center. Alternatively, another option would be to use the Buffer tool to add a 3 mile radius around your line then use the Spatial Match tool to find where the target contains the universe, for example.
Hope this helps!
@Kenda Yes! Excellent thank you it's all coming together now.
This thread is full of golden breadcrumbs!
In my use case using the line as the universe appeared to work as desired, any pitfalls that I'm not thinking of?
I want to find out the distance of an individual lat/long point to a metro area on a map and 'm having an issue where if the target is a point, but the universe is a polygon, the Find Nearest tool will return zero matches.
Flip the target/universe around, and matches do appear, but conceptually this doesn't work for me.
Edit: I *thought* I solved this by using a summarise tool to turn the shape polygon into points, then it would do the find nearest calcs correctly......but it turns out it just returns what seems to be incorrect data.
Are there other tools where we can measure how far a point is to a spatial shape or area?
Hi @dbmurray
Thanks for checking out this article and posting your question. I wonder if you have your maximum distance set up to be large enough when the point is your target.
One thing you could try is to use a Formula tool to calculate the distance. To do this, set your data up so that your point and polygon spatial objects that you want to compare are in different fields:
Next, use a Formula tool to create a new field using this function:
ST_Distance(object1,object2,"Mi")
Recall that for the Find Nearest tool, if the target is not a point-type object, the centroid is used for analysis. This applies when using the Formula to measure distance as well. Test it out by switching which spatial object you use for the first and second object in your expression.
Here is an example where SpatialObj is a polygon and Split_SpatialObj is a point:
The Formula tool will give different answers, and that is because the second expression will use the centroid for SpatialObj. Alteryx will warn you of this:
Doing this will hopefully help you find the distance you're looking for and also determine if the maximum distance from your Find Nearest tool was set large enough.
Hey @Kenda - thanks for your very useful reply.
It turned out the solution to my problem wasn't really around the functions in your article here - but rather the setup of my shapefiles!
One file had a non-standard coordinate system. And hence Alteryx was having issues trying to calculate distance between spatial objects sourced from different coordinate systems.
Once I configured the inputs correctly, your approaches above worked like a charm!
Thanks for the hints!