Determining the distance between two ZIP codes (alternatives to mapdist)
Asked Answered
W

3

17

I want to calculate the distance between approx. 100,000 different ZIP codes. I know about the mapdist function in the ggmap package

mapdist works perfectly:

library(ggmap)
mapdist('Washington', 'New York', mode = 'driving')

#         from       to      m      km    miles seconds  minutes    hours
# 1 Washington New York 366284 366.284 227.6089   13997 233.2833 3.888056


mapdist('20001', '10001', mode = 'driving')

#    from    to      m      km    miles seconds minutes    hours
# 1 20001 10001 363119 363.119 225.6421   13713  228.55 3.809167

However, mapdist relies on the Google Geocoding API which is subject to a query limit of 2,500 geolocation requests per day.

Are you aware of any alternative r code to calculate the distance between two points using another service which has a higher request limit (such as Nokia Maps or Bing)?

Whomever answered 28/6, 2013 at 9:52 Comment(2)
As an alternative you can use gdist from Imap package that calculates Great-circle distance but I think you need to get lat/long coordinates...Calchas
@Calchas Good point. There's also a Haversine formula in the rdist package I believe. The advantage of doing it this way is you only need one geocoding lookup per point, instead of one geocoding lookup per pair of points. That could not matter (if you only want observations from distinct pairs of points) or could matter a lot (if all of your routes start from the same point)Marj
M
14

taRifx.geo::georoute (only available here until I push out another update, at which point it will be available via install.packages) can use Bing Maps (which supports I believe 25k per day) and can return a distance.

georoute( c("3817 Spruce St, Philadelphia, PA 19104", 
            "9000 Rockville Pike, Bethesda, Maryland 20892"), 
             verbose=TRUE, returntype="time", 
             service="bing" )

You'll have to get a Bing Maps API key and set it in your R global options (ideal placement is in .Rprofile), but the key is free:

options(BingMapsKey="whateverBingGivesYouForYourKey")
Marj answered 28/6, 2013 at 11:42 Comment(4)
+1! good stuff. and 25k a day is really a good point for Bing.Calchas
You can use taRifx.geo::geocode to use Bing for geocoding also.Marj
Geocoding is it the process of finding (lat/lon)? Sorry I don't know geos terms...Calchas
Yup. Turns addresses into lat/lon pairs. ggmaps has a function that's more complete in some ways but Google Maps-only.Marj
M
1

This might be trivial, but one completely free option is to use Census ZCTA geography data to get co-ordinates for each zip code, and then calculate Haversine distances (or some similar distance metric) between coordinates.

Mckenna answered 21/4, 2018 at 3:41 Comment(0)
T
0

If you start a new R session and run library(ggmap) in the new session, you can make another 2500 queries.

Function distQueryCheck() shows how many queries are remaining.

Thai answered 25/6, 2014 at 18:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.