[R-sig-Geo] unique spatial polygons

Roger Bivand Roger.Bivand at nhh.no
Wed Apr 29 19:32:56 CEST 2009


On Wed, 29 Apr 2009, Alina Sheyman wrote:

> Using examples in Chapter 5, I am trying to get rid of non-unique polygons.

Actually, maybe my advice wasn't well-judged, unless you are happy to 
interpret code in a fairly naked setting, you may actually need the book 
text to see what is going on. The code treats a number of different 
issues, not all of which are necessarily relevant for your case.

> I'm working with a Massachusetts shapefile by zips. The file consists the
> following fields
> ZCTA  NAME LSAD   LSAD_TRANS
>
> I've done the following  to get rid of duplicate records
>
>> zipmaps1_df <- as(zipmaps1, "data.frame")[!duplicated(zipmaps1$LSAT_TRANS)]
# not LSAD_TRANS?
>> row.names(zipmaps1_df) <- zipmaps1_df$LSAD_TRANS
>> zipmaps1a <- SpatialPolygonsDataFrame(zipmaps1, zipmaps1_df) ,
>

I guess that you need all of the features (geometries) in zipmaps1, but 
that some zipcodes have several features. Don't discard geometries 
unless you know that that is what you really want to do. Even if they are 
not contiguous, use:

zipmaps1u <- unionSpatialPolygons(as(zipmaps1, "SpatialPolygons"),
   as.character(zipmaps1$LSAD_TRANS))

to group all of the features belonging to each value of LSAD_TRANS as 
Polygon objects in unique Polygons objects (with multiple features).

The output (re. your next mail) is a SpatialPolygons object. How you then 
aggregate the data.frame part of the input object is up to you. The ID 
values of the Polygons objects in zipmaps1u will be the unique values of 
LSAD_TRANS. Unless you need to work with attribute data, you could stay 
just with SpatialPolygons objects until you are through.

> but get the following error message
> Error in SpatialPolygonsDataFrame(zipmaps1, zipmaps1_df) :
>  row.names of data and Polygons IDs do not match
>
>
> How do I find out what field R is treating as Polygon ID, so that I can set
> my row.names to that?

If you need it later on, you could look at spChFIDs as I suggested before.

Hope this helps,

Roger

>
>
> On Wed, Apr 29, 2009 at 2:42 AM, Roger Bivand <Roger.Bivand at nhh.no> wrote:
>
>> On Tue, 28 Apr 2009, Alina Sheyman wrote:
>>
>>  I'm trying to combine two shapefiles using
>>> zipmaps5 <- spRbind(zipmaps1,zipmaps2)
>>>
>>> and getting the following error message
>>> Error in spRbind(as(obj, "SpatialPolygons"), as(x, "SpatialPolygons")) :
>>>  non-unique polygon IDs
>>>
>>> Does anyone know how I can get rid of duplicate polygons in a shapefile?
>>>
>>
>> This isn't the problem. The real problem is that zipmaps1 and zipmaps2 have
>> non-unique polygon IDs, so that it isn't obvious to the function what you
>> want to do. By default:
>>
>> sapply(slot(zipmaps1, "polygons"), slot, "ID")
>>
>> is set to the FID of the shapefile in readOGR() and equivalently in
>> readShapeSpatial() in maptools. These typically run 0:(n-1). For two
>> objects, they obviously overlap. Please use spChFIDs() methods in maptools
>> to assign IDs that are unique for the output object; in the worst case:
>>
>> zipmaps1 <- spChFIDs(zipmaps1, paste("map1", sapply(slot(zipmaps1,
>>  "polygons"), slot, "ID"), sep="_"))
>>
>> and a different "map*" string for zipmaps2 will work, but a unique,
>> meaningful ID is best.
>>
>> This is covered in detail in the code examples for Chapter 5 in our book,
>> and on the help page for ?"spChFIDs-methods".
>>
>> Hope this helps,
>>
>> Roger
>>
>>
>>> thank you!
>>>
>>>        [[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
>>
>>
>

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