The hardware and bandwidth for this mirror is donated by METANET, the Webhosting and Full Service-Cloud Provider.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]metanet.ch.
Tidygeocoder provides a unified interface for performing both forward and reverse geocoding queries with a variety of geocoding services. In forward geocoding you provide an address to the geocoding service and you get latitude and longitude coordinates in return. In reverse geocoding you provide the latitude and longitude and the geocoding service will return that location’s address. In both cases, other data about the location can be provided by the geocoding service.
The geocode()
and geo()
functions are for forward geocoding while the reverse_geocode()
and reverse_geo()
functions perform reverse geocoding. The geocode()
and reverse_geocode()
functions extract either addresses (forward geocoding) or coordinates (reverse geocoding) from the input dataframe and pass this data to the geo()
and reverse_geo()
functions respectively which execute the geocoding queries. All extra arguments (...
) given to geocode()
are passed to geo()
and extra arguments given to reverse_geocode()
are passed to reverse_geo()
.
library(tibble)
library(dplyr)
library(tidygeocoder)
<- tibble(singlelineaddress = c(
address_single "11 Wall St, NY, NY",
"600 Peachtree Street NE, Atlanta, Georgia"
))<- tribble(
address_components ~street, ~cty, ~st,
"11 Wall St", "NY", "NY",
"600 Peachtree Street NE", "Atlanta", "GA"
)
You can use the address
argument to specify single-line addresses. Note that when multiple addresses are provided, the batch geocoding functionality of the Census geocoding service is used. Additionally, verbose = TRUE
displays logs to the console.
<- address_single %>%
census_s1 geocode(address = singlelineaddress, method = "census", verbose = TRUE)
#>
#> Number of Unique Addresses: 2
#> Executing batch geocoding...
#> Batch limit: 10,000
#> Passing 2 addresses to the US Census batch geocoder
#> Querying API URL: https://geocoding.geo.census.gov/geocoder/locations/addressbatch
#> Passing the following parameters to the API:
#> format : "json"
#> benchmark : "Public_AR_Current"
#> vintage : "Current_Current"
#> Query completed in: 1 seconds
singlelineaddress | lat | long |
---|---|---|
11 Wall St, NY, NY | 40.70747 | -74.01122 |
600 Peachtree Street NE, Atlanta, Georgia | 33.77085 | -84.38505 |
Alternatively you can run the same query with the geo()
function by passing the address values from the dataframe directly. In either geo()
or geocode()
, the lat
and long
arguments are used to name the resulting latitude and longitude fields. Here the method
argument is used to specify the “osm” (Nominatim) geocoding service. Refer to the geo()
function documentation for the possible values of the method
argument.
<- geo(
osm_s1 address = address_single$singlelineaddress, method = "osm",
lat = latitude, long = longitude
)#> Passing 2 addresses to the Nominatim single address geocoder
#> Query completed in: 2 seconds
address | latitude | longitude |
---|---|---|
11 Wall St, NY, NY | 40.70707 | -74.01117 |
600 Peachtree Street NE, Atlanta, Georgia | 33.77086 | -84.38614 |
Instead of single-line addresses, you can use any combination of the following arguments to specify your addresses: street
, city
, state
, county
, postalcode
, and country
.
<- address_components %>%
census_c1 geocode(street = street, city = cty, state = st, method = "census")
#> Passing 2 addresses to the US Census batch geocoder
#> Query completed in: 2.5 seconds
street | cty | st | lat | long |
---|---|---|---|---|
11 Wall St | NY | NY | 40.70747 | -74.01122 |
600 Peachtree Street NE | Atlanta | GA | 33.77085 | -84.38505 |
To return the full geocoding service results (not just latitude and longitude), specify full_results = TRUE
. Additionally, for the Census geocoder you can get fields for geographies such as Census tracts by specifying api_options = list(census_return_type = 'geographies')
. Be sure to use full_results = TRUE
with the “geographies” return type in order to allow the Census geography columns to be returned.
<- address_single %>% geocode(
census_full1 address = singlelineaddress,
method = "census", full_results = TRUE, api_options = list(census_return_type = 'geographies')
)#> Passing 2 addresses to the US Census batch geocoder
#> Query completed in: 1.2 seconds
singlelineaddress | lat | long | id | input_address | match_indicator | match_type | matched_address | tiger_line_id | tiger_side | state_fips | county_fips | census_tract | census_block |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
11 Wall St, NY, NY | 40.70747 | -74.01122 | 1 | 11 Wall St, NY, NY, , , | Match | Exact | 11 WALL ST, NEW YORK, NY, 10005 | 59659656 | R | 36 | 061 | 000700 | 1004 |
600 Peachtree Street NE, Atlanta, Georgia | 33.77085 | -84.38505 | 2 | 600 Peachtree Street NE, Atlanta, Georgia, , , | Match | Non_Exact | 600 PEACHTREE ST, ATLANTA, GA, 30308 | 17343689 | L | 13 | 121 | 001902 | 2003 |
As mentioned earlier, the geocode()
function passes addresses in dataframes to the geo()
function for geocoding so we can also directly use the geo()
function in a similar way:
<- geo("Salzburg, Austria", method = "osm", full_results = TRUE) %>%
salz select(-licence)
#> Passing 1 address to the Nominatim single address geocoder
#> Query completed in: 1 seconds
address | lat | long | place_id | osm_type | osm_id | boundingbox | display_name | class | type | importance | icon |
---|---|---|---|---|---|---|---|---|---|---|---|
Salzburg, Austria | 47.79813 | 13.04648 | 147539 | node | 34964314 | 47.6381346, 47.9581346, 12.8864806, 13.2064806 | Salzburg, 5020, Österreich | place | city | 0.6854709 | https://nominatim.openstreetmap.org/ui/mapicons//poi_place_city.p.20.png |
For reverse geocoding you’ll use reverse_geocode()
instead of geocode()
and reverse_geo()
instead of geo()
. Note that the reverse geocoding functions are structured very similarly to the forward geocoding functions and share many of the same arguments (method
, limit
, full_results
, etc.). For reverse geocoding you will provide latitude and longitude coordinates as inputs and the location’s address will be returned by the geocoding service.
Below, the reverse_geocode()
function is used to geocode coordinates in a dataframe. The lat
and long
arguments specify the columns that contain the latitude and longitude data. The address
argument can be used to specify the single line address column name that is returned from the geocoder. Just as with forward geocoding, the method
argument is used to specify the geocoding service.
<- tibble(
lat_longs1 latitude = c(38.895865, 43.6534817),
longitude = c(-77.0307713, -79.3839347)
)
<- lat_longs1 %>%
rev1 reverse_geocode(lat = latitude, long = longitude, address = addr, method = "osm")
#> Passing 2 coordinates to the Nominatim single coordinate geocoder
#> Query completed in: 2 seconds
latitude | longitude | addr |
---|---|---|
38.89587 | -77.03077 | Freedom Plaza, 1455, Pennsylvania Avenue Northwest, Washington, District of Columbia, 20004, United States |
43.65348 | -79.38393 | Toronto City Hall, 100, Queen Street West, Financial District, Spadina—Fort York, Old Toronto, Toronto, Golden Horseshoe, Ontario, M5H 2N2, Canada |
The same query can also be performed by passing the latitude and longitudes directly to the reverse_geo()
function. Here we will use full_results = TRUE
so that the full results are returned (not just the single line address column).
<- reverse_geo(
rev2 lat = lat_longs1$latitude,
long = lat_longs1$longitude,
method = "osm",
full_results = TRUE
)#> Passing 2 coordinates to the Nominatim single coordinate geocoder
#> Query completed in: 2 seconds
glimpse(rev2)
#> Rows: 2
#> Columns: 22
#> $ lat <dbl> 38.89587, 43.65348
#> $ long <dbl> -77.03077, -79.38393
#> $ address <chr> "Freedom Plaza, 1455, Pennsylvania Avenue Northwest, Washington, District of Columbia, 20004, United States", "Toronto City Hall, 1…
#> $ place_id <int> 284009208, 148364261
#> $ licence <chr> "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright", "Data © OpenStreetMap contributors, ODbL 1.0. https://osm…
#> $ osm_type <chr> "relation", "way"
#> $ osm_id <int> 8060882, 198500761
#> $ osm_lat <chr> "38.895849999999996", "43.6536032"
#> $ osm_lon <chr> "-77.03077367444483", "-79.38400546703345"
#> $ tourism <chr> "Freedom Plaza", NA
#> $ house_number <chr> "1455", "100"
#> $ road <chr> "Pennsylvania Avenue Northwest", "Queen Street West"
#> $ city <chr> "Washington", "Old Toronto"
#> $ state <chr> "District of Columbia", "Ontario"
#> $ postcode <chr> "20004", "M5H 2N2"
#> $ country <chr> "United States", "Canada"
#> $ country_code <chr> "us", "ca"
#> $ boundingbox <list> <"38.8956276", "38.896068", "-77.03182", "-77.0297273">, <"43.6529946", "43.6541458", "-79.3848438", "-79.3830415">
#> $ amenity <chr> NA, "Toronto City Hall"
#> $ neighbourhood <chr> NA, "Financial District"
#> $ quarter <chr> NA, "Spadina—Fort York"
#> $ state_district <chr> NA, "Golden Horseshoe"
Only unique input data (either addresses or coordinates) is passed to geocoding services even if your data contains duplicates. NA and blank inputs are excluded from queries. Input latitudes and longitudes are also limited to the range of possible values.
Below is an example of how duplicate and missing data is handled by tidygeocoder. As the console messages shows, only the two unique addresses are passed to the geocoding service.
# create a dataset with duplicate and NA addresses
<- address_single %>%
duplicate_addrs bind_rows(address_single) %>%
bind_rows(tibble(singlelineaddress = rep(NA, 3)))
<- duplicate_addrs %>%
duplicates_geocoded geocode(singlelineaddress, verbose = TRUE)
#>
#> Number of Unique Addresses: 2
#> Passing 2 addresses to the Nominatim single address geocoder
#>
#> Number of Unique Addresses: 1
#> Querying API URL: https://nominatim.openstreetmap.org/search
#> Passing the following parameters to the API:
#> q : "11 Wall St, NY, NY"
#> limit : "1"
#> format : "json"
#> HTTP Status Code: 200
#> Query completed in: 0.2 seconds
#> Total query time (including sleep): 1 seconds
#>
#>
#> Number of Unique Addresses: 1
#> Querying API URL: https://nominatim.openstreetmap.org/search
#> Passing the following parameters to the API:
#> q : "600 Peachtree Street NE, Atlanta, Georgia"
#> limit : "1"
#> format : "json"
#> HTTP Status Code: 200
#> Query completed in: 0.2 seconds
#> Total query time (including sleep): 1 seconds
#>
#> Query completed in: 2 seconds
singlelineaddress | lat | long |
---|---|---|
11 Wall St, NY, NY | 40.70707 | -74.01117 |
600 Peachtree Street NE, Atlanta, Georgia | 33.77086 | -84.38614 |
11 Wall St, NY, NY | 40.70707 | -74.01117 |
600 Peachtree Street NE, Atlanta, Georgia | 33.77086 | -84.38614 |
NA | NA | NA |
NA | NA | NA |
NA | NA | NA |
As shown above, duplicates will not be removed from your results by default. However, you can return only unique results by using unique_only = TRUE
. Note that passing unique_only = TRUE
to geocode()
or reverse_geocode()
will result in the original dataframe format (including column names) to be discarded in favor of the standard field names (ie. “address”, ‘lat, ’long’, etc.).
<- duplicate_addrs %>%
uniqueonly1 geocode(singlelineaddress, unique_only = TRUE)
#> Passing 2 addresses to the Nominatim single address geocoder
#> Query completed in: 2 seconds
address | lat | long |
---|---|---|
11 Wall St, NY, NY | 40.70707 | -74.01117 |
600 Peachtree Street NE, Atlanta, Georgia | 33.77086 | -84.38614 |
The geocode_combine()
function allows you to execute and combine the results of multiple geocoding queries. The queries are specified as a list of lists with the queries
parameter and are executed in the order provided. By default only addresses that are not found are passed to the next query, but this behavior can be toggled with the cascade
argument.
In the first example below, the US Census service is used for the first query while the Nominatim (“osm”) service is used for the second query. The global_params
argument passes the address
column from the input dataset to both queries.
<- tibble(
addresses_combine address = c('100 Wall Street NY, NY', 'Paris', 'Not An Address')
)
<- addresses_combine %>%
cascade_results1 geocode_combine(
queries = list(
list(method = 'census'),
list(method = 'osm')
),global_params = list(address = 'address')
)#>
#> Passing 3 addresses to the US Census batch geocoder
#> Query completed in: 0.2 seconds
#> Passing 2 addresses to the Nominatim single address geocoder
#> Query completed in: 2 seconds
address | lat | long | query |
---|---|---|---|
100 Wall Street NY, NY | 40.70516 | -74.007350 | census |
Paris | 48.85889 | 2.320041 | osm |
Not An Address | NA | NA |
If cascade
is set to FALSE then all addresses are attempted by each query regardless of if the address was found initially or not.
<- addresses_combine %>%
no_cascade_results1 geocode_combine(
queries = list(
list(method = 'census'),
list(method = 'osm')
),global_params = list(address = 'address'),
cascade = FALSE
)#>
#> Passing 3 addresses to the US Census batch geocoder
#> Query completed in: 0.3 seconds
#> Passing 3 addresses to the Nominatim single address geocoder
#> Query completed in: 3 seconds
address | lat | long | query |
---|---|---|---|
100 Wall Street NY, NY | 40.70516 | -74.007350 | census |
100 Wall Street NY, NY | 40.70522 | -74.006800 | osm |
Paris | NA | NA | census |
Paris | 48.85889 | 2.320041 | osm |
Not An Address | NA | NA | census |
Not An Address | NA | NA | osm |
Additionally, the results from each query can be returned in separate list items by setting return_list = TRUE
.
The limit
argument can be specified to allow multiple results (rows) per input if available. The maximum value for the limit
argument is often 100 for geocoding services. To use the default limit
value for the selected geocoding service you can use limit = NULL
which will prevent the limit parameter from being included in the query.
<- geo(
geo_limit c("Lima, Peru", "Cairo, Egypt"),
method = "osm",
limit = 3, full_results = TRUE
)#> Passing 2 addresses to the Nominatim single address geocoder
#> Query completed in: 2 seconds
glimpse(geo_limit)
#> Rows: 6
#> Columns: 13
#> $ address <chr> "Lima, Peru", "Lima, Peru", "Lima, Peru", "Cairo, Egypt", "Cairo, Egypt", "Cairo, Egypt"
#> $ lat <dbl> -11.96784, -12.03089, -11.95785, 30.04439, 30.08695, 30.22503
#> $ long <dbl> -77.01094, -77.09072, -77.04139, 31.23573, 31.96162, 31.69733
#> $ place_id <int> 128601425, 118029294, 128061402, 283020077, 216381765, 272362266
#> $ licence <chr> "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright", "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.o…
#> $ osm_type <chr> "way", "way", "way", "relation", "way", "way"
#> $ osm_id <int> 116948976, 71187508, 115715296, 5466227, 544272017, 914722135
#> $ boundingbox <list> <"-11.9678367", "-11.9672995", "-77.0117952", "-77.0109387">, <"-12.0308994", "-12.0304024", "-77.0911106", "-77.090276">, <"-11.9608…
#> $ display_name <chr> "Peru, San Juan de Lurigancho, Huascar, Lima, Lima Metropolitana, Lima, 15423, Perú", "Peru, San Martín de Porres, Lima, Lima Metropo…
#> $ class <chr> "highway", "highway", "highway", "place", "highway", "highway"
#> $ type <chr> "residential", "residential", "residential", "city", "motorway", "trunk"
#> $ importance <dbl> 0.3200000, 0.3200000, 0.3200000, 0.6960286, 0.1000000, 0.1000000
#> $ icon <chr> NA, NA, NA, "https://nominatim.openstreetmap.org/ui/mapicons//poi_place_city.p.20.png", NA, NA
To directly specify specific API parameters for a given method
you can use the custom_query
parameter. For example, the Nominatim (OSM) geocoder has a ‘polygon_geojson’ argument that can be used to return GeoJSON geometry content. To pass this parameter you can insert it with a named list using the custom_query
argument:
<- geo("Cairo, Egypt",
cairo_geo method = "osm", full_results = TRUE,
custom_query = list(polygon_geojson = 1), verbose = TRUE
)#>
#> Number of Unique Addresses: 1
#> Passing 1 address to the Nominatim single address geocoder
#>
#> Number of Unique Addresses: 1
#> Querying API URL: https://nominatim.openstreetmap.org/search
#> Passing the following parameters to the API:
#> q : "Cairo, Egypt"
#> limit : "1"
#> polygon_geojson : "1"
#> format : "json"
#> HTTP Status Code: 200
#> Query completed in: 0.2 seconds
#> Total query time (including sleep): 1 seconds
#>
#> Query completed in: 1 seconds
glimpse(cairo_geo)
#> Rows: 1
#> Columns: 15
#> $ address <chr> "Cairo, Egypt"
#> $ lat <dbl> 30.04439
#> $ long <dbl> 31.23573
#> $ place_id <int> 283020077
#> $ licence <chr> "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright"
#> $ osm_type <chr> "relation"
#> $ osm_id <int> 5466227
#> $ boundingbox <list> <"29.7483062", "30.3209168", "31.2200331", "31.9090054">
#> $ display_name <chr> "القاهرة, محافظة القاهرة, مصر"
#> $ class <chr> "place"
#> $ type <chr> "city"
#> $ importance <dbl> 0.6960286
#> $ icon <chr> "https://nominatim.openstreetmap.org/ui/mapicons//poi_place_city.p.20.png"
#> $ geojson.type <chr> "Polygon"
#> $ geojson.coordinates <list> <<array[1 x 119 x 2]>>
To test a query without sending any data to a geocoding service, you can use no_query = TRUE
(NA results are returned).
<- geo(c("Vancouver, Canada", "Las Vegas, NV"),
noquery1 no_query = TRUE,
method = "arcgis"
)#>
#> Number of Unique Addresses: 2
#> Passing 2 addresses to the ArcGIS single address geocoder
#>
#> Number of Unique Addresses: 1
#> Querying API URL: https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/findAddressCandidates
#> Passing the following parameters to the API:
#> SingleLine : "Vancouver, Canada"
#> maxLocations : "1"
#> f : "json"
#>
#> Number of Unique Addresses: 1
#> Querying API URL: https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/findAddressCandidates
#> Passing the following parameters to the API:
#> SingleLine : "Las Vegas, NV"
#> maxLocations : "1"
#> f : "json"
#> Query completed in: 0 seconds
address | lat | long |
---|---|---|
Vancouver, Canada | NA | NA |
Las Vegas, NV | NA | NA |
Additional usage notes for the geocode()
, geo()
, reverse_geocode()
, and reverse_geo()
functions:
quiet = TRUE
to silence console logs displayed by default (how many inputs were submitted, to what geocoding service, and the elapsed time).progress_bar
argument to control if a progress bar is displayed.verbose
, quiet
, and progress_bar
arguments can be set globally with options
. For instance options(tidygeocoder.verbose = TRUE)
will set verbose to TRUE
for all queries by default.api_options
or api_url
arguments. See ?geo
or ?reverse_geo
for details.min_time
argument will default to a value based on the maximum query rate of the given geocoding service. If you are using a local Nominatim server or have a commercial geocoder plan that has less restrictive usage limits then you can manually set min_time
to a lower value (such as 0).mode
argument can be used to specify whether the batch geocoding or single address/coordinate geocoding should be used. By default batch geocoding will be used if available when more than one address or coordinate is provided (with some noted exceptions for slower batch geocoding services).return_addresses
and return_coords
parameters (for forward and reverse geocoding respectively) can be used to toggle whether the input addresses or coordinates are returned. Setting these parameters to FALSE
is necessary to use batch geocoding if limit
is greater than 1 or NULL.reverse_geocode()
and geocode()
functions, the return_input
argument can be used to toggle if the input dataset is included in the returned dataframe.geocode()
and reverse_geocode()
functions. See #154 for details.These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.