*These are updates. Original version from Corey Sparks, Ph.D. can be found here.

install.packages("censusxy")

Setup

In this example, we will geocode addresses using the Census Bureau’s batch geocoding service. The work will be done by the censusxy package.

Read in the address data

We will use the from the CSV created in the Lab 3 exercise.

library(readr)
library(data.table)
addr<-read.csv("https://raw.githubusercontent.com/jkwolf21/2023-DEM-5093-7093/main/Code/wic_west_side.csv") #Note this is the RAW data* 
head(addr)
addr<-addr[c(6, 12:14)]
names(addr)<-c("street", "city", "st", "zip")
head(addr)
library(censusxy)
results<-cxy_geocode(addr,
                     street = "street",
                     city = "city",
                     state ="st",
                     zip = "zip",
                     class="sf",
                     output = "simple")
## 5 rows removed to create an sf object. These were addresses that the geocoder could not match.

Basic interactive map of the points

library(mapview)
mapview(results, layer.name="WIC Services")

Title: Spatial Geocode Data for WIC Services in the Westside of San Antonio

Author: Brandon Flores

Source: AtoZDatabases, (2023)

Save the results if you want

We can write the results out to a shapefile now

library(sf)
st_write(results,dsn="C:/Users/.../GIS Class/Lab 3", layer="westside_wic", driver = "ESRI Shapefile",delete_layer = T, append=T)

Longitude and Latitude

From Dr. Sparks’ RPubs

library(sf)
library(dplyr)
addrLL<-read.csv(url("https://raw.githubusercontent.com/jkwolf21/2023-DEM-5093-7093/main/Code/wic_west_side.csv"))
resultsWIC <- st_as_sf(addrLL, coords=c("Longitude", "Latitude"), crs=4269,agr="constant")
resultsWIC.proj<-st_transform(resultsWIC,
                           crs = 2278)
mapview(resultsWIC.proj)

Title: Spatial Longitudinal/Latitude Data for WIC Services in the Westside of San Antonio

Author: Brandon Flores

Source: AtoZDatabases, (2023)

library(readr)
library(data.table)
addr1<-read.csv("C:/Users/BTP/Documents/GIS CLASS/Lab 3 HW 4/gstores.csv")

head(addr1)
addr2<-addr1[c(6, 12:14)]
names(addr1)<-c("street", "city", "st", "zip")
head(addr1)
library(censusxy)
results<-cxy_geocode(addr,
                     street = "street",
                     city = "city",
                     state ="st",
                     zip = "zip",
                     class="sf",
                     output = "simple")
## 5 rows removed to create an sf object. These were addresses that the geocoder could not match.
library(mapview)
mapview(results, layer.name="Grocery Stores")

Title: Spatial Geocode Data for Grocery Stores in the Westside of San Antonio

Author: Brandon Flores

Source: AtoZDatabases, (2023)

Longitude and Latitude

library(sf)
library(dplyr)
addrLL2<-read.csv("C:/Users/BTP/Documents/GIS CLASS/Lab 3 HW 4/gstores.csv")
resultsGrocery <- st_as_sf(addrLL2, coords=c("Longitude", "Latitude"), crs=4269,agr="constant")
resultsGrocery.proj<-st_transform(resultsGrocery,
                           crs = 2278)
mapview(resultsGrocery.proj)

Title: Spatial Longitudinal/Latitude Data for Grocery Stores in the Westside of San Antonio

Author: Brandon Flores

Source: AtoZDatabases, (2023)

Map Overlay

mapview(list(results, resultsWIC.proj,results,resultsGrocery.proj),
        layer.name = c("WIC Locations", "WIC LL", "Grocery Stores", "Grocery StoresLL"), col.regions=list("brown","black", "blue", "red"),col=list("brown","black", "blue", "red"))

Title: Overlay of 4 different types of Spatial Data for WIC Services & Grocery Stores in the Westside of San Antonio

Author: Brandon Flores

Source: AtoZDatabases, (2023)