-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwalkshed.R
executable file
·65 lines (62 loc) · 2.28 KB
/
walkshed.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
#This function was taken and slightly modified from the function in the walkscoreAPI package
#Package reference: https://cran.r-project.org/web/packages/walkscoreAPI/index.html
walkshedMod <- function(x,y,key){
URL <- paste("http://api.walkscore.com/walk_shed?lat=",y,"&lon=",x,"&wsapikey=",key,sep="")
X <- character(0)
X <- c(X, scan(file = URL, what = "", sep = " ", quiet = TRUE))
status <- X[grep("{status:",X,fixed=TRUE) + 1]
status = gsub("}","",status,fixed = TRUE)
status <- as.numeric(gsub(",","",status,fixed=TRUE))
if(status==1){
lat <- X[grep("{lat",X,fixed=TRUE) + 1]
lat <- as.numeric(gsub(",","",lat))
lon <- X[grep("{lat",X,fixed=TRUE) + 3]
lon <- as.numeric(gsub("},","",lon))
geo <- X[grep("{type:",X,fixed=TRUE) + 1]
geo <- gsub(",","",geo)
cstart <- grep("coordinates:",X,fixed=TRUE) + 1
cend <- grep("radius:",X,fixed=TRUE) - 1
lonlist <- c()
latlist <- c()
count <- 0
for (i in cstart:cend){
count <- count + 1
if (count %% 2 == 1){
n <- X[i]
n <- gsub("[","",n,fixed=TRUE)
n <- gsub(",","",n,fixed=TRUE)
n <- as.numeric(n)
lonlist <- c(lonlist,n)
}
if (count %% 2 == 0){
n <- X[i]
n <- gsub("]","",n,fixed=TRUE)
n <- gsub(",","",n,fixed=TRUE)
n <- gsub("}","",n,fixed=TRUE)
n <- as.numeric(n)
latlist <- c(latlist,n)
}
}
coords <- data.frame(lonlist,latlist)
rad <- X[grep("radius:",X,fixed=TRUE) + 1]
rad <- as.numeric(gsub("},","",rad,fixed=TRUE))
slat <- X[grep("snapped_lat:",X,fixed=TRUE) + 1]
slat <- as.numeric(gsub(",","",slat,fixed=TRUE))
slon <- X[grep("snapped_lon:",X,fixed=TRUE) + 1]
slon <- as.numeric(gsub("}","",slon,fixed=TRUE))
object <- list()
class(object) <- "Walkshed"
object$status <- status
object$origin <- c(lon,lat)
object$geometry <- geo
object$coordinates <- coords
object$radius <- rad
object$snappedLong <- slon
object$snappedLat <- slat
}else{
object <- list()
class(object) <- "Walkshed"
object$status <- status
}
return(object)
}