writeWKB: Convert Spatial Objects to WKB

Description Usage Arguments Details Value See Also Examples

View source: R/writeWKB.R

Description

Converts Spatial objects to well-known binary (WKB) geometry representations.

Usage

1
writeWKB(obj, endian = "little")

Arguments

obj

object inheriting class Spatial.

endian

The byte order ("big" or "little") for encoding numeric types. The default is "little".

Details

The argument obj may be an object of class SpatialPoints, SpatialPointsDataFrame, SpatialLines, SpatialLinesDataFrame, SpatialPolygons, or SpatialPolygonsDataFrame, or a list in which each element is an object of class SpatialPoints or SpatialPointsDataFrame.

Value

A list with class AsIs. The length of the returned list is the same as the length of the argument obj. Each element of the returned list is a raw vector consisting of a WKB geometry representation. The WKB geometry type depends on the class of obj as shown in the table below.

Class of obj Type of WKB geometry
SpatialPoints or SpatialPointsDataFrame Point
list of SpatialPoints or SpatialPointsDataFrame MultiPoint
SpatialLines or SpatialLinesDataFrame MultiLineString
SpatialPolygons or SpatialPolygonsFrame Polygon or MultiPolygon

A SpatialPolygons or SpatialPolygonsFrame object is represented as WKB Polygons if each Polygons object within it represents a single polygon; otherwise it is represented as WKB MultiPolygons.

The byte order of numeric types in the returned WKB geometry representations depends on the value of the argument endian. Little-endian byte order is known as NDR encoding, and big-endian byte order is known as XDR encoding.

When this function is run in TIBCO Enterprise Runtime for R (TERR), the return value has the SpotfireColumnMetaData attribute set to enable TIBCO Spotfire to recognize it as a WKB geometry representation.

See Also

writeEnvelope, readWKB

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
# load package sp
library(sp)

# create an object of class SpatialPoints
x = c(1, 2)
y = c(3, 2)
obj <- SpatialPoints(data.frame(x, y))

# convert to WKB Point
wkb <- writeWKB(obj)


# create a list of objects of class SpatialPoints
x1 = c(1, 2, 3, 4, 5)
y1 = c(3, 2, 5, 1, 4)
x2 <- c(9, 10, 11, 12, 13)
y2 <- c(-1, -2, -3, -4, -5)
Sp1 <- SpatialPoints(data.frame(x1, y1))
Sp2 <- SpatialPoints(data.frame(x2, y2))
obj <- list("a"=Sp1, "b"=Sp2)

# convert to WKB MultiPoint
wkb <- writeWKB(obj)


# create an object of class SpatialLines
l1 <- data.frame(x = c(1, 2, 3), y = c(3, 2, 2))
l1a <- data.frame(x = l1[, 1] + .05, y = l1[, 2] + .05)
l2 <- data.frame(x = c(1, 2, 3), y = c(1, 1.5, 1))
Sl1 <- Line(l1)
Sl1a <- Line(l1a)
Sl2 <- Line(l2)
S1 <- Lines(list(Sl1, Sl1a), ID = "a")
S2 <- Lines(list(Sl2), ID = "b")
obj <- SpatialLines(list(S1, S2))

# convert to WKB MultiLineString
wkb <- writeWKB(obj)


# create an object of class SpatialPolygons
triangle <- Polygons(
  list(
    Polygon(data.frame(x = c(2, 2.5, 3, 2), y = c(2, 3, 2, 2)))
  ), "triangle")
rectangles <- Polygons(
   list(
     Polygon(data.frame(x = c(0, 0, 1, 1, 0), y = c(0, 1, 1, 0, 0))),
     Polygon(data.frame(x = c(0, 0, 2, 2, 0), y = c(-2, -1, -1, -2, -2)))
   ), "rectangles")
obj <- SpatialPolygons(list(triangle, rectangles))

# convert to WKB MultiPolygon
wkb <- writeWKB(obj)


# use the WKB as a column in a data frame
ds <- data.frame(ID = c("a","b"), Geometry = wkb)

# calculate envelope columns and cbind to the data frame
coords <- writeEnvelope(obj)
ds <- cbind(ds, coords)

ianmcook/wkb documentation built on Dec. 6, 2019, 8:46 a.m.