## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library("banR")

table_test <- tibble::tibble(
  adress = c("39 quai André Citroën", "64 Allée de Bercy", "20 avenue de Ségur"),
  postal_code = c("75015", "75012", "75007"),
  z = rnorm(3)
  )

Geocode

Geocoding is the process of transforming a human readable address into a location (ie a pair of latitude and longitude).

A single address

geocode(query = "39 quai André Citroën, Paris") %>%
  glimpse()
## 200
## Rows: 1
## Columns: 18
## $ label       <chr> "39 Quai André Citroën 75015 Paris"
## $ score       <dbl> 0.9802091
## $ housenumber <chr> "39"
## $ id          <chr> "75115_0318_00039"
## $ name        <chr> "39 Quai André Citroën"
## $ postcode    <chr> "75015"
## $ citycode    <chr> "75115"
## $ x           <dbl> 647082.8
## $ y           <dbl> 6861010
## $ city        <chr> "Paris"
## $ district    <chr> "Paris 15e Arrondissement"
## $ context     <chr> "75, Paris, Île-de-France"
## $ type        <chr> "housenumber"
## $ importance  <dbl> 0.7823
## $ street      <chr> "Quai André Citroën"
## $ type_geo    <chr> "Point"
## $ longitude   <dbl> 2.278922
## $ latitude    <dbl> 48.84696

The BAN API sends back both projected/Cartesian coordinates (x and y columns - they use Lambert 93 projection, aka as EPSG:2154), and lon/lat (i.e. WGS84) coordinates (longitude and latitude columns). It also indicates the degree of confidence it has in each result (column score). The above example only sends back one result, but sometimes the API will send back several suggestion for the same query. They are ordered by descending order of confidence.

A data frame

In addition to the adress, geocode_tbl() can take as argument either the postal code or the French official code (INSEE code) of the commune.

geocode_tbl(tbl = table_test, adresse = adress) %>%
  glimpse()
## Writing tempfile to.../tmp/RtmplA1eOF/file155e0309cb996.csv
## If file is larger than 50 MB, it must be splitted
## Size is : 70 bytes
## SuccessOKSuccess: (200) OK
## Rows: 3
## Columns: 19
## $ postal_code        <chr> "75015", "75012", "75007"
## $ z                  <dbl> 1.0123532, -0.3553875, -0.8148182
## $ adress             <chr> "39 quai André Citroën", "64 Allée de Bercy", "20 a…
## $ latitude           <dbl> 48.84696, 48.84254, 48.85070
## $ longitude          <dbl> 2.278922, 2.376011, 2.308628
## $ result_label       <chr> "39 Quai André Citroën 75015 Paris", "64 Allée de B…
## $ result_score       <dbl> 0.98, 0.97, 0.97
## $ result_type        <chr> "housenumber", "housenumber", "housenumber"
## $ result_id          <chr> "75115_0318_00039", "75112_0874_00064", "75107_8909…
## $ result_housenumber <chr> "39", "64", "20"
## $ result_name        <chr> "Quai André Citroën", "Allée de Bercy", "Avenue de …
## $ result_street      <chr> NA, NA, NA
## $ result_postcode    <chr> "75015", "75012", "75007"
## $ result_city        <chr> "Paris", "Paris", "Paris"
## $ result_context     <chr> "75, Paris, Île-de-France", "75, Paris, Île-de-Fran…
## $ result_citycode    <chr> "75115", "75112", "75107"
## $ result_oldcitycode <chr> NA, NA, NA
## $ result_oldcity     <chr> NA, NA, NA
## $ result_district    <chr> "Paris 15e Arrondissement", "Paris 12e Arrondisseme…
geocode_tbl(tbl = table_test, adresse = adress, code_postal = postal_code) %>%
  glimpse()
