Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

(TL;DR: author begins quest to use undiagnosed OCD to come up with different paths to walk to work…all in the name of weight loss)

I love to walk.

I’m in between jobs. Despite being offered the same rent for another year after concluding my data-driven battle with my real estate agent, I’ve moved apartments. All of this means less time to cook, and more time for fast food. McDonald’s Monopoly is around so how could I resist? I’ve won a Big Mac, a sundae and a small fries so far. What else could I win?

As a result, I’ve put on a bit of weight. So I have started to walk to work a lot. But I find myself getting off the train at Newtown station and walking to the city via the the same route everyday.

I need a way to mix up the routes to work between Newtown and the CBD.

More importantly, I need to feed my tendency to obsess over most things mathematical and technical.

# Graphs, maps and almost no maths

A street map can be represented as a graph. When you hear the word ‘graph’, you probably think of some sort of chart. You might even think of the graphs of mathematical functions that you would have encountered in high school:

But what I mean by the word ‘graph’ is something like this:

The circles are vertices, and the lines connecting the vertices are edges.

The graph above is an undirected graph – there is no restriction on the direction of travel along any given edge. Here is a directed graph, where there may be restrictions placed on which way we may travel along a given edge:

When talking about directed graphs, the circles are often called nodes and the directed edges are often called arcs. But let’s not worry about this distinction. For our purposes, a graph G is a mathematical structure defined as

G = (V, E)

where V is a set of vertices and E is a set of edges. In other words, a graph is a collection of vertices and edges. Not too difficult, right?

## What does this have to do with walking to work?

Let’s take a look at a common representation of a street map. This is one of an area in Newtown:

Let’s strip out everything but the basics. This is an abstract representation of the same map:

This is a graph! Pretty cool, right?

We can see that each intersection is represented by a vertex. There are also some vertices that don’t appear to be connected by edges. These represent points of interest such as shops.

# Where the data at? OpenStreetMap!

I don’t know what I would do without access to open-source data and software. I’m a big fan of the open-source philosophy. In honour of R (which is open-source) and open-source slack emojis, let’s take a minute to dance:

Note: The above works on several levels. The parrot that inspired this amazing Slack emoji is the endangered kakapo from New Zealand. R was created in New Zealand. I’m part kiwi. We’re a bunch of open-source kiwis! For more hilarious party parrot emojis, visit the Cult of the Party Parrot. You won’t be disappointed!

## OpenStreetMap

The OpenStreetMap project was founded in 2004 by Steve Coast. Its goal is to promote the development and distribution of open-source geospatial data. Data is contributed to OpenStreetMap by a community of mappers. Click here to find out how to start contributing yourself.

### How do I get data out of this thing? Exporting data directly from OpenStreetMap

On the OpenStreetMap website, you can navigate to an area of a map and export the data directly from your browser:

This approach to get map data works well when you’re zoomed in and, as a result, you don’t need to download much map information from the OpenStreetMap API. If you zoom out and try to export a larger area of a map that contains more than 50,000 nodes, you will be presented with an error.

I need to find another source. Remember the coordinates present in the image, above. We’ll use these later.

The good people at Geofabrik update huge extracts of OpenStreetMap data each evening. The world is carved up into a collection of regions, which may be further split into sub-regions. Even Antarctica makes an appearance.

We’re only interested in Australia so let’s navigate to ‘Australia and Oceania’ under ‘Sub Region’.

Next, we’re presented with a number of different file types. The R package that we’ll use below has a function to read ‘.osm’ files directly into R. However, the ‘.osm’ file for Australia is about 7 gigs when uncompressed. R takes far too long to read this into memory. So we’ll download the ‘.osm.bz2’ file, extract the single file contained within, and subset it outside of R into a smaller, more manageable region. This smaller region will be a map containing the area between Newtown and the CBD only.

## Process the data: Osmosis, I choose you

Osmosis is a Java application that is built to process OpenStreetMap data. Specifically, we’re going to use it subset the file containing all of Australia into the area encolsed by this bounding box stated in terms of latitude and longitude in the decimal degrees format:

