[R-sig-Geo] Map labels

Roger Bivand Roger.Bivand at nhh.no
Mon Nov 23 21:55:41 CET 2009


On Mon, 23 Nov 2009, tom sgouros wrote:

>
> Is this another way to say that I've stumbled onto one of the borders
> between the set of tasks that R does well and the set of tasks I should
> use a GIS for?  Or should I persevere with R on this?

Not really, this is a generic problem for shapefiles, which can have 
multiple external and internal (hole) rings in a single entity (as can 
Polygons objects) and OGC SFS, where an entity is an external ring with 
internal rings.

This means that unless a collection of such external rings is seen as an 
entity, there is an ambiguity, which data producers focussed more on 
borders than content resolve by duplicating content, and joining to a 
content table. Most GIS given such a shapefile would face the same 
problem, but might be able to join to a "town-wise" table for symbology. 
In R, the key view of the data is the data.frame, so duplicate rows are 
not an option.

Maybe by this time next year an OGC SFS-compliant set of classes may be 
ready for trying out, but since shapefiles are the most popular way of 
sharing data, and are not OGC SFS compliant (they do not contain 
information on which internal rings belong to which external rings in an 
entity), this isn't easy.

>
> Second question: if I want to just do this assembly myself, so to keep
> some control over placement, is there an add-a-Polygon-to-my-Polygons
> method, or do you have an example of something like that?

?unionSpatialPolygons, followed by SpatialPolygonsDataFrame to re-assemble 
after removing the duplicated rows in the data.frame belonging to the 
object. See the example in ch 5 in our book, code on:

http://www.asdar-book.org/book/cm2_mod.R

around chunk 27 (16-30 is the initial part, 31-36 aggregates into SMSAs). 
The first part should be runnable since the shapefiles are from the US 
Census, the second by downloading the bundle from

http://www.asdar-book.org/bundles/cm2_bundle.zip

Roger

>
> Thanks for the help,
>
> -tom
>
>
> Roger Bivand <Roger.Bivand at nhh.no> wrote:
>
>>> This doesn't, though.  When I read in my data, the IDvar is supposed to
>>> be the column that is unique for each record, correct?  But there are
>>> several records (closed polygons) for each town.  These are linked by
>>> another variable.  The coordinates(rep.map) gives me a list of 419
>>> coordinates for all the closed shapes, not 75 coordinates for all the
>>> towns.  How do I get the biggest polygons for each town?  I have an Area
>>> attribute, so I guess I can just calculate it.  Is that the right way to
>>> go, or is there some method already existing for this?
>>>
>>> I thought maybe my readSpatial... was done wrong, so I changed the IDvar
>>> to point to the iD for each town, but that gives me an error saying that
>>> duplicate row names are not allowed.  Is there a step I needed to take
>>> to link the polygons?
>>
>> Yes, see the unionSpatialPolygons() function in maptools. It uses
>> functions in gpclib to dissolve boundaries between member entities if
>> they touch, so makes a Polygons object with possibly multiple member
>> Polygon objects from multiple Polygons objects with the same ID values
>> (towns for you). You then need to drop rows from the data.frame
>> (as(rep.map, "data.frame")), and re-assemble using
>> SpatialPolygonsDataFrame() with the row.names of the data.frame set to
>> the town IDs used in unionSpatialPolygons().
>>
>> The mess is caused by the underlying data being kept in OGC SFS
>> (simple features) form, which does not store well in shapefiles (a
>> similar remark could be made about SpatialPolygons objects, which are
>> more like shapefiles than like SFS).
>>
>> The gpclib package should not be used if its license conditions are
>> inappropriate. A free software alternative is being prepared.
>>
>> Roger
>>
>>>
>>> Thank you,
>>>
>>> -tom
>>>
>>>
>>
>> --
>> 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