@@ -175,7 +175,7 @@ epsg3857 <- "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y
175
175
176
176
# ' Add a raster image as a layer
177
177
# '
178
- # ' Create an image overlay from a \code{RasterLayer} or a \code{SpatRaster}
178
+ # ' Create an image overlay from a \code{RasterLayer} or a \code{SpatRaster}
179
179
# ' object. \emph{This is only suitable for small to medium sized rasters},
180
180
# ' as the entire image will be embedded into the HTML page (or passed over
181
181
# ' the websocket in a Shiny context).
@@ -190,13 +190,13 @@ epsg3857 <- "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y
190
190
# '
191
191
# ' To reduce the size of a SpatRaster, you can use \code{\link[terra]{spatSample}}
192
192
# ' as in \code{x = spatSample(x, 100000, method="regular", as.raster=TRUE)}. With
193
- # ' a \code{RasterLayer} you can use \code{\link[raster]{sampleRegular}} as in
193
+ # ' a \code{RasterLayer} you can use \code{\link[raster]{sampleRegular}} as in
194
194
# ' \code{sampleRegular(x, 100000, asRaster=TRUE)}.
195
195
# '
196
196
# ' By default, the \code{addRasterImage} function will project the raster data
197
- # ' \code{x} to the Pseudo-Mercator projection (EPSG:3857). This can be a
197
+ # ' \code{x} to the Pseudo-Mercator projection (EPSG:3857). This can be a
198
198
# ' time-consuming operation for even moderately sized rasters; although it is much
199
- # ' faster for SpatRasters than for RasterLayers.
199
+ # ' faster for SpatRasters than for RasterLayers.
200
200
# ' If you are repeatedly adding a particular raster to your Leaflet
201
201
# ' maps, you can perform the projection ahead of time using
202
202
# ' \code{projectRasterForLeaflet()}, and call \code{addRasterImage} with
@@ -250,37 +250,37 @@ addRasterImage <- function(
250
250
maxBytes = 4 * 1024 * 1024 ,
251
251
data = getMapData(map )
252
252
) {
253
- if (inherits(x , " SpatRaster" )) {
254
- addRasterImage_SpatRaster(
255
- map = map ,
256
- x = x ,
257
- colors = colors ,
258
- opacity = opacity ,
259
- attribution = attribution ,
260
- layerId = layerId ,
261
- group = group ,
262
- project = project ,
263
- method = method ,
264
- maxBytes = maxBytes ,
265
- data = data
266
- )
267
- } else if (inherits(x , " RasterLayer" )) {
268
- addRasterImage_RasterLayer(
269
- map = map ,
270
- x = x ,
271
- colors = colors ,
272
- opacity = opacity ,
273
- attribution = attribution ,
274
- layerId = layerId ,
275
- group = group ,
276
- project = project ,
277
- method = method ,
278
- maxBytes = maxBytes ,
279
- data = data
280
- )
281
- } else {
282
- stop(" Don't know how to get path data from object of class " , class(x )[[1 ]])
283
- }
253
+ if (inherits(x , " SpatRaster" )) {
254
+ addRasterImage_SpatRaster(
255
+ map = map ,
256
+ x = x ,
257
+ colors = colors ,
258
+ opacity = opacity ,
259
+ attribution = attribution ,
260
+ layerId = layerId ,
261
+ group = group ,
262
+ project = project ,
263
+ method = method ,
264
+ maxBytes = maxBytes ,
265
+ data = data
266
+ )
267
+ } else if (inherits(x , " RasterLayer" )) {
268
+ addRasterImage_RasterLayer(
269
+ map = map ,
270
+ x = x ,
271
+ colors = colors ,
272
+ opacity = opacity ,
273
+ attribution = attribution ,
274
+ layerId = layerId ,
275
+ group = group ,
276
+ project = project ,
277
+ method = method ,
278
+ maxBytes = maxBytes ,
279
+ data = data
280
+ )
281
+ } else {
282
+ stop(" Don't know how to get path data from object of class " , class(x )[[1 ]])
283
+ }
284
284
}
285
285
286
286
@@ -298,7 +298,7 @@ addRasterImage_RasterLayer <- function(
298
298
data = getMapData(map )
299
299
) {
300
300
301
-
301
+
302
302
raster_is_factor <- raster :: is.factor(x )
303
303
method <- match.arg(method )
304
304
if (method == " auto" ) {
@@ -373,10 +373,10 @@ addRasterImage_SpatRaster <- function(
373
373
) {
374
374
375
375
if (terra :: nlyr(x ) > 1 ) {
376
- x <- x [[1 ]]
377
- warning(" using the first layer in 'x'" , call. = FALSE )
376
+ x <- x [[1 ]]
377
+ warning(" using the first layer in 'x'" , call. = FALSE )
378
378
}
379
-
379
+
380
380
raster_is_factor <- terra :: is.factor(x )
381
381
method <- match.arg(method )
382
382
if (method == " ngb" ) method = " near"
@@ -395,13 +395,13 @@ addRasterImage_SpatRaster <- function(
395
395
# do not project data
396
396
projected <- x
397
397
}
398
-
398
+
399
399
bounds <- terra :: ext(
400
400
terra :: project(
401
- terra :: project(
402
- terra :: as.points(terra :: ext(x ), crs = terra :: crs(x )),
403
- epsg3857 ),
404
- epsg4326 )
401
+ terra :: project(
402
+ terra :: as.points(terra :: ext(x ), crs = terra :: crs(x )),
403
+ epsg3857 ),
404
+ epsg4326 )
405
405
)
406
406
407
407
if (! is.function(colors )) {
@@ -444,30 +444,30 @@ addRasterImage_SpatRaster <- function(
444
444
# ' @export
445
445
projectRasterForLeaflet <- function (x , method ) {
446
446
if (inherits(x , " SpatRaster" )) {
447
- if (method == " ngb" ) {
447
+ if (method == " ngb" ) {
448
448
method = " near"
449
- }
449
+ }
450
450
terra :: project(
451
- x ,
452
- y = epsg3857 ,
453
- method = method
454
- )
451
+ x ,
452
+ y = epsg3857 ,
453
+ method = method
454
+ )
455
455
} else {
456
- raster_is_factor <- raster :: is.factor(x );
457
- projected <- raster :: projectRaster(
458
- x ,
459
- raster :: projectExtent(x , crs = sp :: CRS(epsg3857 )),
460
- method = method
456
+ raster_is_factor <- raster :: is.factor(x );
457
+ projected <- raster :: projectRaster(
458
+ x ,
459
+ raster :: projectExtent(x , crs = sp :: CRS(epsg3857 )),
460
+ method = method
461
461
)
462
462
# if data is factor data, make the result factors as well.
463
- # only meaningful if ngb was used
463
+ # only meaningful if ngb was used
464
464
if ((raster_is_factor ) && (method == " ngb" )) {
465
465
raster :: as.factor(projected )
466
466
} else {
467
- projected
468
- }
469
-
470
- }
467
+ projected
468
+ }
469
+
470
+ }
471
471
}
472
472
473
473
# ' @rdname remove
0 commit comments