library("tibble")
library("dplyr")
## 
## Attachement du 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")
# generate fake data
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.9634337
## $ housenumber <chr> "39"
## $ id          <chr> "75115_0318_00039"
## $ type        <chr> "housenumber"
## $ x           <dbl> 647082.8
## $ y           <dbl> 6861010
## $ importance  <dbl> 0.5977708
## $ name        <chr> "39 Quai André Citroën"
## $ postcode    <chr> "75015"
## $ citycode    <chr> "75115"
## $ city        <chr> "Paris"
## $ district    <chr> "Paris 15e Arrondissement"
## $ context     <chr> "75, Paris, Île-de-France"
## $ 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/RtmpswR9wc/file162a8a53f358c.csv
## If file is larger than 8 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.0959294, 0.6502475, -1.6287054
## $ adress             <chr> "39 quai André Citroën", "64 Allée de Bercy", "20 …
## $ 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 …
## $ result_score       <dbl> 0.96, 0.96, 0.96
## $ result_type        <chr> "housenumber", "housenumber", "housenumber"
## $ result_id          <chr> "75115_0318_00039", "75112_0874_00064", "75107_890…
## $ 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-Fra…
## $ 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 Arrondissem…
geocode_tbl(tbl = table_test, adresse = adress, code_postal = postal_code) %>%
  glimpse()
## Writing tempfile to.../tmp/RtmpswR9wc/file162a8a1cf89d36.csv
## If file is larger than 8 MB, it must be splitted
## Size is : 100 bytes
## SuccessOKSuccess: (200) OK
## Rows: 3
## Columns: 19
## $ z                  <dbl> -1.0959294, 0.6502475, -1.6287054
## $ adress             <chr> "39 quai André Citroën", "64 Allée de Bercy", "20 …
## $ 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 …
## $ result_score       <dbl> 0.96, 0.96, 0.96
## $ result_type        <chr> "housenumber", "housenumber", "housenumber"
## $ result_id          <chr> "75115_0318_00039", "75112_0874_00064", "75107_890…
## $ 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-Fra…
## $ 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 Arrondissem…
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/RtmpswR9wc/file162a8a23215505.csv
## If file is larger than 8 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", "0…
## $ bureau             <chr> "09", "09", "09", "09", "09", "09", "09", "09", "0…
## $ numero             <int> 4, 5, 6, 7, 8, 11, 12, 13, 14, 16, 3, 4, 5, 6, 7, …
## $ voie               <chr> "RUE DE L", "RUE DE L", "RUE DE L", "RUE DE L", "R…
## $ nom                <chr> "ABBAYE", "ABBAYE", "ABBAYE", "ABBAYE", "ABBAYE", …
## $ nb                 <int> 1, 1, 20, 2, 17, 2, 9, 15, 17, 8, 13, 6, 6, 3, 9, …
## $ ID                 <chr> "0609", "0609", "0609", "0609", "0609", "0609", "0…
## $ adresse            <chr> "4 RUE DE L ABBAYE", "5 RUE DE L ABBAYE", "6 RUE D…
## $ code_insee         <chr> "75106", "75106", "75106", "75106", "75106", "7510…
## $ latitude           <dbl> 48.85405, 48.85407, 48.85414, 48.85410, 48.85425, …
## $ longitude          <dbl> 2.335715, 2.335172, 2.335352, 2.335041, 2.334903, …
## $ result_label       <chr> "4 Rue de l'Abbaye 75006 Paris", "5 Rue de l'Abbay…
## $ result_score       <dbl> 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.…
## $ result_type        <chr> "housenumber", "housenumber", "housenumber", "hous…
## $ result_id          <chr> "75106_0002_00004", "75106_0002_00005", "75106_000…
## $ result_housenumber <chr> "4", "5", "6", "7", "8", "11", "12", "13", "14", "…
## $ result_name        <chr> "Rue de l'Abbaye", "Rue de l'Abbaye", "Rue de l'Ab…
## $ result_street      <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
## $ result_postcode    <chr> "75006", "75006", "75006", "75006", "75006", "7500…
## $ result_city        <chr> "Paris", "Paris", "Paris", "Paris", "Paris", "Pari…
## $ result_context     <chr> "75, Paris, Île-de-France", "75, Paris, Île-de-Fra…
## $ result_citycode    <chr> "75106", "75106", "75106", "75106", "75106", "7510…
## $ 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 Arrondissemen…

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> "39 a Quai André Citroën 75015 Paris"
## $ score       <dbl> 1
## $ housenumber <chr> "39 a"
## $ id          <chr> "75115_0318_00039_a"
## $ type        <chr> "housenumber"
## $ x           <dbl> 647094.3
## $ y           <dbl> 6860995
## $ importance  <dbl> 0.5977708
## $ name        <chr> "39 a Quai André Citroën"
## $ postcode    <chr> "75015"
## $ citycode    <chr> "75115"
## $ city        <chr> "Paris"
## $ district    <chr> "Paris 15e Arrondissement"
## $ context     <chr> "75, Paris, Île-de-France"
## $ 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/RtmpswR9wc/file162a8a5097f9ef.csv
## If file is larger than 8 MB, it must be splitted
## Size is : 386 bytes
## SuccessOKSuccess: (200) OK
## Rows: 10
## Columns: 19
## $ nom                <chr> "c", "y", "i", "l", "d", "j", "m", "t", "o", "b"
## $ longitude          <dbl> 2.286960, 2.459247, 2.444266, 2.431339, 2.261998, …
## $ latitude           <dbl> 48.89355, 48.88446, 48.85827, 48.85081, 48.86620, …
## $ result_latitude    <dbl> 48.89368, 48.88455, 48.85805, 48.85077, 48.86696, …
## $ result_longitude   <dbl> 2.286829, 2.458534, 2.444606, 2.431287, 2.261532, …
## $ result_label       <chr> "12 Rue Pasteur 92300 Levallois-Perret", "12 Avenu…
## $ result_distance    <int> 17, 52, 34, 5, 91, 77, 9, 24, 19, 17
## $ result_type        <chr> "housenumber", "housenumber", "housenumber", "hous…
## $ result_id          <chr> "92044_7120_00012", "93053_2555_00012", "93048_155…
## $ result_housenumber <chr> "12", "12", "30", "27", "81", "8", "9", "83", "11"…
## $ result_name        <chr> "Rue Pasteur", "Avenue du 18 Avril 1944", "Rue Con…
## $ result_street      <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
## $ result_postcode    <chr> "92300", "93130", "93100", "94300", "75016", "9313…
## $ result_city        <chr> "Levallois-Perret", "Noisy-le-Sec", "Montreuil", "…
## $ result_context     <chr> "92, Hauts-de-Seine, Île-de-France", "93, Seine-Sa…
## $ result_citycode    <chr> "92044", "93053", "93048", "94080", "75116", "9305…
## $ 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> NA, NA, NA, NA, "Paris 16e Arrondissement", NA, NA…