## Writing tempfile to.../tmp/RtmplA1eOF/file155e02ccbb1a2.csv
## If file is larger than 50 MB, it must be splitted
## Size is : 100 bytes
## SuccessOKSuccess: (200) OK
## Rows: 3
## Columns: 19
## $ z                  <dbl> 1.0123532, -0.3553875, -0.8148182
## $ adress             <chr> "39 quai André Citroën", "64 Allée de Bercy", "20 a…
## $ postal_code        <chr> "75015", "75012", "75007"
## $ latitude           <dbl> 48.84696, 48.84254, 48.85070
## $ longitude          <dbl> 2.278922, 2.376011, 2.308628
## $ result_label       <chr> "39 Quai André Citroën 75015 Paris", "64 Allée de B…
## $ result_score       <dbl> 0.98, 0.97, 0.97
## $ result_type        <chr> "housenumber", "housenumber", "housenumber"
## $ result_id          <chr> "75115_0318_00039", "75112_0874_00064", "75107_8909…
## $ result_housenumber <chr> "39", "64", "20"
## $ result_name        <chr> "Quai André Citroën", "Allée de Bercy", "Avenue de …
## $ result_street      <chr> NA, NA, NA
## $ result_postcode    <chr> "75015", "75012", "75007"
## $ result_city        <chr> "Paris", "Paris", "Paris"
## $ result_context     <chr> "75, Paris, Île-de-France", "75, Paris, Île-de-Fran…
## $ result_citycode    <chr> "75115", "75112", "75107"
## $ result_oldcitycode <chr> NA, NA, NA
## $ result_oldcity     <chr> NA, NA, NA
## $ result_district    <chr> "Paris 15e Arrondissement", "Paris 12e Arrondisseme…
data("paris2012")
paris2012 %>%
  slice(1:100) %>%
  mutate(
    adresse = paste(numero, voie, nom),
    code_insee = paste0("751", arrondissement)
    ) %>%
  geocode_tbl(adresse = adresse, code_insee = code_insee) %>%
  glimpse()
## Writing tempfile to.../tmp/RtmplA1eOF/file155e070ddf487.csv
## If file is larger than 50 MB, it must be splitted
## Size is : 3 Kb
## SuccessOKSuccess: (200) OK
## Rows: 100
## Columns: 25
## $ arrondissement     <chr> "06", "06", "06", "06", "06", "06", "06", "06", "06…
## $ bureau             <chr> "09", "09", "09", "09", "09", "09", "09", "09", "09…
## $ numero             <int> 4, 5, 6, 7, 8, 11, 12, 13, 14, 16, 3, 4, 5, 6, 7, 8…
## $ voie               <chr> "RUE DE L", "RUE DE L", "RUE DE L", "RUE DE L", "RU…
## $ nom                <chr> "ABBAYE", "ABBAYE", "ABBAYE", "ABBAYE", "ABBAYE", "…
## $ nb                 <int> 1, 1, 20, 2, 17, 2, 9, 15, 17, 8, 13, 6, 6, 3, 9, 1…
## $ ID                 <chr> "0609", "0609", "0609", "0609", "0609", "0609", "06…
## $ adresse            <chr> "4 RUE DE L ABBAYE", "5 RUE DE L ABBAYE", "6 RUE DE…
## $ code_insee         <chr> "75106", "75106", "75106", "75106", "75106", "75106…
## $ latitude           <dbl> 48.85405, 48.85407, 48.85414, 48.85410, 48.85425, 4…
## $ longitude          <dbl> 2.335715, 2.335172, 2.335352, 2.335041, 2.334903, 2…
## $ result_label       <chr> "4 Rue de l’Abbaye 75006 Paris", "5 Rue de l’Abbaye…
## $ result_score       <dbl> 0.97, 0.97, 0.97, 0.97, 0.97, 0.97, 0.97, 0.97, 0.9…
## $ result_type        <chr> "housenumber", "housenumber", "housenumber", "house…
## $ result_id          <chr> "75106_0002_00004", "75106_0002_00005", "75106_0002…
## $ result_housenumber <chr> "4", "5", "6", "7", "8", "11", "12", "13", "14", "1…
## $ result_name        <chr> "Rue de l’Abbaye", "Rue de l’Abbaye", "Rue de l’Abb…
## $ result_street      <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ result_postcode    <chr> "75006", "75006", "75006", "75006", "75006", "75006…
## $ result_city        <chr> "Paris", "Paris", "Paris", "Paris", "Paris", "Paris…
## $ result_context     <chr> "75, Paris, Île-de-France", "75, Paris, Île-de-Fran…
## $ result_citycode    <chr> "75106", "75106", "75106", "75106", "75106", "75106…
## $ result_oldcitycode <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ result_oldcity     <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ result_district    <chr> "Paris 6e Arrondissement", "Paris 6e Arrondissement…

