Prepare the environment

Reset R's brain

#----------------------------------------------------------
# Reset R's brain
#----------------------------------------------------------
rm(list=ls())

Set working directory

#-------------------------------------------------
# Make sure the working directory is set to the desired location
#-------------------------------------------------
getwd()

#-------------------------------------------------
# If the working directory is incorrect
# Uncomment the following command and change the directory path before execution
#-------------------------------------------------
# setwd("<path_to_dir>")

Load rtry package

#-------------------------------------------------
# Make sure the rtry package is installed before loading
#-------------------------------------------------
library(rtry)
packageVersion("rtry")

#-------------------------------------------------
# Check if the additional packages are installed
# If not, install the required packages before loading
#-------------------------------------------------
if (!require(dplyr)) install.packages('dplyr')
library(dplyr)

Geocoding using rtry

Load the locations from a .csv file

path_to_data <- system.file("testdata", "data_locations.csv", package = "rtry")
path_to_data
input_locations <- rtry_import(path_to_data, separator = ",", encoding = "UTF-8", quote = "\"")

head(input_locations)

Extract and combine the location and country names

The address format required: <location>, <country>

input_addresses <- paste(input_locations$Location, input_locations$Country, sep = ", ")

head(input_addresses)

Apply rtry_geocoding

Please remember to change the email address into your own email address.

Note that for location which is unknown to OpenStreetMap, the resulting latitude and longitude will remain or marked as NA.

# Prepare counter for printed progress messages
counter <- 1
output_coordinates <- NULL # somethings received error messages 'no object found'

# Use lapply to apply function to the list of addresses
output_coordinates <- lapply(input_addresses, function(address) {
  # Calling the Nominatim OpenStreetMap API
  # Please change the email address into your own email address
  geocode_output <- rtry_geocoding(address, email = "john.doe@example.com")

  # No heavy uses (an absolute maximum of 1 request per second)
  # Here set to 2 seconds between each search
  Sys.sleep(2)

  # Print message in console to see the progress
  message("Geocoding ", counter, "/", nrow(input_locations), " completed.")
  counter <<- counter + 1

  # Return data.frame with the input address, output of the rtry_geocoding function
  return(data.frame(address = address, geocode_output))
}) %>%
  # Stack the list output into data.frame
  bind_rows() %>% data.frame()

# View data
head(output_coordinates)

Substitute the coordinates into the input list

# Add the output coordinates to the corresponding columns in the input data
input_locations$Latitude <- output_coordinates$lat
input_locations$Longitude <- output_coordinates$lon

# If necessary, re-arrange the columns
input_locations <- rtry_select_col(input_locations, "Country code", Country, Location, Latitude, Longitude, showOverview = FALSE)

# View data
head(input_locations)

Export into .csv

output_file = file.path(tempdir(), "locations_to_coordinates.csv")
rtry_export(input_locations, output_file)

Reverse geocoding using rtry

Load the coordinates from a .csv file

path_to_data <- system.file("testdata", "data_coordinates.csv", package = "rtry")
path_to_data
input_coordinates <- rtry_import(path_to_data, separator = ",", encoding = "UTF-8", quote = "\"")

head(input_coordinates)

Extract the latitude and longitude

input_lat_lon <- data.frame(lat = input_coordinates$Latitude, lon = input_coordinates$Longitude)

head(input_lat_lon)

Apply rtry_revgeocoding

Please remember to change the email address into your own email address.

Note that for some coordinates, OpenStreetMap might not have the town/city information, in such case, the Location column will be marked as NA.

# Prepare counter for printed progress messages
counter <- 1
output_locations <- NULL # somethings received error messages 'no object found'

# Use apply to apply function to the data.frame that contains the coordinates
# Please change the email address to your own email address
output_locations <- apply(input_lat_lon, 1, function(lat_lon) {
  # Calling the Nominatim OpenStreetMap API
  rev_geocode_output <- rtry_revgeocoding(lat_lon, email = "john.doe@example.com")

  # No heavy uses (an absolute maximum of 1 request per second)
  # Here set to 2 seconds between each search
  Sys.sleep(2)

  # Print message in console to see the progress
  message("Reverse Geocoding ", counter, "/", length(input_lat_lon$lat), " completed.")
  counter <<- counter + 1

  # Return data.frame with the input coordinates, output of the rtry_revgeocoding function
  return(data.frame(lat = lat_lon[1], lon = lat_lon[2], rev_geocode_output))
}) %>%
  # Stack the list output into data.frame
  bind_rows() %>% data.frame()

# View data
head(output_locations)

Substitute the location information

Substitute the country_code and country into the corresponding columns of the input list, while the location information is extracted from either town or city.

# Add the output location information to the corresponding columns in the input data
input_coordinates$'Country code' <- output_locations$country_code
input_coordinates$Country <- output_locations$country
input_coordinates$Location <- ifelse(!is.na(output_locations$town), output_locations$town, output_locations$city)

# If necessary, re-arrange the columns
input_coordinates <- rtry_select_col(input_coordinates, Latitude, Longitude, "Country code", Country, Location, showOverview = FALSE)

# View data
head(input_coordinates)

Export into .csv

output_file = file.path(tempdir(), "coordinates_to_locations.csv")
rtry_export(input_coordinates, output_file)


MPI-BGC-Functional-Biogeography/rtry documentation built on Aug. 26, 2023, 7 a.m.