[R-sig-Geo] plotKML kml_layer.Raster problem
Frede Aakmann Tøgersen
frtog at vestas.com
Mon Nov 10 08:16:30 CET 2014
Hi (cc’ing maintainer since I find the documentation to be unclear on how to use some of the arguments to the kml functions)
Well, I cannot run your example since I don’t have the r.ex, bigraster, and e.cRamplette() objects.
Using the following I can reproduce your error.
## using SAGA_pal instead of e.cRamplette()
data(SAGA_pal)
pheight = 100
pwidth = 200
raster_layer <- raster(ncols=pwidth, nrows=pheight,
xmn=20, xmx=25,
ymn=30, ymx=35,
crs=CRS("+proj=longlat +datum=WGS84"))
values(raster_layer) <- rnorm(pwidth*pheight, mean = 50, sd = 10)
kml_open(file.name="test.kml",
folder.name='.',
kml_visibility=TRUE)
kml_layer.Raster(raster_layer, folder.name='raster_sub_folder',
plot.legend=TRUE,
metadata=NULL,
colour=SAGA_pal[[1]],
raster_name='raster_layer',
layer.name=names(raster_layer)[1],
png.width=1000,
png.height=833
)
kml_close(file.name="test.kml")
> traceback()
6: stop("values must be numeric, integer, logical or factor")
5: setValues(x, value)
4: setValues(x, value)
3: `values<-`(`*tmp*`, value = c("#000180", "#0007A7", "#0019CA",
"#0035E6", "#0058F8", "#017FFF", "#07A7F8", "#19CAE6", "#35E6CA",
"#58F8A7", "#80FF80", "#A7F858", "#CAE635", "#E6CA19", "#F8A707",
"#FF8001", "#F85800", "#E63500", "#CA1900", "#A70700"))
2: `values<-`(`*tmp*`, value = c("#000180", "#0007A7", "#0019CA",
"#0035E6", "#0058F8", "#017FFF", "#07A7F8", "#19CAE6", "#35E6CA",
"#58F8A7", "#80FF80", "#A7F858", "#CAE635", "#E6CA19", "#F8A707",
"#FF8001", "#F85800", "#E63500", "#CA1900", "#A70700"))
1: kml_layer.Raster(raster_layer, folder.name = "raster_sub_folder",
plot.legend = TRUE, metadata = NULL, colour = SAGA_pal[[1]],
raster_name = "raster_layer", layer.name = names(raster_layer)[1],
png.width = 1000, png.height = 833)
Debugging the kml_layer.Raster function shows the “culprit” is this part of code:
if (is.call(call[["colour"]]) | is.name(call[["colour"]])) {
x <- data.frame(getValues(obj))
names(x) <- names(obj)
x <- eval(call[["colour"]], x)
obj <- raster(obj)
values(obj) <- x
}
else {
This looks strange so specifying the colour scheme using the colour argument seems to be wrong.
Looking at the man pages for kml and kml_layer.Raster there one finds that the colour scheme is specified by the colour_grade argument.
Indeed, your example works if one use that argument.
Well information on how to use colour or colour_grade argument is not well documented. Here for ?kml_layer.Raster:
kml_layer.Raster package:plotKML R Documentation
Writes raster objects to KML
Description:
Writes rasters to PNG images and makes a KML code (ground
overlays). Works with '"RasterLayer"' and '"RasterStack"' class
objects. Target attributes can be specified using aesthetics
arguments (e.g. '"colour"').
And from ?kml:
kml-methods package:plotKML R Documentation
Write to a KML file
Description:
Writes any 'Spatial*' object (from the sp package) or 'Raster*'
object (from the raster package) to a KML file via the
'plotKML.fileIO' environment. Various _aesthetics_ parameters can
be set via 'colour', 'alpha', 'size', 'shape' arguments. Their
availability depends on the class of the object to plot.
And ?kml_aes does not really help:
aesthetics package:plotKML R Documentation
Plotting aesthetics parameters
Description:
Parses various object parameters / columns to KML aesthetics: size
of the icons, fill color, labels, altitude, width, ...
Usage:
kml_aes(obj, ...)
Arguments:
obj: space-time object for plotting
...: other arguments
Details:
Valid aesthetics: 'colour = "black"', 'fill = "white"', 'shape',
'whitening', 'alpha', 'width = 1', 'labels', 'altitude = 0',
'size', 'balloon = FALSE'. Specific features (target variables and
the connected hot-spots) can be emphasized by using two or three
graphical parameters for the same variable. See plotKML package
homepage / vignette for more examples.
Author(s):
Pierre Roudier
See Also:
'kml-methods'
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: 7. november 2014 18:12
To: Frede Aakmann Tøgersen
Cc: r-sig-geo at r-project.org
Subject: Re: [R-sig-Geo] plotKML kml_layer.Raster problem
Hi Frede,
This is the test case (using just values of 1): (traceback() below)
pheight = 100
pwidth = 200
> raster_layer <- raster(ncols=pwidth, nrows=pheight,
+ xmn=xmin(r.ex), xmx=xmax(r.ex),
+ ymn=ymin(r.ex), ymx=ymax(r.ex),
+ crs=CRS(projection(bigraster)))
> 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 file opened for writing...
> kml_layer.Raster(raster_layer, folder.name<http://folder.name/>='raster_sub_folder',
+ plot.legend=TRUE,
+ metadata=NULL,
+ colour=e.cRampPalette(255),
+ raster_name='raster_layer',
+ layer.name<http://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
> kml_close(file.name<http://file.name/>="test.kml")
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<http://folder.name/> = "raster_sub_folder",
plot.legend = TRUE, metadata = NULL, colour = e.cRampPalette(255),
raster_name = "raster_layer", layer.name<http://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<http://file.name>="test.kml",
+ folder.name<http://folder.name>='top_folder',
+ kml_visibility=TRUE)
KML file opened for writing...
> kml_layer.Raster(raster_layer, folder.name<http://folder.name>='raster_sub_folder',
+ plot.legend=TRUE,
+ metadata=NULL,
+ colour_scheme=e.cRampPalette(255),
+ raster_name='raster_layer',
+ layer.name<http://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
> kml_close(file.name<http://file.name>="test.kml")
Closing test.kml
> traceback()
2: is.factor(obj at data[, 1])
1: kml_layer.Raster(raster_layer, folder.name<http://folder.name> = "raster_sub_folder",
plot.legend = TRUE, metadata = NULL, colour_scheme = e.cRampPalette(255),
raster_name = "raster_layer", layer.name<http://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<mailto:frtog at vestas.com>> wrote:
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<tel:%2B45%209730%205135>
M +45 2547 6050<tel:%2B45%202547%206050>
frtog at vestas.com<mailto: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<http://www.vestas.com/legal/notice>
If you have received this e-mail in error please contact the sender.
> -----Original Message-----
> From: r-sig-geo-bounces at r-project.org<mailto:r-sig-geo-bounces at r-project.org> [mailto:r-sig-geo-bounces at r-<mailto:r-sig-geo-bounces at r->
> project.org<http://project.org>] On Behalf Of Andreas Gros
> Sent: 6. november 2014 23:47
> To: r-sig-geo at r-project.org<mailto:r-sig-geo at r-project.org>
> Subject: [R-sig-Geo] plotKML kml_layer.Raster problem
>
> 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!
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org<mailto:R-sig-Geo at r-project.org>
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
[[alternative HTML version deleted]]
More information about the R-sig-Geo
mailing list