[R-sig-Geo] How to replace coordinates of Polygons

Roger Bivand Roger.Bivand at nhh.no
Mon Jul 19 19:30:19 CEST 2010


On Mon, 19 Jul 2010, Ben Mazzotta wrote:

> Thank you very much, Roger.
>
> I was afraid that might be the answer. Once I have the new Polygon and 
> Polygons objects, how can I merge all the slots from the old Polygon and 
> Polygons to the new Polygon and Polygons objects? Ultimately I'd like to 
> bring the other fields from the old object into the new one.

If they have the same IDs, no problem, just make a 
SpatialPolygonsDataFrame with data=as(old, "data.frame").

>
>
> The context is this: I am transforming the coordinates using the cartogram() 
> function from {Rcartogram}. The argument to cartogram() is a rectangular 
> matrix, which contains sample densities of a statistic at regular intervals 
> over the map. Cartogram() returns a lookup table in the form of two matrices, 
> which specify x_new as a function of (x_old, y_old) and y_new, again as a 
> function of (x_old, y_old). After cleaning up missing observations, I can 
> interpolate new coordinates for every pair of old coordinates.
>
> Therefore, I'd like to create a new version of the world map by transforming 
> every pair of coordinates in the original polygon.

Firstly, the input is a planar grid of values of a variable, so the 
queries on the two output grids need to accommodate the loss of resolution 
implied there. Try on something simple first. If you can get a wrapper 
around cartogram() to take a SpatialPolygonsDataFrame, make a grid from it 
for the chosen variable (including NA handling), then do cartogram, 
followed by interpolation for Polygons object by Polygons object (remember 
objects may be multiple Polygon objects, and may have holes), you may be 
able to get there. Constructing the output objects from computed 
coordinates looks like the least of your challenges.

Good luck!

Roger



>
>
>
>
>
>
> On 07/19/2010 12:09 PM, Roger Bivand wrote:
>> On Mon, 19 Jul 2010, Ben Mazzotta wrote:
>> 
>>> Dear R geographers:
>>> 
>>> I'm able to replace the coordinates of any given polygon, but not 
>>> construct apply() family commands or for() loops to accomplish the same 
>>> task. How can I construct a simple loop or apply() command that will 
>>> sequentially replace all of the coordinates in a SpatialPolygons object?
>> 
>> Just build lists of new Polygon then Polygons objects, and build a new 
>> SpatialPolygons at the end. I don't think that you'll see big memory 
>> problems, and the approach you propose will probably have poor timings, 
>> because the containing objects will probably be rebuilt for each update.
>> 
>> Are you looking for the elide() method for SpatialPolygons in maptools? Do 
>> you need an extra operation added to it?
>> 
>> Roger
>> 
>> 
>>> 
>>> As far as I understand, the coordinates of a polygon are kept in a slot 
>>> referred as follows:
>>> map at polygons[[x]]@Polygons[[y]]@coords
>>> 
>>> Using which, I can create mapply(foo, indexp, indexP) commands to extract 
>>> the original coordinates and transform them.
>>> 
>>> # Define a pair of vectors indexp, indexP that together index every Polyon 
>>> in the SpatialPolygons object map.
>>> # Then extract coordinates as follows:
>>> coords <- function(x,y) {map at polygons[[indexp]]@Polygons[[indexP]]@coords}
>>> # Or operate a function foo on the old coordinates
>>> coords.alt <- mapply(foo, 
>>> map at polygons[[indexp]]@Polygons[[indexP]]@coords[,1], 
>>> map at polygons[[indexp]]@Polygons[[indexP]]@coords[,2])
>>> 
>>> So I can create lists and matrices of coordinates, and transform the 
>>> coordinates.
>>> 
>>> It is possible to write a command that replaces coordinates using 
>>> integers; but these commands fail when I attempt to use mapply() to 
>>> replace existing coordinates. For example,
>>> 
>>> p at polygons[[1]]@Polygons[[1]]@coords <- foo(1,1)
>>> 
>>> will work fine, but the following will not.
>>> 
>>> newcoords <- function(indp, indP) {
>>>    p at polygons[[indp]]@Polygons[[indP]]@coords <- foo(indp, indP)
>>>    }
>>> mapply(newcoords, indexp, indexP)
>>> 
>>> 
>>> Have I missed something in the manual on coordinates that would enable me 
>>> to transform the coordinates of all the Polygons in a 
>>> SpatialPolygonsDataFrame according to some regular function of the old 
>>> coordinates (x,y)? Is the only alternative to build a new SpatialPolygons 
>>> data frame from scratch, beginning with the new coordinates?
>>> 
>>> If it would be useful, I can provide sample code that works on wrld_simpl.
>>> 
>>> Please advise. Thank you!
>>> 
>>> 
>>> 
>> 
>
>

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