Learn R Programming

SpatialEpi (version 1.2.8)

polygon2spatial_polygon: Convert a Polygon to a Spatial Polygons Object

Description

Converts a polygon (a matrix of coordinates with NA values to separate subpolygons) into a Spatial Polygons object.

Usage

polygon2spatial_polygon(
  poly,
  coordinate.system,
  area.names = NULL,
  nrepeats = NULL
)

Value

An object of class SpatialPolygons (See SpatialPolygons-class from the sp package).

Arguments

poly

a 2-column matrix of coordinates, where each complete subpolygon is separated by NA's

coordinate.system

the coordinate system to use

area.names

names of all areas

nrepeats

number of sub polygons for each area

Author

Albert Y. Kim

Details

Just as when plotting with the graphics::polygon() function, it is assumed that each subpolygon is to be closed by joining the last point to the first point. In the matrix poly, NA values separate complete subpolygons. In the case with an area consists of more than one separate closed polygon, nrepeats specifies the number of closed polygons associated with each area.

References

Bivand, R. S., Pebesma E. J., and Gomez-Rubio V. (2008) Applied Spatial Data Analysis with R. Springer Series in Statistics. E. J. Pebesma and R. S. Bivand. (2005) Classes and methods for spatial data in R. R News, 5, 9--13.

Examples

Run this code

data(scotland)

polygon <- scotland$polygon$polygon
coord.system <- "+proj=eqc +lat_ts=0 +lat_0=0 +lon_0=0 +x_0=0 +y_0=0 "
coord.system <- paste(coord.system, "+ellps=WGS84 +datum=WGS84 +units=m +no_defs", sep = "")
names <- scotland$data$county.names
nrepeats <- scotland$polygon$nrepeats

spatial.polygon <- polygon2spatial_polygon(polygon,coord.system,names,nrepeats)

par(mfrow=c(1,2))
# plot using polygon function
plot(polygon,type='n',xlab="Eastings (km)",ylab="Northings (km)",main="Polygon File")
polygon(polygon)

# plot as spatial polygon object
plot(spatial.polygon,axes=TRUE)
title(xlab="Eastings (km)",ylab="Northings (km)",main="Spatial Polygon")

# Note that area 23 (argyll-bute) consists of 8 separate polygons
nrepeats[23]
plot(spatial.polygon[23],add=TRUE,col="red")

Run the code above in your browser using DataLab