• left = 151.1681
• bottom = -33.9039
• right = 151.2142
• top = -33.8595

How did I come up with these? And why the need for 4 decimal places of precision? Am I that pedantic? I will answer these questions in reverse order:

• Am I that pedantic? – No.
• Why do I need 4 decimal places of precision? – I don’t.
• How did I come up with these? – Remember when we tried to export map data directly from the OpenStreetMap website? I simply zoomed into an area of the map that contained both Newtown and the CBD, clicked on the ‘Export’ button and voila! Coordinates!

```<span class="n">osmosis_path</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="s1">'C:\\osmosis-latest\\bin'</span><span class="w">
</span>```

Note the double backslashes. I’m on a Windows machine where paths are delimited by backslashes. A backslash is a special character in R that means ‘process the character immediately to the right as text’. This is called ‘escaping’. So I need to ‘escape’ the backslash itself with another backslash so that the first backslash is processed as text, and not as the ‘escaping’ character. Sorry about the gratuitous use of the word ‘backslash’.

As this is a Java application, you will need to have the Java Runtime Environment installed on your computer. Install it and take note of the full path to the ‘javapath’ folder. For me, this is where it exists:

```<span class="n">java_path</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="s1">'C:\\Program Files (x86)\\Common Files\\Oracle\\Java\\javapath'</span><span class="w">
</span>```

We will be using this information…right now!

### Set the PATH

The Java folder may already exist in your system PATH variable. Osmosis may also exist in the variable. You can find out by issuing:

```<span class="n">Sys.getenv</span><span class="p">(</span><span class="s1">'PATH'</span><span class="p">)</span><span class="w">
</span>```

But who cares? Let’s make sure that they are in your PATH variable by putting them there now. If you ran the above line, you will see that the variables are separated by semicolons. So we will make sure to use semicolons to separate our new paths:

```<span class="n">Sys.setenv</span><span class="p">(</span><span class="n">PATH</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">paste</span><span class="p">(</span><span class="n">Sys.getenv</span><span class="p">(</span><span class="s1">'PATH'</span><span class="p">),</span><span class="w"> </span><span class="n">osmosis_path</span><span class="p">,</span><span class="w"> </span><span class="n">java_path</span><span class="p">,</span><span class="w"> </span><span class="n">sep</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">';'</span><span class="p">))</span><span class="w">
</span>```

### Subset our OpenStreetMap XML

My normal project workflow is to have ‘input’ and ‘output’ folders located in my working directory. Let’s set up variables pointing to these two folders:

```<span class="n">in_dir</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">file.path</span><span class="p">(</span><span class="n">getwd</span><span class="p">(),</span><span class="w"> </span><span class="s1">'input'</span><span class="p">)</span><span class="w">
</span><span class="n">out_dir</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">file.path</span><span class="p">(</span><span class="n">getwd</span><span class="p">(),</span><span class="w"> </span><span class="s1">'output'</span><span class="p">)</span><span class="w">
</span>```

I have extracted the ‘.osm’ file we downloaded earlier into the ‘input’ folder. Let’s take note of the path to this file.

```<span class="n">osm_file_path</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">file.path</span><span class="p">(</span><span class="n">in_dir</span><span class="p">,</span><span class="w"> </span><span class="s1">'australia-latest.osm'</span><span class="p">)</span><span class="w">
</span>```

Remember how we added the ‘bin’ directory of Osmosis to our PATH variable? If you open up your terminal and type ‘osmosis’, you will see that it does something:

If you don’t see any Osmosis-related info, then you will need to make sure that the ‘bin’ directory of Osmosis is in fact in your PATH variable. Google and Stack Overflow are your friends!

Note: Most of the following Osmosis-related code is based on the vignette contained within the osmar package which will use in this artice soon. I give full credit to those great authors.

We are going to issue a command to the terminal like this one:

