polygon: Make WKT polygon objects

Description Usage Arguments Details See Also Examples

View source: R/polygon.R

Description

Make WKT polygon objects

Usage

1
polygon(..., fmt = 16, third = "z")

Arguments

...

A GeoJSON-like object representing a Point, LineString, Polygon, MultiPolygon, etc.

fmt

Format string which indicates the number of digits to display after the decimal point when formatting coordinates. Max: 20

third

(character) Only applicable when there are three dimensions. If m, assign a M value for a measurement, and if z assign a Z value for three-dimenionsal system. Case is ignored. An M value represents a measurement, while a Z value usually represents altitude (but can be something like depth in a water based location).

Details

You can create nested polygons with list and data.frame inputs, but not from numeric inputs. See examples.

See Also

Other R-objects: circularstring(), geometrycollection(), linestring(), multilinestring(), multipoint(), multipolygon(), point()

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
## empty polygon
polygon("empty")
# polygon("stuff")

# numeric
polygon(c(100.001, 0.001), c(101.12345, 0.001), c(101.001, 1.001),
  c(100.001, 0.001), fmt=2)

# data.frame
## single polygon
df <- us_cities[2:5,c('long','lat')]
df <- rbind(df, df[1,])
wktview(polygon(df, fmt=2), zoom=4)
## nested polygons
df2 <- data.frame(long = c(-85.9, -85.9, -93, -93, -85.9),
                  lat = c(37.5, 35.3, 35.3, 37.5, 37.5))
wktview(polygon(df, df2, fmt=2), zoom=4)

# matrix
mat <- matrix(c(df$long, df$lat), ncol = 2)
polygon(mat)

# list
# single list - creates single polygon
ply <- list(c(100.001, 0.001), c(101.12345, 0.001), c(101.001, 1.001),
  c(100.001, 0.001))
wktview(polygon(ply, fmt=2), zoom=7)
# nested list - creates nested polygon
vv <- polygon(list(c(35, 10), c(45, 45), c(15, 40), c(10, 20), c(35, 10)),
   list(c(20, 30), c(35, 35), c(30, 20), c(20, 30)), fmt=2)
wktview(vv, zoom=3)
# multiple lists nested within a list
zz <- polygon(list(list(c(35, 10), c(45, 45), c(15, 40), c(10, 20), c(35, 10)),
   list(c(20, 30), c(35, 35), c(30, 20), c(20, 30))), fmt=2)
wktview(zz, zoom=3)


## a 3rd point is included
### numeric
polygon(c(100, 0, 30), c(101, 0, 30), c(101, 1, 30),
  c(100, 0, 30), fmt = 2)
polygon(c(100, 0, 30), c(101, 0, 30), c(101, 1, 30),
  c(100, 0, 30), fmt = 2, third = "m")

### data.frame
df <- us_cities[2:5, c('long','lat')]
df <- rbind(df, df[1,])
df$altitude <- round(runif(NROW(df), 10, 50))
polygon(df, fmt=2)
polygon(df, fmt=2, third = "m")

### matrix
mat <- matrix(c(df$long, df$lat, df$altitude), ncol = 3)
polygon(mat, fmt=2)
polygon(mat, fmt=2, third = "m")

### list
ply <- list(c(100, 0, 1), c(101, 0, 1), c(101, 1, 1),
  c(100, 0, 1))
polygon(ply, fmt=2)
polygon(ply, fmt=2, third = "m")


## a 4th point is included
### numeric
polygon(c(100, 0, 30, 3.5), c(101, 0, 30, 3.5), c(101, 1, 30, 3.5),
  c(100, 0, 30, 3.5), fmt = 2)

### data.frame
df <- us_cities[2:5, c('long','lat')]
df <- rbind(df, df[1,])
df$altitude <- round(runif(NROW(df), 10, 50))
df$weight <- round(runif(NROW(df), 0, 1), 1)
polygon(df, fmt=2)

### matrix
mat <- matrix(unname(unlist(df)), ncol = 4)
polygon(mat, fmt=2)

### list
ply <- list(c(100, 0, 1, 40), c(101, 0, 1, 44), c(101, 1, 1, 45),
  c(100, 0, 1, 49))
polygon(ply, fmt=2)

Example output

Attaching package: 'wellknown'

The following object is masked from 'package:graphics':

    polygon

[1] "POLYGON EMPTY"
[1] "POLYGON ((100.001 0.001, 101.1235 0.0010, 101.001 1.001, 100.001 0.001))"
[1] "POLYGON ((-81.5199999999999960 41.0799999999999983, -122.2600000000000051 37.7700000000000031, -84.1800000000000068 31.5799999999999983, -73.7999999999999972 42.6700000000000017, -81.5199999999999960 41.0799999999999983))"
[1] "POLYGON Z((100.00 0.00 30.00, 101.00 0.00 30.00, 101.00 1.00 30.00, 100.00 0.00 30.00))"
[1] "POLYGON M((100.00 0.00 30.00, 101.00 0.00 30.00, 101.00 1.00 30.00, 100.00 0.00 30.00))"
[1] "POLYGON Z((-81.52 41.08 12.00, -122.26 37.77 36.00, -84.18 31.58 40.00, -73.80 42.67 40.00, -81.52 41.08 40.00))"
[1] "POLYGON M((-81.52 41.08 12.00, -122.26 37.77 36.00, -84.18 31.58 40.00, -73.80 42.67 40.00, -81.52 41.08 40.00))"
[1] "POLYGON Z((-81.52 41.08 12.00, -122.26 37.77 36.00, -84.18 31.58 40.00, -73.80 42.67 40.00, -81.52 41.08 40.00))"
[1] "POLYGON M((-81.52 41.08 12.00, -122.26 37.77 36.00, -84.18 31.58 40.00, -73.80 42.67 40.00, -81.52 41.08 40.00))"
[1] "POLYGON Z((100.00 0.00 1.00, 101.00 0.00 1.00, 101.00 1.00 1.00, 100.00 0.00 1.00))"
[1] "POLYGON M((100.00 0.00 1.00, 101.00 0.00 1.00, 101.00 1.00 1.00, 100.00 0.00 1.00))"
[1] "POLYGON ZM((100.00 0.00 30.00 3.50, 101.00 0.00 30.00 3.50, 101.00 1.00 30.00 3.50, 100.00 0.00 30.00 3.50))"
[1] "POLYGON ZM((-81.52 41.08 38.00 0.70, -122.26 37.77 43.00 0.90, -84.18 31.58 44.00 0.50, -73.80 42.67 23.00 0.10, -81.52 41.08 12.00 0.90))"
[1] "POLYGON ZM((-81.52 41.08 38.00 0.70, -122.26 37.77 43.00 0.90, -84.18 31.58 44.00 0.50, -73.80 42.67 23.00 0.10, -81.52 41.08 12.00 0.90))"
[1] "POLYGON ZM((100.00 0.00 1.00 40.00, 101.00 0.00 1.00 44.00, 101.00 1.00 1.00 45.00, 100.00 0.00 1.00 49.00))"

wellknown documentation built on May 26, 2021, 1:06 a.m.