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

Roger Bivand Roger.Bivand at nhh.no
Fri Jun 10 19:31:58 CEST 2011


On Fri, 10 Jun 2011, Barry Rowlingson wrote:

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

It doesn't complain in principle because the user turned off ID checking, 
but you are right that nrow(df) should be checked even if ID checking is 
turned off.

Thanks for noticing an open barn door!

Roger

>
> [Also, you only need about three of those packages loaded in your file]
>
> Barry
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> 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