```osmosis <span class="nt">--read-xml</span> <span class="nv">file</span><span class="o">=</span>D:<span class="se">\\</span>blog<span class="se">\\</span>input<span class="se">\\</span>australia-latest.osm <span class="nv">enableDateParsing</span><span class="o">=</span>no
<span class="nt">--bounding-box</span> <span class="nv">top</span><span class="o">=</span><span class="nt">-33</span>.8595 <span class="nv">left</span><span class="o">=</span>151.1681 <span class="nv">bottom</span><span class="o">=</span><span class="nt">-33</span>.9039 <span class="nv">right</span><span class="o">=</span>151.2142
<span class="nt">--write-xml</span> <span class="nv">file</span><span class="o">=</span>C:<span class="se">\\</span>temp<span class="se">\\</span>file2cc826c5214a```

This command reads in the ‘.osm’ file we downloaded, subsets it based on some coordinates, and outputs the subset to a temporary location. Once it is output to this temporary location, we will read it into R and we are done with the command line.

We alredy know where our ‘.osm’ file is located, so the input part of the command is sorted. We will now create variables to hold the location of the edges of our bounding box. We came up with these earlier by using the OpenStreetMap website:

```<span class="n">left</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="m">151.1681</span><span class="w">
</span><span class="n">bottom</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="m">-33.9039</span><span class="w">
</span><span class="n">right</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="m">151.2142</span><span class="w">
</span><span class="n">top</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="m">-33.8595</span><span class="w">
</span>```

Next, we create a temporary file to write the subset to. The tempfile() function returns the path to the temporary file we will be creating.

```<span class="n">temp_file</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">tempfile</span><span class="p">()</span><span class="w">
</span>```

Let’s build the command that we will issue to the terminal.

```<span class="n">input_command</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">sprintf</span><span class="p">(</span><span class="s1">'--read-xml file=%s'</span><span class="p">,</span><span class="w"> </span><span class="n">osm_file_path</span><span class="p">)</span><span class="w">
</span><span class="n">args</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">sprintf</span><span class="p">(</span><span class="s1">'--bounding-box top=%s left=%s bottom=%s right=%s'</span><span class="p">,</span><span class="w"> </span><span class="n">top</span><span class="p">,</span><span class="w"> </span><span class="n">left</span><span class="p">,</span><span class="w"> </span><span class="n">bottom</span><span class="p">,</span><span class="w"> </span><span class="n">right</span><span class="p">)</span><span class="w">
</span><span class="n">output_command</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">sprintf</span><span class="p">(</span><span class="s1">'--write-xml file=%s'</span><span class="p">,</span><span class="w"> </span><span class="n">temp_file</span><span class="p">)</span><span class="w">
</span><span class="n">request</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">sprintf</span><span class="p">(</span><span class="s1">'osmosis %s %s %s'</span><span class="p">,</span><span class="w"> </span><span class="n">input_command</span><span class="p">,</span><span class="w"> </span><span class="n">args</span><span class="p">,</span><span class="w"> </span><span class="n">output_command</span><span class="p">)</span><span class="w">
</span>```

Let’s take a look at what we’ve built.

```<span class="n">print</span><span class="p">(</span><span class="n">request</span><span class="p">)</span><span class="w">
</span>```
`## [1] "osmosis --read-xml file=D:/Desktop/blog/walking_the_train_line_part_un/input/australia-latest.osm --bounding-box top=-33.8595 left=151.1681 bottom=-33.9039 right=151.2142 --write-xml file=C:\\Users\\eustin\\AppData\\Local\\Temp\\RtmpqQhzdO\\file34c02503f71"`

Good. Execute!

```<span class="n">system</span><span class="p">(</span><span class="s1">'cmd.exe'</span><span class="p">,</span><span class="w"> </span><span class="n">input</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">request</span><span class="p">)</span><span class="w">
</span>```

The output should look something like this:

Once Osmosis finishes processing the file, we can read in the subset ‘.osm’ file it created. The file is in OSM XML format and we will use a function provided in the osmar package (introduced next) to convert it into an osmar object.