Reverse geocode

Reverse geocoding is the process of back (reverse) coding of a point location (latitude, longitude) to a human readable address.

A single adress

reverse_geocode() takes longitude and latitude as arguments and returns a data frame with addresses.

reverse_geocode(long =  2.279092, lat = 48.84683)  %>%
  glimpse()
## 200
## Rows: 1
## Columns: 19
## $ label       <chr> "39a Quai André Citroën 75015 Paris"
## $ score       <dbl> 1
## $ housenumber <chr> "39a"
## $ id          <chr> "75115_0318_00039_a"
## $ name        <chr> "39a Quai André Citroën"
## $ postcode    <chr> "75015"
## $ citycode    <chr> "75115"
## $ x           <dbl> 647094.3
## $ y           <dbl> 6860995
## $ city        <chr> "Paris"
## $ district    <chr> "Paris 15e Arrondissement"
## $ context     <chr> "75, Paris, Île-de-France"
## $ type        <chr> "housenumber"
## $ importance  <dbl> 0.7823
## $ street      <chr> "Quai André Citroën"
## $ distance    <int> 0
## $ type_geo    <chr> "Point"
## $ longitude   <dbl> 2.279081
## $ latitude    <dbl> 48.84683

A data frame

reverse_geocode_tbl takes the names of the longitude and latitude columns and returns a data frame with adresses.

test_df <- tibble::tibble(
  nom = sample(letters, size = 10, replace = FALSE),
  lon = runif(10, 2.19, 2.47),
  lat = runif(10, 48.8, 48.9)
)

test_df %>% 
  reverse_geocode_tbl(lon, lat) %>% 
  glimpse
## Writing tempfile to.../tmp/RtmplA1eOF/file155e02e9b9b32.csv
## If file is larger than 50 MB, it must be splitted
## Size is : 391 bytes
## SuccessOKSuccess: (200) OK
## Rows: 10
## Columns: 19
## $ nom                <chr> "b", "d", "o", "z", "h", "e", "u", "n", "s", "c"
## $ longitude          <dbl> 2.280913, 2.393396, 2.443009, 2.401468, 2.365696, 2…
## $ latitude           <dbl> 48.86070, 48.88343, 48.86584, 48.80958, 48.88799, 4…
## $ result_latitude    <dbl> 48.86059, 48.88330, 48.86581, 48.80966, 48.88795, 4…
## $ result_longitude   <dbl> 2.281157, 2.393637, 2.443042, 2.401171, 2.366197, 2…
## $ result_label       <chr> "62a Rue de la Tour 75016 Paris", "19 Rue de la Pré…
## $ result_distance    <int> 21, 22, 3, 23, 36, 13, 7, 51, 4, 35
## $ result_type        <chr> "housenumber", "housenumber", "housenumber", "house…
## $ result_id          <chr> "75116_9362_00062_a", "75119_7826_00019", "93048_44…
## $ result_housenumber <chr> "62a", "19", "33ter", "30", "20a", "91", "107", "35…
## $ result_name        <chr> "Rue de la Tour", "Rue de la Prévoyance", "Boulevar…
## $ result_street      <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
## $ result_postcode    <chr> "75016", "75019", "93100", "94200", "75018", "75012…
## $ result_city        <chr> "Paris", "Paris", "Montreuil", "Ivry-sur-Seine", "P…
## $ result_context     <chr> "75, Paris, Île-de-France", "75, Paris, Île-de-Fran…
## $ result_citycode    <chr> "75116", "75119", "93048", "94041", "75118", "75112…
## $ result_oldcitycode <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
## $ result_oldcity     <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
## $ result_district    <chr> "Paris 16e Arrondissement", "Paris 19e Arrondisseme…