-
Notifications
You must be signed in to change notification settings - Fork 0
/
FinalGISTrees.R
209 lines (156 loc) · 7.74 KB
/
FinalGISTrees.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
#Here is the entire tool. If you want to use the API to calculate all the IVE Scores you can
#I have also save a copy as a csv that can be picked up later in the sript.
#I have also saved a full copy of the global environment should you wich to use that
#though it doesn't need it
wd <- getwd()
DataFolder <- paste(wd, "/data", sep = "")
graphopper_API <- Sys.getenv("ghAPI")
#libraries
library(tidyverse)
library(maptools)
library(RColorBrewer)
library(classInt)
library(OpenStreetMap)
library(sp)
library(rgeos)
library(tmap)
library(tmaptools)
library(sf)
library(rgdal)
library(geojsonio)
library(spatstat)
library(GISTools)
library(readr)
library(stplanr)
#load in BNG
UKBNG <- "+init=epsg:27700"
#Tree Data
TreeData <- read.csv(paste(DataFolder, "/Borough_tree_list_may16.csv", sep = ""))
#Turn csv into datapoints
TreeDataSP <- SpatialPointsDataFrame(TreeData[,6:7], TreeData, proj4string = CRS(UKBNG))
#Green Open Space Data
PublicOpenSpace <- readOGR(paste(DataFolder, "/POS_and_AoD/London_POS_region.shp", sep = ""))
#transform to BNG
PublicOpenSpaceBNG <- spTransform(PublicOpenSpace,UKBNG)
#Access to open space data
AccesstoOpenSpace <- read.csv(paste(DataFolder, "/Access to Public Space/public-open-space-nature-ward_access-to-nature_general.csv", sep = ""))
AccesstoOpenSpaceComplex <- read.csv(paste(DataFolder, "/Access to Public Space/access-public-open-space-nature-ward_each_by_POS-type.csv", sep = ""))
CamdenAccessPOSComplex <- AccesstoOpenSpaceComplex[grep("E09000007", AccesstoOpenSpaceComplex$Borough.Code), ]
CamdenAccessPOS <- AccesstoOpenSpace[grep("E09000007", AccesstoOpenSpace$Borough.Code), ]
#Ward shape files
LondonWards <- readOGR(paste(DataFolder, "/LondonWardsBoundaries/LondonWardsNew.shp", sep = ""))
#transform to BNG
LondonWardsBNG <- spTransform(LondonWards,UKBNG)
#Borough Boundaries
EW <- geojson_read("http://geoportal.statistics.gov.uk/datasets/8edafbe3276d4b56aec60991cbddda50_2.geojson", what = "sp")
#Choose only Camden Borough boundary
CamdenMap <- EW[grep("^E09000007",EW@data$lad15cd),]
#transform to BNG
CamdenMapBNG <- spTransform(CamdenMap,UKBNG)
#Use Camden Borough boundary to clip other data sets
#clip all london wards outside of camden
CamdenWards <- LondonWardsBNG[grep("^00AGG", LondonWardsBNG@data$WD11CDO),]
#need a WGS84 version for the API
CamdenWardsWGS84 <- LondonWards[grep("^00AGG", LondonWardsBNG@data$WD11CDO),]
CamdenWardsWGS84 <-spTransform(CamdenWards, CRS("+proj=longlat +datum=WGS84"))
#clip all trees outside of camden
CamdenTreeData <- TreeDataSP[CamdenMapBNG, ]
#clip all open space data outside of camden
CamdenPublicOpenSpace <- PublicOpenSpaceBNG[CamdenMapBNG, ]
#set tm_view to interactive
tmap_mode("view")
#view the map
tm_shape(CamdenWards) +
tm_polygons(col = NA, alpha = 0.5) +
tm_shape(CamdenTreeData) +
tm_dots(col = "green") +
tm_shape(CamdenPublicOpenSpace) +
tm_polygons(col = "blue", alpha = 0.5)
#functions for working out
#for CalculateNewRoute the StartCoords and EndCoords must be WGS84 coords in the format "lon, lat" (string)
CalculateNewRoute <- function(StartCoords, EndCoords) {
NewRoute <- route_graphhopper(StartCoords, EndCoords, vehicle = "foot", pat = graphopper_API, base_url = "https://graphhopper.com")
NewRoute <- spTransform(NewRoute, UKBNG)
return(NewRoute)
}
#for CalculateIVE the route must be a linestring in BNG coordinates, ideally it should all be within the camden wards as tree/openspace data for outside of this area is not used
CalculateIVE <- function(NewRoute){
NewRouteBuffer <- gBuffer(NewRoute, width = 2)
CamdenTreeDataSF <- st_as_sf(CamdenTreeData)
CamdenPublicOpenSpaceSF <- st_as_sf(CamdenPublicOpenSpace)
NewRouteBufferSF <- st_as_sf(NewRouteBuffer)
NewRouteSF <- st_as_sf(NewRoute)
TreeCount <- st_intersects(CamdenTreeDataSF[NewRouteBufferSF, ], NewRouteBufferSF, sparse = FALSE)
ParkCount <- st_intersects(CamdenPublicOpenSpaceSF[NewRouteBufferSF, ], NewRouteBufferSF, sparse = FALSE)
IVETotal <- length(TreeCount) + 3*(length(ParkCount))
IVEPerM <- IVETotal/line_length(NewRoute)
print(class(IVEPerM))
return(IVEPerM)
}
#create dataframe to store results of route/IVE calculation
AverageIVEList <- data.frame("id" = 128:145)
#testroute to ensure API is working
TestRoute <- CalculateNewRoute("51.5738, -0.1859", "51.53394, -0.13711")
#for loop to created walking routes that cross each of the wards in camdenwards
for(j in 1:length(CamdenWardsWGS84@data$OBJECTID)){
print(j)
#use OBJECTID to select the ward to be used
i <- CamdenWardsWGS84@data$OBJECTID[j]
print(i)
ThisWard <- CamdenWardsWGS84[CamdenWardsWGS84@data$OBJECTID==i,]
#creat start and end coordinates from the bbox of each ward
#also paste them together in the format needed for the graphhopper API
CoordsNW <- paste(ThisWard@bbox[2,2],",", ThisWard@bbox[1,1], sep = "")
CoordsSE <- paste(ThisWard@bbox[2,1],",", ThisWard@bbox[1,2], sep = "")
CoordsSW <- paste(ThisWard@bbox[2,2],",",ThisWard@bbox[1,2], sep = "")
CoordsNE <- paste(ThisWard@bbox[2,1],",",ThisWard@bbox[1,1], sep = "")
#Calculate each of the new routes
NewRouteA <- CalculateNewRoute(CoordsNW, CoordsSE)
NewRouteB <- CalculateNewRoute(CoordsSW, CoordsNE)
#use the CalculateIVE function to find a score for each route
NewIVEA <- CalculateIVE(NewRouteA)
NewIVEB <- CalculateIVE(NewRouteB)
#average the two routes to find a vague IVE score for each ward
AverageIVE <- ((NewIVEA + NewIVEB)/2)
#fill the dataframe created earlier with these results to attach to the wards later
AverageIVEList$Average_IVE[j] <- AverageIVE
}
#I have saved a previous list as csv in case you don't want to create a new one with the API
write.csv(AverageIVEList, paste(DataFolder, "/AverageIVE.csv", sep = ""))
#load in saved csv of AverageIVE here if you haven't created a new one
AverageIVEList <- read.csv(paste(DataFolder, "/AverageIVE.csv", sep = ""))
#Find min, max and avg IVE score to compare to user input route
MinIVE <- min(AverageIVEList[ ,"Average_IVE"])
MaxIVE <- max(AverageIVEList[ ,"Average_IVE"])
MeanIVE <- mean(AverageIVEList[ ,"Average_IVE"])
#Append list to CamdenWards
CamdenWards@data <- data.frame(CamdenWards@data,AverageIVEList[match(CamdenWards@data[,"OBJECTID"],AverageIVEList[,"id"]),])
#Add Access to open sapce data to CamdenWards
CamdenWards@data <- data.frame(CamdenWards@data, CamdenAccessPOS[match(CamdenWards@data[,"WD11CD"], CamdenAccessPOS[,"Ward"]),])
CamdenWards@data <- data.frame(CamdenWards@data, CamdenAccessPOSComplex[match(CamdenWards@data[,"WD11CD"], CamdenAccessPOSComplex[,"Ward"]),])
#User Input Route
UserStartCoordsA <- "51.522622, -0.136346"
UserEndCoordsA <- "51.555999, -0.145973"
UserStartCoordsB <- "51.548249, -0.177642"
UserEndCoordsB <- "51.537714, -0.134746"
UserStartCoordsC <- "51.535494, -0.157181"
UserEndCoordsC <- "51.527622, -0.130775"
#Change the valuse in CalculateNew Route to any of the above to calculate different routes and IVE Scores
TestRoute <- CalculateNewRoute("51.5738, -0.1859", "51.53394, -0.13711")
#Calculate IVE Score
TestRouteIVE <- CalculateIVE(TestRoute)
#Add IVE to TestRoute@data
TestRoute@data$IVE_Score <- TestRouteIVE
#plot map of CamdenWards with TestRoute
TestRouteMap <- tm_shape(CamdenWards) +
tm_polygons(col = NA, alpha = 0.5) +
tm_shape(CamdenTreeData) +
tm_dots(col = "green") +
tm_shape(CamdenPublicOpenSpace) +
tm_polygons(col = "blue", alpha = 0.3) +
tm_shape(TestRoute) +
tm_lines(col = "red")
tm_view(TestRouteMap)
#showing IVE rating for TestRoute, alongside average, min and max
#Print out information regarding the map
print(paste("The IVE score for this route is ", TestRoute@data$IVE_Score, ", the mean for the borough is ", MeanIVE, ", the smallest is ", MinIVE, ", and the largest is ", MaxIVE, ".", sep = ""))