```<span class="n">osm_data</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">readLines</span><span class="p">(</span><span class="n">temp_file</span><span class="p">)</span><span class="w">
</span><span class="n">osm_data</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">as_osmar</span><span class="p">(</span><span class="n">xmlParse</span><span class="p">(</span><span class="n">osm_data</span><span class="p">))</span><span class="w">
</span>```

Let’s do some housekeeping and remove our temp file.

```<span class="n">unlink</span><span class="p">(</span><span class="n">temp_file</span><span class="p">)</span><span class="w">
</span>```

For some reason, I get problems with the timestamp field when using the piping operator, so let’s remove that field.

```<span class="n">osm_data</span><span class="o">\$</span><span class="n">nodes</span><span class="o">\$</span><span class="n">attrs</span><span class="o">\$</span><span class="n">timestamp</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="kc">NULL</span><span class="w">
</span><span class="n">osm_data</span><span class="o">\$</span><span class="n">ways</span><span class="o">\$</span><span class="n">attrs</span><span class="o">\$</span><span class="n">timestamp</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="kc">NULL</span><span class="w">
</span><span class="n">osm_data</span><span class="o">\$</span><span class="n">relations</span><span class="o">\$</span><span class="n">attrs</span><span class="o">\$</span><span class="n">timestamp</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="kc">NULL</span><span class="w">
</span>```

# A little bit about the OpenStreetMap data model

The basic elements that make up the OpenStreetMap data model are nodes, ways and relations. Each one of these can have tags associated with them which provide more information about the elements in question.

Nodes are points on the Earth’s surface defined by their latitudes and longitudes. An example of a node is an intersection. Ways are ordered lists of nodes. An example of a way is a road. Relations describe the relationship between nodes, ways and even other relations. We will not be using them here.

Tags are described in ‘key:value’ format. An example of a tag on a node is ‘name:Australia Post’, where:

key = name

and

value = Australia Post

Enough! Let’s use our data.

# Working with the data – osmar

The osmar package allows us to work with OSM XML within the R-universe. It contains functions to convert our osmar objects into graphs which will become important in the second part of this article.

You can easily plot the map contained within our osmar object.

```<span class="n">plot_ways</span><span class="p">(</span><span class="n">osm_data</span><span class="p">)</span><span class="w">
</span>```

But I’ll be plotting using the pretty_graph() function I wrote…because it’s (slightly) prettier! It also uses ggplot2 which means that the default settings for our visualisations will likely be pretty.

```<span class="n">pretty_graph</span><span class="p">(</span><span class="n">osm_data</span><span class="p">)</span><span class="w">
</span>```

Full code for pretty_graph() is provided at the end of this article.

## Find the start and end point of our paths

Back to my original goal – finding paths between Newtown and the CBD. I will show you two ways to find the starting and ending nodes of the paths we want to find on our map.

### Export data directly from OpenStreetMap

This is probably the simpler of the two ways I will show you:

1. Go to openstreetmap.org
2. Navigate to a part of the map that is small enough to export using the ‘Export’ button.
4. Use ‘Find’ in your text editor to find the node ID you’re after.

Here, I searched for the word ‘station’ and located the node ID for Newtown Station:

### Use osmar’s find() function

In this tricker (and more fun) version, we will use osmar’s find() function, which is defined as follows:

find(osmar_object, elements_to_extract(where_to_look(conditions)))

In (almost) plain English:

1. within our osmar_object,
2. find elements_to_extract that meet some conditions applied to data contained within where_to_look, and
3. return those elements_to_extract that meet those conditions

elements_to_extract are OpenStreetMap elements such as nodes, ways and relations. where_to_look will normally be the tags associated with nodes, ways and relations. The conditions will normally be something like “where the value in the tag is ‘station’”.

Here is a concrete example. To return all nodes that contain the tag ‘railway:station’, we can issue:

```<span class="n">find</span><span class="p">(</span><span class="n">osm_data</span><span class="p">,</span><span class="w"> </span><span class="n">node</span><span class="p">(</span><span class="n">tags</span><span class="p">(</span><span class="n">k</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s1">'railway'</span><span class="w"> </span><span class="o">&</span><span class="w"> </span><span class="n">v</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s1">'station'</span><span class="p">)))</span><span class="w">
</span>```

