You will most likely need the following:
library(tidyverse)
library(maptools)
library(maps)
library(rgdal)
library(sp)
library(sf)
library(raster)
library(dplyr)
library(rmapshaper)
Download 2016 CropScape data for Cache County, Utah. Remember that the CropScape metadata describing the physical meaning of numbers in the dataset can be found here.
cdl <- raster("/Users/glendonvansandt/Desktop/NR6950 - R/Week 8/CDL_2016_49005-1/CDL_2016_49005.tif")
plot(cdl)
Download US Census tracts for the state of Utah found here.
In the large folder, download the CensusTracts2010_shp.zip
folder. Open this folder on your machine, unzip, and dump the contents
of the folder in your data folder, then finally, load the
Census tract data in R.
ct <- shapefile("/Users/glendonvansandt/Desktop/NR6950 - R/Week 8/CensusTracts2010/CensusTracts2010.shp")
plot(ct)
Create a new shapefile that includes ONLY Census tracts Cache County
using base R or spdplyr. Hint: You will need to look at the
STATEFP10 (state FIPS code) and COUNTYFP10
(county FIPS code) columns. The state FIPS code for Utah is
49. The county FIPS codes are the remaining three digits in
this column. This
website provides information on the full six-digit FIPS codes for a
particular county.
head(ct)
## STATEFP10 COUNTYFP10 TRACTCE10 GEOID10 NAME10 FUNCSTAT10 INTPTLAT10
## 1 49 003 960601 49003960601 9606.01 S +41.5159999
## 2 49 003 960602 49003960602 9606.02 S +41.5292534
## 3 49 003 960702 49003960702 9607.02 S +41.4966363
## 4 49 003 960400 49003960400 9604 S +41.5599748
## 5 49 003 960300 49003960300 9603 S +41.7027473
## 6 49 003 960200 49003960200 9602 S +41.7070478
## INTPTLON10 LOGRECNO AREALAND AREAWATR POP100 HU100 P0020001 P0020002
## 1 -112.0089180 0000009 2185421 0 3720 1395 3720 396
## 2 -111.9843889 0000010 31111836 0 2429 793 2429 109
## 3 -112.0069151 0000012 3141945 0 2281 861 2281 292
## 4 -112.2854779 0000007 657894066 101858980 4872 1632 4872 289
## 5 -112.1516054 0000006 35869732 0 8186 2748 8186 927
## 6 -112.0582397 0000005 259632270 547505 6901 2303 6901 406
## P0020003 P0020004 P0020005 P0020006 P0020007 P0020008 P0020009 P0020010 MTFCC
## 1 3324 3262 3188 17 27 15 6 9 G5020
## 2 2320 2276 2245 3 16 8 4 0 G5020
## 3 1989 1947 1887 11 28 18 3 0 G5020
## 4 4583 4531 4437 20 13 51 2 8 G5020
## 5 7259 7158 6959 22 34 120 14 9 G5020
## 6 6495 6416 6289 16 18 74 18 1 G5020
## P0010011 P0010012 P0010013 P0010014 SqMiles SHAPE_Leng SHAPE_Area
## 1 11 30 17 0 0 6709.768 2184057
## 2 9 23 12 0 0 31613.347 31092117
## 3 2 24 16 0 0 7088.986 3139977
## 4 2 26 26 0 0 147127.817 759360502
## 5 8 43 45 0 0 43217.387 35849141
## 6 9 34 27 0 0 123915.036 260023433
cc_ct <- ct[ct$STATEFP10 == 49 & ct$COUNTYFP10 == "005",]
class(cc_ct)
## [1] "SpatialPolygonsDataFrame"
## attr(,"package")
## [1] "sp"
head(cc_ct@data)
## STATEFP10 COUNTYFP10 TRACTCE10 GEOID10 NAME10 FUNCSTAT10 INTPTLAT10
## 254 49 005 001001 49005001001 10.01 S +41.7204166
## 255 49 005 001201 49005001201 12.01 S +41.6712829
## 256 49 005 001401 49005001401 14.01 S +41.6293361
## 257 49 005 001202 49005001202 12.02 S +41.6662824
## 258 49 005 001402 49005001402 14.02 S +41.6361325
## 259 49 005 001002 49005001002 10.02 S +41.7234385
## INTPTLON10 LOGRECNO AREALAND AREAWATR POP100 HU100 P0020001 P0020002
## 254 -111.8535734 0000035 1690874 0 3026 869 3026 814
## 255 -111.8145562 0000039 16605498 0 4955 1413 4955 159
## 256 -111.8266522 0000042 15436324 0 4096 1158 4096 583
## 257 -111.8556675 0000040 23240042 70433 5387 1450 5387 562
## 258 -111.8749803 0000043 13998256 13847 3665 1086 3665 734
## 259 -111.8452269 0000036 4056983 0 4560 1848 4560 1062
## P0020003 P0020004 P0020005 P0020006 P0020007 P0020008 P0020009 P0020010
## 254 2212 2165 2014 34 10 91 13 3
## 255 4796 4736 4672 7 18 29 9 1
## 256 3513 3470 3389 8 12 25 36 0
## 257 4825 4762 4689 8 29 25 10 1
## 258 2931 2897 2842 5 20 9 21 0
## 259 3498 3443 3139 46 50 168 28 12
## MTFCC P0010011 P0010012 P0010013 P0010014 SqMiles SHAPE_Leng SHAPE_Area
## 254 G5020 10 14 23 0 0 6244.119 1689730
## 255 G5020 8 25 12 0 0 25342.826 16594099
## 256 G5020 0 32 17 0 0 19677.560 15425777
## 257 G5020 10 40 15 0 0 22485.812 23294748
## 258 G5020 8 22 0 0 0 19790.003 14002734
## 259 G5020 19 25 11 0 0 12260.874 4054227
plot(cc_ct)
ct_1 <- readShapePoly("/Users/glendonvansandt/Desktop/NR6950 - R/Week 8/CensusTracts2010/CensusTracts2010.shp")
## Warning: shapelib support is provided by GDAL through the sf and terra packages
## among others
cc_ct <- spTransform(cc_ct, CRSobj = projection(cdl))
## Warning: PROJ support is provided by the sf and terra packages among others
plot(cdl)
plot(cc_ct, add = TRUE, col = "transparent", border = "black")
#This one was tricky. I had to play around with the coding for a while but finally got it to work.
print(cc_ct)
## class : SpatialPolygonsDataFrame
## features : 26
## extent : -1323599, -1267690, 2148454, 2223364 (xmin, xmax, ymin, ymax)
## crs : +proj=aea +lat_0=23 +lon_0=-96 +lat_1=29.5 +lat_2=45.5 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs
## variables : 31
## names : STATEFP10, COUNTYFP10, TRACTCE10, GEOID10, NAME10, FUNCSTAT10, INTPTLAT10, INTPTLON10, LOGRECNO, AREALAND, AREAWATR, POP100, HU100, P0020001, P0020002, ...
## min values : 49, 005, 000101, 49005000101, 1.01, S, +41.5533919, -111.6452526, 0000020, 126980514, 0, 105, 496, 105, 1, ...
## max values : 49, 005, 980100, 49005980100, 9801, S, +41.9677820, -112.0086429, 0000045, 7301843, 70433, 7492, 2321, 7492, 1272, ...
head(cc_ct, n=26)
## STATEFP10 COUNTYFP10 TRACTCE10 GEOID10 NAME10 FUNCSTAT10 INTPTLAT10
## 254 49 005 001001 49005001001 10.01 S +41.7204166
## 255 49 005 001201 49005001201 12.01 S +41.6712829
## 256 49 005 001401 49005001401 14.01 S +41.6293361
## 257 49 005 001202 49005001202 12.02 S +41.6662824
## 258 49 005 001402 49005001402 14.02 S +41.6361325
## 259 49 005 001002 49005001002 10.02 S +41.7234385
## 260 49 005 000501 49005000501 5.01 S +41.7419145
## 261 49 005 000502 49005000502 5.02 S +41.7604905
## 262 49 005 000702 49005000702 7.02 S +41.7481472
## 263 49 005 000402 49005000402 4.02 S +41.7904829
## 264 49 005 000202 49005000202 2.02 S +41.8429042
## 265 49 005 000101 49005000101 1.01 S +41.9409746
## 266 49 005 001300 49005001300 13 S +41.6521492
## 267 49 005 980100 49005980100 9801 S +41.6749816
## 268 49 005 000401 49005000401 4.01 S +41.7668412
## 269 49 005 000900 49005000900 9 S +41.7302760
## 270 49 005 000800 49005000800 8 S +41.7389185
## 271 49 005 001102 49005001102 11.02 S +41.7259264
## 272 49 005 000600 49005000600 6 S +41.7510412
## 273 49 005 001101 49005001101 11.01 S +41.7091627
## 274 49 005 000701 49005000701 7.01 S +41.7520991
## 275 49 005 000403 49005000403 4.03 S +41.7948654
## 276 49 005 000300 49005000300 3 S +41.8043700
## 277 49 005 000102 49005000102 1.02 S +41.9677820
## 278 49 005 000201 49005000201 2.01 S +41.8500551
## 279 49 005 001500 49005001500 15 S +41.5533919
## INTPTLON10 LOGRECNO AREALAND AREAWATR POP100 HU100 P0020001 P0020002
## 254 -111.8535734 0000035 1690874 0 3026 869 3026 814
## 255 -111.8145562 0000039 16605498 0 4955 1413 4955 159
## 256 -111.8266522 0000042 15436324 0 4096 1158 4096 583
## 257 -111.8556675 0000040 23240042 70433 5387 1450 5387 562
## 258 -111.8749803 0000043 13998256 13847 3665 1086 3665 734
## 259 -111.8452269 0000036 4056983 0 4560 1848 4560 1062
## 260 -111.8451684 0000028 2721767 0 5128 2028 5128 1153
## 261 -111.8449036 0000029 5095127 0 5231 1933 5231 793
## 262 -111.8098462 0000032 1630586 0 2720 500 2720 240
## 263 -111.7872496 0000026 15839550 0 3118 945 3118 48
## 264 -111.8032647 0000023 32739463 0 6953 2129 6953 410
## 265 -111.8218062 0000020 154954217 637677 4599 1493 4599 281
## 266 -111.9698969 0000041 196784353 571431 6117 1785 6117 181
## 267 -111.6452526 0000045 1838580222 1045466 105 496 105 1
## 268 -111.8197363 0000025 5626494 21402 3338 1224 3338 328
## 269 -111.8240408 0000034 1694692 0 3374 1383 3374 376
## 270 -111.8272143 0000033 1767849 0 4795 2132 4795 440
## 271 -111.7996665 0000038 6177769 3564 4230 1406 4230 135
## 272 -111.8241952 0000030 2434351 0 7068 2321 7068 457
## 273 -111.8181470 0000037 7301843 0 5596 1988 5596 349
## 274 -111.7932222 0000031 5053931 32944 4520 1405 4520 108
## 275 -111.8434213 0000027 23743784 0 5754 1621 5754 324
## 276 -111.9496294 0000024 256174650 15849834 7492 2160 7492 1272
## 277 -112.0086429 0000021 215897148 1060792 1563 541 1563 131
## 278 -111.8559897 0000022 40624713 200124 3228 1070 3228 212
## 279 -111.8596173 0000044 126980514 1757193 2038 640 2038 63
## P0020003 P0020004 P0020005 P0020006 P0020007 P0020008 P0020009 P0020010
## 254 2212 2165 2014 34 10 91 13 3
## 255 4796 4736 4672 7 18 29 9 1
## 256 3513 3470 3389 8 12 25 36 0
## 257 4825 4762 4689 8 29 25 10 1
## 258 2931 2897 2842 5 20 9 21 0
## 259 3498 3443 3139 46 50 168 28 12
## 260 3975 3914 3706 48 44 92 12 12
## 261 4438 4326 3981 82 63 145 52 3
## 262 2480 2416 2017 42 20 320 9 8
## 263 3070 3037 2961 11 3 58 2 2
## 264 6543 6471 6393 31 5 36 4 2
## 265 4318 4271 4210 18 28 9 3 3
## 266 5936 5859 5809 13 14 13 10 0
## 267 104 104 104 0 0 0 0 0
## 268 3010 2961 2824 11 20 80 21 5
## 269 2998 2950 2824 10 27 63 19 7
## 270 4355 4288 4038 38 47 142 20 3
## 271 4095 4032 3877 12 3 130 8 2
## 272 6611 6515 6158 67 33 204 42 11
## 273 5247 5177 5083 7 6 61 15 5
## 274 4412 4366 4140 22 11 176 15 2
## 275 5430 5370 5179 12 11 129 38 1
## 276 6220 6137 5957 37 50 68 14 11
## 277 1432 1430 1422 0 1 3 0 4
## 278 3016 2965 2918 24 5 14 2 2
## 279 1975 1956 1937 3 1 6 8 1
## MTFCC P0010011 P0010012 P0010013 P0010014 SqMiles SHAPE_Leng SHAPE_Area
## 254 G5020 10 14 23 0 0 6244.119 1689730
## 255 G5020 8 25 12 0 0 25342.826 16594099
## 256 G5020 0 32 17 0 0 19677.560 15425777
## 257 G5020 10 40 15 0 0 22485.812 23294748
## 258 G5020 8 22 0 0 0 19790.003 14002734
## 259 G5020 19 25 11 0 0 12260.874 4054227
## 260 G5020 9 20 21 0 0 7386.913 2719916
## 261 G5020 15 38 39 0 0 11843.230 5091679
## 262 G5020 4 1 36 0 0 5808.452 1629459
## 263 G5020 4 1 22 0 0 22978.460 15828550
## 264 G5020 13 14 31 0 0 31111.450 32716857
## 265 G5020 9 16 20 0 0 65673.785 155485220
## 266 G5020 5 30 14 0 0 86270.747 197229689
## 267 G5020 0 0 0 0 0 252512.886 1838290401
## 268 G5020 15 11 14 0 0 9715.263 5644018
## 269 G5020 10 15 14 0 0 6759.033 1693532
## 270 G5020 7 16 25 0 0 5954.697 1766642
## 271 G5020 14 11 30 0 0 14833.768 6177070
## 272 G5020 15 16 52 0 0 6259.515 2432687
## 273 G5020 11 13 32 0 0 15497.090 7296833
## 274 G5020 6 11 26 0 0 14383.847 5083356
## 275 G5020 14 9 22 0 0 22150.252 23727666
## 276 G5020 11 43 20 0 0 108156.852 271847825
## 277 G5020 0 10 1 0 0 73061.804 216822759
## 278 G5020 9 26 11 0 0 32117.697 40797281
## 279 G5020 3 13 1 0 0 55939.427 128650827
#By using the print code, we can see that there are 26 features. I then further explored this by using the head command. This allowed me to easily examine each census tract and their corresponding data.
class(ct)
## [1] "SpatialPolygonsDataFrame"
## attr(,"package")
## [1] "sp"
poly_ct <- aggregate(cc_ct, by = "TRACTCE10", FUN = function(x)x[1])
area <- area(poly_ct)
sum(area)
## [1] 3038122970
#I started by using the aggregate code to dissolve the cache county census tracts into one single polygon. I used "TRACTCE10" as an identifier since it seemed to be unique for each tract. I then used the area function to easily show the total area for each tract. Finally, i used the sum area to add all the individual areas together.
111)? Use the functional approach shown in the tutorial.
Note that the extract function make take awhile to run. It if takes too
long, use velox::extract().crs(cdl)
## Coordinate Reference System:
## Deprecated Proj.4 representation:
## +proj=aea +lat_0=23 +lon_0=-96 +lat_1=29.5 +lat_2=45.5 +x_0=0 +y_0=0
## +datum=NAD83 +units=m +no_defs
## WKT2 2019 representation:
## PROJCRS["unknown",
## BASEGEOGCRS["unknown",
## DATUM["North American Datum 1983",
## ELLIPSOID["GRS 1980",6378137,298.257222101,
## LENGTHUNIT["metre",1]],
## ID["EPSG",6269]],
## PRIMEM["Greenwich",0,
## ANGLEUNIT["degree",0.0174532925199433],
## ID["EPSG",8901]]],
## CONVERSION["unknown",
## METHOD["Albers Equal Area",
## ID["EPSG",9822]],
## PARAMETER["Latitude of false origin",23,
## ANGLEUNIT["degree",0.0174532925199433],
## ID["EPSG",8821]],
## PARAMETER["Longitude of false origin",-96,
## ANGLEUNIT["degree",0.0174532925199433],
## ID["EPSG",8822]],
## PARAMETER["Latitude of 1st standard parallel",29.5,
## ANGLEUNIT["degree",0.0174532925199433],
## ID["EPSG",8823]],
## PARAMETER["Latitude of 2nd standard parallel",45.5,
## ANGLEUNIT["degree",0.0174532925199433],
## ID["EPSG",8824]],
## PARAMETER["Easting at false origin",0,
## LENGTHUNIT["metre",1],
## ID["EPSG",8826]],
## PARAMETER["Northing at false origin",0,
## LENGTHUNIT["metre",1],
## ID["EPSG",8827]]],
## CS[Cartesian,2],
## AXIS["(E)",east,
## ORDER[1],
## LENGTHUNIT["metre",1,
## ID["EPSG",9001]]],
## AXIS["(N)",north,
## ORDER[2],
## LENGTHUNIT["metre",1,
## ID["EPSG",9001]]]]
cc_ct_new <- spTransform(cc_ct, CRS("+proj=aea +lat_0=23 +lon_0=-96 +lat_1=29.5 +lat_2=45.5 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs"))
## Warning: PROJ support is provided by the sf and terra packages among others
proj4string(cc_ct_new)
## [1] "+proj=aea +lat_0=23 +lon_0=-96 +lat_1=29.5 +lat_2=45.5 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs"
proj4string(cdl)
## [1] "+proj=aea +lat_0=23 +lon_0=-96 +lat_1=29.5 +lat_2=45.5 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs"
#Here, I ensured that both the shapefile and raster were using the same projection.
all_pixels <- raster::extract(cdl, cc_ct_new, na.rm=TRUE, df=TRUE)
#I then extracted all the pixel values for cache county. This gave us 2 columns. The first was a simple ID column and the other had a code for the type of can cover.
ras_data <- all_pixels$CLASS_NAME
length(ras_data[ras_data == 111])/length(ras_data)
## [1] 0.006643712
#This set of code then takes that code and reports the the percent of the pixels that are given the value corrisponding to open water (111).Results from this gave me a percent of roughly 0.664.
Remember that the CropScape metadata can be found here
length(ras_data[ras_data == 36])/length(ras_data)
## [1] 0.1053994
#taking the code from question 5, I simply replaced the code for open water (111) with the code for alfalfa (36). I was able to determine the alfalfa code through a search in the metadata. This all said, the ran code gave me a percent of 10.53.
LiquorStores_shp and dump all files
into your data folder. How many liquor stores are in Cache
County?Hint: Consider using the over() function.
liq <- shapefile("/Users/glendonvansandt/Desktop/NR6950 - R/Week 8/LiquorStores/LiquorStores.shp")
plot(liq)
cc_liq <- liq[liq$COUNTY == "Cache",]
nrow(cc_liq)
## [1] 2
plot(cc_liq)
head(cc_liq)
## STORENUMBE TYPE NAME ADDRESS CITY COUNTY ZIP
## 6 6 Liquor Store <NA> 75 W 400 N Logan Cache 84321
## 78 171 Package Agency <NA> 16 W Main St Richmond Cache 84333
## PHONE ONLYSTORE LAT LONG
## 6 (435) 752-4145 true 41.73924 -111.8368
## 78 (435) 258-2047 true 41.92255 -111.8092
#To determine the number of liquor stores in Cache County, I simply uploaded the liquor store shapefile, filtered the points by county, then used the nrow command to count the number of stores meeting the criteria. All this said, there are only 2 liquor stores here in Cache County.
STORENUMBE == 6?Unsure of how to build a buffer? Look at this tutorial.
Hint: Consider using spdplyr first to clean up the
shapefile. And check out the table() function, which is
like the freq() function for non-raster objects, such as
the data you extract() from the CropScape data.
store_6 <- liq[liq$STORENUMBE == 6,]
store_6_sf <- st_as_sf(store_6, CRS(proj4string(cdl)))
store_6_sf <- st_transform(store_6_sf, crs = st_crs(cdl))
extent6 <- st_buffer(store_6_sf, dist = 100)
extent6 <- st_transform(extent6, crs = crs(cdl))
raster_subset <- crop(cdl, extent(extent6))
raster_mask <- rasterize(extent6, raster_subset)
raster_values <- extract(raster_subset, extent6)
print(unique(raster_values))
## [[1]]
## [1] 122 122 122 122 122 122 122 123 122 123 123 123 124 124 124 123 123 123 124
## [20] 124 124 124 123 124 124 124 124 124 124 124 124 124 124 124 123 124 124
most_common_land_use <- names(sort(table(raster_values), decreasing = TRUE))[1]
most_common_land_use
## [1] "124"
spdplyr to find only liquor stores in
Salt Lake City. Compute the average distance between these facilities in
kilometers. Don’t forget to set the distance between a facility
and itself to NA.slc_liq <- liq[liq$CITY == "Salt Lake City",]
slc_liq_sf <- st_as_sf(slc_liq, coords = c("LONG", "LAT"), crs = "EPSG:4326")
slc_liq_sf <- st_transform(slc_liq_sf,crs = "EPSG:32612")
dist_matrix <- st_distance(slc_liq_sf)
diag(dist_matrix) <- NA
avg_distance <- mean(dist_matrix, na.rm = TRUE)
avg_distance
## 8198.627 [m]