sets spatial coordinates to create spatial data, or retrieves spatial coordinates

polygons(obj)
polygons(object) <- value

Arguments

obj

object of class "SpatialPolygons" or "SpatialPolygonsDataFrame"

object

object of class "data.frame"

value

object of class "SpatialPolygons"

Value

polygons returns the SpatialPolygons of obj; polygons<- promotes a data.frame to a SpatialPolygonsDataFrame object

Examples

grd <- GridTopology(c(1,1), c(1,1), c(10,10))
polys <- as.SpatialPolygons.GridTopology(grd)
centroids <- coordinates(polys)
x <- centroids[,1]
y <- centroids[,2]
z <- 1.4 + 0.1*x + 0.2*y + 0.002*x*x
df <- data.frame(x=x, y=y, z=z, row.names=row.names(polys))
polygons(df) <- polys
class(df)
#> [1] "SpatialPolygonsDataFrame"
#> attr(,"package")
#> [1] "sp"
summary(df)
#> Object of class SpatialPolygonsDataFrame
#> Coordinates:
#>   min  max
#> x 0.5 10.5
#> y 0.5 10.5
#> Is projected: NA 
#> proj4string : [NA]
#> Data attributes:
#>        x              y              z        
#>  Min.   : 1.0   Min.   : 1.0   Min.   :1.702  
#>  1st Qu.: 3.0   1st Qu.: 3.0   1st Qu.:2.626  
#>  Median : 5.5   Median : 5.5   Median :3.123  
#>  Mean   : 5.5   Mean   : 5.5   Mean   :3.127  
#>  3rd Qu.: 8.0   3rd Qu.: 8.0   3rd Qu.:3.614  
#>  Max.   :10.0   Max.   :10.0   Max.   :4.600