-
Notifications
You must be signed in to change notification settings - Fork 0
/
point2polygon_euclidean.R
97 lines (81 loc) · 3.25 KB
/
point2polygon_euclidean.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
# euclidean distance
# calculate the nearest distance to a park
# calculate the number of parks within certain parameters
### load packages ----
suppressWarnings(library(sp)) #to prep using gDistance
suppressWarnings(library(spatstat)) #nncross
suppressWarnings(library(maptools)) #readShapeLines; read shapefiles
suppressWarnings(library(rgeos)) #gDistance
### read student data ----
students <- read.csv("students.csv", stringsAsFactors = FALSE) #clean up the data so it has two columns: x and y
### euclidean distance, nearest park ----
### use nncross
# convert students into ppp object
coords.students <- ppp(students$x, students$y,
window=owin(xrange=c(0, max(students$x)),
yrange=c(0, max(students$y))))
# read parks
parks <- readShapeLines("parks.shp")
parks <- as.psp(parks)
# find nearest park
dist <- nncross(coords.students, parks, what="dist")
students <- cbind(students, dist)
colnames(students)[3] <- "nearest"
write.csv(students, "dist_to_nearest.csv")
### count the num of parks ----
park.info <- read.csv("list_of_parks.csv", stringsAsFactors = FALSE) #provides unique park ids (var: parknum) and the type of park (var: landuse)
names(park.info) #clean up so that park.info has 2 vars: parknum and landuse
parks <- readShapeSpatial("parks.shp") #gDistance reads polygons as SpatialPolygons feture class
#create functions to count numbr of parks within different parameters
# buffers: 40, 264, 660, 1320, 2640 ft
# these buffers roughly represent the width of a street, one city block, 1/8 of a mile, a quarter of a mile and half a mile
# how wide should a street be:
# http://plannersweb.com/2013/09/wide-neighborhood-street-part-1/
sum40 <- function(x) { #park is right across the street
num <- sum(x<40)
return(num)
}
sum264 <- function(x) { #1 city block
num <- sum(x<264)
return(num)
}
sum660 <- function(x) { #1/8 of a mile
num <- sum(x<660)
return(num)
}
sum1320 <- function(x) { #1 quarter of a mile
num <- sum(x<1320)
return(num)
}
sum2640 <- function(x) { #half a mile
num <- sum(x<2640)
return(num)
}
# slice students into smaller groups to calculate student to park distance
coords.students <- SpatialPoints(students)
dist <- gDistance(parks, coords.students,byid=TRUE)
dist <- as.data.frame(dist)
row <- dim(dist)[1]
col <- dim(dist)[2]
colnames(dist)[1:col] <- park.info$parknum
# export raw results first for archive
write.csv(dist, "raw_output.csv")
# find nearest park by landuse
group <- data.frame(t(dist))
group$landuse <- park.info$landuse
group <- aggregate(group, by=list(group$landuse), min)
group$Group.1 <- NULL
group <- data.frame(t(group))
colnames(group) <- as.character(unlist(group["landuse", ]))
group <- group[-(row+1), ]
dist <- cbind(dist, group)
# find nearest park and count the num of parks
dist$nearest <- apply(dist[, 1:col], 1, min) #this is essentially the same as using nncross in the previous part of the script
dist$n40 <- apply(dist[, 1:col], 1, sum40)
dist$n264 <- apply(dist[, 1:col], 1, sum264)
dist$n660 <- apply(dist[, 1:col], 1, sum660)
dist$n1320 <- apply(dist[, 1:col], 1, sum1320)
dist$n2640 <- apply(dist[, 1:col], 1, sum2640)
dist <- dist[, -c(1:col)]
dist.all <- cbind(students, dist.all)
write.csv(dist.all, "nearest_count_park.csv")