generated from edgi-govdata-archiving/Template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest_controller.r
117 lines (114 loc) · 2.9 KB
/
rest_controller.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
# Based on https://github.com/petegordon/RCloudRun
library(EJAM)
library(geojsonsf)
#* Generate a report
#* @param lat The input sites lat
#* @param lon The input sites long
#* @param shape The input sites geojson
#* @param buffer The input sites buffer
#* @get /report
#* @serializer html
function(lat = NULL,
lon = NULL,
shape = NULL,
fips = NULL,
buffer = 3) {
html <<- ""
error <<- "<html><body><h3>Error</h3>" # default error template
buffer <- as.numeric(buffer)
# test if buffer is too big
flag <<- 0
if (buffer > 15) {
html <- paste(error,
"Please select a buffer 15 miles in radius or less</body></html>",
sep = " ")
flag <<- 1
} else if (is.character(lat) &
is.character(lon)) {
# test if point input
lat <- as.numeric(lat)
lon <- as.numeric(lon)
sites <- data.frame(lat, lon)
tryCatch(
latlon_from_anything(sites),
error = function(e) {
flag <<- 1
},
warning = function(w) {
flag <<- 1
}
)
tryCatch(
x <- ejamit(sites, radius = buffer),
error = function(e) {
flag <<- 1
},
warning = function(w) {
flag <<- 1
}
)
if (flag == 1) {
html <<- paste(
error,
"There seems to be an issue with the coordinates you provided.</body></html>",
sep = " "
)
}
} else if (is.character(shape)) {
# test if shape input
# try to convert to sf https://stackoverflow.com/questions/66457617/trycatch-in-r-programming
tryCatch(
shape <- geojson_sf(shape),
error = function(e) {
flag <<- 1
}
)
if (flag == 1) {
html <<- paste(error,
"There seems to be an issue with the shape you provided.</body></html>",
sep = " ")
} else {
x <- ejamit(radius = buffer, shapefile = shape)
}
} else if (is.character(fips)) {
tryCatch(
x <- ejamit(radius = buffer, fips = fips),
error = function(e) {
flag <<- 1
}
)
if (flag == 1) {
html <<- paste(
error,
"There seems to be an issue with the Census FIPS code provided.</body></html>",
sep = " "
)
}
} else{
# No points, shape, or FIPS. EJAM produces a dummy report by default, but we won't do that
flag <<- 1
html <<- paste(
error,
"You need to provide valid points, a shape, or a Census FIPS code.</body></html>",
sep = " "
)
}
if (flag == 1) {
html <<- html # return error message
} else {
tryCatch(
html <<- ejam2report(x, return_html = TRUE, launch_browser = TRUE),
#False
error = function(e) {
flag <<- 1
}
)
if (flag == 1) {
html <<- paste(error, "There seems to be an issue.</body></html>", sep =
" ")
}
}
return(html)
}
#* @assets ./assets /
list()