Skip to content

Commit

Permalink
update documents
Browse files Browse the repository at this point in the history
  • Loading branch information
seifer08ms committed Nov 23, 2016
1 parent 8c43d81 commit 16d5c42
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 74 deletions.
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Imports:
png,
RCurl,
rjson,
XML
XML,
parallel
License: GPL-2
LazyData: true
RoxygenNote: 5.0.1
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by roxygen2 (4.1.1): do not edit by hand
# Generated by roxygen2: do not edit by hand

export(geoconv)
export(getBaiduMap)
Expand Down
65 changes: 40 additions & 25 deletions R/getCoordinate.R
Original file line number Diff line number Diff line change
@@ -1,28 +1,4 @@
#' Get coordiante from address
#' Take in address and return the coordinate
#' @param address address
#' @param city the city of the address, optional
#' @param output should be "json" or "xml", the type of the result
#' @param formatted logical value, return the coordinates or the original results
#' @return A vector contains the corresponding coordiante. If "formatted=TRUE", return the numeric coordinates, otherwise return json or xml type result, depents on the argument "output". If the length of address is larger than 1, the result is a matrix.
#' @export getCoordinate
#' @examples
#'
#' \dontrun{
#' ## json output
#' getCoordinate('beijingdaxue')
#'
#' ## xml output
#' getCoordinate('beijingdaxue', output='xml')
#'
#' ## formatted
#' getCoordinate('beijingdaxue', formatted = T)
#'
#' ## vectorization, return a matrix
#' getCoordinate(c('beijingdaxue', 'qinghuadaxue'), formatted = T)
#' }
#'
getCoordinate = function(address, city=NULL, output='json', formatted = F){
getCoordinate.core = function(address, city=NULL, output='json', formatted = F){
### address
if (grepl(' |#', address)) warning('address should have blank character!')
address = gsub(' |#', '', address)
Expand Down Expand Up @@ -58,4 +34,43 @@ getCoordinate = function(address, city=NULL, output='json', formatted = F){

### final
return(result)
}
#' Get coordiante from address
#' Take in address and return the coordinate
#' @param address address
#' @param city the city of the address, optional
#' @param output should be "json" or "xml", the type of the result
#' @param formatted logical value, return the coordinates or the original results
#' @param limit integer value.If the length of address exceeded limit, function will run in parallel
#' @return A vector contains the corresponding coordiante. If "formatted=TRUE", return the numeric coordinates, otherwise return json or xml type result, depents on the argument "output". If the length of address is larger than 1, the result is a matrix.
#' @export getCoordinate
#' @examples
#' \dontrun{
#' ## json output
#' getCoordinate('beijingdaxue')
#'
#' ## xml output
#' getCoordinate('beijingdaxue', output='xml')
#'
#' ## formatted
#' getCoordinate('beijingdaxue', formatted = T)
#'
#' ## vectorization, return a matrix
#' getCoordinate(c('beijingdaxue', 'qinghuadaxue'), formatted = T)
#' }
getCoordinate=function(address, city=NULL, output='json', formatted = F,limit=600){
if(length(address)<limit){
res<-getCoordinate.core(address, city, output , formatted)
}else if(require(parallel)){
cl <- makeCluster(getOption("cl.cores", detectCores()*0.8))
res<-parLapply(cl,X = address,fun = function(x){
getCoordinate.core(x, city, output , formatted)
})
res<-do.call('rbind',res)
stopCluster(cl)
}else{
warning('can not run in parallel mode without package parallel')
res<-getCoordinate.core(address, city, output , formatted)
}
res
}
73 changes: 46 additions & 27 deletions R/getLocation.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@
map_ak = 'wwZFCIqxIqjRGMVsZ0qgTh7D'

getLocation.core = function(location, output='json', formatted = F, pois=0){
##### URL
if (!class(location) %in% c('matrix', 'data.frame')){
location = matrix(location, ncol=2, byrow=T)
}
lon = location[, 1]
lat = location[, 2]
url_head = paste0("http://api.map.baidu.com/geocoder/v2/?ak=", map_ak, "&location=")
url_tail = paste0("&output=", output, "&", "pois=", pois, collapse='')
url = paste0(url_head, lat, ",", lon, url_tail)

##### result
result = tryCatch(getURL(url),error = function(e) {getURL(url, timeout = 200)})
names(result) = paste0("lon=", lon, ";lat=", lat)

##### if formatted, return a nice result but loss some information
if (formatted){
if (output == 'json'){
result = gsub('.*"formatted_address":"(.*?)".*', '\\1', result)
} else if (output == 'xml') {
result = gsub(".*<formatted_address>(.*?)</formatted_address>.*", '\\1', result)
}
}

#### final
return(result)
}
#' Get location from coordinate
#' Take in coordiantes and return the location
#' @param location longtitude and latitude
#' @param output should be "json" or "xml", the type of the result
#' @param formatted logical. whether to return a nice result
#' @param pois whether to return the PIO around the location
#' @param pois whether to return the POI around the location
#' @param limit integer value.If the number of row exceeded limit, function will run in parallel
#' @return the corresponding locations
#' @export getLocation
#' @importFrom RCurl getURL
Expand Down Expand Up @@ -32,30 +61,20 @@ map_ak = 'wwZFCIqxIqjRGMVsZ0qgTh7D'
#' ## formatted
#' location = getLocation(loc, formatted = T)
#' }
getLocation = function(location, output='json', formatted = F, pois=0){
##### URL
if (!class(location) %in% c('matrix', 'data.frame')){
location = matrix(location, ncol=2, byrow=T)
}
lon = location[, 1]
lat = location[, 2]
url_head = paste0("http://api.map.baidu.com/geocoder/v2/?ak=", map_ak, "&location=")
url_tail = paste0("&output=", output, "&", "pois=", pois, collapse='')
url = paste0(url_head, lat, ",", lon, url_tail)

##### result
result = tryCatch(getURL(url),error = function(e) {getURL(url, timeout = 200)})
names(result) = paste0("lon=", lon, ";lat=", lat)

##### if formatted, return a nice result but loss some information
if (formatted){
if (output == 'json'){
result = gsub('.*"formatted_address":"(.*?)".*', '\\1', result)
} else if (output == 'xml') {
result = gsub(".*<formatted_address>(.*?)</formatted_address>.*", '\\1', result)
#'
getLocation=
function (location, output = "json", formatted = F, pois = 0,limit=600) {
if(nrow(location)<limit){
res<-getLocation.core(location, output, formatted , pois)
}else if(require(parallel)){
cl <- makeCluster(getOption("cl.cores", detectCores()*0.8))
res<-parApply(cl,X = location,MARGIN = 1,FUN = function(x){
getLocation.core(x, output, formatted , pois)
})
stopCluster(cl)
}else{
warning('can not run in parallel mode without package parallel')
res<-getLocation.core(location, output, formatted , pois)
}
}

#### final
return(result)
}
res
}
2 changes: 1 addition & 1 deletion man/geoconv.Rd

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

5 changes: 3 additions & 2 deletions man/getBaiduMap.Rd

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

9 changes: 6 additions & 3 deletions man/getCoordinate.Rd

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

19 changes: 12 additions & 7 deletions man/getLocation.Rd

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

5 changes: 3 additions & 2 deletions man/getPlace.Rd

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

9 changes: 5 additions & 4 deletions man/getRoute.Rd

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

3 changes: 2 additions & 1 deletion man/url_character.Rd

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

0 comments on commit 16d5c42

Please sign in to comment.