[R-sig-Geo] converting bounding box to shapefile problem

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Fri Jun 10 18:06:49 CEST 2011


On Fri, Jun 10, 2011 at 3:38 PM, mihai.niculita <mihai.niculita at uaic.ro> wrote:
> Hy all,
>
> I need to convert the bounding box of several rasters to shapefile. I wrote
> a script starting from several scripts available over the Internet, and I am
> stuck at the final command which should write the bounding box polygons
> (contained in spatpoldf.list) obtained with sp package to shapefile using
> rgdal:
>
>> # export to a GIS format:
>> write.list <- list()
>> for(i in 1:length(spatpol.list[[i]])) {
> + write.list[[i]] <- writeOGR(spatpoldf.list[[i]],
> + ".", layer=map.list[[i]], driver="ESRI Shapefile")
> + }
> Error in writeOGR(spatpoldf.list[[i]], ".", layer = map.list[[1]], driver =
> "ESRI Shapefile") :
>  number of objects mismatch
>>
>
> map.list is a vector containing the raster names and looks like this:
>
> [1] "N21W157.asc" "N21W158.asc" .....
>
> I mention that the bounding box polygon does not have CRS because I can not
> attach decimal degree coordinates with sp package. I really need the
> Lat/Long coordinates because finally I must apply the script to a big number
> of SRTM1 rasters which cover several UTM zones.
>
> The entire script and the data is available at:
> http://www.geomorphologyonline.com/script.r
> http://www.geomorphologyonline.com/data.tgz
>
> I work with R.2.12.1 x64 on Windows and I have the latest sp and rgdal for
> x64.

 This is because the SpatialPolygonDataFrames you try to write to
shapefiles have 7 items in their data but only one polygon - a square.
The data should only be a data frame with one row.

 It comes from this:

ID <- array(map.list, dim=c(7,1))   # why 7 here?
IDD <- as.data.frame(ID)

and then:

spatpoldf.list[[i]] <- SpatialPolygonsDataFrame(spatpol.list[[i]],
IDD, match.ID = FALSE)

I'm not sure why SpatialPolygonsDataFrame doesnt complain at this
point. I do know that if you replace IDD with data.frame(ID=1) or some
other single-row data frame when you construct it then it will work.

[Also, you only need about three of those packages loaded in your file]

Barry



More information about the R-sig-Geo mailing list