1818# ' library("sf")
1919# '
2020# ' set.seed(123)
21- # ' lonlat <- data.frame(lon = runif(1, 10, 12),
21+ # ' lonlat = data.frame(lon = runif(1, 10, 12),
2222# ' lat = runif(1, 45, 47))
2323# '
24- # ' gjson <- as.geojson(lonlat)
24+ # ' gjson = as.geojson(lonlat)
2525# '
2626# ' #################
2727# '
3030# ' library("sf")
3131# '
3232# ' set.seed(123)
33- # ' lonlat <- data.frame(lon = runif(5, 10, 12),
33+ # ' lonlat = data.frame(lon = runif(5, 10, 12),
3434# ' lat = runif(5, 45, 47))
3535# '
36- # ' lonlat <- st_as_sf(lonlat, coords = c("lon","lat"))
36+ # ' lonlat = st_as_sf(lonlat, coords = c("lon","lat"))
3737# '
38- # ' gjson <- as.geojson(lonlat)
38+ # ' gjson = as.geojson(lonlat)
3939# '
4040# ' @importFrom sf st_point st_sfc st_buffer st_write st_as_sf
4141# ' @export
42- as.geojson <- function (lonlat ,
42+ as.geojson = function (lonlat ,
4343 dist = 0.00001 ,
4444 nQuadSegs = 2L ,
4545 ... ) {
@@ -48,61 +48,61 @@ as.geojson <- function(lonlat,
4848
4949# ' @rdname as.geojson
5050# ' @export
51- as.geojson.default <- function (lonlat ,
51+ as.geojson.default = function (lonlat ,
5252 dist = 0.00001 ,
5353 nQuadSegs = 2L ,
5454 ... ) {
55- n <- dim(lonlat )[[1 ]]
55+ n = dim(lonlat )[[1 ]]
5656
5757 # lonlat into matrix
58- lonlat <- as.matrix(lonlat )
58+ lonlat = as.matrix(lonlat )
5959
6060 # split lonlat by rows
61- lonlat <- split(lonlat , seq_len(n ))
61+ lonlat = split(lonlat , seq_len(n ))
6262
6363 # transform into sf points
64- lonlat <- lapply(lonlat , function (l ) {
64+ lonlat = lapply(lonlat , function (l ) {
6565 sf :: st_point(l )
6666 })
6767
6868 # and then into a geometry list column
69- lonlat <- sf :: st_sfc(lonlat ,
69+ lonlat = sf :: st_sfc(lonlat ,
7070 crs = " +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0" )
7171
7272 # set the buffer around the points
73- lonlatb <- sf :: st_buffer(lonlat ,
73+ lonlatb = sf :: st_buffer(lonlat ,
7474 dist = dist ,
7575 nQuadSegs = nQuadSegs )
7676
7777 # transform into a sf object
78- lonlatb <- sf :: st_as_sf(x = lonlatb )
78+ lonlatb = sf :: st_as_sf(x = lonlatb )
7979
8080
8181 # write the geojson string
82- tf <- tempfile(fileext = " .geojson" )
82+ tf = tempfile(fileext = " .geojson" )
8383 sf :: st_write(lonlatb , tf , quiet = TRUE )
8484
8585 # capture these strings
86- gj <- readLines(tf )
86+ gj = readLines(tf )
8787
8888 # keep only the geometry vectors
89- index <- which(grepl(" geometry" , unlist(gj )))
89+ index = which(grepl(" geometry" , unlist(gj )))
9090
91- gj <- gj [index ]
91+ gj = gj [index ]
9292
9393 # remove spaces and extra commas
94- gj <- lapply(gj , function (x ) {
95- x <- strsplit(x , " }," )[[1 ]][2 ]
96- x <- gsub(" " , " " , x )
97- x <- gsub(" }}," , " }}" , x )
98- x <- gsub(' "geometry\" :' , " " , x )
99- x <- gsub(' ]}}' , " ]}" , x )
94+ gj = lapply(gj , function (x ) {
95+ x = strsplit(x , " }," )[[1 ]][2 ]
96+ x = gsub(" " , " " , x )
97+ x = gsub(" }}," , " }}" , x )
98+ x = gsub(' "geometry\" :' , " " , x )
99+ x = gsub(' ]}}' , " ]}" , x )
100100 x
101101 })
102102
103- result <- unlist(gj )
103+ result = unlist(gj )
104104
105- class(result ) <- c(" geojson" , " json" , class(result ))
105+ class(result ) = c(" geojson" , " json" , class(result ))
106106
107107 return (result )
108108
@@ -111,15 +111,15 @@ as.geojson.default <- function(lonlat,
111111# ' @rdname as.geojson
112112# ' @method as.geojson sf
113113# ' @export
114- as.geojson.sf <- function (lonlat ,
114+ as.geojson.sf = function (lonlat ,
115115 dist = 0.00001 ,
116116 nQuadSegs = 2L ,
117117 ... ) {
118118 # check geometry type
119- type <- c(" POINT" , " POLYGON" )
119+ type = c(" POINT" , " POLYGON" )
120120
121121 # check for supported types
122- supp_type <-
122+ supp_type =
123123 c(all(grepl(type [[1 ]], sf :: st_geometry_type(lonlat ))),
124124 all(grepl(type [[2 ]], sf :: st_geometry_type(lonlat ))))
125125
@@ -130,53 +130,53 @@ as.geojson.sf <- function(lonlat,
130130 )
131131 }
132132
133- type <- type [which(supp_type )]
133+ type = type [which(supp_type )]
134134
135135 if (type == " POINT" ) {
136- n <- dim(lonlat )[[1 ]]
136+ n = dim(lonlat )[[1 ]]
137137
138138 # set the buffer around the points
139- lonlatb <- sf :: st_buffer(lonlat ,
139+ lonlatb = sf :: st_buffer(lonlat ,
140140 dist = dist ,
141141 nQuadSegs = nQuadSegs ,
142142 ... )
143143
144144 # transform into a sf object
145- lonlatb <- sf :: st_as_sf(lonlatb ,
145+ lonlatb = sf :: st_as_sf(lonlatb ,
146146 crs = " +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0" )
147147 }
148148
149149 if (type == " POLYGON" ) {
150- lonlatb <- lonlat
150+ lonlatb = lonlat
151151
152152 }
153153
154154
155155 # write the geojson string
156- tf <- tempfile(fileext = " .geojson" )
156+ tf = tempfile(fileext = " .geojson" )
157157 sf :: st_write(lonlatb , tf , quiet = TRUE )
158158
159159 # capture these strings
160- gj <- readLines(tf )
160+ gj = readLines(tf )
161161
162162 # keep only the geometry vectors
163- index <- which(grepl(" geometry" , unlist(gj )))
163+ index = which(grepl(" geometry" , unlist(gj )))
164164
165- gj <- gj [index ]
165+ gj = gj [index ]
166166
167167 # remove spaces and extra commas
168- gj <- lapply(gj , function (x ) {
169- x <- strsplit(x , " }," )[[1 ]][2 ]
170- x <- gsub(" " , " " , x )
171- x <- gsub(" }}," , " }}" , x )
172- x <- gsub(' "geometry\" :' , " " , x )
173- x <- gsub(' ]}}' , " ]}" , x )
168+ gj = lapply(gj , function (x ) {
169+ x = strsplit(x , " }," )[[1 ]][2 ]
170+ x = gsub(" " , " " , x )
171+ x = gsub(" }}," , " }}" , x )
172+ x = gsub(' "geometry\" :' , " " , x )
173+ x = gsub(' ]}}' , " ]}" , x )
174174 x
175175 })
176176
177- result <- unlist(gj )
177+ result = unlist(gj )
178178
179- class(result ) <- c(" geojson" , " json" , class(result ))
179+ class(result ) = c(" geojson" , " json" , class(result ))
180180
181181 return (result )
182182
0 commit comments