[R-sig-Geo] error for converting between KML and Shape file

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Thu Dec 10 21:41:33 CET 2009


On Thu, Dec 10, 2009 at 8:17 PM, rusers.sh <rusers.sh at gmail.com> wrote:
> Hi all,
>  I try to use the readOGR and writeOGR in rgdal package to do the
> conversion KML and Shape files, but failed. Following is my codes for a
> simulated dataset.
> #library(rgdal);library(maptools);ogrDrivers()
> #generate a simulated point SHAPE file,its name is "pointexamle0" and save
> in "h:/example".
> x<-runif(5)*180;y<-runif(5)*90
> pointsam<-SpatialPoints(data.frame(x,y), proj4string=CRS("+proj=longlat
> +ellps=WGS84"))
> write.pointShape(coordinates(pointsam), df=as(pointsam, "data.frame"),
> file="h:/example/pointexamle0")
>
> #1. Conver SHAPE file to KML file
> importshp <- readOGR(dsn="h:/example",layer="pointexamle0") #correct
> proj4string(importshp)<-CRS("+proj=longlat +ellps=WGS84")
> writeOGR(obj=importshp,dsn="h:/example",driver="KML",layer="output")
>  #????Errors
> #Error in writeOGR(obj = importshp, dsn = "h:/example", driver = "KML",  :
>          GDAL Error 4: Failed to create KML file h:/example.
>

 The devil, as we say, is in the details (which for some reason are truncated!):

Details:

    Working out which combination of dsn, layer, and driver (and
    option) values give the desired output takes time and care, and is
    constrained by the ability of drivers to write output; many ar

The parameters for writeOGR are:

writeOGR(object, fileName, layerName,driver="KML")

Here 'object' is your R Spatial Points/Poly/etc, 'fileName' is the
path (including filename) for your output KML, and 'layerName' is a
label that your layer will get in the KML - you'll see this in the
layer list in Google Earth, for example.

 Example:

writeOGR(myPoints,"/tmp/test.kml","TestLayer",driver="KML")


Barry
-- 
blog: http://geospaced.blogspot.com/
web: http://www.maths.lancs.ac.uk/~rowlings
web: http://www.rowlingson.com/
twitter: http://twitter.com/geospacedman
pics: http://www.flickr.com/photos/spacedman



More information about the R-sig-Geo mailing list