Applying our description above to this example:

1. within our osmar_object,
2. find nodes where the key is ‘railway’ and the value is ‘station’ within the tags associated with the nodes, and
3. return those nodes that meet the condition in 2, above.

This function returns a vector of node IDs that match the condition we specified:

```<span class="n">station_node_ids</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">find</span><span class="p">(</span><span class="n">osm_data</span><span class="p">,</span><span class="w"> </span><span class="n">node</span><span class="p">(</span><span class="n">tags</span><span class="p">(</span><span class="n">k</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s1">'railway'</span><span class="w"> </span><span class="o">&</span><span class="w"> </span><span class="n">v</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s1">'station'</span><span class="p">)))</span><span class="w">
</span><span class="n">print</span><span class="p">(</span><span class="n">station_node_ids</span><span class="p">)</span><span class="w">
</span>```
```##  [1] 1692720118 1763901257 1763901265 1763901272 1763901273 1853704257
##  [7] 1886265379 4735625743 4735626773 5692088717 5692088718 5692088719
## [13] 5692088720 5692089421 5692089422 5692089423 5692089424 5692089425
## [19] 5692089426 5692089427 5692089432 5692089433```

But we’re not finished yet. We now have a bunch of node IDs for stations in our map. We’re specifically after the node ID of Newtown Station.

### Subsetting osmar objects

We can visually inspect a more manageable subset of our broader osmar object using the subset() function. We’ll use the node IDs we’ve found, above, as an argument to this function.

```<span class="n">station_nodes</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">subset</span><span class="p">(</span><span class="n">osm_data</span><span class="p">,</span><span class="w"> </span><span class="n">station_node_ids</span><span class="p">)</span><span class="w">
</span>```

The tags associated with nodes we’ve extracted are conveniently available in data.frame format:

```<span class="n">station_nodes</span><span class="o">\$</span><span class="n">nodes</span><span class="o">\$</span><span class="n">tags</span><span class="w"> </span><span class="o">%>%</span><span class="w">
</span>```
```##              id         k                                v
## 3111 1692720118      name                    Circular Quay
## 3112 1692720118     layer                                1
## 3113 1692720118   railway                          station
## 3114 1692720118  operator                    Sydney Trains
## 3115 1692720118  wikidata                         Q5121652
## 3116 1692720118 wikipedia en:Circular Quay railway station```

By visually (or programatically) looking for the word ‘Newtown’ in the value column (named ‘v’), we find our target! Node ID ‘4735625743’.

```<span class="n">rows_to_get</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">station_nodes</span><span class="o">\$</span><span class="n">nodes</span><span class="o">\$</span><span class="n">tags</span><span class="o">\$</span><span class="n">v</span><span class="w"> </span><span class="o">%like%</span><span class="w"> </span><span class="s1">'Newtown'</span><span class="w">
</span><span class="n">newtown_station</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">station_nodes</span><span class="o">\$</span><span class="n">nodes</span><span class="o">\$</span><span class="n">tags</span><span class="p">[</span><span class="n">rows_to_get</span><span class="p">,</span><span class="w"> </span><span class="s1">'id'</span><span class="p">]</span><span class="w">
</span><span class="n">print</span><span class="p">(</span><span class="n">newtown_station</span><span class="p">)</span><span class="w">
</span>```
`## [1] 4735625743`

Let’s double check by looking at all rows that match our lovely node ID.

```<span class="n">rows_to_get</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">station_nodes</span><span class="o">\$</span><span class="n">nodes</span><span class="o">\$</span><span class="n">tags</span><span class="o">\$</span><span class="n">id</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">newtown_station</span><span class="w">
</span><span class="n">station_nodes</span><span class="o">\$</span><span class="n">nodes</span><span class="o">\$</span><span class="n">tags</span><span class="p">[</span><span class="n">rows_to_get</span><span class="p">,</span><span class="w"> </span><span class="p">]</span><span class="w">
</span>```
```##               id                k       v
## 14938 4735625743             name Newtown
## 14939 4735625743 public_transport station
## 14940 4735625743          railway station```

