Skip to content

plotKML kml_layer.Raster problem

8 messages · Tomislav Hengl, Andreas Gros, Frede Aakmann Tøgersen

#
In version 0.4-6 of plotKML, when I'm adding a kml_layer.Raster (single
layer, no stack/brick) to a kml file, I get the following error message:

Error in setValues(x, value) :
values must be numeric, integer, logical or factor


The values in the raster range between -57 and 2000 and it also occurs if I
replace NA values first with some arbitrary numerical value.
Has anyone come across this problem and knows how to fix/circumnavigate it?

Thanks!
#
Hi Andreas

Can you provide a small example showing the details of what you're trying to do. I don't think that anyone can help you with the little information you have given.

If you do not have any example please show us the output from traceback() immediately after the error.

I don't think this has anything to do with NA's in your data values as I don't get any errors doing this (from man of kml_layer.Raster):


data(eberg_grid)
library(sp)
coordinates(eberg_grid) <- ~x+y
gridded(eberg_grid) <- TRUE
proj4string(eberg_grid) <- CRS("+init=epsg:31467")
data(SAGA_pal)
library(raster)
r <- raster(eberg_grid["TWISRT6"])

## Put some NA's into data
r at data@values[1:10] <- NA

kml(r, colour_scale = SAGA_pal[[1]], colour = TWISRT6) 


 

Yours sincerely / Med venlig hilsen


Frede Aakmann T?gersen
Specialist, M.Sc., Ph.D.
Plant Performance & Modeling

Technology & Service Solutions
T +45 9730 5135
M +45 2547 6050
frtog at vestas.com
http://www.vestas.com

Company reg. name: Vestas Wind Systems A/S
This e-mail is subject to our e-mail disclaimer statement.
Please refer to www.vestas.com/legal/notice
If you have received this e-mail in error please contact the sender.
#
I would assume that the value column in the Raster object is somehow 
converted to "character" class.

If you could provide access to your data and code we can do a better job 
(it could be that there is also a bug in plotKML i.e. completely not 
your fault).

HTH,

T. Hengl
On 7-11-2014 7:52, Frede Aakmann T?gersen wrote:
#
Hi Frede,

This is the test case (using just values of 1): (traceback() below)

pheight = 100
pwidth = 200
+                        xmn=xmin(r.ex), xmx=xmax(r.ex),
+                        ymn=ymin(r.ex), ymx=ymax(r.ex),
+                        crs=CRS(projection(bigraster)))
+   folder.name='top_folder',
+   kml_visibility=TRUE)
KML file opened for writing...
+   plot.legend=TRUE,
+   metadata=NULL,
+   colour=e.cRampPalette(255),
+   raster_name='raster_layer',
+   layer.name=names(raster_layer)[1],
+   png.width=1000,
+   png.height=833
+ )

Error in setValues(x, value) :
  values must be numeric, integer, logical or factor
Closing  test.kml

Traceback:
traceback()
6: stop("values must be numeric, integer, logical or factor")
5: setValues(x, value)
4: setValues(x, value)
3: `values<-`(`*tmp*`, value = c("#000000", "#010101", "#020202",
   "#030303", "#040404", "#060606", "#070707", "#080808", "#090909",
   "#0A0A0A", "#0C0C0C", "#0D0D0D", "#0E0E0E", "#0F0F0F", "#0F0F0F",
   "#101010", "#111111", "#121212", "#131313", "#141414", "#141414",
   "#151515", "#161616", "#161616", "#171717", "#181818", "#181818",
   "#191919", "#1A1A1A", "#1A1A1A", "#1B1B1B", "#1C1C1C", "#1C1C1C",
   "#1D1D1D", "#1E1E1E", "#1E1E1E", "#1F1F1F", "#202020", "#202020",
   "#212121", "#222222", "#222222", "#232323", "#242424", "#242424",
   "#252525", "#262626", "#272727", "#272727", "#282828", "#292929",
   "#292929", "#2A2A2A", "#2B2B2B", "#2B2B2B", "#2C2C2C", "#2D2D2D",
   "#2E2E2E", "#2E2E2E", "#2F2F2F", "#303030", "#303030", "#313131",
   "#323232", "#333333", "#333333", "#343434", "#353535", "#363636",
   "#363636", "#373737", "#383838", "#383838", "#393939", "#3A3A3A",
   "#3B3B3B", "#3B3B3B", "#3C3C3C", "#3D3D3D", "#3E3E3E", "#3E3E3E",

...

   ))

2: `values<-`(`*tmp*`, value = c("#000000", "#010101", "#020202",
   "#030303", "#040404", "#060606", "#070707", "#080808", "#090909",
   "#0A0A0A", "#0C0C0C", "#0D0D0D", "#0E0E0E", "#0F0F0F", "#0F0F0F",
   "#101010", "#111111", "#121212", "#131313", "#141414", "#141414",
   "#151515", "#161616", "#161616", "#171717", "#181818", "#181818",
   "#565656", "#575757", "#585858", "#595959", "#5A5A5A", "#5A5A5A",
...

 ))

