Skip to content

Commit 2a2f018

Browse files
committed
initial commit
0 parents  commit 2a2f018

25 files changed

+757
-0
lines changed

.Rbuildignore

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
^.*\.Rproj$
2+
^\.Rproj\.user$
3+
^\.travis\.yml$
4+
^README\.*Rmd$
5+
^README\.*html$
6+
^NOTES\.*Rmd$
7+
^NOTES\.*html$
8+
^\.codecov\.yml$
9+
^README_files$
10+
^doc$

.codecov.yml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
comment: false

.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.DS_Store
2+
.Rproj.user
3+
.Rhistory
4+
.RData
5+
.Rproj
6+
src/*.o
7+
src/*.so
8+
src/*.dll

.travis.yml

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
language: r
2+
3+
warnings_are_errors: true
4+
5+
sudo: required
6+
7+
cache: packages
8+
9+
r:
10+
- oldrel
11+
- release
12+
- devel
13+
14+
apt_packages:
15+
- libv8-dev
16+
- xclip
17+
18+
env:
19+
global:
20+
- CRAN: http://cran.rstudio.com
21+
22+
after_success:
23+
- Rscript -e 'covr::codecov()'
24+
25+
notifications:
26+
email:
27+
28+
irc:
29+
channels:
30+
- "104.236.112.222#builds"
31+
nick: travisci

DESCRIPTION

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Package: rgeocodio
2+
Type: Package
3+
Title: Tools to Work with the <geocod.io> 'API'
4+
Version: 0.1.0
5+
Date: 2017-03-05
6+
Author: Bob Rudis ([email protected])
7+
Maintainer: Bob Rudis <[email protected]>
8+
Description: A set of tools to Work with the <geocod.io> 'API'.
9+
URL: https://github.com/hrbrmstr/rgeocodio
10+
BugReports: https://github.com/hrbrmstr/rgeocodio/issues
11+
License: AGPL
12+
Suggests:
13+
testthat,
14+
covr
15+
Depends:
16+
R (>= 3.2.0)
17+
Imports:
18+
purrr,
19+
httr,
20+
jsonlite
21+
RoxygenNote: 6.0.1

NAMESPACE

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Generated by roxygen2: do not edit by hand
2+
3+
export(gio_auth)
4+
export(gio_batch_geocode)
5+
export(gio_batch_reverse)
6+
export(gio_geocode)
7+
export(gio_geocode_components)
8+
export(gio_reverse)
9+
import(httr)
10+
import(purrr)
11+
importFrom(jsonlite,fromJSON)

NEWS.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
0.1.0
2+
* Initial release

R/geocod-auth.R

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#' Get or set GEOCODIO_API_KEY value
2+
#'
3+
#' The API wrapper functions in this package all rely on a geocod.io API
4+
#' key residing in the environment variables `GEOCODIO_API_KEY`. The easiest way to
5+
#' accomplish this is to set it in the `.Renviron` file in your home directory.
6+
#'
7+
#' @md
8+
#' @param force force setting a new geocod.io API key for the
9+
#' current environment?
10+
#' @return `list` containing the combined geocod.io API key
11+
#' @export
12+
gio_auth <- function(force=FALSE) {
13+
14+
key <- Sys.getenv('GEOCODIO_API_KEY')
15+
16+
if ((!identical(key, "")) && !force) {
17+
return(key)
18+
}
19+
20+
if (!interactive()) {
21+
stop(paste0("Please set env var GEOCODIO_API_KEY",
22+
"to your geocod.io API key", sep="", collapse=""),
23+
call. = FALSE)
24+
}
25+
26+
if (identical(key, "")) {
27+
28+
message("Couldn't find env var GEOCODIO_API_KEY See ?gio_auth for more details.")
29+
message("Please enter your API key and press enter:")
30+
pat <- readline(": ")
31+
32+
if (identical(pat, "")) {
33+
stop("geocod.io API key entry failed", call. = FALSE)
34+
}
35+
36+
message("Updating GEOCODIO_API_KEY env var to PAT")
37+
Sys.setenv(GEOCODIO_API_KEY = pat)
38+
39+
return(pat)
40+
41+
}
42+
43+
}

R/geocod-batch.R

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#' Geocode up to 10,000 addresses
2+
#'
3+
#' @md
4+
#' @param addresses character vector of addresses (10K max)
5+
#' @param api_key `geocod.io` API key
6+
#' @export
7+
#' @examples
8+
#' gio_batch_geocode(c(
9+
#' "1109 N Highland St, Arlington VA",
10+
#' "525 University Ave, Toronto, ON, Canada",
11+
#' "4410 S Highway 17 92, Casselberry FL",
12+
#' "15000 NE 24th Street, Redmond WA",
13+
#' "17015 Walnut Grove Drive, Morgan Hill CA"
14+
#' ))
15+
gio_batch_geocode <- function(addresses, api_key=gio_auth()) {
16+
17+
if (length(addresses) > 10000) {
18+
message("Too many addresses. Submitting first 10,000")
19+
addresses <- addresses[1:10000]
20+
}
21+
22+
res <- httr::POST("https://api.geocod.io/v1/geocode",
23+
query=list(api_key=api_key),
24+
body=as.list(addresses), encode="json")
25+
26+
httr::stop_for_status(res)
27+
28+
jsonlite::fromJSON(httr::content(res, as="text", encoding="UTF-8"))
29+
30+
}

R/geocod-one.R

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#' Geocode a single address
2+
#'
3+
#' @md
4+
#' @param address address to geocode
5+
#' @param api_key `geocod.io` API key
6+
#' @export
7+
#' @examples
8+
#' gio_geocode("1109 N Highland St, Arlington, VA")
9+
gio_geocode <- function(address, api_key=gio_auth()) {
10+
11+
res <- httr::GET("https://api.geocod.io/v1/geocode",
12+
query=list(q=address, api_key=api_key))
13+
14+
httr::stop_for_status(res)
15+
16+
jsonlite::fromJSON(httr::content(res, as="text", encoding="UTF-8"))
17+
18+
}
19+
20+
#' Geocode a single address
21+
#'
22+
#' @md
23+
#' @param street,city,state,postal_code,country address components
24+
#' @param api_key `geocod.io` API key
25+
#' @export
26+
#' @examples
27+
#' gio_geocode_components("1109 N Highland St", "Arlington", "VA")
28+
gio_geocode_components <- function(street, city, state, postal_code, country,
29+
api_key=gio_auth()) {
30+
31+
params <- list(api_key=api_key)
32+
33+
if (!missing(street)) params$street <- street
34+
if (!missing(city)) params$city <- city
35+
if (!missing(state)) params$state <- state
36+
if (!missing(postal_code)) params$postal_code <- postal_code
37+
if (!missing(country)) params$country <- country
38+
39+
res <- httr::GET("https://api.geocod.io/v1/geocode", query=params)
40+
41+
httr::stop_for_status(res)
42+
43+
jsonlite::fromJSON(httr::content(res, as="text", encoding="UTF-8"))
44+
45+
}

R/geocod-reverse-batch.R

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#' Reverse geocode up to 10,000 coordinates
2+
#'
3+
#' @md
4+
#' @param coordinates data frame of coordinates with `lat` and `lon` columns
5+
#' @param api_key `geocod.io` API key
6+
#' @export
7+
#' @examples
8+
#' data.frame(
9+
#' lat = c(35.9746000, 32.8793700, 33.8337100, 35.4171240),
10+
#' lon = c(-77.9658000, -96.6303900, -117.8362320, -80.6784760)
11+
#' ) -> to_code
12+
#' gio_batch_reverse(to_code)
13+
gio_batch_reverse <- function(coordinates, api_key=gio_auth()) {
14+
15+
if (nrow(coordinates) > 10000) {
16+
message("Too many addresses. Submitting first 10,000")
17+
coordinates <- coordinates[1:10000,]
18+
}
19+
20+
if (!("lat" %in% colnames(coordinates))) {
21+
stop("coordinates data frame must have a 'lat' column")
22+
}
23+
24+
if (!("lon" %in% colnames(coordinates))) {
25+
stop("coordinates data frame must have a 'lon' column")
26+
}
27+
28+
pairs <- sprintf("%s,%s", coordinates$lat, coordinates$lon)
29+
30+
res <- httr::POST("https://api.geocod.io/v1/reverse",
31+
query=list(api_key=api_key),
32+
body=as.list(pairs), encode="json")
33+
34+
httr::stop_for_status(res)
35+
36+
jsonlite::fromJSON(httr::content(res, as="text", encoding="UTF-8"))
37+
38+
}
39+

R/geocod-reverse.R

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#' Reverse geocode a single lat/lon pair
2+
#'
3+
#' @md
4+
#' @param lat,lon lat/lon pair to reverse geocode
5+
#' @param api_key `geocod.io` API key
6+
#' @export
7+
#' @examples
8+
#' gio_reverse(38.9002898, -76.9990361)
9+
gio_reverse <- function(lat, lon, api_key=gio_auth()) {
10+
11+
res <- httr::GET("https://api.geocod.io/v1/reverse",
12+
query=list(q=sprintf("%s,%s", lat, lon), api_key=api_key))
13+
14+
httr::stop_for_status(res)
15+
16+
jsonlite::fromJSON(httr::content(res, as="text", encoding="UTF-8"))
17+
18+
}

R/rgeocodio-package.R

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#' Tools to Work with the <geocod.io> 'API'
2+
#'
3+
#' NOTE: You need an [API key](https://dash.geocod.io/) to use this package.
4+
#'
5+
#' @md
6+
#' @name rgeocodio
7+
#' @docType package
8+
#' @author Bob Rudis (bob@@rud.is)
9+
#' @import purrr httr
10+
#' @importFrom jsonlite fromJSON
11+
NULL

README.Rmd

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
---
2+
output: rmarkdown::github_document
3+
---
4+
5+
`rgeocodio` : Tools to Work with the <https://geocod.io/> 'API'
6+
7+
NOTE: You need an [API key](https://dash.geocod.io/) to use this package.
8+
9+
The following functions are implemented:
10+
11+
- `gio_auth`: Get or set GEOCODIO_API_KEY value
12+
- `gio_batch_geocode`: Geocode up to 10,000 addresses
13+
- `gio_batch_reverse`: Reverse geocode up to 10,000 coordinates
14+
- `gio_geocode`: Geocode a single address
15+
- `gio_geocode_components`: Geocode a single address
16+
- `gio_reverse`: Reverse geocode a single lat/lon pair
17+
18+
### TODO
19+
20+
- enable selection of additional fields
21+
- better return types
22+
- more error checking
23+
- tests
24+
25+
### Installation
26+
27+
```{r eval=FALSE}
28+
devtools::install_github("hrbrmstr/rgeocodio")
29+
```
30+
31+
```{r message=FALSE, warning=FALSE, error=FALSE, include=FALSE}
32+
options(width=120)
33+
```
34+
35+
### Usage
36+
37+
```{r message=FALSE, warning=FALSE, error=FALSE}
38+
library(rgeocodio)
39+
40+
# current verison
41+
packageVersion("rgeocodio")
42+
43+
gio_geocode("1109 N Highland St, Arlington, VA")
44+
45+
gio_geocode_components("1109 N Highland St", "Arlington", "VA")
46+
47+
gio_reverse(38.9002898, -76.9990361)
48+
49+
gio_batch_geocode(c(
50+
"1109 N Highland St, Arlington VA",
51+
"525 University Ave, Toronto, ON, Canada",
52+
"4410 S Highway 17 92, Casselberry FL",
53+
"15000 NE 24th Street, Redmond WA",
54+
"17015 Walnut Grove Drive, Morgan Hill CA"
55+
))
56+
57+
data.frame(
58+
lat = c(35.9746000, 32.8793700, 33.8337100, 35.4171240),
59+
lon = c(-77.9658000, -96.6303900, -117.8362320, -80.6784760)
60+
) -> to_code
61+
62+
gio_batch_reverse(to_code)
63+
```
64+
65+
### Test Results
66+
67+
```{r message=FALSE, warning=FALSE, error=FALSE}
68+
library(rgeocodio)
69+
library(testthat)
70+
71+
date()
72+
73+
test_dir("tests/")
74+
```
75+

0 commit comments

Comments
 (0)