[R-sig-Geo] plotKML package - Invalid multibyte string 1?

Azimoth til.dlh at gmx.de
Wed Jun 4 17:29:44 CEST 2014


Hi,
apologies if this is already explained somewhere, but I could not find it so
far.

I tried to figure out how to use the plotKML package and ran into the
following error:
Error in nchar(levels(as.factor(x))) : invalid multibyte string 1

Is this a bug in the package or my mistake? How can I make it work?

This is my first post in any mailing list at all, so I don't have experience
in what you need to help me to help myself.

Since I don't know a better way I will just provide, what I think might be
helpful underneath.

I put it in this order:
1. My first try to understand this problem
2. Output of version
3. Output of traceback()
4. The code I used (but no local data is required so it should run on any
machine)

Any tips how to post a question in a more effective way or way that's more
convenient for you are welcome.
And tips on how to narrow down a problem as well.

--------------------

I tried to find a solution by myself without success. Here's what I did:

I tried to find out, what "x" is and this is what seemed possible to me (all
names and variables taken either from the sample code below or from files
included in the plotKML package):

1. I looked for "nchar(levels(as.factor(x)))" from the error traceback and
found it only in line 23 of a file called "legend.bar.R"
And there was a comment right above this line:
 ### NOTE : This is a not a perfect implementation for a factor with a lot
of categories!

2. When I looked for "legend.bar" I found this in line 136 of a file called
"layer.SpatialPixels"
kml_legend.bar(x = obj at data[,1], legend.file = legend_name, legend.pal =
colour_scale, z.lim = eval(call.lst[["z.lim"]]))

I assume that "x = obj at data[,1]" was what I was looking for so my question
changed to "what is x = obj at data[,1]?"

3. "layer.SpatialPixels" can be found in plotKML.sp.R, where in line 365 it
says
kml_layer.SpatialPixels(obj[[i]], colour = colour, raster_name =
raster_name_i, metadata = metadata, plot.legend=FALSE, ...)

4. That's what my code calls in the last line
plotKML(ORCDRC_sd1_country["val"],
colour_scale=soil.legends[["ORCDRC"]]$COLOR)

So I think obj = ORCDRC_sd1_country["val"].

Since I found kml_legend.bar(x = obj at data[,1],[…]) in step 2 above I tried

> max(nchar(levels(as.factor(ORCDRC_sd1_country["val"]@data[,1]))))*5+70
Error in nchar(levels(as.factor(ORCDRC_sd1_country["val"]@data[, 1]))) :
invalid multibyte string 1 

So I could reproduce the error I got before. (Or just found another way to
the same dead end...)
That's why I think this might be what happens when my code is executed.

