In the last few installments of this blog, I have shown how you can use WPS and the Bridge to R to calculate drive distances, geocode records and pull down a map from Google maps. I want to use this post to pull all this together and show how you can geocode your addresses and plot them on a street level map.
First some background you need to know about using Google for geocoding and mapping. There are limits to what Google will allow you to do with their services before they want you to start paying. You can geocode 2,500 records a day for free. You can pull down 25,000 maps a day for free. Once you start moving past these limits, there are fees involved.
One thing that you should probably start to consider is caching records locally that have been geocoded so that you don’t have to go back to the Google geocoder every time you want to plot some points on a map. I could easily run through 2,500 addresses in a day. The limitations on the number of maps is just not an issue for me. I think 25,000 maps a day is a very liberal offering for the kind of work that I would want to use the service for.
In the sample code below, I split the mapping process into two components for ease in understanding the entire process. I first geocode the file to get the latitude and longitude for each record. The second part of the process is creating a map and using the lat’s and long’s to plot points on the map. We could have put this into a single step but it wouldn’t be as clear or as flexible.
Without further ado, here’s the code using the Bridge to R and WPS.
data gasstations; input company $1-29 address $30-52 city $53-64 state $66-67; addr2geocode=trim(address)||', '||trim(city)||', '||trim(state); cards; Citgo Gas Station 5189 28th St Se Grand Rapids MI 28th Street BP 5155 28th St Se Grand Rapids MI Twenty-Eighth Street C Store 5556 28th St Se Grand Rapids MI Speedway 4045 28th St Se Grand Rapids MI Speedway 2305 E Paris Ave Se Grand Rapids MI Superamerica 2305 E Paris Ave Se Grand Rapids MI Shell Food Mart 3960 28th St Se Grand Rapids MI Admiral Petroleum 3927 28th St Se Grand Rapids MI Cascade C Store 4591 Cascade Rd Se Grand Rapids MI Friendly Food Shops 6799 Cascade Rd Se Grand Rapids MI Family Fare Quick Stop 6799 Cascade Rd Se Grand Rapids MI Cascade Citgo 6820 Cascade Rd Se Grand Rapids MI Dutton Fuel Mart LLC 2560 E Beltline Ave Se Grand Rapids MI Centerpointe Marathon 2560 E Beltline Ave Se Grand Rapids MI Shell Food Mart 2600 E Beltline Ave Se Grand Rapids MI Speedway 4018 Cascade Rd Se Grand Rapids MI Grand Rapids Gas Incorporated3214 28th St Se Grand Rapids MI Cascade Shell 4033 Cascade Rd Se Grand Rapids MI Speedway 4665 44th St Se Kentwood MI Super Petroleum Incorporated 2411 28th St Se Grand Rapids MI ;;;; run; *--> Geocode the addresses using the Google Geocoder. Keep the geocoded records in the output dataset names locs for further processing.; %rstart(dataformat=csv,data=gasstations,rGraphicsFile=); datalines4; ## options(repos=structure(c(CRAN="http://cran.case.edu/"))) ## install.packages("ggmap", dependencies = FALSE) attach(gasstations) library(ggmap) gaddress <- as.character(gasstations$addr2geocode) locs <- geocode(gaddress,output="more") ;;;; %rstop(import=locs); *--> Pull a Google map that is centered on a particular address and plot the locations on the map. Use the data set that was created (locs) above that contains the lat and longs to plot the points.; Title 'Gas Stations on or Near 28th Street'; Title2 'Grand Rapids, Michigan'; %rstart(dataformat=csv,data=locs,rGraphicsFile=); datalines4; attach(locs) addr <- locs; library(ggmap) map.center <- geocode('3960 28th St Se, Grand Rapids, MI'); grmap <- qmap(c(lon=map.center$lon, lat=map.center$lat), zoom = 13,color = 'color', legend = 'topleft') grmap +geom_point(aes(x = lon, y = lat, size=3.0), data = addr) ;;;; %rstop(import=);
The map that is created looks like this:
I cropped this down a bit and got rid of the borders so that it would be easier to view on this blog. Note the black points on the map that indicate the locations of the gas stations. We could continue this exercise by plotting a label to the points with the names of the service stations but that would be a good exercise for the reader who wants to learn more about using ggmap and street level mapping.
If you want to learn more about ggmap and street level mapping, I encourage you to take a look at the following document, “ggmap: Spatial Visualization with ggplot2 – the R Journal” and can be viewed in PDF format here. What I have presented is really a quick and dirty set of examples that just begin to scratch the surface of what ggmap can do for you.
About the author: Phil Rack is President of MineQuest Business Analytics, LLC located in Grand Rapids, Michigan. Phil has been a SAS language developer for more than 25 years. MineQuest provides WPS and SAS consulting and contract programming services and is a authorized reseller of WPS in North America.