Skip to content

Subsetted SpatialPolygonsDataFrame cannot writeOGR to ESRI shapefiles

4 messages · christiaan pauw, Roger Bivand

#
On Thu, 3 Jun 2010, christiaan pauw wrote:

            
Please make the input data objects to spCbind available on a website, with 
a copy of the spCbind command. Use save() to save as a portable RData 
file. Please also report your exact GDAL version and source, as this may 
also be platform dependent.

Roger

  
    
#
Thanks Roger.

I get the following from my gdal_version.h

GDAL_RELEASE_DATE ? ? 20100423
GDAL_RELEASE_NAME ? ? "1.7.2"

Downloaded rgdal yesterday from
http://www(dot)kyngchaos(dot)com/files/software/unixport/rgdal-0.6.26-1.zip

I am on Mac OS X 10.5.8

I place the data files together with a sample program here:
https://sites(dot)google(dot)com/site/christiaanpauw/file-cabinet

There is one thing that I noticed while uploading the files that
strikes me as strange. The total size of the SPDF is 28k (containing
144 polygons with an associated dataframe with 3 variables) while the
dataframe(containing 144 rows of 19 variables) in 1.4M. Is it possible
that there is something corrupt in the dataframe that only comes to
the fore in the writeOGR operation?

Thanks for your help
best regards
Christiaan
On 3 June 2010 09:03, Roger Bivand <Roger.Bivand at nhh.no> wrote:
#
On Fri, 4 Jun 2010, christiaan pauw wrote:

            
Christiaan:

The underlying cause is the difference between more forgiving R and the 
less tolerant OGR/Shapefile driver with reference to variable/field names. 
Even though the OGR driver tries to launder the field names, it doesn't 
succeed for fields c(4, 5, 13:16, 20). Please replace the field names 
before using writeOGR(). Other OGR drivers may be less restrictive here, 
so this is up to the user, not to writeOGR:

load("Bluegum.Rda")
load("SPbluegumImp.Rda")
library(maptools)
SPbluegumADD=spCbind(SPbluegumImp,Bluegum)
library(rgdal)
writeOGR(SPbluegumImp,".","SPbluegumImp",driver="ESRI Shapefile")
writeOGR(SPbluegumADD,".","SPbluegumADD",driver="ESRI Shapefile")
summary(SPbluegumADD)
names(SPbluegumADD) <- paste("V", 1:22, sep="")
summary(SPbluegumADD)
writeOGR(SPbluegumADD,".","SPbluegumADD",driver="ESRI Shapefile")
summary(readOGR(".","SPbluegumADD"))

Hope this helps,

Roger

PS. The saved data frame is large because all the roughly 40K original 
factor levels are still retained for each factor variable on subsetting. 
If you don't need factors anyway, you can control this with the 
stringsAsFactors= arguments to data import functions (the default is TRUE, 
but if FALSE, you get a character vector instead).