1: kml_layer.Raster(raster_layer, folder.name = "raster_sub_folder",
       plot.legend = TRUE, metadata = NULL, colour = e.cRampPalette(255),
       raster_name = "raster_layer", layer.name = names(raster_layer)[1],
       png.width = 1000, png.height = 833)


And when I'm trying to use colour_scheme I'm gettiing an error that lets me
assume that kml_layer.Raster is not compatible with 'raster':

kml_open(file.name="test.kml",
+   folder.name='top_folder',
+   kml_visibility=TRUE)
KML file opened for writing...
+   plot.legend=TRUE,
+   metadata=NULL,
+   colour_scheme=e.cRampPalette(255),
+   raster_name='raster_layer',
+   layer.name=names(raster_layer)[1],
+   png.width=1000,
+   png.height=833
+ )

Error in is.factor(obj at data[, 1]) :
  error in evaluating the argument 'x' in selecting a method for function
'is.factor': Error in obj at data[, 1] : object of type 'S4' is not subsettable
Closing  test.kml
2: is.factor(obj at data[, 1])
1: kml_layer.Raster(raster_layer, folder.name = "raster_sub_folder",
       plot.legend = TRUE, metadata = NULL, colour_scheme =
e.cRampPalette(255),
       raster_name = "raster_layer", layer.name = names(raster_layer)[1],
       png.width = 1000, png.height = 833)

On Thu, Nov 6, 2014 at 10:52 PM, Frede Aakmann T?gersen <frtog at vestas.com>
wrote:

  
  
