34
34
35
35
wkt_vis <- function (x , zoom = 6 , maptype = " terrain" , browse = TRUE ) {
36
36
long = lat = group = NULL
37
+
37
38
stopifnot(! is.null(x ))
38
39
stopifnot(is.character(x ))
39
-
40
- out <- wkt_read(gsub(" \n |\n\\ s+" , " " , strtrim(x )))
41
-
42
- if (inherits(out $ coordinates [,,1 ], " matrix" )) {
43
- longs <- data.frame (out $ coordinates [,,1 ])
44
- lats <- data.frame (out $ coordinates [,,2 ])
45
- } else {
46
- longs <- t(data.frame (out $ coordinates [,,1 ]))
47
- lats <- t(data.frame (out $ coordinates [,,2 ]) )
48
- }
49
- tocentroid <- list ()
50
- dfs <- list ()
51
- for (i in 1 : NROW(longs )) {
52
- tocentroid [[i ]] <- tmp <- data.frame (long = as.numeric(longs [i ,]), lat = as.numeric(lats [i ,]))
53
- dfs [[i ]] <- apply(tmp , 1 , function (x ) as.list(x [c(' long' ,' lat' )]))
54
- }
55
- centroid <- get_centroid(do.call(" rbind" , tocentroid ))
40
+ x <- gsub(" \n |\n\\ s+" , " " , strtrim(x ))
41
+ out <- wicket :: wkt_coords(x )
42
+ centroid <- wicket :: get_centroid(x )
43
+ dfs <- unname(lapply(split(out , out $ ring ), function (z ) {
44
+ unname(
45
+ apply(z , 1 , function (x ) {
46
+ as.list(stats :: setNames(x [c(' lng' ,' lat' )], c(' long' , ' lat' )))
47
+ })
48
+ )
49
+ }))
56
50
57
51
whiskout <- list ()
58
52
for (i in seq_along(dfs )) {
59
53
dats <- dfs [[i ]]
60
54
whiskout [[i ]] <- whisker.render(features )
61
55
}
62
56
rend <- paste0(map_header , paste(whiskout , sep = " " , collapse = " ," ), map_end )
63
-
64
- foot <- sprintf(footer , centroid [2 ], centroid [1 ], zoom )
57
+ foot <- sprintf(footer , centroid $ lat , centroid $ lng , zoom )
65
58
res <- paste(rend , foot )
66
59
tmpfile <- tempfile(pattern = ' spocc' , fileext = " .html" )
67
60
write(res , file = tmpfile )
68
61
if (browse ) browseURL(tmpfile ) else tmpfile
69
62
}
70
63
71
- get_centroid <- function (x ) {
72
- x <- unname(as.matrix(x ))
73
- geojson <- jsonlite :: toJSON(list (type = " Polygon" , coordinates = list (x )), auto_unbox = TRUE )
74
- cent $ eval(sprintf(" var out = centroid(%s);" , geojson ))
75
- cent $ get(" out.geometry.coordinates" )
76
- }
64
+
65
+ # out <- wkt_read(gsub("\n|\n\\s+", "", strtrim(x)))
66
+ # if (inherits(out$coordinates[,,1], "matrix")) {
67
+ # longs <- data.frame(out$coordinates[,,1])
68
+ # lats <- data.frame(out$coordinates[,,2])
69
+ # } else {
70
+ # longs <- t(data.frame(out$coordinates[,,1]))
71
+ # lats <- t(data.frame(out$coordinates[,,2]) )
72
+ # }
73
+ # tocentroid <- list()
74
+ # dfs <- list()
75
+ # for (i in 1:NROW(longs)) {
76
+ # tocentroid[[i]] <- tmp <- data.frame(long = as.numeric(longs[i,]),
77
+ # lat = as.numeric(lats[i,]))
78
+ # dfs[[i]] <- apply(tmp, 1, function(x) as.list(x[c('long','lat')]))
79
+ # }
80
+ # centroid <- get_centroid(do.call("rbind", tocentroid))
81
+
82
+ # get_centroid <- function(x) {
83
+ # x <- unname(as.matrix(x))
84
+ # geojson <- jsonlite::toJSON(list(type = "Polygon", coordinates = list(x)), auto_unbox = TRUE)
85
+ # cent$eval(sprintf("var out = centroid(%s);", geojson))
86
+ # cent$get("out.geometry.coordinates")
87
+ # }
77
88
78
89
map_header <- '
79
90
<!DOCTYPE html>
@@ -82,8 +93,8 @@ map_header <- '
82
93
<meta charset=utf-8 />
83
94
<title>spocc WKT Viewer</title>
84
95
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
85
- <script src="https://api.tiles.mapbox.com/mapbox.js/v2.2.2 /mapbox.js"></script>
86
- <link href="https://api.tiles.mapbox.com/mapbox.js/v2.2.2 /mapbox.css" rel="stylesheet" />
96
+ <script src="https://api.tiles.mapbox.com/mapbox.js/v3.0.1 /mapbox.js"></script>
97
+ <link href="https://api.tiles.mapbox.com/mapbox.js/v3.0.1 /mapbox.css" rel="stylesheet" />
87
98
<style>
88
99
body { margin:0; padding:0; }
89
100
#map { position:absolute; top:0; bottom:0; width:100%; }
0 commit comments