diff --git a/.Rbuildignore b/.Rbuildignore new file mode 100644 index 0000000..91114bf --- /dev/null +++ b/.Rbuildignore @@ -0,0 +1,2 @@ +^.*\.Rproj$ +^\.Rproj\.user$ diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..807ea25 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.Rproj.user +.Rhistory +.RData diff --git a/1.html b/1.html new file mode 100644 index 0000000..e51885a --- /dev/null +++ b/1.html @@ -0,0 +1,119 @@ + + + + + + +百度地图API自定义地图 + + + + + + + +
+ + + diff --git a/1.html~ b/1.html~ new file mode 100644 index 0000000..8c44e7a --- /dev/null +++ b/1.html~ @@ -0,0 +1,119 @@ + + + + + + +百度地图API自定义地图 + + + + + + + +
+ + + diff --git a/DESCRIPTION b/DESCRIPTION new file mode 100644 index 0000000..4bb27fd --- /dev/null +++ b/DESCRIPTION @@ -0,0 +1,14 @@ +Package: baidumap +Title: A package for spatial visualization with Baidu +Version: 0.1 +Authors: yalei Du +Description: Just like ggmap, but get map from baidu instead of google or openstreet +Depends: + R (>= 3.1.1) +Imports: + ggmap, + RgoogleMaps, + png, + RCurl +License: GPL-2 +LazyData: true diff --git a/DESCRIPTION~ b/DESCRIPTION~ new file mode 100644 index 0000000..1770b64 --- /dev/null +++ b/DESCRIPTION~ @@ -0,0 +1,11 @@ +Package: baidumap +Title: A package for spatial visualization with Baidu +Version: 0.1 +Authors@R: "yalei Du" +Description: Just like ggmap, but get map from baidu instead of google or openstreet +Depends: R (>= 3.1.1) +Imports: ggmap, RgoogleMaps, png, RCurl, rjson, XML +License: GPL-2 +LazyData: true + + diff --git a/NAMESPACE b/NAMESPACE new file mode 100644 index 0000000..fcf7aca --- /dev/null +++ b/NAMESPACE @@ -0,0 +1,9 @@ +# Generated by roxygen2 (4.0.2): do not edit by hand + +export(getBaiduMap) +export(getCoordinate) +export(getLocation) +importFrom(RCurl,getURL) +importFrom(RgoogleMaps,XY2LatLon) +importFrom(ggmap,ggmap) +importFrom(png,readPNG) diff --git a/R/getBaiduMap.R b/R/getBaiduMap.R new file mode 100644 index 0000000..20b4bed --- /dev/null +++ b/R/getBaiduMap.R @@ -0,0 +1,80 @@ +#' Get map from Baidu API +#' +#' Take in coordiantes and return the location +#' +#' @param lon longtitude of the center of the map +#' @param lat latitude of the center of the map +#' @param width width of the map +#' @param height height of the map +#' @param zoom map zoom, an integer from 3 (continent) to 21 (building), default value 10 (city) +#' @param scale multiplicative factor for the number of pixels returned. possible values are 2 or anything else. +#' @param color "color" or "bw", color or black-and-white +#' @param messaging logical. whether to print the download messages. +#' @return A ggmap object. a map image as a 2d-array of colors as hexadecimal strings representing pixel fill values. +#' @export +#' @importFrom png readPNG +#' @importFrom RgoogleMaps XY2LatLon +#' @importFrom ggmap ggmap +#' @examples +#' +#' \dontrun{ +#' library(ggmap) +#' ## default map: Beijing +#' p <- getBaiduMap() +#' ggmap(p) +#' +#' ## black-and-white +#' p <- getBaiduMap(color='bw') +#' ggmap(p) +#' +#' ## do not print messages +#' p <- getBaiduMap(messaging = F) +#' } +getBaiduMap = function(lon=116.354431, lat=39.942333, width=400, height = 400, zoom=10, scale=2, color = "color", messaging = TRUE){ + ## set url + url_head = "http://api.map.baidu.com/staticimage?" + url = paste0(url_head, "width=", width, "&height=", height, "¢er=", + lon, ",", lat, "&zoom=", zoom) + if (scale == 2) url = paste0(url, "&scale=2") + + ## download image + if (!'baiduMapFileDrawer' %in% list.dirs(full.names= F, recursive=F)) { + dir.create('baiduMapFileDrawer') + } + destfile = paste0(lon, ";", lat, ".png") + download.file(url, destfile = paste0("baiduMapFileDrawer/", destfile), + quiet = !messaging, mode = "wb") + if (messaging) message(paste0("Map from URL : ", url)) + + ## read image and transform to ggmap obejct + map = readPNG(paste0("baiduMapFileDrawer/", destfile)) + # format file + if(color == "color"){ + map <- apply(map, 2, rgb) + } else if(color == "bw"){ + mapd <- dim(map) + map <- gray(.30 * map[,,1] + .59 * map[,,2] + .11 * map[,,3]) + dim(map) <- mapd[1:2] + } + class(map) <- c("ggmap","raster") + + # map spatial info + ll <- XY2LatLon( + list(lat = lat, lon = lon, zoom = zoom), + -width/2 + 0.5, + -height/2 - 0.5 + ) + ur <- XY2LatLon( + list(lat = lat, lon = lon, zoom = zoom), + width/2 + 0.5, + height/2 - 0.5 + ) + attr(map, "bb") <- data.frame( + ll.lat = ll[1], ll.lon = ll[2], + ur.lat = ur[1], ur.lon = ur[2] + ) + + # transpose + out <- t(map) + out +} diff --git a/R/getLocation.R b/R/getLocation.R new file mode 100644 index 0000000..339e22b --- /dev/null +++ b/R/getLocation.R @@ -0,0 +1,103 @@ +map_ak = 'wwZFCIqxIqjRGMVsZ0qgTh7D' +#' Get location from coordinate +#' Take in coordiantes and return the location +#' @param lon longtitude +#' @param lat 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 +#' @return the corresponding locations +#' @export getLocation +#' @importFrom RCurl getURL +#' @examples +#' +#' \dontrun{ +#' ## get one location +#' location_one = getLocation(118.12845, 24.57742) +#' +#' ## vectorization +#' lon = matrix(c(117.93780, 24.55730, 117.93291, 24.57745, 117.23530, 24.64210, 117.05890, 24.74860), byrow=T, ncol=2) +#' ### json +#' location_json = getLocation(lon[, 1], lon[, 2], output='json') +#' ### get district +#' library(rjson) +#' getDistrict = function(x_json){ +#' x_list = fromJSON(x_json) +#' x_list$result$addressComponent$district +#' } +#' location_district = sapply(location_json, getDistrict) +#' +#' ### xml +#' location_xml = getLocation(lon[, 1], lon[, 2], output='xml') +#' +#' ## formatted +#' location = getLocation(lon[, 1], lon[, 2], formatted = T) +#' } +getLocation = function(lon, lat, output='json', formatted = F, pois=0){ + 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 = getURL(url) + 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(".*(.*?).*", '\\1', result) + } + } + 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 +#' @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('北京大学') +#' +#' ## xml output +#' getCoordinate('北京大学', output='xml') +#' +#' ## formatted +#' getCoordinate('北京大学', formatted = T) +#' +#' ## vectorization, return a matrix +#' getCoordinate(c('北京大学', '清华大学'), formatted = T) +#' } +getCoordinate = function(address, city=NULL, output='json', formatted = F){ + url_head = paste0("http://api.map.baidu.com/geocoder/v2/?ak=", map_ak, "&") + url = paste0(url_head, "output=", output, "&address=", address) + if (!is.null(city)) url = paste0(url, "&city=", city) + result = getURL(url) + names(result) = address + + ### transform data from json/xml + trans = function(x, out = output){ + if (out == 'xml') { + lat = gsub('.*?([\\.0-9]*).*', '\\1', x) + long = gsub('.*?([\\.0-9]*).*', '\\1', x) + }else if (out == 'json'){ + lat = gsub('.*?"lat":([\\.0-9]*).*', '\\1', x) + long = gsub('.*?"lng":([\\.0-9]*).*', '\\1', x) + } + long = as.numeric(long); lat = as.numeric(lat) + return(c("longtitude" = long, "latitude" = lat)) + } + if (formatted) { + if (length(result) > 1) { + result = t(sapply(result, trans)) + } else { + result = trans(result) + } + } + return(result) +} \ No newline at end of file diff --git a/baidumap.Rproj b/baidumap.Rproj new file mode 100644 index 0000000..afa6575 --- /dev/null +++ b/baidumap.Rproj @@ -0,0 +1,17 @@ +Version: 1.0 + +RestoreWorkspace: Default +SaveWorkspace: Default +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 4 +Encoding: UTF-8 + +RnwWeave: knitr +LaTeX: XeLaTeX + +BuildType: Package +PackageUseDevtools: Yes +PackageInstallArgs: --no-multiarch --with-keep.source diff --git a/man/getBaiduMap.Rd b/man/getBaiduMap.Rd new file mode 100644 index 0000000..1f50cfc --- /dev/null +++ b/man/getBaiduMap.Rd @@ -0,0 +1,48 @@ +% Generated by roxygen2 (4.0.2): do not edit by hand +\name{getBaiduMap} +\alias{getBaiduMap} +\title{Get map from Baidu API} +\usage{ +getBaiduMap(lon = 116.354431, lat = 39.942333, width = 400, + height = 400, zoom = 10, scale = 2, color = "color", + messaging = TRUE) +} +\arguments{ +\item{lon}{longtitude of the center of the map} + +\item{lat}{latitude of the center of the map} + +\item{width}{width of the map} + +\item{height}{height of the map} + +\item{zoom}{map zoom, an integer from 3 (continent) to 21 (building), default value 10 (city)} + +\item{scale}{multiplicative factor for the number of pixels returned. possible values are 2 or anything else.} + +\item{color}{"color" or "bw", color or black-and-white} + +\item{messaging}{logical. whether to print the download messages.} +} +\value{ +A ggmap object. a map image as a 2d-array of colors as hexadecimal strings representing pixel fill values. +} +\description{ +Take in coordiantes and return the location +} +\examples{ +\dontrun{ +library(ggmap) +## default map: Beijing +p <- getBaiduMap() +ggmap(p) + +## black-and-white +p <- getBaiduMap(color='bw') +ggmap(p) + +## do not print messages +p <- getBaiduMap(messaging = F) +} +} + diff --git a/man/getCoordinate.Rd b/man/getCoordinate.Rd new file mode 100644 index 0000000..6eb11de --- /dev/null +++ b/man/getCoordinate.Rd @@ -0,0 +1,40 @@ +% Generated by roxygen2 (4.0.2): do not edit by hand +\name{getCoordinate} +\alias{getCoordinate} +\title{Get coordiante from address +Take in address and return the coordinate} +\usage{ +getCoordinate(address, city = NULL, output = "json", formatted = F) +} +\arguments{ +\item{address}{address} + +\item{city}{the city of the address, optional} + +\item{output}{should be "json" or "xml", the type of the result} + +\item{formatted}{logical value, return the coordinates or the original results} +} +\value{ +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. +} +\description{ +Get coordiante from address +Take in address and return the coordinate +} +\examples{ +\dontrun{ +## json output +getCoordinate('北京大学') + +## xml output +getCoordinate('北京大学', output='xml') + +## formatted +getCoordinate('北京大学', formatted = T) + +## vectorization, return a matrix +getCoordinate(c('北京大学', '清华大学'), formatted = T) +} +} + diff --git a/man/getLocation.Rd b/man/getLocation.Rd new file mode 100644 index 0000000..1b5fcbd --- /dev/null +++ b/man/getLocation.Rd @@ -0,0 +1,51 @@ +% Generated by roxygen2 (4.0.2): do not edit by hand +\name{getLocation} +\alias{getLocation} +\title{Get location from coordinate +Take in coordiantes and return the location} +\usage{ +getLocation(lon, lat, output = "json", formatted = F, pois = 0) +} +\arguments{ +\item{lon}{longtitude} + +\item{lat}{latitude} + +\item{output}{should be "json" or "xml", the type of the result} + +\item{formatted}{logical. whether to return a nice result} + +\item{pois}{whether to return the PIO around the location} +} +\value{ +the corresponding locations +} +\description{ +Get location from coordinate +Take in coordiantes and return the location +} +\examples{ +\dontrun{ +## get one location +location_one = getLocation(118.12845, 24.57742) + +## vectorization +lon = matrix(c(117.93780, 24.55730, 117.93291, 24.57745, 117.23530, 24.64210, 117.05890, 24.74860), byrow=T, ncol=2) +### json +location_json = getLocation(lon[, 1], lon[, 2], output='json') +### get district +library(rjson) +getDistrict = function(x_json){ + x_list = fromJSON(x_json) + x_list$result$addressComponent$district +} +location_district = sapply(location_json, getDistrict) + +### xml +location_xml = getLocation(lon[, 1], lon[, 2], output='xml') + +## formatted +location = getLocation(lon[, 1], lon[, 2], formatted = T) +} +} + diff --git "a/\346\234\252\345\221\275\345\220\215 txt~" "b/\346\234\252\345\221\275\345\220\215 txt~" new file mode 100644 index 0000000..e69de29