toSDT: toSDT

Description Usage Arguments Examples

Description

Converts Spatial objects (from packages sp and sf) into a data.table

Usage

1
toSDT(sobj, encode = FALSE, id = NULL)

Arguments

sobj

Spatial or sf object

encode

logical

id

an id

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
## Not run: 

library(googleway)
library(sp)

## SpatialPointsDataFrame
sp <- SpatialPointsDataFrame(coords = tram_stops[, c("stop_lon", "stop_lat")],
    data = tram_stops[, setdiff(names(tram_stops), c("stop_lon", "stop_lat"))])
dt <- toSDT(sp)

## spLine
spLine <- Line(tram_route[, c("shape_pt_lat", "shape_pt_lon")])
dt <- toSDT(spLine)

## spLines
spLines <- Lines(Line(tram_route[, c("shape_pt_lat", "shape_pt_lon")]), ID = 1)
dt <- toSDT(spLines)

## SpatialLines
spLines <- SpatialLines(list(Lines(Line(tram_route[, c("shape_pt_lat", "shape_pt_lon")]), ID = 1)))
dt <- toSDT(spLines)

## SpatialLinesDataFrame
spdf <- SpatialLinesDataFrame(spLines, data = data.frame(route = 35, operator = "Yarra Trams"))
dt <- toSDT(spdf)

## Polygon
df <- data.frame(lat = c(25.774, 18.466, 32.321, 28.745, 29.570, 27.339),
                 lon = c(-80.190, -66.118, -64.757, -70.579, -67.514, -66.668),
                 id = c(rep('outer', 3), rep('inner', 3)))
pl_outer <- Polygon(df[df$id == "outer", c("lat", "lon")])
dt <- toSDT(pl_outer)

## Polygons
pl_inner <- Polygon(df[df$id == "inner", c("lat", "lon")])
pl <- Polygons(list(pl_outer, pl_inner), ID = "bermuda")
dt <- toSDT(pl)

## SpatialPolygons
sppl <- SpatialPolygons(list(pl))
dt <- toSDT(sppl)

## SpatialPolygonsDataFrame
spdf <- SpatialPolygonsDataFrame(sppl, data = data.frame(ID = c("bermuda")), match.ID = FALSE)
dt <- toSDT(spdf)



library(sf)


## sfc MULTIPOINT
p <- rbind(c(3.2,4), c(3,4.6), c(3.8,4.4), c(3.5,3.8), c(3.4,3.6), c(3.9,4.5))
mp <- st_multipoint(p)
dt <- toSDT(mp)

## sfc LINESTRING
s1 <- rbind(c(0,3),c(0,4),c(1,5),c(2,5))
ls <- st_linestring(s1)
toSDT(ls)

## sfc MULTILINESTRING
s2 <- rbind(c(0.2,3), c(0.2,4), c(1,4.8), c(2,4.8))
s3 <- rbind(c(0,4.4), c(0.6,5))
mls <- st_multilinestring(list(s1,s2,s3))
dt <- toSDT(mls)

## sfc POLYGONS
p1 <- rbind(c(0,0), c(1,0), c(3,2), c(2,4), c(1,4), c(0,0))
p2 <- rbind(c(1,1), c(1,2), c(2,2), c(1,1))
pol <-st_polygon(list(p1,p2))
dt <- toSDT(pol)

## sfc MULTIPOLYGON
p3 <- rbind(c(3,0), c(4,0), c(4,1), c(3,1), c(3,0))
p4 <- rbind(c(3.3,0.3), c(3.8,0.3), c(3.8,0.8), c(3.3,0.8), c(3.3,0.3))[5:1,]
p5 <- rbind(c(3,3), c(4,2), c(4,3), c(3,3))
mpol <- st_multipolygon(list(list(p1,p2), list(p3,p4), list(p5)))
dt <- toSDT(mpol)


## End(Not run)

SymbolixAU/spatialdatatable documentation built on May 6, 2019, 11:20 a.m.