Convert WKT polygons to SpatialPolygonsDataFrame

Description

Convert a data.frame containing WKT polygons to a SpatialPolygonsDataFrame.

Extract vertices from a SpatialPolygonsDataFrame and optionally applies an aggregating function to each Polygon.

Usage

1
2
3
4
5
6
WKT2SpatialPolygonsDataFrame(dat, geom, id)

vertices(object, FUN)

## S4 method for signature 'SpatialPolygons'
vertices(object, FUN)

Arguments

dat

data.frame

geom

is the name (character vector) of the column in the data.frame containing the geometry.

id

is the name (character vector) of the column in the data.frame identifying the polygon. when id is not unique then polygons are combined using gUnionCascaded.

object

An object.

FUN

A function.

Value

a SpatialPolygonsDataFrame object.

A SpatialPointsDataFrame containing an id column corresponding to each extracted Polygon.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
require(rangeMapper)
require(rgeos)

# generate a few random polygons
randPoly = function(mean, sd) {
  writeWKT(
    gConvexHull(
     readWKT(paste("MULTIPOINT (",
             paste(apply(matrix(rnorm(n= 100, mean, sd), ncol = 2), 1,
             paste, collapse = ' '), collapse = ","), ")"))))
}
n = 50
d = data.frame( nam = sample(letters, n, TRUE),
               range = mapply(randPoly, mean = sample(1:2, n, TRUE),
               sd = sample(1:2/5, n, TRUE) ))


X = WKT2SpatialPolygonsDataFrame(d, 'range', 'nam')


dbcon = rangeMap.start(file = "test.sqlite", overwrite = TRUE, dir = tempdir() )
global.bbox.save(con = dbcon, bbox = X)
gridSize.save(dbcon)
canvas.save(dbcon)
processRanges(spdf = X, con =  dbcon, ID = "nam")
rangeMap.save(dbcon)
plot(rangeMap.fetch(dbcon))

require(rangeMapper)
require(rgdal)
f = system.file(package = "rangeMapper", "extdata", "wrens", "vector")
# path to Campylorhynchus_gularis breeding range:
camgul = selectShpFiles(f, ogr = TRUE, polygons.only = TRUE)[6, ]
r = readOGR(camgul$dsn, camgul$layer)
mp = vertices(r, mean)
v = vertices(r)

plot(r)
points(mp, col = 2, pch = 3, cex = 2)
points(v, pch = 3, cex = .5)

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.