Good…good…

### Our terminal node

Let’s just say that I want to lose weight. But I don’t want to lose too much weight. So my terminal node will be Small Bar on Erskine Street (where they do awesome burgers).

Now you give it a go. Try to find that node ID yourself. Assign the node ID of Small Bar to a variable named small_bar.

# Wrapping it up (for now)

## Plotting our start and end nodes on our pretty map

First, we will subset our osmar object using our start node ID (newtown_station) and our end node ID (small_bar).

```<span class="n">newtown_station</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">subset</span><span class="p">(</span><span class="n">osm_data</span><span class="p">,</span><span class="w"> </span><span class="n">node</span><span class="p">(</span><span class="n">newtown_station</span><span class="p">))</span><span class="w">
</span><span class="n">small_bar</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">subset</span><span class="p">(</span><span class="n">osm_data</span><span class="p">,</span><span class="w"> </span><span class="n">node</span><span class="p">(</span><span class="n">small_bar</span><span class="p">))</span><span class="w">
</span>```

We use the ‘start’ and ‘end’ arguments of the pretty_graph function to mark our start and end points on top of our map layer.

```<span class="n">pretty_graph</span><span class="p">(</span><span class="n">osm_data</span><span class="p">,</span><span class="w"> </span><span class="n">start</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">newtown_station</span><span class="p">,</span><span class="w"> </span><span class="n">end</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">small_bar</span><span class="p">)</span><span class="w">
</span>```

Done!

## Next time…

Next time we’ll be doing a little bit of this and completing our quest:

Can you guess what this is showing?

Time to get algorithmic!

Justin

# Code

## Packages used

The usual arsenal + osmar.

```<span class="n">library</span><span class="p">(</span><span class="n">ggplot2</span><span class="p">)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">dplyr</span><span class="p">)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">data.table</span><span class="p">)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">osmar</span><span class="p">)</span><span class="w">
</span>```

## pretty_graph function

I’ve written some comments in the code below explaining some things. In the above article, we’ve used only used the osm_data, start and end arguments. Here is a description of the arguments we haven’t used:

