Geocodes HW 4

Author

Coda Rayo-Garza

Let’s map some businesses in San Antonio!

For this project, I am using the AtoZ Database to download businesses by NAICS Codes. I used 445110 to identify grocery store locations in the zip codes 78207, 78201, and 78204. I used 624190 NAICS code to identify the addresses of WIC Clinics in the same zip codes.

Mapping WIC Clinics in San Antonio

Create A Basic Map

library(leaflet)
library(readr)
library(data.table)
wic <- read_csv("wic_west_side.csv", show_col_types = FALSE)
head(wic)
# A tibble: 6 × 227
  Source…¹ Date  Obsol…² Busin…³ Legal…⁴ Physi…⁵ Physi…⁶ Physi…⁷ Physi…⁸ Physi…⁹
  <chr>    <chr> <chr>   <chr>   <chr>   <chr>     <dbl> <chr>   <chr>   <chr>  
1 AtoZDat… 2/23… 8/23/2… Coalit… <NA>    1013 S…    1013 S       San Ja… St     
2 AtoZDat… 2/23… 8/23/2… San An… <NA>    1102 D…    1102 <NA>    Delgado St     
3 AtoZDat… 2/23… 8/23/2… Center… <NA>    1115 W…    1115 W       Martin  St     
4 AtoZDat… 2/23… 8/23/2… Profes… <NA>    1115 W…    1115 W       Martin  St     
5 AtoZDat… 2/23… 8/23/2… Linda … <NA>    1115 W…    1115 W       Martin  St     
6 AtoZDat… 2/23… 8/23/2… Adrian… Center… 1115 W…    1115 W       Martin  St     
# … with 217 more variables: `Physical Post Direction` <lgl>,
#   `Physical City` <chr>, `Physical State` <chr>, `Physical ZIP` <dbl>,
#   `Physical ZIP 4` <dbl>, `Key Executive Name` <chr>, `First Name` <chr>,
#   `Middle Initial` <chr>, `Last Name` <chr>, Title <chr>, Gender <chr>,
#   `Location Employee Size` <dbl>, `Corporate Employee Size` <dbl>,
#   `Revenue / Yr` <chr>, `Mailing Address` <chr>,
#   `Mailing Address Number` <dbl>, `Mailing Pre Direction` <chr>, …
wic<-wic[c(6, 12:14)]
names(wic)<-c("street", "city", "st", "zip")
head(wic)
# A tibble: 6 × 4
  street                city        st      zip
  <chr>                 <chr>       <chr> <dbl>
1 1013 S San Jacinto St San Antonio TX    78207
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)
wicresults<-cxy_geocode(wic,
                     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(wicresults, layer.name="WIC Services")

Create the Map Using Latitude/Longitude Coordinates

This code also uses the coordinate reference system ( ) and transformes it to ()

library(sf)
library(dplyr)

wicLL <- read_csv("wic_west_side.csv", show_col_types = FALSE)
resultsWIC <- st_as_sf(wicLL, coords=c("Longitude", "Latitude"), crs=4269,agr="constant")
resultsWIC.proj<-st_transform(resultsWIC,
                           crs = 2278)

mapview(resultsWIC.proj)

Mapping Grocery Stores

Step 1: Create the Basic Map

Load the libraries and import the data.

library(censusxy)
library(readr)
library(data.table)

grocerystores <- read_csv("grocerystores.csv", show_col_types = FALSE)
head(grocerystores)
# A tibble: 6 × 228
  Source…¹ Date  Obsol…² Busin…³ Legal…⁴ Physi…⁵ Physi…⁶ Physi…⁷ Physi…⁸ Physi…⁹
  <chr>    <chr> <chr>   <chr>   <chr>   <chr>     <dbl> <chr>   <chr>   <chr>  
1 AtoZDat… 02/2… 08/23/… Surlea… <NA>    1545 S…    1545 S       San Ma… <NA>   
2 AtoZDat… 02/2… 08/23/… Super … <NA>    4522 F…    4522 <NA>    Freder… Rd     
3 AtoZDat… 02/2… 08/23/… Pico D… <NA>    111 S …     111 S       Leona   St     
4 AtoZDat… 02/2… 08/23/… Target  Target… 4522 F…    4522 <NA>    Freder… Rd     
5 AtoZDat… 02/2… 08/23/… H-E-B … <NA>    3485 F…    3485 <NA>    Freder… Rd     
6 AtoZDat… 02/2… 08/23/… Balcon… <NA>    3309 H…    3309 <NA>    Hillcr… Dr     
# … with 218 more variables: `Physical Post Direction` <lgl>,
#   `Physical City` <chr>, `Physical State` <chr>, `Physical ZIP` <dbl>,
#   `Physical ZIP 4` <dbl>, `Key Executive Name` <chr>, `First Name` <chr>,
#   `Middle Initial` <chr>, `Last Name` <chr>, Title <chr>, Gender <chr>,
#   `Location Employee Size` <dbl>, `Corporate Employee Size` <dbl>,
#   `Revenue / Yr` <chr>, `Mailing Address` <chr>,
#   `Mailing Address Number` <dbl>, `Mailing Pre Direction` <chr>, …

Step 2: Data Prep

Select the appropriate columns, then select the column headers. Then, save the selected variables in a dataframe. Mine is called “results”.

gro1<-grocerystores[c(4,6, 12:14)]
names(gro1)<-c("business name","street", "city", "st", "zip" )
head(gro1)
# A tibble: 6 × 5
  `business name`     street                 city        st      zip
  <chr>               <chr>                  <chr>       <chr> <dbl>
1 Surlean Foods       1545 S San Marcos      San Antonio TX    78207
2 Super Target        4522 Fredericksburg Rd San Antonio TX    78201
3 Pico De Gallo       111 S Leona St         San Antonio TX    78207
4 Target              4522 Fredericksburg Rd San Antonio TX    78201
5 H-E-B LP            3485 Fredericksburg Rd San Antonio TX    78201
6 Balcones Food Store 3309 Hillcrest Dr      San Antonio TX    78201
library(censusxy)
results<-cxy_geocode(gro1,
                     street = "street",
                     city = "city",
                     state ="st",
                     zip = "zip",
                     class="sf",
                     output = "simple")

Step 3: Create Interactive Map

Use mapview to create an interactive map. I modified my csv file to be able to include legal business name.

library(mapview)
mapview(results, layer.name="Grocery Stores")

Using Latitude/Longitude to Create Map

library(sf)
library(dplyr)

gcLL <- st_as_sf(grocerystores, coords=c("Longitude", "Latitude"), crs=4269,agr="constant")

resultsgs.proj<-st_transform(gcLL,
                           crs = 2278)

mapview(resultsgs.proj)

Extra Extra!

The challenge: Create one map with the different layers. I’ve renamed my layers to identify those that use lat/long by adding “LL” to the end of the map name. Basically, I used this code below to combine the maps, then created them as layers. Then I used col. regions to show them with different colors because it’s harder to see them when they’re all the default blue-ish color.

mapview(list(wicresults, resultsWIC.proj,results,resultsgs.proj),
        layer.name = c("WIC Locations", "WIC LL", "Grocery Stores", "Grocery StoresLL"), col.regions=list("red","blue", "green", "orange"),col=list("red","blue", "green", "orange"))