-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab2.Rmd
More file actions
176 lines (142 loc) · 3.8 KB
/
Copy pathLab2.Rmd
File metadata and controls
176 lines (142 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
---
title: 'L2: GIS in R'
author: "Chad Fisher"
date: "`r Sys.Date()`"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Data and Libraries
```{r libraries}
#Set and get working directory
setwd("C:/Users/Chad/Box/UEP236_Labs")
getwd()
#load libraries
library(terra)
library(sf)
library(tmap)
library(exactextractr)
```
# Vectors and Rasters
```{r terra_vector}
# load neighborhood shapefile with terra
nyc <- vect('Lab2/Lab2/nyc_neighborhood.shp')
nyc
summary(nyc)
class(nyc)
names(nyc)
# extract attribute table
nyc.df <- as.data.frame(nyc)
#Plot by area
plot(nyc,'shape_area')
#Export copy of data
writeVector(nyc,'Lab2/Lab2/nyc_copy.shp',overwrite=TRUE)
```
```{r sf_vector}
# Read in vector using sf package instead
nyc_sf <- read_sf('Lab2/Lab2/nyc_neighborhood.shp')
nyc_sf
class(nyc_sf)
names(nyc_sf)
#Plot sf objects
plot(st_geometry(nyc_sf))
plot(nyc_sf["shape_area"])
#write to file
write_sf(nyc_sf, "nyc_sf.geojson", APPEND=F)
```
```{r terra_raster}
#Read in rasters
nyc_pm <- rast('Lab2/Lab2/AnnAvg1_12_300mRaster/aa12_pm300m')
nyc_pm
nyc_elev <- rast('Lab2/Lab2/be_NYC_025_agg30.tif')
nyc_elev
```
```{r projections}
#current CRS
crs(nyc)
#Project to new CRS
newcrs <- crs('EPSG:32618')
nyc_proj <- terra::project(nyc,newcrs)
```
```{r geocoding}
#Read and plot points from spreadsheet
nyc_tweets <- read.csv('Lab2/Lab2/NYC_Tweets.csv')
plot(nyc_tweets$Lon,nyc_tweets$Lat,pch=16,cex=0.5,col='blue')
#Convert to vector
nyc_tweet_pts <- st_as_sf(nyc_tweets,coords=c('Lon','Lat'),crs=4326)
plot(nyc_tweet_pts["Tweet_ID"],col='blue')
class(nyc_tweet_pts)
plot(nyc,add=TRUE)
#Fixing projection issues so data layers align on plot
nyc_tweet_pts_pr <- st_transform(nyc_tweet_pts, crs(nyc_proj))
plot(nyc_proj)
plot(nyc_tweet_pts_pr, add=T)
```
# Geoprocessing
```{r attribute_joins}
#Read population table
nyc_poptable <- read.csv('Lab2/Lab2/nyc_population_neighborhood.csv')
# Compare data tables
names(nyc_poptable)
names(nyc)
#Join data
nyc_NBHD_pop <- merge(nyc,nyc_poptable,by='ntacode')
names(nyc_NBHD_pop)
#Plot
plot(nyc_NBHD_pop,'Pop2010')
#Calculate pop density
nyc_NBHD_pop$dens10 <- 1000*(nyc_NBHD_pop$Pop2010/nyc_NBHD_pop$shape_area)
plot(nyc_NBHD_pop,'dens10')
#Convert to SF
nyc_NBHD_pop_sf <- st_as_sf(nyc_NBHD_pop)
qtm(nyc_NBHD_pop_sf,fill='dens10')
```
```{r spatial_joins}
#Check type and CRS
class(nyc_tweet_pts)
class(nyc_NBHD_pop_sf)
st_crs(nyc_tweet_pts)
st_crs(nyc_NBHD_pop_sf)
#Project
newcrs <- crs('EPSG:2263')
nyc_NBHD_pop_2263 <- st_transform(nyc_NBHD_pop_sf, newcrs)
tweets_sf_2263 <- st_transform(nyc_tweet_pts, newcrs)
#Spatial Join
nhood_target <- st_join(nyc_NBHD_pop_2263, tweets_sf_2263)
tweets_target <- st_join(tweets_sf_2263, nyc_NBHD_pop_2263)
nhood_target
tweets_target
#Aggregate
tweets_by_NBHD <- aggregate(nhood_target,by=list(nhood_target$ntaname),FUN=length)
#Remove duplicate columns and rename
tweets_by_NBHD <- tweets_by_NBHD[,1:2]
colnames(tweets_by_NBHD) <- c("ntaname", "count","geometry")
# Map
qtm(tweets_by_NBHD, fill = "count")
#Tweets per capita
tweets_by_NBHD_pop <- merge(tweets_by_NBHD, nyc_poptable,
by.x = "ntaname", by.y = "NTA.Name")
tweets_by_NBHD_pop$tweet_per_capita <-
(tweets_by_NBHD_pop$count/tweets_by_NBHD_pop$Pop2010)*10000
qtm(tweets_by_NBHD_pop, fill = "tweet_per_capita")
```
```{r zonal_stats}
nyc_NBHD_pop_proj <- terra::project(nyc_NBHD_pop,crs(nyc_pm))
plot(nyc_pm)
plot(nyc_NBHD_pop_proj,add=T)
#Zonal stats
nyc_pm_by_NBHD <- exact_extract(
x=nyc_pm,
y=st_as_sf(nyc_NBHD_pop_proj),
fun='mean',
append_cols='ntacode'
)
nyc_NBHD_meanpm <- merge(
nyc_NBHD_pop_sf,
nyc_pm_by_NBHD,
by='ntacode'
)
#Map
qtm(nyc_NBHD_meanpm,fill='mean')
```