-
Notifications
You must be signed in to change notification settings - Fork 61
/
topojson_write.R
344 lines (317 loc) · 14.2 KB
/
topojson_write.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#' Write TopoJSON from various inputs
#'
#'
#' @export
#' @inheritParams geojson_write
#' @param object_name (character) name to give to the TopoJSON object created.
#' Default: "foo"
#' @param quantization (numeric) quantization parameter, use this to
#' quantize geometry prior to computing topology. Typical values are powers of
#' ten (`1e4`, `1e5`, ...), default is `0` to not perform quantization.
#' For more information about quantization, see this by Mike Bostock
#' https://stackoverflow.com/questions/18900022/topojson-quantization-vs-simplification/18921214#18921214
#' @return A `topojson_write` class, with two elements:
#'
#' - path: path to the file with the TopoJSON
#' - type: type of object the TopoJSON came from, e.g., SpatialPoints
#'
#' @seealso [geojson_write()], [topojson_read()]
#' @details Under the hood we simply wrap [geojson_write()], then
#' take the GeoJSON output of that operation, then convert to TopoJSON with
#' [geo2topo()], then write to disk.
#'
#' Unfortunately, this process requires a number of round trips to disk, so
#' speed ups will hopefully come soon.
#'
#' Any intermediate geojson files are cleaned up (deleted).
topojson_write <- function(input, lat = NULL, lon = NULL, geometry = "point",
group = NULL, file = "myfile.topojson",
overwrite = TRUE, precision = NULL,
convert_wgs84 = FALSE, crs = NULL,
object_name = "foo", quantization = 0, ...) {
UseMethod("topojson_write")
}
#' @export
topojson_write.default <- function(input, lat = NULL, lon = NULL, geometry = "point",
group = NULL, file = "myfile.topojson",
overwrite = TRUE, precision = NULL,
convert_wgs84 = FALSE, crs = NULL,
object_name = "foo", quantization = 0, ...) {
stop("no 'topojson_write' method for ", class(input), call. = FALSE)
}
#' @export
topojson_write.SpatialPolygons <- function(input, lat = NULL, lon = NULL, geometry = "point",
group = NULL, file = "myfile.topojson",
overwrite = TRUE, precision = NULL,
convert_wgs84 = FALSE, crs = NULL,
object_name = "foo", quantization = 0, ...) {
sp_helper(input,
file = file, precision = precision,
convert_wgs84 = convert_wgs84, crs = crs,
class = "SpatialPolygons", object_name = object_name,
quantization = quantization, ...
)
}
#' @export
topojson_write.SpatialPolygonsDataFrame <- function(input, lat = NULL, lon = NULL, geometry = "point",
group = NULL, file = "myfile.topojson",
overwrite = TRUE, precision = NULL,
convert_wgs84 = FALSE, crs = NULL,
object_name = "foo", quantization = 0, ...) {
sp_helper(input,
file = file, precision = precision,
convert_wgs84 = convert_wgs84, crs = crs,
class = "SpatialPolygonsDataFrame", object_name = object_name,
quantization = quantization, ...
)
}
#' @export
topojson_write.SpatialPoints <- function(input, lat = NULL, lon = NULL, geometry = "point",
group = NULL, file = "myfile.topojson",
overwrite = TRUE, precision = NULL,
convert_wgs84 = FALSE, crs = NULL,
object_name = "foo", quantization = 0, ...) {
sp_helper(input,
file = file, precision = precision,
convert_wgs84 = convert_wgs84, crs = crs,
class = "SpatialPoints", object_name = object_name,
quantization = quantization, ...
)
}
#' @export
topojson_write.SpatialPointsDataFrame <- function(input, lat = NULL, lon = NULL, geometry = "point",
group = NULL, file = "myfile.topojson",
overwrite = TRUE, precision = NULL,
convert_wgs84 = FALSE, crs = NULL,
object_name = "foo", quantization = 0, ...) {
sp_helper(input,
file = file, precision = precision,
convert_wgs84 = convert_wgs84, crs = crs,
class = "SpatialPointsDataFrame", object_name = object_name,
quantization = quantization, ...
)
}
#' @export
topojson_write.SpatialLines <- function(input, lat = NULL, lon = NULL, geometry = "point",
group = NULL, file = "myfile.topojson",
overwrite = TRUE, precision = NULL,
convert_wgs84 = FALSE, crs = NULL,
object_name = "foo", quantization = 0, ...) {
sp_helper(input,
file = file, precision = precision,
convert_wgs84 = convert_wgs84, crs = crs,
class = "SpatialLines", object_name = object_name,
quantization = quantization, ...
)
}
#' @export
topojson_write.SpatialLinesDataFrame <- function(input, lat = NULL, lon = NULL, geometry = "point",
group = NULL, file = "myfile.topojson",
overwrite = TRUE, precision = NULL,
convert_wgs84 = FALSE, crs = NULL,
object_name = "foo", quantization = 0, ...) {
sp_helper(input,
file = file, precision = precision,
convert_wgs84 = convert_wgs84, crs = crs,
class = "SpatialLinesDataFrame", object_name = object_name,
quantization = quantization, ...
)
}
#' @export
topojson_write.SpatialGrid <- function(input, lat = NULL, lon = NULL, geometry = "point",
group = NULL, file = "myfile.topojson", overwrite = TRUE, precision = NULL,
convert_wgs84 = FALSE, crs = NULL, object_name = "foo", quantization = 0, ...) {
sp_helper(input,
file = file, precision = precision,
convert_wgs84 = convert_wgs84, crs = crs,
class = "SpatialGrid", object_name = object_name,
quantization = quantization, ...
)
}
#' @export
topojson_write.SpatialGridDataFrame <- function(input, lat = NULL, lon = NULL,
geometry = "point", group = NULL, file = "myfile.topojson",
overwrite = TRUE, precision = NULL, convert_wgs84 = FALSE, crs = NULL,
object_name = "foo", quantization = 0, ...) {
sp_helper(input,
file = file, precision = precision,
convert_wgs84 = convert_wgs84, crs = crs,
class = "SpatialGridDataFrame", object_name = object_name,
quantization = quantization, ...
)
}
#' @export
topojson_write.SpatialPixels <- function(input, lat = NULL, lon = NULL, geometry = "point",
group = NULL, file = "myfile.topojson",
overwrite = TRUE, precision = NULL,
convert_wgs84 = FALSE, crs = NULL,
object_name = "foo", quantization = 0, ...) {
sp_helper(input,
file = file, precision = precision,
convert_wgs84 = convert_wgs84, crs = crs,
class = "SpatialPixelsDataFrame", object_name = object_name,
quantization = quantization, ...
)
}
#' @export
topojson_write.SpatialPixelsDataFrame <- function(input, lat = NULL, lon = NULL, geometry = "point",
group = NULL, file = "myfile.topojson",
overwrite = TRUE, precision = NULL,
convert_wgs84 = FALSE, crs = NULL,
object_name = "foo", quantization = 0, ...) {
sp_helper(input,
file = file, precision = precision,
convert_wgs84 = convert_wgs84, crs = crs,
class = "SpatialPixelsDataFrame", object_name = object_name, quantization = quantization, ...
)
}
## normal R classes -----------------
#' @export
topojson_write.numeric <- function(input, lat = NULL, lon = NULL, geometry = "point",
group = NULL, file = "myfile.topojson", overwrite = TRUE, precision = NULL,
convert_wgs84 = FALSE, crs = NULL, object_name = "foo", quantization = 0, ...) {
sp_helper(input,
lat = lat, lon = lon, geometry = geometry,
file = file, precision = precision, overwrite = overwrite,
class = "numeric", object_name = object_name, quantization = quantization, ...
)
}
#' @export
topojson_write.data.frame <- function(input, lat = NULL, lon = NULL, geometry = "point",
group = NULL, file = "myfile.topojson", overwrite = TRUE, precision = NULL,
convert_wgs84 = FALSE, crs = NULL, object_name = "foo", quantization = 0, ...) {
sp_helper(input,
lat = lat, lon = lon, geometry = geometry, group = group,
file = file, precision = precision, overwrite = overwrite,
class = "data.frame", object_name = object_name, quantization = quantization, ...
)
}
#' @export
topojson_write.list <- function(input, lat = NULL, lon = NULL, geometry = "point",
group = NULL, file = "myfile.topojson", overwrite = TRUE, precision = NULL,
convert_wgs84 = FALSE, crs = NULL, object_name = "foo", quantization = 0, ...) {
sp_helper(input,
lat = lat, lon = lon, geometry = geometry, group = group,
file = file, precision = precision, overwrite = overwrite,
class = "list", object_name = object_name, quantization = quantization, ...
)
}
#' @export
topojson_write.geo_list <- function(input, lat = NULL, lon = NULL, geometry = "point",
group = NULL, file = "myfile.topojson", overwrite = TRUE, precision = NULL,
convert_wgs84 = FALSE, crs = NULL, object_name = "foo", quantization = 0, ...) {
sp_helper(input,
file = file, overwrite = overwrite, class = "geo_list",
object_name = object_name, quantization = quantization, ...
)
}
# JSON -----------------
#' @export
topojson_write.json <- function(input, lat = NULL, lon = NULL, geometry = "point",
group = NULL, file = "myfile.topojson", overwrite = TRUE, precision = NULL,
convert_wgs84 = FALSE, crs = NULL, object_name = "foo", quantization = 0, ...) {
topo_file(write_topojson(unclass(input), file, ...), "json")
}
## sf classes --------------
#' @export
topojson_write.sf <- function(input, lat = NULL, lon = NULL, geometry = "point",
group = NULL, file = "myfile.topojson",
overwrite = TRUE, precision = NULL,
convert_wgs84 = FALSE, crs = NULL,
object_name = "foo", quantization = 0, ...) {
topo_write_sf(
input, convert_wgs84, crs, file, overwrite, "sf",
object_name, quantization, ...
)
}
#' @export
topojson_write.sfc <- function(input, lat = NULL, lon = NULL, geometry = "point",
group = NULL, file = "myfile.topojson",
overwrite = TRUE, precision = NULL,
convert_wgs84 = FALSE, crs = NULL,
object_name = "foo", quantization = 0, ...) {
topo_write_sf(
input, convert_wgs84, crs, file, overwrite, "sfc",
object_name, quantization, ...
)
}
#' @export
topojson_write.sfg <- function(input, lat = NULL, lon = NULL, geometry = "point",
group = NULL, file = "myfile.topojson",
overwrite = TRUE, precision = NULL,
convert_wgs84 = FALSE, crs = NULL,
object_name = "foo", quantization = 0, ...) {
topo_write_sf(
input, convert_wgs84, crs, file, overwrite, "sfg",
object_name, quantization, ...
)
}
topo_write_sf <- function(input, convert_wgs84, crs, file, overwrite,
class, object_name, quantization, ...) {
tmp <- suppressMessages(
geojson_write(input,
convert_wgs84 = convert_wgs84,
crs = crs, file = tempfile(fileext = ".geojson"),
overwrite = overwrite, ...
)
)
on.exit(unlink(tmp$path))
topo_file(
write_topojson(
geo2topo(
paste0(readLines(tmp$path, warn = FALSE), collapse = ""),
object_name, quantization
), file
),
"sfc"
)
}
# helpers ------------------
write_topojson <- function(x, file, ...) {
if (is.null(file)) {
stop("'file' required with character string as input",
call. = FALSE
)
}
file <- path.expand(file)
fcon <- file(file)
on.exit(close(fcon))
writeLines(x, con = fcon)
message("Success! File at ", file)
return(file)
}
sp_helper <- function(input, lat = NULL, lon = NULL, geometry = "point",
group = NULL, file = "myfile.topojson",
overwrite = TRUE, precision = NULL,
convert_wgs84 = FALSE, crs = NULL,
class, object_name, quantization, ...) {
res <- suppressMessages(
geojson_write(
input,
lat = lat, lon = lon, geometry = geometry, group = group,
file = sub("\\.topojson|\\.json", "\\.geojson", file),
overwrite = overwrite, precision = precision,
convert_wgs84 = convert_wgs84, crs = crs, ...
)
)
on.exit(unlink(res$path))
topo_file(
write_topojson(
geo2topo(
paste0(readLines(res$path, warn = FALSE), collapse = ""),
object_name, quantization
),
file
),
class
)
}
topo_file <- function(path, type) {
structure(list(path = path, type = type), class = "topojson_file")
}
#' @export
print.topojson_file <- function(x, ...) {
cat("<topojson-file>", "\n", sep = "")
cat(" Path: ", x$path, "\n", sep = "")
cat(" From class: ", x$type, "\n", sep = "")
}