[R-sig-Geo] Replace a polygon by a new one of a SpatialPolygonsDataFrame

Edzer Pebesma edzer.pebesma at uni-muenster.de
Tue May 12 19:48:03 CEST 2015


On 05/12/2015 07:07 PM, Hans-Jörg Bibiko wrote:
> Dear all,
> 
> I believe I do not see the forest for the trees. Since hours I've been trying something very basal. Maybe someone could give me an hint.
> 
> Imaging the following scenario:
> 
> I have a SpatialPolygonsDataFrame which contains lots of spatial polygons and associated data. Now I'd like to replace one polygon (a list of a Polygons-class) of that SpatialPolygonsDataFrame by a new one. How can I do this.
> 
> 
> Here is a very naïve, reproducible example:
> 
> Task: Replace the Polygons showing "Western Sahara" of the data set "world_simpl" by a new one which only shows its rectangle (bbox).
> 
> ---------------------------------------------------------------
> library(maptools)
> data(wrld_simpl)
> 
> #wrld_simpl[237,] := Western Sahara
> r <- bbox(wrld_simpl[237,])
> 
> theID <- wrld_simpl[237,]@polygons[[1]]@ID
> 
> # create the new spatial polygon (a rectangle of bbox coordinates) 
> theNew <- list(Polygons(list(Polygon(cbind(c(r[1,1],r[1,1],r[1,2],r[1,2]),c(r[2,1],r[2,2],r[2,2],r[2,1])))), theID))
> 
> # now I thought that one can do the this:
> wrld_simpl[237,]@polygons <- theNew

The "[<-" method for Spatial* objects is limited to work on attributes.
Try:

wrld_simpl at polygons[237] <- theNew

> 
> ---------------------------------------------------------------
> 
> But the last line does't work albeit the class and list resp. are of the same structure. :/
> 
> Old:
>> str(wrld_simpl[237,]@polygons)
> List of 1
>  $ :Formal class 'Polygons' [package "sp"] with 5 slots
>   .. ..@ Polygons :List of 1
>   .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
>   .. .. .. .. ..@ labpt  : num [1:2] -13.1 24.7
>   .. .. .. .. ..@ area   : num 24
>   .. .. .. .. ..@ hole   : logi FALSE
>   .. .. .. .. ..@ ringDir: int 1
>   .. .. .. .. ..@ coords : num [1:23, 1:2] -15.7 -17 -17.1 -16.9 -16.7 ...
>   .. ..@ plotOrder: int 1
>   .. ..@ labpt    : num [1:2] -13.1 24.7
>   .. ..@ ID       : chr "ESH"
>   .. ..@ area     : num 24
> 
> 
> New:
>> str(theNew)
> List of 1
>  $ :Formal class 'Polygons' [package "sp"] with 5 slots
>   .. ..@ Polygons :List of 1
>   .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
>   .. .. .. .. ..@ labpt  : num [1:2] -14.3 10.5
>   .. .. .. .. ..@ area   : num 159
>   .. .. .. .. ..@ hole   : logi FALSE
>   .. .. .. .. ..@ ringDir: int 1
>   .. .. .. .. ..@ coords : num [1:5, 1:2] -17.05 -17.05 -8.67 -8.67 -17.05 ...
>   .. ..@ plotOrder: int 1
>   .. ..@ labpt    : num [1:2] -14.3 10.5
>   .. ..@ ID       : chr "ESH"
>   .. ..@ area     : num 159
> 
> ?
> 
> I really appreciate any help someone can provide!
> 
> Cheers, Hans
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> 

-- 
Edzer Pebesma
Institute for Geoinformatics (ifgi),  University of Münster,
Heisenbergstraße 2, 48149 Münster, Germany; +49 251 83 33081
Journal of Statistical Software:   http://www.jstatsoft.org/
Computers & Geosciences:   http://elsevier.com/locate/cageo/
Spatial Statistics Society http://www.spatialstatistics.info



-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 490 bytes
Desc: OpenPGP digital signature
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20150512/6cc7b8a7/attachment.bin>


More information about the R-sig-Geo mailing list