[R-sig-Geo] Subsetted SpatialPolygonsDataFrame cannot writeOGR to ESRI shapefiles
Roger Bivand
Roger.Bivand at nhh.no
Fri Jun 4 10:50:24 CEST 2010
On Fri, 4 Jun 2010, christiaan pauw wrote:
> 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?
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).
>
> Thanks for your help
> best regards
> Christiaan
>
> On 3 June 2010 09:03, Roger Bivand <Roger.Bivand at nhh.no> wrote:
>>
>> 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 to Rick and Roger for their respective replies. Both helped.
>>>
>>> I updated R, GDAL and rgdal (I now have: R version 2.11.1
>>> (2010-05-31) x86_64-apple-darwin9.8.0 with rgdal_0.6-26).
>>> I then Ricks proposed writeOGR statement below. This works.
>>>
>>> After adding some data (a dataframe called Bluegum) to the data already
>>> contained in the SPDF with spCbind like this:
>>>>
>>>> SPbluegumADD=spCbind(SPbluegumImP,Bluegum)
>>>
>>> I get a SpatialPolygonsDataFrame (according to class()) that can be plotted
>>> and looks OK on the plot. I can even use the newly added variables to define
>>> colours in the plot so I am sure the spCbind was successful.
>>>
>>> But when I try to write it to a shapefile I get an error again:
>>>
>>>> writeOGR(SPbluegumADD,".","SPbluegumImPX",driver="ESRI Shapefile")
>>>
>>> Error in writeOGR(SPbluegumADD, ".", "SPbluegumImPX", driver = "ESRI
>>> Shapefile") :
>>>
>>> GDAL Error 1: Invalid index : -1
>>
>>
>
--
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