Using plotKML::kml_layer inside a function causes error if object passed to z.lim argument

123 Views Asked by At

I'm having an odd issue with the R plotKML package in which I can use the kml_layer() function just fine up until I try to incorporate the kml_layer into my own function and pass arguments from my custom function along to the kml_layer function. I'm encountering this issue only with the kml_layer.Raster() method so far. The error that is produces is: Error in eval(call.lst[["z.lim"]]) : object 'rangeArg' not found with 'rangeArg' being my own object from the example below.

I assume this is a general environment/scoping issue but I've never encountered it before and I can't reproduce it anywhere else. Here is a reproducible example.

library(raster)
library(plotKML)
# load a small raster dataset referenced by raster package:
elevation <- getData('alt', country='CHE') 
ElevationRange <- c(2000,3000)

#Works:----------------
plotKML::kml_open("test",overwrite=TRUE)
plotKML::kml_layer(obj = elevation,
                   colour=CHE_msk_alt,
                   z.lim=c(2000,3000))  #Testing  z.lim argument without named object
plotKML::kml_close(file.name="test.kml")

#Works:-------------------------
plotKML::kml_open("test",overwrite=TRUE)
plotKML::kml_layer(obj = elevation,
                   colour=CHE_msk_alt,
                   z.lim=ElevationRange) #Testing z.lim with an object being passed
plotKML::kml_close(file.name="test.kml")

#Doesn't Work:-----------------------
writeSimpleRasterKML <- function(rangeArg = ElevationRange){
  plotKML::kml_open("test",overwrite=TRUE)
  plotKML::kml_layer(obj = elevation,
                     colour=CHE_msk_alt,
                     z.lim=rangeArg)  #Wrapping code in function and passing a function argument to z.lim
  plotKML::kml_close(file.name="test.kml")
}

writeSimpleRasterKML()  #ERROR HERE

See:

> writeSimpleRasterKML()
KML file opened for writing...
Error in eval(call.lst[["z.lim"]]) : object 'rangeArg' not found
Called from: eval(call.lst[["z.lim"]])
1

There are 1 best solutions below

0
Adam C On

I posted this question because it is a bug in a commonly used R package that others might come across when they google the error message. Here's the fix that I used. I would love for someone to explain the origin of the error so that it could be fixed in a less ham-fisted way. But for now, reclassifying your raster based on your desired range within your custom function should fix the problem. See below:

writeSimpleRasterKMLFixed <- function(rasterArg = elevation, rangeArg = ElevationRange){

  tempRaster <- rasterArg
  tempRaster[tempRaster < rangeArg[1]] <- rangeArg[1]
  tempRaster[tempRaster > rangeArg[2]] <- rangeArg[2]

  plotKML::kml_open("test",overwrite=TRUE)
  plotKML::kml_layer(obj = tempRaster,colour=CHE_msk_alt)
  plotKML::kml_close(file.name="test.kml")
}

writeSimpleRasterKMLFixed()

It works:

> writeSimpleRasterKMLFixed()
KML file opened for writing...
Writing to KML...
Closing  test.kml