MyNixOS website logo
Description

An Interface to the 'AutoNavi Maps' API Geocoding Services.

Getting and parsing data of location geocode/reverse-geocode and administrative regions from 'AutoNavi Maps'<https://lbs.amap.com/api/webservice/summary> API.

amapGeocode

Total downloadsbadge CRANstatus Lifecycle:maturing License:MIT DOI Codecov testcoverage 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:

Then get results of geocoding, by getCoord function.

library(amapGeocode)
# An individual request
res <- getCoord("四川省中医院")
knitr::kable(res)
lnglatformatted_addresscountryprovincecitydistricttownshipstreetnumbercitycodeadcode
104.043130.6678四川省成都市金牛区四川省中医院中国四川省成都市金牛区NANANA028510106
# Batch requests
res <- getCoord(c("四川省中医院", "四川省人民医院", "成都中医药大学十二桥校区"))
knitr::kable(res)
lnglatformatted_addresscountryprovincecitydistricttownshipstreetnumbercitycodeadcode
104.043130.66780四川省成都市金牛区四川省中医院中国四川省成都市金牛区NANANA028510106
104.039030.66362四川省成都市青羊区四川省人民医院中国四川省成都市青羊区NANANA028510105
104.043930.66629四川省成都市金牛区成都中医药大学十二桥校区中国四川省成都市金牛区NANANA028510106

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] "510106"
#> 
#> $geocodes[[1]]$street
#> list()
#> 
#> $geocodes[[1]]$number
#> list()
#> 
#> $geocodes[[1]]$location
#> [1] "104.043284,30.666864"
#> 
#> $geocodes[[1]]$level
#> [1] "兴趣点"

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

tb <- extractCoord(res)
knitr::kable(tb)
lnglatformatted_addresscountryprovincecitydistricttownshipstreetnumbercitycodeadcode
104.043330.66686四川省成都市金牛区成都中医药大学中国四川省成都市金牛区NANANA028510106

Reverse Geocoding

get results of reverse geocoding, by getLocation function.

res <- getLocation(104.043284, 30.666864)
knitr::kable(res)
formatted_addresscountryprovincecitydistricttownshipcitycodetowncode
四川省成都市金牛区西安路街道成都中医药大学附属医院腹泻门诊成都中医药大学十二桥校区中国四川省成都市金牛区西安路街道028510106024000

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)
lnglatnamelevelcitycodeadcode
106.753731.85881巴中市city0827511900
104.065730.65946成都市city028510100
105.829832.43367广元市city0839510800
106.083030.79528南充市city0817511300
104.398731.12799德阳市city0838510600
104.741731.46402绵阳市city0816510700
105.571330.51331遂宁市city0825510900
104.641930.12221资阳市city0832512000
106.633430.45640广安市city0826511600
105.066129.58708内江市city1832511000
107.502331.20948达州市city0818511700
103.831830.04832眉山市city1833511400
104.773429.35277自贡市city0813510300
105.443328.88914泸州市city0830510500
104.630828.76019宜宾市city0831511500
103.761329.58202乐山市city0833511100
101.716026.58045攀枝花市city0812510400
102.258727.88676凉山彝族自治州city0834513400
103.001029.98772雅安市city0835511800
102.221431.89979阿坝藏族羌族自治州city0837513200
101.963830.05066甘孜藏族自治州city0836513300
lnglatnamelevelcitycodeadcode
103.627930.99114都江堰市district028510181
103.941230.98516彭州市district028510182
103.522430.58660大邑县district028510129
104.254930.88344青白江区district028510113
103.671030.63148崇州市district028510184
104.550330.39067简阳市district028510185
103.511530.19436蒲江县district028510131
104.415630.85842金堂县district028510121
103.812430.41428新津区district028510118
103.461430.41327邛崃市district028510183
103.836830.69800温江区district028510115
104.051730.63086武侯区district028510107
103.922730.57324双流区district028510116
103.887830.80875郫都区district028510117
104.043530.69206金牛区district028510106
104.160230.82422新都区district028510114
104.269230.56065龙泉驿区district028510112
104.103130.66027成华区district028510108
104.055730.66765青羊区district028510105
104.081030.65769锦江区district028510104
lnglatnamelevelcitycodeadcode
116.991935.59279曲阜市district0537370881
116.487135.72175汶上县district0537370830
116.966735.40526邹城市district0537370883
117.273635.65322泗水县district0537370831
116.595335.41483任城区district0537370811
116.342935.39810嘉祥县district0537370829
116.089635.80184梁山县district0537370832
116.650034.99771鱼台县district0537370827
116.310435.06977金乡县district0537370828
116.829035.55644兖州区district0537370812
117.128634.80953微山县district0537370826

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("116.481499,39.990475", coordsys = "gps")
knitr::kable(res)
lnglat
116.487639.99175

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.

Metadata

Version

0.6.0

License

Unknown

Platforms (77)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows