Knowing how to do geocoding, the process of matching an address with coordinates, is a valuable skill. There are numerous ways of doing it and here is one of them. This R script will:
- Read a spreadsheet with addresses
- Process the data
- Geocode the addresses using the Nominatim service
- Create a simple map of the geocoded addresses
- Write the result to a GeoPackage file
# Load libraries
library(mapview)
library(readxl)
library(sf)
library(tidygeocoder)
# Set working directory
setwd("C:\\R\\")
# Read excel file with address table
address_table <- read_excel("address_table.xlsx")
# Concatenate fields to create full address for Nominatim
address_table$conc_address <- paste(address_table$Address, address_table$Postal_code, address_table$City, address_table$Country, sep = ',')
# Geocode addresses
address_table_geocoded <- address_table %>%
geocode(conc_address, method = 'osm', lat = latitude , long = longitude)
# Create point geometries from coordinates
addresses_points = st_as_sf(address_table_geocoded, coords = c("longitude", "latitude"), crs = 4326)
# View geocoded addresses on a map
mapview(addresses_points)
# Write points to a GeoPackage file
st_write(addresses_points, "Address_points.gpkg")
This will geocode the addresses and save as points in a GeoPackage file.