Connect-the-Dots: Drawing Shortest-Distance Lines between Features in a Dataset

Last week, my colleague Dani Riebe asked for some help. Her problem? She wanted to make a GIS map of connect-the-dots, only the dots were sites in her study region, and she wanted to connect just the sites that were closest to each other. Since her study region is relatively flat, she didn’t need to worry about topographic changes – in other words, Euclidean distance would be just fine.

The sites in Dani's study region

The sites in Dani’s study region

There is, actually, a non-GIS solution to this problem. She could measure the distance between the sites using a program like Google Earth, write down all the distances, and figure out which sites were closest to which other sites. Then she could manually draw lines to connect the sites using a program like Photoshop. Of course, if any of her calculations were wrong, she’d have to redraw the map.

Together (and with the help of the StackExchange forum), we figured out a way to do this all in ArcGIS.

1. First, double-check that you are working in the correct map projection and units. In the Table of Contents, right-click “Layers,” select “Properties.” First check the “Coordinate System” tab to make sure the correct projection is selected – in this case, UTM. Then, check the “General” tab to make sure the correct units – in this case, meters.

2. Add coordinates to your database of sites. Later, these will act as the coordinates of our starting sites for building lines. In this case, we’re working with a shapefile called “Sites.” To find out if the coordinates are already listed, right-click the layer and select “Open Attribute Table.” If you need to add coordinates, go to the Arc Toolbox / Data Management Tools / Features, and run the “Add XY Coordinates” tool. Voila!

The "Add XY Coordinates" tool

The “Add XY Coordinates” tool

3. Calculate the distance between each site. Go to ArcToolbox / Analysis Tools / Proximity, and run the “Generate Near Table” tool.

  • Select your “Sites” shapefile as both the “Input Features” and the “Near Features” (this asks the program to calculate the distance between each feature and all the other features in the same dataset).
  • Name your output file.
  • Check the “Location” box. This will add XY coordinates for each target site to the output table.
  • If you want to calculate just one closest feature, you are set to go – hit OK. Otherwise, deselect the “Find only closest feature” box, and type in how many sites you want to run calculations for. In this case, we want to find the 2 closest sites. Then hit OK.

    The "Generate Near Table" tool

    The “Generate Near Table” tool

4. Open the output table to see how pretty it is. You should see a bunch of fields, including “Near_Dist” (the Euclidean distance between each site), “Near_Rank” (in our case, whether it is the first- or second-closest site), “From_X” and “From_Y” (the coordinates of the starting sites), and “Near_X” and “Near_Y” (the coordinates of the target sites). If everything is in order, move on to Step 6.

5. If the starting site coordinates are missing…  When Dani and I ran this the first time, our output table did not have the starting site coordinates. We had to figure out another way! It turns out that there is another field, “In_FID,” which tells us which is the starting site.  If you open the attribute table of the “Sites” file, the FIDs should match up with the numbers listed in “In_FID.” All we have to do to copy the site information to the output file.

  • Right-click your output table, select “Joins and Relates” and then “Join.”
  • Make sure the join is based on “In_FID,” and that you are joining data from the “Sites” file and the “FID” field.
  • Hit OK. The tool automatically updates the output table, which now contains all the information about each starting site – including the coordinates! If you didn’t have this information before, you should now.
  • Since “Join” doesn’t permanently save information to the table, save a copy of it. Right-click the output table, and select “Export” (you may have to save it as a .txt file).

6. Now, the fun part! Go to ArcToolbox / Data Management Tools / Features and select the “XY to Line” tool.

  • Load the updated table.
  • Name the output file.
  • Select “From_X” and “From_Y” as the starting site coordinates, and “Near_X” and “Near_Y” as the target site coordinates.
  • Check that the spatial reference is correct. In this case, we needed to change it to a UTM projection.
  • Hit OK.
The "XY to Line" tool

The “XY to Line” tool

Ta da! You should have a shapefile of lines that connect the closest sites in your database. You could even change the colors or line properties to distinguish the first- and second-closest sites.


Lines connecting sites to the two nearest sites – i.e., the shortest-distance lines


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s