[R-sig-Geo] rbind problems with SpatialPolygonsDataFrame

Roger Bivand Roger.Bivand at nhh.no
Wed Sep 14 08:53:11 CEST 2011


On Tue, 13 Sep 2011, Jonathan Greenberg wrote:

> I tried that, but is there an equivalent of makeUniqueIDs=TRUE for
> that command?  I get this error when running spRbind:
>
> Error in spRbind(as(obj, "SpatialPolygons"), as(x, "SpatialPolygons")) :
>  non-unique polygon IDs

There is no equivalent by design. The IDs are what are used to match the 
geometries with the rows of the attribute data (if any), so cannot be 
arbitrary. The user should assign suitable valyes first, it isn't hard, 
just imposes necessary discipline.

In your case I would never extend the list of Polygons objects as you do - 
I would always first count the total number of Polygons objects in the 
list of SpatialPolygons* objects, create a list of the correct length, 
create and assign unique IDs to the input list components, assign to the 
new list, and only then build the output SpatialPolygons* object. This 
avoids the wasteful reallocation of memory each time round the loop.

Hope this clarifies,

Roger

>
> --j
>
> On Tue, Sep 13, 2011 at 5:17 PM, Adam Sparks <adamhsparks at gmail.com> wrote:
>> Have you tried spRbind?
>>
>> On Wed, Sep 14, 2011 at 4:42 AM, Jonathan Greenberg
>> <greenberg at ucdavis.edu>wrote:
>>
>>> R-sig-geo'ers:
>>>
>>> I apologize that I can't provide the input data, but maybe the info
>>> below can illuminate the problem.  Basically, I'm trying to rbind two
>>> SpatialPolygonDataFrames together, but I'm getting a strange error:
>>>
>>> 'no method or default for coercing "logical" to "SpatialPolygons"'
>>>
>>> Does rbind only work on SpatialPolygons (e.g. should I rbind the
>>> SpatialPolygons and the data.frame separately, then piece them back
>>> together afterwards?)
>>>
>>> Note the "replacement" call was because I couldn't figure out how to
>>> do.call(rbind) and pass along the "makeUniqueIDs=TRUE"
>>>
>>>> i=2
>>>>
>>> boundary_vectors=rbind(boundary_vectors,boundary_vectors_unmerged[[i]],makeUniqueIDs=TRUE)
>>> Error in as(x, "SpatialPolygons") :
>>>  no method or default for coercing "logical" to "SpatialPolygons"
>>>> class(boundary_vectors)
>>> [1] "SpatialPolygonsDataFrame"
>>> attr(,"package")
>>> [1] "sp"
>>>> class(boundary_vectors_unmerged[[i]])
>>> [1] "SpatialPolygonsDataFrame"
>>> attr(,"package")
>>> [1] "sp"
>>>
>>> This was a single step from a larger looping I used in lieu of do.call:
>>>
>>>> class(boundary_vectors_unmerged)
>>>        for(i in (1:length(boundary_vectors_unmerged)))
>>>        {
>>>                print(i)
>>>                if(i==1)
>>>                {
>>>                        boundary_vectors=boundary_vectors_unmerged[[i]]
>>>                } else
>>>                {
>>>
>>>  boundary_vectors=rbind(boundary_vectors,boundary_vectors_unmerged[[i]],makeUniqueIDs=TRUE)
>>>                }
>>>        }
>>>
>>>
>>> --j
>>>
>>> --
>>> Jonathan A. Greenberg, PhD
>>> Assistant Project Scientist
>>> Center for Spatial Technologies and Remote Sensing (CSTARS)
>>> Department of Land, Air and Water Resources
>>> University of California, Davis
>>> One Shields Avenue
>>> Davis, CA 95616
>>> Phone: 415-763-5476
>>> AIM: jgrn307, MSN: jgrn307 at hotmail.com, Gchat: jgrn307
>>>
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> R-sig-Geo at r-project.org
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>
>>
>>
>>
>> --
>> best,
>>
>> Adam Sparks
>> Post-Doctoral Fellow
>> IRRI
>> DAPO Box 7777
>> Metro, Manila
>> Los Baños, Laguna, Philippines
>>
>>        [[alternative HTML version deleted]]
>>
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>>
>
>
>
>

-- 
Roger Bivand
Department of Economics, NHH Norwegian School of Economics,
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