writeWKB {wkb}R Documentation

Convert Spatial Objects to WKB

Description

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

Usage

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

# 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)

[Package wkb version 0.4-0 Index]