[R-sig-Geo] writeOGR unknown data type

Roger Bivand Roger.Bivand at nhh.no
Fri Aug 14 12:58:05 CEST 2009


On Fri, 14 Aug 2009, Wesley Roberts wrote:

> Dear r-sig-geo,
>
> I am having trouble exporting an sp object created in R using writeOGR. Below is the code I am using, basically it creates a regular grid to cover a point data set opened using readShapePoints. I then convert the spatialpoints to polygons and then finally to a SpatialPolygonsDataFrame. I would now like to export this sp data set as a shapefile so that I can add some attributes to it. Unfortunately I receive the following error each time I try to run the export,
>
>> poly<-readShapePoints("Poly_one_pts.shp", proj4string=CRS("+proj=tmerc +south +ellips=WGS84 +datum=WGS84 +lon_o=31 +k_0=1 +units=m +no_defs"))
>> grd<-spsample(poly,cellsize=c(4,4),type="regular")
>> grd<-as(as(as(grd,"SpatialPixels"),"SpatialPolygons"),"SpatialPolygonsDataFrame")

Coersion can be fun, but don't overdo it without checking. Thr output 
object is a SpatialPolygonsDataFrame object, but has an empty data frame 
in the data slot (actually, this coersion needs blocking, or changing to 
prevent the problem):

library(rgdal)
scot_BNG <- readOGR(system.file("vectors", package = "rgdal")[1],
   "scot_BNG")
grd<-spsample(scot_BNG, cellsize=c(40000, 40000), type="regular")
grd1 <- as(grd,"SpatialPixels")
grd2 <- as(grd1, "SpatialPolygons")
grd3 <- as(grd2, "SpatialPolygonsDataFrame")
str(grd3, max.level=2)

Try:

df <- data.frame(FID=row.names(grd2), row.names=row.names(grd2))
grd3 <- SpatialPolygonsDataFrame(grd2, data=df)
str(grd3, max.level=2)

Hope this helps,

Roger


>> tp<-tempdir()
>> writeOGR(grd,tp,"grd",driver="ESRI Shapefile",verbose = FALSE)
> Error in writeOGR(grd, tp, "grd", driver = "ESRI Shapefile", verbose = FALSE) :
>  NULL NULL unknown data type
>
>
> An export of poly runs fine but it seems when I try to export grd it is an unknown data type. Both classes seem to be similar and writeOGR should be able to export both data sets.
>
>> class(poly)
> [1] "SpatialPointsDataFrame"
> attr(,"package")
> [1] "sp"
>> class(grd)
> [1] "SpatialPolygonsDataFrame"
> attr(,"package")
> [1] "sp"
>
>
> Any help on this issue would be greatly appreciated.
> Many thanks and kind regards,
> Wesley
>
> Wesley Roberts MSc.
> Researcher: Earth Observation
> Natural Resources & the Environment (NRE)
> CSIR
> Tel: +27 (0)21 888-2490
> Fax: +27 (0)21 888-2693
> "To know the road ahead, ask those coming back."
> - Chinese proverb
>
>
>
>

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no



More information about the R-sig-Geo mailing list