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.
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.
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')
Before start geocoding and reverse geocoding, please apply a AutoNavi
Map API Key. Set amap_key
globally by
following command:
Then get results of geocoding, by getCoord
function.
library(amapGeocode)
# An individual request
res <- getCoord("四川省博物馆")
knitr::kable(res)
lng | lat | formatted_address | country | province | city | district | township | street | number | citycode | adcode |
---|---|---|---|---|---|---|---|---|---|---|---|
104.0339 | 30.66069 | 四川省成都市青羊区四川省博物馆 | 中国 | 四川省 | 成都市 | 青羊区 | NA | NA | NA | 028 | 510105 |
# Batch requests
res <- getCoord(c("四川省博物馆", "成都市博物馆", "四川省成都市武侯区金楠天街"))
knitr::kable(res)
lng | lat | formatted_address | country | province | city | district | township | street | number | citycode | adcode |
---|---|---|---|---|---|---|---|---|---|---|---|
104.0339 | 30.66069 | 四川省成都市青羊区四川省博物馆 | 中国 | 四川省 | 成都市 | 青羊区 | NA | NA | NA | 028 | 510105 |
104.0637 | 30.65733 | 四川省成都市青羊区成都市博物馆 | 中国 | 四川省 | 成都市 | 青羊区 | NA | NA | NA | 028 | 510105 |
103.9962 | 30.64848 | 四川省成都市武侯区金楠天街 | 中国 | 四川省 | 成都市 | 武侯区 | NA | NA | NA | 028 | 510107 |
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
#> $status
#> [1] "1"
#>
#> $info
#> [1] "OK"
#>
#> $infocode
#> [1] "10000"
#>
#> $count
#> [1] "1"
#>
#> $geocodes
#> $geocodes[[1]]
#> $geocodes[[1]]$formatted_address
#> [1] "四川省成都市青羊区四川省博物馆"
#>
#> $geocodes[[1]]$country
#> [1] "中国"
#>
#> $geocodes[[1]]$province
#> [1] "四川省"
#>
#> $geocodes[[1]]$citycode
#> [1] "028"
#>
#> $geocodes[[1]]$city
#> [1] "成都市"
#>
#> $geocodes[[1]]$district
#> [1] "青羊区"
#>
#> $geocodes[[1]]$township
#> list()
#>
#> $geocodes[[1]]$neighborhood
#> $geocodes[[1]]$neighborhood$name
#> list()
#>
#> $geocodes[[1]]$neighborhood$type
#> list()
#>
#>
#> $geocodes[[1]]$building
#> $geocodes[[1]]$building$name
#> list()
#>
#> $geocodes[[1]]$building$type
#> list()
#>
#>
#> $geocodes[[1]]$adcode
#> [1] "510105"
#>
#> $geocodes[[1]]$street
#> list()
#>
#> $geocodes[[1]]$number
#> list()
#>
#> $geocodes[[1]]$location
#> [1] "104.033944,30.660691"
#>
#> $geocodes[[1]]$level
#> [1] "兴趣点"
extractCoord
is created to get a result as a data.table.
tb <- extractCoord(res)
knitr::kable(tb)
lng | lat | formatted_address | country | province | city | district | township | street | number | citycode | adcode |
---|---|---|---|---|---|---|---|---|---|---|---|
104.0339 | 30.66069 | 四川省成都市青羊区四川省博物馆 | 中国 | 四川省 | 成都市 | 青羊区 | NA | NA | NA | 028 | 510105 |
get results of reverse geocoding, by getLocation
function.
res <- getLocation(103.996, 30.6475)
knitr::kable(res)
formatted_address | country | province | city | district | township | citycode | towncode |
---|---|---|---|---|---|---|---|
四川省成都市武侯区晋阳街道晋吉西一街66号龙湖金楠天街·C馆(天街里店) | 中国 | 四川省 | 成都市 | 武侯区 | 晋阳街道 | 028 | 510107011000 |
extractLocation
is created to get a result as a data.table.
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.
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)
lng | lat |
---|---|
103.9983 | 30.6449 |
extractConvertCoord
is created to get result as data.table.
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.
Hex Sticker was created by hexSticker package with the world data from rnaturalearth.
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.