The only way that SOMETHING worked (I don't know what it does exactly) was:

> max(nchar(levels(as.factor(ORCDRC_sd1_country at data[,1]))))*5+70
[1] 85

Does this make sense? Or is this line just successfully computing rubbish?

--------------------
> version
               _                           
platform       x86_64-apple-darwin10.8.0   
arch           x86_64                      
os             darwin10.8.0                
system         x86_64, darwin10.8.0        
status                                     
major          3                           
minor          0.3                         
year           2014                        
month          03                          
day            06                          
svn rev        65126                       
language       R                           
version.string R version 3.0.3 (2014-03-06)
nickname       Warm Puppy                  

--------------------

> traceback()
8: nchar(levels(as.factor(x)))
7: kml_legend.bar(x = obj at data[, 1], legend.file = legend_name, 
       legend.pal = colour_scale)
6: .local(obj, ...)
5: kml_layer(obj, colour = colour, raster_name = raster_name, metadata =
metadata, 
       ...)
4: kml_layer(obj, colour = colour, raster_name = raster_name, metadata =
metadata, 
       ...)
3: .local(obj, ...)
2: plotKML(ORCDRC_sd1_country["val"], colour_scale =
soil.legends[["ORCDRC"]]$COLOR)
1: plotKML(ORCDRC_sd1_country["val"], colour_scale =
soil.legends[["ORCDRC"]]$COLOR)

--------------------
### The code I had that led to the error ###

library(RCurl)
library(R.utils)
library(maptools)
library(XML)
library(gdalUtils)
library(rgdal)
library(GSIF)
library(plotKML)

## Getting necessary data
## location of soilgrids:
sg.ftp <- "ftp://soilgrids:soilgrids@ftp.soilgrids.org/data/recent/"
filenames = getURL(sg.ftp, ftp.use.epsv = FALSE, dirlistonly = TRUE)
filenames = strsplit(filenames, "\r*\n")[[1]]
filenames[1:5]

ORC.name <- filenames[grep(filenames, pattern="ORCDRC_sd1_M")]
ORC.name

## CAUTION!!! 165.4 Mb to download in total, unzipped 1,87 GB
download.file(paste(sg.ftp, ORC.name[1], sep=""), ORC.name)
library(R.utils)
gunzip(ORC.name[1])

## check that everything is OK:
ORC.tif <- strsplit(ORC.name[1], ".gz")[[1]][1]
GDALinfo(ORC.tif)

## Getting the shape-file (5.1 Mb in total) that contains the desired
country
download.file("http://www.nacis.org/naturalearth/10m/cultural/ne_10m_admin_0_countries.zip",
"ne_10m_admin_0_countries.zip")
unzip("ne_10m_admin_0_countries.zip")

## loading and reducing the shape-file to the desired country
countries <- readShapeSpatial("ne_10m_admin_0_countries.shp")
desired_country <- "Germany"
country_name <- toString(countries$FORMAL_EN[grep(desired_country,
countries$ADMIN)])
country <- countries[grep(desired_country, countries$ADMIN),]
country_lines <- as(country, "SpatialLines")

#plot(country[1]) #Check if everything so far worked as expected

getOption("gdalUtils_gdalPath")
gdal_setInstallation() ## CAN TAKE 1-2 MINUTES!
getOption("gdalUtils_gdalPath")

te = as.vector(country at bbox) #Numeric. (c(xmin,ymin,xmax,ymax)). set
georeferenced extents of output file to be created (in target SRS).

tile_file <- paste("ORC_sd1_", ".tif", sep = desired_country)
gdalwarp(ORC.tif, dstfile= tile_file, te=te)
ORCDRC_sd1_country <- readGDAL(tile_file)


## Web Coverage Service
## location of service:
wcs = "http://wms3.isric.org/geoserver/soilgrids1km/wcs?"
## create an XML file:
l1 <- newXMLNode("WCS_GDAL")
l1.s <- newXMLNode("ServiceURL", wcs, parent=l1)
l1.l <- newXMLNode("CoverageName", "ORCDRC_sd1_M", parent=l1)
l1
xml.out = "ORCDRC_sd1_M.xml"
saveXML(l1, file=xml.out)

## check if the layer exists:
gdalinfo(xml.out)

## plot the map:
data(soil.legends)
class.labels = paste(soil.legends[["ORCDRC"]]$MIN, "\226",
soil.legends[["ORCDRC"]]$MAX)
ORCDRC_sd1_country$val <- cut(ORCDRC_sd1_country$band1,
breaks=c(soil.legends[["ORCDRC"]]$MIN[1], soil.legends[["ORCDRC"]]$MAX),
labels = class.labels)
bnd <- list(
#list("sp.points", sites, pch="+", col="black"), 
list("sp.lines", country_lines))
spplot(ORCDRC_sd1_country["val"], main="Organic carbon in permilles (0\2265
cm)", col.regions=soil.legends[["ORCDRC"]]$COLOR, sp.layout=bnd,
scales=list(draw=TRUE))

## Visualization of maps in Google Earth:
plotKML(ORCDRC_sd1_country["val"],
colour_scale=soil.legends[["ORCDRC"]]$COLOR)





--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/plotKML-package-Invalid-multibyte-string-1-tp7586554.html
Sent from the R-sig-geo mailing list archive at Nabble.com.



More information about the R-sig-Geo mailing list