Advent of Code is back! Unwrap daily challenges to sharpen your Alteryx skills and earn badges along the way! Learn more now.
Free Trial

Tool Mastery

Explore a diverse compilation of articles that take an in-depth look at Designer tools.
Become a Tool Master

Learn how you can share your expertise with the Community

LEARN MORE

Tool Mastery | Find Nearest

Kenda
16 - Nebula
16 - Nebula
Created
Find Nearest.png

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!

melting ice cream cone.gif

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.

1.png2.png

This tool accepts up to 2 inputs and has 2 outputs.

  • Inputs:
    • 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.
  • Outputs
    • M
    • U

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.

3.png

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.

michelle ice cream.gif

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.

4.png

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.

5.png

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.

6.png

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 LineFind 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.

7.png

8.png

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 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.

Attachments
Comments
ChristineB
Alteryx Alumni (Retired)

Great explanations and examples, @Kenda!     Now, off to locate my nearest ice cream shop! Smiley Very Happy

MojiMiracle
7 - Meteor

Thank you so much ^^

davidhe
Alteryx Alumni (Retired)

Excellent work as always @Kenda !!!

Kenda
16 - Nebula
16 - Nebula

Thanks @davidhe! Glad you were able to find this useful 😊 

davidhe
Alteryx Alumni (Retired)

@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? 

Kenda
16 - Nebula
16 - Nebula

@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?

davidhe
Alteryx Alumni (Retired)

@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! 

Kenda
16 - Nebula
16 - Nebula

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!

davidhe
Alteryx Alumni (Retired)

@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? 

Kenda
16 - Nebula
16 - Nebula

@davidhe  hmm it doesn't sound like it. Check your matched output on a sample set of records to make sure you're getting what you expect!

dbmurray
8 - Asteroid

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?

Kenda
16 - Nebula
16 - Nebula

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:

Kenda_0-1673530235914.png

 

 

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:

Kenda_2-1673530640108.png

 

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:

Kenda_3-1673530733635.png

 

 

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.

dbmurray
8 - Asteroid

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!

rohityaman
8 - Asteroid

Quick tip: If you ever get confused which spatial object should be selected as Target and which as Universe, question this - "What output do you expect from unmatched output of the tool?" Unmatched output will only have Target input records which did not have any match with Universe. 

kmbassett01
8 - Asteroid

Is the Maximum Distance capped at 100 miles? How can I find nearest over a larger area?

Kenda
16 - Nebula
16 - Nebula

Thanks for checking out the article, @kmbassett01 

 

You should be able to type in whatever distance you'd like:

image.png