library(tidycensus, quietly = T)
library(sf, quietly = T)
library(dplyr, quietly = T)

Read in Bexar county tracts

sa_acs<-get_acs(geography = "tract",
                state = "TX",
                county = "Bexar",
                year = 2017,
                variables = c("DP05_0001E", "DP03_0119PE"),
                geometry = T, 
                output = "wide")

#create a county FIPS code - 5 digit
sa_acs$county<-substr(sa_acs$GEOID, 1, 5)

#rename variables and filter missing cases
sa_acs2<-sa_acs %>%
  mutate(totpop = DP05_0001E, ppov=DP03_0119PE) %>%
  na.omit()

# mydat<-sf::st_read("~/OneDrive - University of Texas at San Antonio/classes/gis_classwork/bexarpolygon.shp")
# plot(mydat)

find coordinate system of current map

This shows that the current coordinate reference system is NAD83

st_crs(sa_acs2)
## Coordinate Reference System:
##   User input: NAD83 
##   wkt:
## GEOGCRS["NAD83",
##     DATUM["North American Datum 1983",
##         ELLIPSOID["GRS 1980",6378137,298.257222101,
##             LENGTHUNIT["metre",1]]],
##     PRIMEM["Greenwich",0,
##         ANGLEUNIT["degree",0.0174532925199433]],
##     CS[ellipsoidal,2],
##         AXIS["latitude",north,
##             ORDER[1],
##             ANGLEUNIT["degree",0.0174532925199433]],
##         AXIS["longitude",east,
##             ORDER[2],
##             ANGLEUNIT["degree",0.0174532925199433]],
##     ID["EPSG",4269]]

Un projected distance calculation

The distance between the centroids of the two tracts is 20,787.28 meters. This distance is not interpretable because it is not mapped in its actual location on the earthโ€™s surface.

new_sa<-st_transform(sa_acs2, crs = 2278)

#Extract two tracts
twtr<-new_sa%>%
  filter(GEOID %in% c(48029110100, 48029181820))

twtr<-sa_acs2%>%
  filter(GEOID %in% c(48029110100, 48029181820))

# get centroid coordinates for two tracts
tr_co<-st_centroid(twtr)

#Measure feet apart
st_distance(tr_co)
## Units: [m]
##          [,1]     [,2]
## [1,]     0.00 20787.28
## [2,] 20787.28     0.00

Projected distance calculation

The distance between the centroids of the two tracts in its correct projection is 68,091.77 ft. We see the unit of measurement has changed. The actual difference in the measurement of the unprojected and projected measurements is about 100 ft, but running spatial analyses with the incorrect map projections can yield false results. Something may appear to be closer or farther than it actually is when using unprojected spatial data.

new_sa<-st_transform(sa_acs2, crs = 2278)

#Extract two tracts
twtr<-new_sa%>%
  filter(GEOID %in% c(48029110100, 48029181820))

# get centroid coordinates for two tracts
tr_co<-st_centroid(twtr)

#Measure feet apart
st_distance(tr_co)
## Units: [US_survey_foot]
##          [,1]     [,2]
## [1,]     0.00 68091.77
## [2,] 68091.77     0.00