An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20100603/503925e5/attachment.pl>
Subsetted SpatialPolygonsDataFrame cannot writeOGR to ESRI shapefiles
4 messages · christiaan pauw, Roger Bivand
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
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 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
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