• path_nodes: We will be using the path_nodes argument next time to plot the paths we find from our start node to our end node.
• path_alpha: This simply controls the opacity of the nodes along the path that we will be plotting.
```<span class="n">pretty_graph</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">osm_data</span><span class="p">,</span><span class="w"> </span><span class="n">start</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">NULL</span><span class="p">,</span><span class="w"> </span><span class="n">end</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">NULL</span><span class="p">,</span><span class="w"> </span><span class="n">path_nodes</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">NULL</span><span class="p">,</span><span class="w">
</span><span class="n">path_alpha</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0.5</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="c1">#</span><span class="w">
</span><span class="c1"># input:  osm_data     = an osmar object</span><span class="w">
</span><span class="c1">#         start, end   = subset of an osmar object containing the start node </span><span class="w">
</span><span class="c1">#                        and end node, respectively</span><span class="w">
</span><span class="c1">#         path_nodes   = a character vector of the node IDs we want to plot </span><span class="w">
</span><span class="c1">#                        along our path</span><span class="w">
</span><span class="c1">#         path_alpha   = the opacity of the nodes along the path we are </span><span class="w">
</span><span class="c1">#                        plotting</span><span class="w">
</span><span class="c1">#</span><span class="w">
</span><span class="c1"># output: ggplot2 plot of our map</span><span class="w">
</span><span class="c1">#</span><span class="w">

</span><span class="n">require</span><span class="p">(</span><span class="n">ggplot2</span><span class="p">)</span><span class="w">
</span><span class="n">require</span><span class="p">(</span><span class="n">dplyr</span><span class="p">)</span><span class="w">

</span><span class="c1"># extract all node coordinates from our osmar object</span><span class="w">
</span><span class="n">nodes</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">osm_data</span><span class="o">\$</span><span class="n">nodes</span><span class="o">\$</span><span class="n">attrs</span><span class="w">
</span><span class="n">nodes</span><span class="o">\$</span><span class="n">timestamp</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="kc">NULL</span><span class="w">
</span><span class="n">node_lat</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">nodes</span><span class="o">\$</span><span class="n">lat</span><span class="w">
</span><span class="n">node_lon</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">nodes</span><span class="o">\$</span><span class="n">lon</span><span class="w">
</span><span class="n">node_coords</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">data.frame</span><span class="p">(</span><span class="n">lat</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">node_lat</span><span class="p">,</span><span class="w"> </span><span class="n">lon</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">node_lon</span><span class="p">)</span><span class="w">

</span><span class="c1"># if path_nodes provided, extract the coordinates of the </span><span class="w">
</span><span class="c1"># nodes along the path we have provided</span><span class="w">
</span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="o">!</span><span class="nf">is.null</span><span class="p">(</span><span class="n">path_nodes</span><span class="p">))</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="n">path_nodes</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">find_up</span><span class="p">(</span><span class="n">osm_data</span><span class="p">,</span><span class="w"> </span><span class="n">node</span><span class="p">(</span><span class="n">path_nodes</span><span class="p">))</span><span class="w">
</span><span class="n">path_nodes</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">subset</span><span class="p">(</span><span class="n">osm_data</span><span class="p">,</span><span class="w"> </span><span class="n">ids</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">path_nodes</span><span class="p">)</span><span class="w">
</span><span class="n">path_nodes</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">path_nodes</span><span class="o">\$</span><span class="n">nodes</span><span class="o">\$</span><span class="n">attrs</span><span class="w"> </span><span class="o">%>%</span><span class="w">
</span><span class="n">select</span><span class="p">(</span><span class="n">lat</span><span class="p">,</span><span class="w"> </span><span class="n">lon</span><span class="p">)</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="c1"># we will be plotting all ways and then highlighting a handful of nodes</span><span class="w">
</span><span class="c1"># rather than plotting all ways and nodes on the map, which makes the</span><span class="w">
</span><span class="c1"># map very 'busy'. taking this approach makes the map look cleaner. </span><span class="w">
</span><span class="n">ways</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">osm_data</span><span class="o">\$</span><span class="n">ways</span><span class="o">\$</span><span class="n">refs</span><span class="w">
</span><span class="n">ways</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">left_join</span><span class="p">(</span><span class="n">ways</span><span class="p">,</span><span class="w"> </span><span class="n">nodes</span><span class="p">,</span><span class="w"> </span><span class="n">by</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="s1">'ref'</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'id'</span><span class="p">))</span><span class="w">

</span><span class="c1"># we will be using geom_segment() from ggplot2 so we need to specify the </span><span class="w">
</span><span class="c1"># start and end points of each segment. we take the nodes along the ways</span><span class="w">
</span><span class="c1"># (which are inherently ordered) and connect them up.</span><span class="w">
</span><span class="n">ways</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">ways</span><span class="w"> </span><span class="o">%>%</span><span class="w">
</span><span class="n">select</span><span class="p">(</span><span class="n">ref</span><span class="p">,</span><span class="w"> </span><span class="n">id</span><span class="p">,</span><span class="w"> </span><span class="n">lat</span><span class="p">,</span><span class="w"> </span><span class="n">lon</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w">
</span><span class="n">group_by</span><span class="p">(</span><span class="n">id</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w">
</span><span class="n">mutate</span><span class="p">(</span><span class="n">lon_end</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">lead</span><span class="p">(</span><span class="n">lon</span><span class="p">),</span><span class="w"> </span><span class="n">lat_end</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">lead</span><span class="p">(</span><span class="n">lat</span><span class="p">))</span><span class="w"> </span><span class="o">%>%</span><span class="w">
</span><span class="n">filter</span><span class="p">(</span><span class="o">!</span><span class="nf">is.na</span><span class="p">(</span><span class="n">lon_end</span><span class="p">)</span><span class="w"> </span><span class="o">&</span><span class="w"> </span><span class="o">!</span><span class="nf">is.na</span><span class="p">(</span><span class="n">lat_end</span><span class="p">))</span><span class="w">

</span><span class="c1"># we start our plotting. our first layer is made up of the ways.</span><span class="w">
</span><span class="n">pretty_plot</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">ggplot</span><span class="p">(</span><span class="n">node_coords</span><span class="p">,</span><span class="w"> </span><span class="n">aes</span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">lon</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">lat</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
</span><span class="n">geom_segment</span><span class="p">(</span><span class="n">data</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ways</span><span class="p">,</span><span class="w"> </span><span class="n">aes</span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">lon</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">lat</span><span class="p">,</span><span class="w"> </span><span class="n">xend</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">lon_end</span><span class="p">,</span><span class="w">
</span><span class="n">yend</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">lat_end</span><span class="p">),</span><span class="w"> </span><span class="n">linetype</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'dashed'</span><span class="p">,</span><span class="w">
</span><span class="n">alpha</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">path_alpha</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
</span><span class="n">labs</span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'Longitude'</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'Latitude'</span><span class="p">)</span><span class="w">

</span><span class="c1"># if the start and/or end nodes have been provided, we layer them next.</span><span class="w">
</span><span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="nf">is.null</span><span class="p">(</span><span class="n">start</span><span class="p">))</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="n">start</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">start</span><span class="o">\$</span><span class="n">nodes</span><span class="o">\$</span><span class="n">attrs</span><span class="w"> </span><span class="o">%>%</span><span class="w">
</span><span class="n">select</span><span class="p">(</span><span class="n">lat</span><span class="p">,</span><span class="w"> </span><span class="n">lon</span><span class="p">)</span><span class="w">
</span><span class="n">pretty_plot</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">pretty_plot</span><span class="w"> </span><span class="o">+</span><span class="w">
</span><span class="n">geom_point</span><span class="p">(</span><span class="n">data</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">start</span><span class="p">,</span><span class="w"> </span><span class="n">aes</span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">lon</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">lat</span><span class="p">),</span><span class="w"> </span><span class="n">colour</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'green'</span><span class="p">,</span><span class="w">
</span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">5</span><span class="p">)</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="nf">is.null</span><span class="p">(</span><span class="n">end</span><span class="p">))</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="n">end</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">end</span><span class="o">\$</span><span class="n">nodes</span><span class="o">\$</span><span class="n">attrs</span><span class="w"> </span><span class="o">%>%</span><span class="w">
</span><span class="n">select</span><span class="p">(</span><span class="n">lat</span><span class="p">,</span><span class="w"> </span><span class="n">lon</span><span class="p">)</span><span class="w">
</span><span class="n">pretty_plot</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">pretty_plot</span><span class="w"> </span><span class="o">+</span><span class="w">
</span><span class="n">geom_point</span><span class="p">(</span><span class="n">data</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">end</span><span class="p">,</span><span class="w"> </span><span class="n">aes</span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">lon</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">lat</span><span class="p">),</span><span class="w"> </span><span class="n">colour</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'red'</span><span class="p">,</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">5</span><span class="p">)</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="c1"># ...and finally, the path nodes.</span><span class="w">
</span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="o">!</span><span class="nf">is.null</span><span class="p">(</span><span class="n">path_nodes</span><span class="p">))</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="n">pretty_plot</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">pretty_plot</span><span class="w"> </span><span class="o">+</span><span class="w">
</span><span class="n">geom_point</span><span class="p">(</span><span class="n">data</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">path_nodes</span><span class="p">,</span><span class="w"> </span><span class="n">aes</span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">lon</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">lat</span><span class="p">),</span><span class="w"> </span><span class="n">colour</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'blue'</span><span class="p">,</span><span class="w">
</span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">2</span><span class="p">,</span><span class="w"> </span><span class="n">alpha</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">path_alpha</span><span class="p">)</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="n">print</span><span class="p">(</span><span class="n">pretty_plot</span><span class="p">)</span><span class="w">
</span><span class="p">}</span><span class="w">
</span>```