In this example, we will geocode addresses using the Census Bureau’s batch geocoding service. The work will be done by the censusxy package.
We will use the from the CSV created in the Lab 3 exercise.
addr<-read.csv(url("https://github.com/coreysparks/data/blob/master/wic_west_side.csv?raw=true"))
addr<-addr[c(6, 12:14)]
names(addr)<-c("street", "city", "st", "zip")
head(addr)
## street city st zip
## 1 1039 W Hildebrand Ave San Antonio TX 78201
## 2 1102 Delgado St San Antonio TX 78207
## 3 1115 W Martin St San Antonio TX 78207
## 4 1115 W Martin St San Antonio TX 78207
## 5 1115 W Martin St San Antonio TX 78207
## 6 1115 W Martin St San Antonio TX 78207
library(censusxy)
results<-cxy_geocode(addr,
street = "street",
city = "city",
state ="st",
zip = "zip",
class="sf",
output = "simple")
## Loading required namespace: sf
## 38 rows removed to create an sf object. These were addresses that the geocoder could not match.
addr2<-read.csv("C:/Users/drayr/OneDrive/Desktop/DEM Spring 2022/DEM 7093/HW5/groc_west_side.csv")
addr2<-addr2[c(6, 12:14)]
names(addr2)<-c("street", "city", "st", "zip")
head(addr2)
## street city st zip
## 1 1314 Fredericksburg Rd San Antonio TX 78201
## 2 1545 S San Marcos San Antonio TX 78207
## 3 4522 Fredericksburg Rd San Antonio TX 78201
## 4 111 S Leona St San Antonio TX 78207
## 5 3485 Fredericksburg Rd San Antonio TX 78201
## 6 1502 W Hildebrand Ave San Antonio TX 78201
library(censusxy)
results22<-cxy_geocode(addr2,
street = "street",
city = "city",
state ="st",
zip = "zip",
class="sf",
output = "simple")
## 4 rows removed to create an sf object. These were addresses that the geocoder could not match.
library(mapview)
mapview(results,col.regions="red", layer.name="WIC Services" )+
mapview(results22,col.regions="blue", layer.name="Grocery Stores" )
We can write the results out to a shapefile now
#library(sf)
#st_write(results,dsn="~/OneDrive - University of Texas at San Antonio/classes/gis_classwork/", layer="westside_wic", driver = "ESRI Shapefile",delete_layer = T, append=T)