[R-sig-Geo] Subsetted SpatialPolygonsDataFrame cannot writeOGR to ESRI shapefiles

Roger Bivand Roger.Bivand at nhh.no
Thu Jun 3 09:03:40 CEST 2010


On Thu, 3 Jun 2010, christiaan pauw wrote:

> 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

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

>
> I tried to compare the structure of the objects as Rick adviced by using
> list.tree (is there a better way?) and get:
>
>> list.tree(SPbluegumImP, depth=2)
> SPbluegumImP = S4 1 (717288 bytes)( SpatialPolygonsDataFrame )
> A  bbox = double 4= named array 2 X 2= 28.395 -26.388 28.405 ...
> A  proj4string = S4 1( CRS )
> A  polygons = list 144
> A .  ...   and 132 more
> A  plotOrder = integer 144= 4 26 33 6 22 92 ...
> A  data = list 3( data.frame )
>
>> list.tree(SPbluegumADD, depth=2)
> SPbluegumADD = S4 1 (17552624 bytes)( SpatialPolygonsDataFrame )
> A  bbox = double 4= named array 2 X 2= 28.395 -26.388 28.405 ...
> A  proj4string = S4 1( CRS )
> A  polygons = list 144
> A .  ...   and 132 more
> A  plotOrder = integer 144= 4 26 33 6 22 92 ...
> A  data = list 24( data.frame )
> A .  ...   and 12 more
>
> The difference seems to be that the SPDF object created by the spCbind
> contains something extra. The "A .  ...   and 12 more" at the end. When I
> set the depth in list.tree() to -1 to get everything and look right at the
> end I get:
>
> for the original object(SPbluegumImP:
>
> A  data = list 3( data.frame )
> A .  TOWNSHIP = integer 144= category (7 levels)( factor )= BLUEGUM VIEW
> BLUEGUM VIEW ...
> A .  SUBURB = integer 144= category (1 levels)( factor )= BLUEGUM VIEW
> BLUEGUM VIEW ...
> A .  PROPDESC = integer 144= category (5198 levels)( factor )= 116 82 138 11
> 127 ...
> A A  row.names = integer 144= 3 85 94 127 166 ...
>
> for the Object created by spCbind (SPbluegumADD)
>
> A  data = list 24( data.frame )
> A .  TOWNSHIP = integer 144= category (7 levels)( factor )= BLUEGUM VIEW
> BLUEGUM VIEW ...
> A .  SUBURB = integer 144= category (1 levels)( factor )= BLUEGUM VIEW
> BLUEGUM VIEW ...
> A .  PROPDESC = integer 144= category (5198 levels)( factor )= 116 82 138 11
> 127 ...
> A .  Response.ID = integer 144= category (44327 levels)( factor )=
> 53cbc1d8-c12a-4aaf-a74f-0037b8e4c577 ...
> A .  Fieldworker = character 144= Sfiso Jiyane  ...
> A .  Received = integer 144= category (43009 levels)( factor )= 2010/05/01
> 12:44:16 PM ...
> A .  Start = integer 144= category (42997 levels)( factor )= 2010/04/13
> 09:26:10 AM ...
> A .  End = integer 144= category (43124 levels)( factor )= 2010/04/13
> 09:26:57 AM ...
> A .  Duration = integer 144= category (2908 levels)( factor )= 00:00:47
> 00:01:02 ...
> A .  Location = integer 144= category (1 levels)( factor )= N/A N/A N/A N/A
> ...
> A .  Language = integer 144= category (1 levels)( factor )= English English
> ...
> A .  PROPDESC.1 = integer 144= 5 5 5 5 5 5 5 5 ...
> A .  ...   and 12 more
> A A  row.names = integer 144= 3 85 94 127 166 ...
>
> I cannot see a real difference except that the second object now contains
> the additional columns added by spCbind.
>
> Can anyone offer some insight?
>
> regards and thank for the help so far
> Christiaan
>
> On 2 June 2010 18:52, rick reeves <reeves at nceas.ucsb.edu> wrote:
>
>> Christian:
>>
>> Compare the object fields carefully to those in the 'parent' object.
>> Assuming that SPbluegumImP is a valid SpatialPolygonsDataFrame,
>> you might try simplifying the writeOGR statement to:
>>
>>  writeOGR(SPbluegumImP,".","SPbluegumImP",driver="ESRI Shapefile")
>>
>> ..which will write the file SPbluegumImP.shp to the current working folder
>> (which you can establish with setwd("path"))
>>
>> You can also send me your file, and I will be glad to have a look.
>>
>> hope this helps,
>> Rick R
>>
>>
>>
>
> 	[[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

-- 
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