2 days later
#
The argument "colour" is reserved for the target variable (name, 
function/call or vector). plotKML allows users to visualize multiple 
variables using different aesthetics 
(http://plotkml.r-forge.r-project.org/aesthetics.html), so in your case 
this would be "colour = layer, colour_scale = e.cRampPalette(255)", 
although it might take you few iterations until you get the right legend 
/ PNG output.

To learn how to use lower level functions such as kml_layer.Raster maybe 
first study the package vignette 
(http://cran.r-project.org/web/packages/plotKML/vignettes/jss1079.pdf) 
and/or the package tutorial 
(http://gsif.isric.org/doku.php?id=wiki:tutorial_plotkml).

Sorry for some confusion between "colour" and "colour_scale".

T. Hengl
On 8-11-2014 1:12, Andreas Gros wrote:
2 days later
#
Thanks guys, but I still get the same error in this little toy example:


library(raster)
library(plotKML)

e.cRampPalette = colorRampPalette(
  c("black", "lightgray"), 1,
  space=c("Lab"),
  interpolate=c("spline"))

pwidth = 50
pheight = 50

r.ex <- extent(0, 1, 0, 1)
pproj <- "+proj=longlat +ellps=WGS84 +datum=WGS84"
raster_layer <- raster(ncols=pwidth, nrows=pheight,
                       xmn=xmin(r.ex), xmx=xmax(r.ex),
                       ymn=ymin(r.ex), ymx=ymax(r.ex),
                       crs=pproj)

values(raster_layer) <- rep(1, pwidth*pheight)

kml_open(file.name="test.kml",
  folder.name='top_folder',
  kml_visibility=TRUE)
kml_layer.Raster(raster_layer, folder.name='raster_sub_folder',
  plot.legend=TRUE,
  metadata=NULL,
  colour=names(raster_layer)[1],
  colour_scale=e.cRampPalette(255),
  raster_name='raster_layer',
  layer.name=names(raster_layer)[1],
  png.width=50,
  png.height=50
)
kml_close(file.name="test.kml")
Error in setValues(x, value) :
values must be numeric, integer, logical or factor
[1] "layer"
class       : RasterLayer
dimensions  : 50, 50, 2500  (nrow, ncol, ncell)
resolution  : 0.02, 0.02  (x, y)
extent      : 0, 1, 0, 1  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +ellps=WGS84 +datum=WGS84
data source : in memory
names       : layer
values      : 1, 1  (min, max)
6: stop("values must be numeric, integer, logical or factor")
5: setValues(x, value)
4: setValues(x, value)
3: `values<-`(`*tmp*`, value = "layer")
2: `values<-`(`*tmp*`, value = "layer")
1: kml_layer.Raster(raster_layer, folder.name = "raster_sub_folder",
       plot.legend = TRUE, metadata = NULL, colour = names(raster_layer)[1],
       colour_scale = e.cRampPalette(255), raster_name = "raster_layer",
       layer.name = names(raster_layer)[1], png.width = 50, png.height = 50)
On Mon, Nov 10, 2014 at 2:01 AM, Tomislav Hengl <hengl at spatial-analyst.net>
wrote:
function/call or vector). plotKML allows users to visualize multiple
variables using different aesthetics (
http://plotkml.r-forge.r-project.org/aesthetics.html), so in your case this
would be "colour = layer, colour_scale = e.cRampPalette(255)", although it
might take you few iterations until you get the right legend / PNG output.
first study the package vignette (
http://cran.r-project.org/web/packages/plotKML/vignettes/jss1079.pdf)
and/or the package tutorial (
http://gsif.isric.org/doku.php?id=wiki:tutorial_plotkml).
e.cRampPalette(255),
names(raster_layer)[1],
me
function
subsettable
names(raster_layer)[1],
trying
information
traceback()
message:

  
  
#
Hi

Tomislav has already given you the answer to this problem. Did you read http://cran.r-project.org/web/packages/plotKML/vignettes/jss1079.pdf as he suggested. There you can see the solution on e.g. page 9. The same thing you can see in ?kml for the eberg raster:
[1] "ID"       "soiltype" "TAXGRSC"  "X"        "Y"        "UHDICM_A"
[7] "LHDICM_A" "SNDMHT_A" "SLTMHT_A" "CLYMHT_A" "UHDICM_B" "LHDICM_B"
[13] "SNDMHT_B" "SLTMHT_B" "CLYMHT_B" "UHDICM_C" "LHDICM_C" "SNDMHT_C"
[19] "SLTMHT_C" "CLYMHT_C" "UHDICM_D" "LHDICM_D" "SNDMHT_D" "SLTMHT_D"
[25] "CLYMHT_D" "UHDICM_E" "LHDICM_E" "SNDMHT_E" "SLTMHT_E" "CLYMHT_E"
# Plot using aesthetics
kml(eberg, colour = SNDMHT_A, size = CLYMHT_A, alpha = 0.75, file = "eberg-1.kml")

You can see that colour is set to the SNDMHT_A layer without apostrophes.

For your raster we have
[1] "layer"
So don?t use

colour=names(raster_layer)[1],

but

colour=layer

Yours sincerely / Med venlig hilsen


Frede Aakmann T?gersen
Specialist, M.Sc., Ph.D.
Plant Performance & Modeling

Technology & Service Solutions
T +45 9730 5135
M +45 2547 6050
frtog at vestas.com<mailto:frtog at vestas.com>
http://www.vestas.com<http://www.vestas.com/>

Company reg. name: Vestas Wind Systems A/S
This e-mail is subject to our e-mail disclaimer statement.
Please refer to www.vestas.com/legal/notice<http://www.vestas.com/legal/notice>
If you have received this e-mail in error please contact the sender.

From: Andreas Gros [mailto:andigros72 at gmail.com]
Sent: 12. november 2014 20:55
To: Tomislav Hengl
Cc: Frede Aakmann T?gersen; r-sig-geo at r-project.org
Subject: Re: [R-sig-Geo] plotKML kml_layer.Raster problem

Thanks guys, but I still get the same error in this little toy example:


library(raster)
library(plotKML)

e.cRampPalette = colorRampPalette(
  c("black", "lightgray"), 1,
  space=c("Lab"),
  interpolate=c("spline"))

pwidth = 50
pheight = 50

r.ex <- extent(0, 1, 0, 1)
pproj <- "+proj=longlat +ellps=WGS84 +datum=WGS84"
raster_layer <- raster(ncols=pwidth, nrows=pheight,
                       xmn=xmin(r.ex), xmx=xmax(r.ex),
                       ymn=ymin(r.ex), ymx=ymax(r.ex),
                       crs=pproj)

values(raster_layer) <- rep(1, pwidth*pheight)

kml_open(file.name<http://file.name>="test.kml",
  folder.name<http://folder.name>='top_folder',
  kml_visibility=TRUE)
kml_layer.Raster(raster_layer, folder.name<http://folder.name>='raster_sub_folder',
  plot.legend=TRUE,
  metadata=NULL,
  colour=names(raster_layer)[1],
  colour_scale=e.cRampPalette(255),
  raster_name='raster_layer',
  layer.name<http://layer.name>=names(raster_layer)[1],
  png.width=50,
  png.height=50
)
kml_close(file.name<http://file.name>="test.kml")
Error in setValues(x, value) :
values must be numeric, integer, logical or factor
[1] "layer"
class       : RasterLayer
dimensions  : 50, 50, 2500  (nrow, ncol, ncell)
resolution  : 0.02, 0.02  (x, y)
extent      : 0, 1, 0, 1  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +ellps=WGS84 +datum=WGS84
data source : in memory
names       : layer
values      : 1, 1  (min, max)
6: stop("values must be numeric, integer, logical or factor")
5: setValues(x, value)
4: setValues(x, value)
3: `values<-`(`*tmp*`, value = "layer")
2: `values<-`(`*tmp*`, value = "layer")
1: kml_layer.Raster(raster_layer, folder.name<http://folder.name> = "raster_sub_folder",
       plot.legend = TRUE, metadata = NULL, colour = names(raster_layer)[1],
       colour_scale = e.cRampPalette(255), raster_name = "raster_layer",
       layer.name<http://layer.name> = names(raster_layer)[1], png.width = 50, png.height = 50)

        
On Mon, Nov 10, 2014 at 2:01 AM, Tomislav Hengl <hengl at spatial-analyst.net<mailto:hengl at spatial-analyst.net>> wrote:

  
  
#
My bad! Thanks a lot, Frede!

On Wed, Nov 12, 2014 at 10:42 PM, Frede Aakmann T?gersen <frtog at vestas.com>
wrote: