-
Notifications
You must be signed in to change notification settings - Fork 0
/
geocoding-gardens.R
211 lines (152 loc) · 9.68 KB
/
geocoding-gardens.R
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
library(tidyverse)
library(tmaptools)
library(sf)
library(leaflet)
library(DT)
## This chunk of code makes revisions to the data file "05-03-gardens_geo.RDS"
# The revisions are based on getting size information for:
# Charlottesville High School garden size (18,125 sq ft) -- shared by Peter Davis on 05/04
# Adding the Incarnation Church garden (1465 Incarnation Dr, Charlottesville, VA 22901; 768 sq ft)
# -- lat and long = 38.073642, -78.475514 -- shared by Muriel Grim on 05/12
gardens <- readRDS("05-03-gardens_geo.RDS")
gardens <- gardens %>%
add_row(Address = "1465 Incarnation Dr, Charlottesville VA 22901", Garden_property_name = "Incarnation Church",
Managed_by = "Incarnation Church", Total_size = "768 sq ft", Size = "768 sq ft",
Status = "Existing", cat = "Other")
gardens$geometry[which(gardens$Garden_property_name == "Incarnation Church")] <- st_point(c(-78.475514, 38.073642))
gardens$Total_size[which(gardens$Garden_property_name == "Charlottesville High School")] <- "18,125 sq ft"
# Saving data with updates
write.csv(gardens, '05-19-gardens_geo.csv', row.names = F)
write_rds(gardens, "05-19-gardens_geo.RDS")
##########################################################################################################################
## This chunk of code makes revisions to the data file "04-21-gardens_geo.RDS"
# The revisions are based on getting size information for the city schoolyard
# gardens on 5/03 and from Bread and Roses on 5/02
gardens <- readRDS("04-21-gardens_geo.RDS")
# Manually adding size information for the following gardens:
# Buford = 4,560 ft2
# Johnson = 2,426 ft2
# Jackson-Via = 3,088 ft2
# Clark = 3,142 ft2
# Venable = 1,395 ft2
# Burnley-Moran = 6,822 ft2
# Greenbrier = 2,950 ft2
# Bread and Roses = 650 square feet
gardens$Total_size[which(gardens$Garden_property_name == "Buford Middle School")] <- "4,560 sq ft"
gardens$Total_size[which(gardens$Garden_property_name == "Johnson Elementary School")] <- "2,426 sq ft"
gardens$Total_size[which(gardens$Garden_property_name == "Jackson Via Elementary School")] <- "3,088 sq ft"
gardens$Total_size[which(gardens$Garden_property_name == "Clark Elementary School")] <- "3,142 sq ft"
gardens$Total_size[which(gardens$Garden_property_name == "Venable Elementary School")] <- "1,395 sq ft"
gardens$Total_size[which(gardens$Garden_property_name == "Burnley-Moran Elementary School")] <- "6,822 sq ft"
gardens$Total_size[which(gardens$Garden_property_name == "Burnley-Moran Elementary School")] <- "6,822 sq ft"
gardens$Total_size[which(gardens$Garden_property_name == "Greenbrier Elementary School")] <- "2,950 sq ft"
gardens$Total_size[which(gardens$Managed_by == "Bread and Roses")] <- "650 sq ft"
gardens$Total_size[which(gardens$Garden_property_name == "Visible records garden")] <- "2000 sq ft"
# Saving data with updated geolocations
write.csv(gardens, '05-03-gardens_geo.csv', row.names = F)
write_rds(gardens, "05-03-gardens_geo.RDS")
##########################################################################################################################
## This chunk of code makes revisions to the data file "04-12-gardens_geo.RDS"
# which was created with code below. The revisions are based on feedback from
# Richard Morris at Cultivate on 4/14
gardens <- readRDS("04-12-gardens_geo.RDS")
# Fixing the visible records garden to have the correct management
gardens$Managed_by[which(gardens$Garden_property_name == "Visible records garden")] <- "Kendall King"
# Fixing the Trinity Episcopal church bread and roses garden to have the correct management
gardens$Managed_by[which(gardens$Location_description == "Trinity Episcopal Church")] <- "Bread and Roses"
# Editing the garden size information for one garden that Richard Morris had info for
gardens$Total_size[which(gardens$Garden_property_name == "6th Street")] <- "4400 sq ft"
gardens$Size[which(gardens$Garden_property_name == "6th Street")] <- "4400 sq ft"
gardens$Total_size[which(gardens$Managed_by == "Urban Agriculture Collective")] <- gardens$Size[which(gardens$Managed_by == "Urban Agriculture Collective")]
# The 6th street garden was lost, but it has been revived
gardens$Status[which(gardens$Garden_property_name == "6th Street")] <- "Existing"
## Manually fixing the geolocations
# West St garden = 38.036166511857765, -78.48929141009091
gardens$geometry[which(gardens$Garden_property_name == "West St")] <- st_point(c(-78.48929141009091, 38.036166511857765))
# CATEC garden = 38.06224403490436, -78.46482075941262
gardens$geometry[which(gardens$Garden_property_name == "CATEC School")] <- st_point(c(-78.46482075941262, 38.06224403490436))
# South 1st St = 38.019160547205594, -78.48690403419546
gardens$geometry[which(gardens$Garden_property_name == "South 1st St")] <- st_point(c(-78.48690403419546, 38.019160547205594))
# 6th st = 38.02520028910517, -78.48115967430914
gardens$geometry[which(gardens$Garden_property_name == "6th Street")] <- st_point(c(-78.48115967430914, 38.02520028910517))
# Friendship court = 38.025798361893564, -78.48067282927684
gardens$geometry[which(gardens$Garden_property_name == "Friendship Court")] <- st_point(c(-78.48067282927684, 38.025798361893564))
# Azalea Park gardens = 38.01023749045567, -78.5138139458892
gardens$geometry[which(gardens$Garden_property_name == "Azalea Park Garden")] <- st_point(c(-78.5138139458892, 38.01023749045567))
# Meadow creek garden = 38.05673992416423, -78.49333775468119
gardens$geometry[which(gardens$Garden_property_name == "Meadow Creek Garden")] <- st_point(c(-78.49333775468119, 38.05673992416423))
# Michie Drive = 38.05948411617561, -78.48700270170862
gardens$geometry[which(gardens$Garden_property_name == "Michie Drive")] <- st_point(c(-78.48700270170862, 38.05948411617561))
# Adding additional category column based on whether the garden is managed by a group or specific person
gardens$cat <- ifelse(gardens$Managed_by == "IRC New Roots" |
gardens$Managed_by == "Urban Agriculture Collective" |
gardens$Managed_by == "Charlottesville Parks Department" |
gardens$Managed_by == "City Schoolyard Garden",
gardens$Managed_by, "Other")
# Making missing values NA's
gardens$Size <- ifelse(gardens$Size == "", NA, gardens$Size)
# Saving data with updated geolocations
write.csv(gardens, '04-21-gardens_geo.csv', row.names = F)
write_rds(gardens, "04-21-gardens_geo.RDS")
## Initial work to geo-code gardens. This chunk of code created the data file
# called "04-12-gardens_geo.csv". I used open street maps and approximate addresses
# for some of the gardens to retrieve their geolocations.
# We received feedback from Richard Morris at Cultivate on 4/14 that some of the geolocations
# for specific gardens were not precise, so in the above code, I manually edited them.
gardens <- read.csv("gardens_nogeo.csv")
## Need to remove the columns that don't have the address--we have their geo-location from the data that Taha shared
gardens <- gardens[-which(gardens$Address == ""),]
locations_df <- geocode_OSM(
gardens[,"Address"],
projection = 4326,
return.first.only = TRUE,
keep.unfound = TRUE,
as.sf = TRUE,
geometry = "point",
server = "https://nominatim.openstreetmap.org"
)
# Manually adding lat and long for one garden that didn't work with
# open street map (found the lat and lon on google maps)
# Bellair Farm = 37.89181455241969, -78.51879205984392
locations_df$x <- ifelse(locations_df$query == "5290 Bellair Farm, Charlottesville VA 22902", -78.51879205984392,
locations_df$x)
locations_df$y <- ifelse(locations_df$query == "5290 Bellair Farm, Charlottesville VA 22902", 37.89181455241969,
locations_df$y)
locations_df$point[(locations_df$query == "5290 Bellair Farm, Charlottesville VA 22902")] <- st_point(c(locations_df$x[5], locations_df$y[5]))
#re-naming column so I can join locations_df with gardens
colnames(locations_df)[colnames(locations_df) == "query"] <- "Address"
# Only need a few columns
locations_df <- locations_df[,c("Address", "x", "y", "point")]
# Changing the projection to the same as the data below
locations_df <- st_transform(locations_df, 4326)
# combining geometry data with other meta data
gardens2 <- locations_df %>%
left_join(gardens)
rename_geometry <- function(g, name) {
current = attr(g, "sf_column")
names(g)[names(g) == current] = name
st_geometry(g) = name
g
}
gardens2 <- rename_geometry(gardens2, "geometry")
# Reading in the other data file to combine everything
files <- list.files("New Data", pattern = ".shp", full.names = TRUE)
# Cultivate gardens
cult_gard <- st_read(files[5], quiet = T)
# Changing the projection to the same as the data frame above
cult_gard <- st_transform(cult_gard, 4326)
# changing the column names to be consistent and adding same columns as above
colnames(cult_gard)[colnames(cult_gard) == "Location"] <- "Garden_property_name"
gardens2[(nrow(gardens2) + 1):((nrow(gardens2) + 1) + nrow(cult_gard) - 1), c('Garden_property_name', 'geometry')] <- cult_gard[, c('Garden_property_name', 'geometry')]
# The 6th st and Monticello garden was in the data twice, so I'm getting rid of the extra
gardens2 <- gardens2[-26,]
# All of the gardens with missing managed by information are managed by City Schoolyard Garden
gardens2$Managed_by <- ifelse(is.na(gardens2$Managed_by == T), "City Schoolyard Garden", gardens2$Managed_by)
# Getting rid of the spare lat and lon columns because they aren't necessary with the
# geometry
gardens3 <- gardens2[,c(1, 4:12)]
# Coding all non-lost gardens as "existing"
gardens3$Status <- ifelse(is.na(gardens3$Status == T), "Existing", gardens3$Status)
# Writing out data file
write.csv(gardens3, '04-12-gardens_geo.csv', row.names = F)
write_rds(gardens3, "04-12-gardens_geo.RDS")