The usual way to have spatial objects in R consists on reading a shapefile in R.
However, there are case whereby users do not have a shapefile.shp but instead they have a series of polygon coordinates and its corresponding projection.
The question is how to “instantiate” spatial objects (i.e. a “polygon object”) directly from this data?
First get the coordinates into a 2-column matrix:
x_coord <- c(16.48438, 17.49512, 24.74609, 22.59277, 16.48438)
y_coord <- c(59.736328125, 55.1220703125, 55.0341796875, 61.142578125, 59.736328125)
xym <- cbind(x_coord, y_coord)
xym
## x_coord y_coord
## [1,] 16.48438 59.73633
## [2,] 17.49512 55.12207
## [3,] 24.74609 55.03418
## [4,] 22.59277 61.14258
## [5,] 16.48438 59.73633
Then create a Polygon, wrap that into a Polygons object, then wrap that into a SpatialPolygons object:
library(sp)
p = Polygon(xym)
ps = Polygons(list(p),1)
sps = SpatialPolygons(list(ps))
plot(sps)
The reason for this level of complexity is that a Polygon is a simple ring, a Polygons object can be several rings with an ID (here set to 1) (so is like a single feature in a GIS) and a SpatialPolygons can have a CRS. Ooh, I should probably set it:
proj4string(sps) = CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0")
If you want to turn it into a SpatialPolygonsDataFrame (which is what comes our of readShapeSpatial when the shapefile is polygons) then do:
data = data.frame(f=99.9)
spdf = SpatialPolygonsDataFrame(sps,data)
spdf
## An object of class "SpatialPolygonsDataFrame"
## Slot "data":
## f
## 1 99.9
##
## Slot "polygons":
## [[1]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] 20.50516 57.72918
##
## Slot "area":
## [1] 36.8548
##
## Slot "hole":
## [1] FALSE
##
## Slot "ringDir":
## [1] 1
##
## Slot "coords":
## x_coord y_coord
## [1,] 16.48438 59.73633
## [2,] 22.59277 61.14258
## [3,] 24.74609 55.03418
## [4,] 17.49512 55.12207
## [5,] 16.48438 59.73633
##
##
##
## Slot "plotOrder":
## [1] 1
##
## Slot "labpt":
## [1] 20.50516 57.72918
##
## Slot "ID":
## [1] "1"
##
## Slot "area":
## [1] 36.8548
##
##
##
## Slot "plotOrder":
## [1] 1
##
## Slot "bbox":
## min max
## x 16.48438 24.74609
## y 55.03418 61.14258
##
## Slot "proj4string":
## CRS arguments:
## +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
summary(spdf)
## Object of class SpatialPolygonsDataFrame
## Coordinates:
## min max
## x 16.48438 24.74609
## y 55.03418 61.14258
## Is projected: FALSE
## proj4string :
## [+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0]
## Data attributes:
## f
## Min. :99.9
## 1st Qu.:99.9
## Median :99.9
## Mean :99.9
## 3rd Qu.:99.9
## Max. :99.9
spplot(spdf)
if(!require(rgeos)){
install.packages("rgeos")
library(rgeos)
}
## Loading required package: rgeos
## rgeos version: 0.3-19, (SVN revision 524)
## GEOS runtime version: 3.4.2-CAPI-1.8.2 r3921
## Linking to sp version: 1.2-3
## Polygon checking: TRUE
centroid = gCentroid(spdf,byid=TRUE)
plot(spdf)
points(coordinates(spdf),pch=1)
points(centroid,pch=2)
Do not forget to replicate this exercise using your own data.
In addition, replicate the exercise posted at http://goo.gl/DdsuL3
Lovelace, R., 2014. Basic mapping and attribute joins in R. Available at http://robinlovelace.net
Oyana T.J. & Margai, F.M., 2016. Spatial Analysis – Statistics, Visualization, and Computational Methods. CRC Press. 294 pp.