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://raw.githubusercontent.com/coreysparks/data/master/wic_west_side.csv"))
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, address = street, city = city, state =st, zip = zip, output = "sf")
## Warning: Expected 2 pieces. Missing pieces filled with `NA` in 13 rows [5, 6, 8,
## 12, 20, 26, 40, 46, 48, 51, 54, 55, 58].
mapview::mapview(results, layer.name="WIC Services")
We can write the results out to a shapefile now
library(sf)
## Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
st_write(results,dsn="~/.", layer="westside_wic", driver = "ESRI Shapefile",delete_layer = T, update=T)
## Warning in abbreviate_shapefile_names(obj): Field names abbreviated for ESRI
## Shapefile driver
## Deleting layer `westside_wic' using driver `ESRI Shapefile'
## Updating layer `westside_wic' to data source `C:\Users\ozd504' using driver `ESRI Shapefile'
## Writing 70 features with 7 fields and geometry type Point.