The get_api_query function can create queries for this function to execute.

query_api(
  api_url,
  query_parameters,
  mode = "single",
  batch_file = NULL,
  input_list = NULL,
  content_encoding = "UTF-8",
  timeout = 20,
  method = ""
)

Arguments

api_url

Base URL of the API. query parameters are appended to this

query_parameters

api query parameters in the form of a named list

mode

determines the type of query to execute

  • "single": geocode a single input (all methods)

  • "list": batch geocode a list of inputs (ex. geocodio)

  • "file": batch geocode a file of inputs (ex. census)

batch_file

a csv file of input data to upload (for mode = 'file')

input_list

a list of input data (for mode = 'list')

content_encoding

Encoding to be used for parsing content

timeout

timeout in minutes

method

if 'mapquest' or 'arcgis' then the query status code is changed appropriately

Value

a named list containing the response content (content) and the HTTP request status (status)

Examples

# \donttest{
raw1 <- query_api(
  "http://nominatim.openstreetmap.org/search",
  get_api_query("osm", list(address = "Hanoi, Vietnam"))
)

raw1$status
#> [1] 200

extract_results("osm", jsonlite::fromJSON(raw1$content))
#>        lat      lon  place_id
#> 1 21.02945 105.8544 298352142
#> 2 21.04456 105.9187 155101977
#> 3 20.99176 105.7968 250857201
#> 4 21.03667 105.7827 271941477
#> 5 21.00400 105.5018 279440201
#> 6 21.03669 105.7827  81447375
#>                                                                  licence
#> 1 Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright
#> 2 Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright
#> 3 Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright
#> 4 Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright
#> 5 Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright
#> 6 Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright
#>   osm_type     osm_id                                      boundingbox
#> 1 relation    1903516 20.5645154, 21.3852777, 105.2848986, 106.0200725
#> 2      way  208502808   21.0437964, 21.0453238, 105.9181301, 105.91946
#> 3      way  736412443  20.9907136, 20.9928221, 105.795787, 105.7978094
#> 4      way  872332878  21.0365605, 21.0367753, 105.7821469, 105.783248
#> 5      way  923220611 20.9897301, 21.0201985, 105.4841565, 105.5219865
#> 6     node 8124813873 21.0366379, 21.0367379, 105.7826432, 105.7827432
#>                                                                                                                           display_name
#> 1                                                                                                           Thành phố Hà Nội, Việt Nam
#> 2 British International School, Hanoi, Hoa Lan, Vinhomes Riverside, Phường Phúc Lợi, Quận Long Biên, Thành phố Hà Nội, 08443, Việt Nam
#> 3             Đại học quốc gia Hà Nội, 182, Đường Lương Thế Vinh, Quận Thanh Xuân, Quận Nam Từ Liêm, Thành phố Hà Nội, 11718, Việt Nam
#> 4                                           Đại học Quốc gia Hà Nội, Đường Xuân Thủy, Quận Cầu Giấy, Thành phố Hà Nội, 11150, Việt Nam
#> 5                                 Đại học Quốc gia Hà Nội, Đường Làng Văn hóa, Thạch Hòa, Huyện Thạch Thất, Thành phố Hà Nội, Việt Nam
#> 6                                           Đại học Quốc gia Hà Nội, Đường Xuân Thủy, Quận Cầu Giấy, Thành phố Hà Nội, 11150, Việt Nam
#>      class           type importance
#> 1 boundary administrative  0.6282962
#> 2 building         school  0.4099978
#> 3  amenity     university  0.3945655
#> 4  railway        station  0.0000100
#> 5  amenity     university  0.0000100
#> 6  railway           stop  0.0000100
#>                                                                                   icon
#> 1 https://nominatim.openstreetmap.org/ui/mapicons/poi_boundary_administrative.p.20.png
#> 2                                                                                 <NA>
#> 3        https://nominatim.openstreetmap.org/ui/mapicons/education_university.p.20.png
#> 4    https://nominatim.openstreetmap.org/ui/mapicons/transport_train_station2.p.20.png
#> 5        https://nominatim.openstreetmap.org/ui/mapicons/education_university.p.20.png
#> 6                                                                                 <NA>

raw2 <- query_api(
  "http://nominatim.openstreetmap.org/reverse",
  get_api_query("osm", custom_parameters = list(lat = 38.895865, lon = -77.0307713))
)

extract_reverse_results("osm", jsonlite::fromJSON(raw2$content))
#> # A tibble: 1 × 16
#>   display_n…¹ place…² licence osm_t…³ osm_id osm_lat osm_lon tourism road  city 
#>   <chr>         <int> <chr>   <chr>    <int> <chr>   <chr>   <chr>   <chr> <chr>
#> 1 L’Enfant's…  2.75e8 Data ©… way     9.00e8 38.895… -77.03… L’Enfa… Penn… Wash…
#> # … with 6 more variables: state <chr>, `ISO3166-2-lvl4` <chr>, postcode <chr>,
#> #   country <chr>, country_code <chr>, boundingbox <list>, and abbreviated
#> #   variable names ¹​display_name, ²​place_id, ³​osm_type
# }