-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathpass1.R
155 lines (133 loc) · 3.91 KB
/
pass1.R
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
options("rgdal_show_exportToProj4_warnings"="none")
library(sp)
data(meuse)
x = meuse
coordinates(x) = cbind(rnorm(155), rnorm(155))
# should pass:
names(x@data)
names(as.data.frame(x))
class(as(x, "data.frame"))
x = meuse
# coordinates defined as data:
coordinates(x) = cbind(xcoord = rnorm(155), ycoord = rnorm(155))
# should pass:
names(x@data)
names(as.data.frame(x))
is.projected(x)
proj4string(x)
set.seed(13131) # make sample reproducable:
x = meuse[, sample(ncol(meuse))] # 'randomly' shuffle columns
# coordinates defined as variable names:
coordinates(x) = c("x", "y") # no matter their position
#plot(x, cex=.05 * sqrt(x@data[,"zinc"]),
plot(x, cex=.05 * sqrt(as.data.frame(x)[["zinc"]]),pch=1)
title("Meuse: zinc bubble plot")
print(summary(x))
# coordinates defined as formula:
x = meuse[, 1:5]
coordinates(x) = ~x+y
print(summary(x))
# a = NULL
# cc = cbind(sample(1:10), sample(1:10), sample(1:10))
# coordinates(a) = cc
# summary(a)
xx = SpatialPointsDataFrame(matrix(1:10,5,2),data.frame(f = 1:5))
rbind(xx,xx,xx,xx)
grd <- GridTopology(c(1,1), c(1,1), c(10,10))
polys <- as.SpatialPolygons.GridTopology(grd)
summary(rbind(polys[1:10], polys[11:20], polys[21:30]))
plot(rbind(polys[1:10],polys[21:30]))
title("2 x 10 blocks -- test rbind on SpatialPolygons")
l1 = cbind(c(1,2,3),c(3,2,2))
l1a = cbind(l1[,1]+.05,l1[,2]+.05)
l2 = cbind(c(1,2,3),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")
Sl = SpatialLines(list(S1,S2))
summary(as(polys, "SpatialLines"))
summary(as(Sl, "SpatialPoints"))
summary(as(Sl, "SpatialPointsDataFrame"))
SlDf = SpatialLinesDataFrame(Sl, data.frame(xx = c("foo", "bar")), match.ID = FALSE)
summary(as(SlDf, "SpatialPointsDataFrame"))
meuse[["xxx"]] = log(meuse$zinc)
summary(meuse)
meuse$xxy = log(meuse[["zinc"]])
summary(meuse)
# test behaviour on zero-length objects:
demo(meuse, ask = FALSE, echo = FALSE)
p = as(meuse, "SpatialPoints")
p[0]
dim(p[0])
meuse[0,]
dim(meuse[1:3,0])
dim(subset(meuse, zinc < 100, 0))
dim(subset(meuse, , 0))
dim(subset(meuse, F, 0))
dim(meuse.grid[0,])
fullgrid(meuse.grid) = TRUE
dim(meuse.grid[0,])
summary(meuse.grid[0,])
dim(meuse.grid[,0])
summary(meuse.grid[,0])
dim(meuse.grid[0,,0])
summary(meuse.grid[0,,0])
# SpatialPolygons:
L = as(meuse.riv, "SpatialPolygons")
length(L[1])
length(L[0])
summary(L[0])
# SpatialPolygonsDataFrame:
L$something = "bla"
class(L)
length(L[1])
summary(L[1])
length(L[0])
summary(L[0])
# SpatialLines
L = as(meuse.riv, "SpatialLines")
length(L[1])
length(L[0])
summary(L[0])
# SpatialLinesDataFrame
L$something = "bla"
class(L)
length(L[1])
summary(L[1])
length(L[0])
summary(L[0])
m = meuse
all.equal(rbind(m[1:3,], m[5:7,]), rbind(m[1:3,], m[0,], m[5:7,]))
all.equal(rbind(m[1:3,], m[5:7,]), rbind(m[0,], m[1:3,], m[0,], m[5:7,]))
all.equal(rbind(m[1:3,], m[5:7,]), rbind(m[1:3,], m[0,], m[5:7,], m[0,]))
# match.ID settings for SpatialPointsDataFrame():
set.seed(1331)
pts = cbind(1:5, 1:5)
dimnames(pts)[[1]] = letters[1:5]
df = data.frame(a = 1:5)
row.names(df) = letters[5:1]
library(sp)
options(warn=1) # show warnings where they occur
SpatialPointsDataFrame(pts, df) # warn
SpatialPointsDataFrame(pts, df, match.ID = TRUE) # don't warn
SpatialPointsDataFrame(pts, df, match.ID = FALSE) # don't warn
df$m = letters[5:1]
SpatialPointsDataFrame(pts, df, match.ID = "m") # don't warn
dimnames(pts)[[1]] = letters[5:1]
pts
SpatialPointsDataFrame(pts, df) # don't warn: match doesn't reorder
# duplicated row name:
dimnames(pts)[[1]] = letters[c(1:4,1)]
pts
xx = try(x <- SpatialPointsDataFrame(pts, df))
class(xx)
xx
SpatialPointsDataFrame(pts, df, match.ID = FALSE)
try(x <- SpatialPointsDataFrame(pts, df, match.ID = TRUE)) # fail
dimnames(pts)[[1]] = letters[5:1]
row.names(df) = letters[6:2] # non-matching row.names
SpatialPointsDataFrame(pts, df) # do not match.ID
SpatialPointsDataFrame(pts, df, match.ID = FALSE)
try(x <- SpatialPointsDataFrame(pts, df, match.ID = TRUE)) # fail