Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Kainu committed Jun 21, 2018
0 parents commit 8427541
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
^.*\.Rproj$
^\.Rproj\.user$
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.Rproj.user
.Rhistory
.RData
.Ruserdata
digitransit.Rproj
13 changes: 13 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Package: digitransit
Type: Package
Title: A client for digitransit API services
Version: 0.1.0
Author: Markus Kainu
Maintainer: Markus Kainu <[email protected]>
Description: Geocode and reverse geocode addresses and poinst in Finland
License: MIT
Depends: httr,
jsonlite
Encoding: UTF-8
LazyData: true
RoxygenNote: 6.0.1
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exportPattern("^[[:alpha:]]+")
20 changes: 20 additions & 0 deletions R/geocode.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#' Geocode a place name or an address
#'
#' @param string plane name or an address
#' @param hits Number of hits returned
#' @return Data frame with latitude and longitude of the place
#' @examples
#' geocode(string = "Eduskuntatalo", hits = 1)


geocode <- function(string = "helsinki", hits = 1) {
string <- gsub(" ", "%20", string)
resp <- httr::GET(paste0("https://api.digitransit.fi/geocoding/v1/search?text=",string,"&size=",hits))
# print("Hello, world!")
tmp <- jsonlite::fromJSON(content(resp, "text"), simplifyVector = FALSE)
dat <- data.frame(string = string,
lon = tmp[[3]][[1]]$geometry$coordinates[[1]],
lat = tmp[[3]][[1]]$geometry$coordinates[[2]],
label = tmp[[3]][[1]]$properties$label)
return(dat)
}
23 changes: 23 additions & 0 deletions R/reverse_geocode.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#' Reverse geocode a geographical location
#'
#' @param lon longitute
#' @param lat A latitude
#' @param hits Number of hits returned
#' @return Data frame with information about the point
#' @examples
#' reverse_geocode(lon = "24.95126", lat = "60.15948", hits = 1)

reverse_geocode <- function(lon = "24.95126", lat = "60.15948", hits = 1) {
resp <- httr::GET(paste0("https://api.digitransit.fi/geocoding/v1/reverse?point.lat=",lat,"&point.lon=",lon,"&size=",hits))
# print("Hello, world!")
tmp <- jsonlite::fromJSON(content(resp, "text"), simplifyVector = FALSE)
dat <- data.frame(lon = lon,
lat = lat,
country = tmp[[3]][[1]]$properties$country,
country_a = tmp[[3]][[1]]$properties$country_a,
region = tmp[[3]][[1]]$properties$region,
localadmin = tmp[[3]][[1]]$properties$localadmin,
locality = tmp[[3]][[1]]$properties$locality,
label = tmp[[3]][[1]]$properties$label)
return(dat)
}
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
R client for digitransit.fi API
==========================================

API documentation: https://digitransit.fi/en/developers/apis/2-geocoding-api/


Install

```r
devtools::install_git("https://gitlab.com/muuankarski/digitransit")
```

Use

```r
digitransit::geocode("Eduskuntatalo")
# string lon lat label
# 1 Eduskuntatalo 24.93315 60.1725 Eduskuntatalo, Mannerheimintie 30, Helsinki
```


```r
digitransit::reverse_geocode(lon = 24.93315, lat = 60.1725)
# lon lat country country_a region localadmin locality label
# 1 24.93315 60.1725 Suomi FIN Uusimaa Helsinki Helsinki Mannerheimintie 30, Helsinki

```

- (C) Markus Kainu 2018
- License: MIT

22 changes: 22 additions & 0 deletions man/geocode.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions man/hello.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
\name{hello}
\alias{hello}
\title{Hello, World!}
\usage{
hello()
}
\description{
Prints 'Hello, world!'.
}
\examples{
hello()
}
24 changes: 24 additions & 0 deletions man/reverse_geocode.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8427541

Please sign in to comment.