# Paths through the U.S. states considering temperature

December 14, 2011, 11:18am

I am heading on a road trip soon that will last more than a year and will visit every U.S. state. In planning this adventure, I started to think about paths to take through the states. As I plan to camp for a good part of this road trip I wanted to ensure staying relatively warm throughout the entire trip. I decided to build a script that would help me figure out these paths and thought it would be useful to share with others.

This code figures out the possible paths through a set of given states, making sure not to visit any state twice. It also factors in climate averages per state and allows for an interval of time to stay in each state. As I move through the possible paths, it checks what month I am in for the state given the stay interval and checks to see if the temperature for that month for that state is within the bounds I've set. If not, that path is tossed. What is left are optimal paths through the states considering temperature.

Here is a short code snippet that shows how it works.

$sp = new StatePaths('climatedata');$states = $sp->getStatesByRegion('W', 'M'); // fetch states from West -> Mountain region$sp->setStartState('NM'); // find paths that start in NM
//$sp->setEndState('AZ'); // and end in AZ$sp->getPathsByStates($states); // fetching possible paths$sp->setStartMonth(4); // start trip in April
$sp->setInterval('1 month'); // spending 1 month in each state$sp->setTempRange(40, 68); // find paths that stay within this temp range

$sp->filterByTempRange(); // applying filter.$sp->listPaths(true);


The result looks like this:

Found 16 possible paths through 8 states
Found 3 paths suitable for temp range.
NM(51.79) => CO(52.50) => UT(65.68) => WY(66.25) => MT(65.64) => ID(56.16) => NV(50.55) => AZ(49.64)

NM(51.79) => CO(52.50) => WY(59.62) => MT(66.43) => ID(65.37) => NV(61.61) => UT(49.61) => AZ(49.64)

NM(51.79) => CO(52.50) => WY(59.62) => MT(66.43) => ID(65.37) => UT(61.53) => NV(50.55) => AZ(49.64)


The code also allows for further detail, such as specifying a temperature range per state as well as specifying how long you intend to stay in each state.

The class can also be used to simply figure out the paths and not apply any temperature filtering.

$sp = new StatePaths('climatedata');$states = array('WA', 'OR', 'CA', 'NM', 'AZ', 'NV', 'ID', 'CO', 'UT', 'MT', 'WY', 'TX', 'OK', 'KS', 'NE', 'SD', 'ND');
$sp->getPathsByStates($states);
// results
Found 6154 possible paths through 17 states
Script completed in 5.071683883667 seconds


As we can see from passing in all the states of the western continental U.S, there are quite a bit of possible paths. As the number of states you process increases, the paths (and computation time) will go up rapidly, depending on the edges (bordering states) each state you add has.

If you wish to play around with the code, you can find a link to the github repo below. I am currently using it to tie together paths from different regions to help me stay warm on my road trip, you may find other useful applications for it, add additional filters other than temperature, or replace states will something entirely different. Hopefully soon, I'll add an interactive demo map, where one can select states and enter parameters to see possible paths on an actual map.