knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%",
  tidy = TRUE
)

amapGeocode

Total downloads badge CRAN status Lifecycle: maturing License: MIT DOI Codecov test coverage R-CMD-check

中文版介绍: 博客 or 知乎

Introduction

Geocoding and Reverse Geocoding Services are widely used to provide data about coordinate and location information, including longitude, latitude, formatted location name, administrative region with different levels. There are some packages can provide geocode service such as tidygeocoder, baidumap and baidugeo. However, some of them do not always provide precise information in China, and some of them are unavailable with the upgrade backend API.

amapGeocode is built to provide high precise geocoding and reverse geocoding service, and it provides an interface for the AutoNavi(高德) Maps API geocoding services. API docs can be found here and here. Here are two main functions to use, one is getCoord() which needs a character location name as an input, while the other one is getLocation() which needs two numeric longitude and latitude values as inputs.

The getCoord() function extracts coordinate information from input character location name and outputs the results as data.table, XML or JSON (as list). And the getLocation() function extracts location information from input numeric longitude and latitude values and outputs the results as data.table, XML or JSON (as list). With the data.table format as output, it's highly readable and can be used as an alternative of data.frame

amapGeocode is inspired by baidumap and baidugeo. If you want to choose the Baidu Map API, these packages are good choices.

However, AutoNavi has significant high precise, in my case, the Results from Baidu were unsatisfactory.

BIG NEWS: Parallel is Here! But you need a plan

Since v0.5.1, parallel framework is implemented by furrr package, of which backend is future package. Refering to A Future for R: Best Practices for Package Developers and avoiding potential modification to the future strategy, we have removed the automatically parallel operation from every function in amapGeocode.

To turn on parallel operation support, just call future::plan(multisession) # or any other future strategy.

Since v0.5, parallel operation finally comes to amapGeocode with the parallel package as the backend. There is a really huge performance improvement for batch queries. And you are welcomed to make a benchmark by following command.

library(amapGeocode)
library(future)
library(readr)
sample_site <-
  read_csv("https://gist.githubusercontent.com/womeimingzi11/0fa3f4744f3ebc0f4484a52649f556e5/raw/47a69157f3e26c4d3bc993f3715b9ba88cda9d93/sample_site.csv")

str(sample_site)

# Here is the old implement
start_time <- proc.time()
old <- lapply(sample_site$address, amapGeocode:::getCoord.individual)
proc.time() - start_time

# Here is the new implement
plan(multisession)
start_time <- proc.time()
new <- getCoord(sample_site$address)
proc.time() - start_time

While parallel support is a totally threads depending operation, so you will get completely different speed on different devices.

Installation

You can install the released version of amapGeocode from CRAN with:

install.packages("amapGeocode")

To install the development version, run following command:

remotes::install_github('womeimingzi11/amapGeocode')

Usage

Geocoding

Before start geocoding and reverse geocoding, please apply a AutoNavi Map API Key. Set amap_key globally by following command:

options(amap_key = "REPLACE THIS BY YOUR KEY")

Then get results of geocoding, by getCoord function.

library(amapGeocode)
# An individual request
res <-
  getCoord("四川省博物馆")
knitr::kable(res)
# Batch requests
res <-
  getCoord(c(
    "四川省博物馆",
    "成都市博物馆",
    "四川省成都市武侯区金楠天街"
  ))
knitr::kable(res)

The responses we get from AutoNavi Map API is JSON or XML. For readability, we transform them to data.table, by setting output argument as data.table by default.

If you want to extract information from JSON or XML. The results can further be parsed by extractCoord.

# An individual request
res <-
  getCoord("四川省博物馆", output = "JSON")
res

extractCoord is created to get a result as a data.table.

tb <-
  extractCoord(res)
knitr::kable(tb)

Reverse Geocoding

get results of reverse geocoding, by getLocation function.

res <-
  getLocation(103.9960,30.6475)
knitr::kable(res)

extractLocation is created to get a result as a data.table.

Get Subordinate Administrative Region

get results of reverse geocoding, by getAdmin function.

There is a difference between getAdmin and other function, no matter the output argument is data.table or not, the result won't be a jointed table by different parent administrative region. For example, with the output = data.table, all the lower level administrative region of Province A and Province B will be bound as one data.table, respectively. But the table of province A and table of province B won't be bound further.

Because this function supports different administrative region levels, it is nonsense to bind their results.

res <-
  getAdmin(c("四川省", "成都市", "济宁市"))
knitr::kable(res)

extractAdmin is created to get results as tibble.

Convert coordinate point from other coordinate system to AutoNavi

get results of reverse geocoding, by convertCoord function, here is how to convert coordinate from gps to AutoNavi.

Please not, this is still a very experimental function because I have no experience at converting coordinates. The implementation of this input method is not as delicate as I expect. If you have any good idea, please let me know or just fork repo and pull a reques.

res <-
  convertCoord("103.9960,30.6475", coordsys = "gps")
knitr::kable(res)

extractConvertCoord is created to get result as data.table.

Bug report

It's very common for API upgrades to make the downstream application, like amapGeocode,which is unavailable. Feel free to let me know once it's broken or just open an Issue.

Acknowledgements

Hex Sticker was created by hexSticker package with the world data from rnaturalearth.

Code of Conduct

Please note that the amapGeocode project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.



womeimingzi11/amapGeocode documentation built on Nov. 5, 2023, 6:24 a.m.