Tagged with gps


I just finished my submission for Look What I Made Weekend 9. Look What I Made Weekend (LWIMW) is a chance for people to create something over the course of 48 hours. The concept is based on Ludum Dare and other game jams, but for LWIMW you don't have to make a game. Instead, you are free to pursue any creative endeavor and show off your results at the end.

NB: The content below is mostly a reprint of my submission at LWIMW.

This is my first app using Swift and also my first app using MapKit.

It's a simple app that takes an average of several GPS coordinates so that you can get a more accurate estimate of the coordinates of a point.

Main VC

Before the weekend, all I had done was a quick sketch of what the app might look like.

What I got done this weekend:

  • GPS tracking (auto mode)
  • Averaging
  • Displaying tracking and averaging on map
  • App icon
  • Sharing

Averaged Coordinates

Saved Coordinates

What still needs to be done:

  • Persistent storing of data
  • Manual mode
  • Multiple coordinate formats
  • Some testing and miscellaneous improvements

You can view the code or download the app on Github.

Tagged , , , , ,

Shapefiles in R

Let's learn how to use Shapefiles in R. This will allow us to map data for complicated areas or jurisdictions like zipcodes or school districts. For the United States, many shapefiles are available from the [Census Bureau](http://www.census.gov/geo/www/tiger/tgrshp2010/tgrshp2010.html. Our example will map U.S. national parks.

First, download the U.S. Parks and Protected Lands shape files from Natural Earth. We'll be using the ne_10m_parks_and_protected_lands_area.shp file.

Next, start working in R. First, we'll load the shapefile and maptools:

# load up area shape file:
area <- readShapePoly("ne_10m_parks_and_protected_lands_area.shp")

# # or file.choose:
# area <- readShapePoly(file.choose())

Next we can set the colors we want to use. And then we can set up our basemap.

colors <- brewer.pal(9, "BuGn")

mapImage <- get_map(location = c(lon = -118, lat = 37.5),
    color = "color",
    source = "osm",
    # maptype = "terrain",
    zoom = 6)

Next, we can use the fortify function from the ggplot2 package. This converts the crazy shape file with all its nested attributes into a data frame that ggmap will know what to do with.

area.points <- fortify(area)

Finally, we can map our shape files!

ggmap(mapImage) +
    geom_polygon(aes(x = long,
            y = lat,
            group = group),
        data = area.points,
        color = colors[9],
        fill = colors[6],
        alpha = 0.5) +
labs(x = "Longitude",
    y = "Latitude")

National Parks and Protected Lands in California and Nevada

Same figure, with a Stamen terrain basemap with ColorBrewer palette "RdPu"

Citations and Further Reading

Tagged , , , , , , , , ,

Elevation Profiles in R

First, let's load up our data. The data are available in a gist. You can convert your own GPS data to .csv by following the instructions here, using gpsbabel.

gps <- read.csv("callan.csv",
    header = TRUE)

Next, we can use the function SMA from the package TTR to calculate a moving average of the altitude or elevation data, if we want to smooth out the curve. We can define a constant for the number of data points we want to average to create each moving average value.

If you don't want to convert meters to feet, a metric version of the code is available in the gist (callanMetric.R).

movingN <- 5 # define the n for the moving average calculations
gps$Altitude <- gps$Altitude * 3.281 # convert m to ft
gps$SMA <- SMA(gps$Altitude,
    n = movingN)
gps <- gps[movingN:length(gps$SMA), ] # remove first n-1 points

Next, we want to calculate the distance of each point. You can skip this step if your dataset already includes distances.

Dist <- 0
for(i in 2:length(gps$Longitude)) {
    Dist[i] = spDistsN1(as.matrix(gps[i,c("Longitude", "Latitude")]),
    c(gps$Longitude[i-1], gps$Latitude[i-1]),
    longlat = TRUE) / 1.609 # longlat so distances will be in km, then divide to convert to miles
gps$Dist <- Dist

DistTotal <- 0
for(i in 2:length(gps$Longitude)) {
    DistTotal[i] = Dist[i] + DistTotal[i-1]
gps$DistTotal <- DistTotal

And finally, we can plot our elevation data using geom_ribbons and ggplot:

ggplot(gps, aes(x = DistTotal)) +
geom_ribbon(aes(ymin = 600, # change this to match your min below
        ymax = SMA),
    fill = "#1B9E77") + # put your altitude variable here if not using moving averages
labs(x = "Miles",
    y = "Elevation") +
scale_y_continuous(limits = c(600,1200)) # change this to limits appropriate for your region

Elevation profile in ggplot2

Code and data available in a gist.

Tagged , , , , , , , ,

GPS Basemaps in R Using get_map

There are many different maps you can use for a background map for your gps or other latitude/longitude data (i.e. any time you're using geom_path, geom_segment, or geom_point.)


Helpfully, there's just one function that will allow you to query Google Maps, OpenStreetMap, Stamen maps, or CloudMade maps: get_map in the ggmap package. You could also use either get_googlemap, get_openstreetmap, get_stamenmap, or get_cloudmademap, but instead you can just use get_map for the same functionality as all of those combined. This makes it easy to try out different basemaps for your data.

You need to supply get_map with your location data and the color, source, maptype, and zoom of the base map.

Let's go ahead and map the trails in Elwyn John Wildlife Sanctuary here in Atlanta. The csv data and R file are available in a gist.

gps <- read.csv("elwyn.csv",
    header = TRUE)

mapImageData <- get_map(location = c(lon = mean(gps$Longitude),
    lat = 33.824),
    color = "color", # or bw
    source = "google",
    maptype = "satellite",
    # api_key = "your_api_key", # only needed for source = "cloudmade"
    zoom = 17)

pathcolor <- "#F8971F"

    extent = "device", # "panel" keeps in axes, etc.
    ylab = "Latitude",
    xlab = "Longitude",
    legend = "right") +
    geom_path(aes(x = Longitude, # path outline
    y = Latitude),
    data = gps,
    colour = "black",
    size = 2) +
    geom_path(aes(x = Longitude, # path
    y = Latitude),
    colour = pathcolor,
    data = gps,
    size = 1.4) # +
# labs(x = "Longitude",
#   y = "Latitude") # if you do extent = "panel"

We'll be changing the four lines marked above in orange to change what basemap is used.

source = "google"

get_map option source = "google" (or using get_googlemap) downloads a map from the Google Maps API. The basemaps are © Google. Google Maps have four different maptype options: terrain, satellite, roadmap, and hybrid.

source = "google", maptype = "terrain"

source = "google", maptype = "terrain", zoom = 14

Max zoom: 14

source = "google", maptype = "satellite"

source = "google", maptype = "satellite", zoom = 17

Max zoom: 20

source = "google", maptype = "roadmap"

source = "google", maptype = "roadmap", zoom = 17

source = "google", maptype = "hybrid"

Hybrid combines roadmap and satellite. source = "google", maptype = "hybrid", zoom = 17

Max zoom: 14

source = "osm"

get_map option source = "osm" (or using get_openstreetmap) downloads a map from OpenStreetMap. These maps are Creative Commons licensed, specifically Attribution-ShareAlike 2.0 (CC-BY-SA). This means you are free to use the maps for commercial purposes, as long as you release your final product under the same Creative Commons license. OpenStreetMap has no maptype options.

source = "osm" (no maptype needed)

source = "osm", zoom = 17

Max zoom: 20

source = "stamen"

get_map option source = "stamen" (or using get_stamenmap) downloads a map from Stamen Maps. The map tiles are by Stamen Design, licensed under CC BY 3.0. The data for Stamen Maps is by OpenStreetMap, licensed under CC BY SA. Stamen has three different maptype options: terrain, watercolor, and toner.

source = "stamen", maptype = "terrain"

source = "stamen", maptype = "terrain", zoom = 17

Max zoom: 18

source = "stamen", maptype = "watercolor"

source = "stamen", maptype = "watercolor", zoom = 17

Max zoom: 18

source = "stamen", maptype = "toner"

source = "stamen", maptype = "toner", zoom = 17

Max zoom: 18

source = "cloudmade"

N.B. As of March 2014, CloudMade no longer provides this API service.

CloudMade styles build on top of OpenStreetMap data. Thousands of CloudMade styles are available. You can browse them on the CloudMade site. You can also make your own styles.

To use CloudMade map styles in R, you will first need to get an API key to insert into your R code so it can access the maps. You can get an API key from the CloudMade site.

Here are just a couple examples of CloudMade basemaps:

source = "cloudmade", maptype = "1", api_key="your_api_key_here, zoom = 17

source = "cloudmade", maptype = "67367", api_key="your_api_key_here, zoom = 17

Max zoom: 18

The code and data are available in a gist.

Tagged , , , , , , , , , , , , ,