Introdução ao pacote sp
description
Transcript of Introdução ao pacote sp
![Page 1: Introdução ao pacote sp](https://reader036.fdocumentos.tips/reader036/viewer/2022070411/56814865550346895db572a1/html5/thumbnails/1.jpg)
Introdução ao pacote spPedro Ribeiro de AndradeDSA/CCST/INPESão José dos Campos, 2012
Apresentação baseada em:Pebesma & Bivand. S Classes and Methods for Spatial Data: the sp Package
![Page 2: Introdução ao pacote sp](https://reader036.fdocumentos.tips/reader036/viewer/2022070411/56814865550346895db572a1/html5/thumbnails/2.jpg)
Pacotes
install.packages() require()
base datasetsgrDevices graphicsgrid methods splines stats stats4 tcltktools utils
KernSmooth MASSMatrix bootclass clustercodetools foreignlattice mgcvnlme nnetrpart spatialsurvival
mais de 2000 pacotes no CRAN
outros pacotes fora do CRAN
![Page 3: Introdução ao pacote sp](https://reader036.fdocumentos.tips/reader036/viewer/2022070411/56814865550346895db572a1/html5/thumbnails/3.jpg)
Rproject -> CRAN -> Mirror ->TaskView-> Spatial
![Page 4: Introdução ao pacote sp](https://reader036.fdocumentos.tips/reader036/viewer/2022070411/56814865550346895db572a1/html5/thumbnails/4.jpg)
sp: Tipos de Dados Espaciais
![Page 5: Introdução ao pacote sp](https://reader036.fdocumentos.tips/reader036/viewer/2022070411/56814865550346895db572a1/html5/thumbnails/5.jpg)
SpatialPointsxc = round(runif(10), 2)
yc = round(runif(10), 2)
xy = cbind(xc, yc)xy.sp = SpatialPoints(xy)class(xy.sp)xy.sp[1:3,]xy.sp[1:3]bbox(xy.sp)summary(xy.sp)coordinates(xy.sp)as(xy.sp, "data.frame")plot(xy.sp)
![Page 6: Introdução ao pacote sp](https://reader036.fdocumentos.tips/reader036/viewer/2022070411/56814865550346895db572a1/html5/thumbnails/6.jpg)
SpatialPointsDataFrame
df = data.frame(ID=paste(1:10), z1 = 5 + rnorm(10), z2 = 20:29)xy.spdf = SpatialPointsDataFrame(xy, df)xy.spdf = SpatialPointsDataFrame(xy.sp, df)
names(xy.spdf)
coordinates(xy.spdf)
xy.spdf[1:2, ]
xy.spdf[,1]
xy.spdf[,"ID"]
xy.spdf[,c("ID","z2")]
xy.spdf[2:5,c("ID","z2")]
![Page 7: Introdução ao pacote sp](https://reader036.fdocumentos.tips/reader036/viewer/2022070411/56814865550346895db572a1/html5/thumbnails/7.jpg)
SpatialPointsDataFrame – plotrequire(lattice)
data(meuse)coordinates(meuse)=~x+yspplot(meuse)spplot(meuse[,"zinc"], scales=list(draw=T))spplot(meuse[1:100,"zinc"], do.log = T)
spplot(meuse[,c("cadmium", "copper")], do.log = T)bubble(meuse,"cadmium", maxsize = 1.5, key.entries =
2^(-1:4))bubble(meuse[meuse$cadmium > 10,],"cadmium",
maxsize = 1.5, key.entries = 2^(-1:4))
![Page 8: Introdução ao pacote sp](https://reader036.fdocumentos.tips/reader036/viewer/2022070411/56814865550346895db572a1/html5/thumbnails/8.jpg)
SpatialLinesl1 = cbind(c(1, 2, 3), c(3, 2, 2))
l1a = cbind(l1[, 1] + 0.05, l1[, 2] + 0.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(Sl)
plot(Sl, col = c("red", "blue"))
![Page 9: Introdução ao pacote sp](https://reader036.fdocumentos.tips/reader036/viewer/2022070411/56814865550346895db572a1/html5/thumbnails/9.jpg)
SpatialLinesDataFrame
df = data.frame(z = c(1, 2), row.names = c("a", "b"))
Sldf = SpatialLinesDataFrame(Sl, data = df)
as.data.frame(Sldf)
as(Sldf, "data.frame")
summary(Sldf)
spplot(Sldf)
![Page 10: Introdução ao pacote sp](https://reader036.fdocumentos.tips/reader036/viewer/2022070411/56814865550346895db572a1/html5/thumbnails/10.jpg)
SpatialPolygons
Sr1 = Polygon(cbind(c(2, 4, 4, 1, 2), c(2, 3, 5, 4, 2)))
Sr2 = Polygon(cbind(c(5, 4, 2, 5), c(2, 3, 2, 2)))
Sr3 = Polygon(cbind(c(4, 4, 5, 10, 4), c(5, 3, 2, 5, 5)))
Srs1 = Polygons(list(Sr1), "s1")
Srs2 = Polygons(list(Sr2), "s2")
Srs3 = Polygons(list(Sr3), "s3")
SpP = SpatialPolygons(list(Srs1, Srs2, Srs3), 1:3)
plot(SpP)
plot(SpP, col=c("red","blue","green"))
![Page 11: Introdução ao pacote sp](https://reader036.fdocumentos.tips/reader036/viewer/2022070411/56814865550346895db572a1/html5/thumbnails/11.jpg)
SpatialPolygonsDataFrame
attr = data.frame(a = 1:3, b = 3:1,
row.names = c("s3", "s2", "s1"))
SrDf = SpatialPolygonsDataFrame(SpP, attr)
as(SrDf, "data.frame")
summary(SrDf)
plot(SrDf)
spplot(SrDf)
spplot(SrDf[c("s1","s2"),])
![Page 12: Introdução ao pacote sp](https://reader036.fdocumentos.tips/reader036/viewer/2022070411/56814865550346895db572a1/html5/thumbnails/12.jpg)
SpatialPolygonsDataFrame – plot
data(meuse.riv)
meuse.riv
p=Polygon(meuse.riv)
P=Polygons(list(p), "meuse.riv")
meuse.sr =SpatialPolygons(list(P))
rv = list("sp.polygons", meuse.sr, fill = "lightblue")
spplot(meuse[,"zinc"], do.log=TRUE, sp.layout=list(rv))
![Page 13: Introdução ao pacote sp](https://reader036.fdocumentos.tips/reader036/viewer/2022070411/56814865550346895db572a1/html5/thumbnails/13.jpg)
SpatialPolygonsDataFrame – plot
library(maptools)
nc <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1], proj4string=CRS("+proj=longlat +datum=NAD27"))
summary(nc)
nc2=nc[c(67:71,84:86),]
plot(nc2,asp=1)
invisible(text(getSpPPolygonsLabptSlots(nc), labels=as.character(nc$NAME), cex=0.75))
plot(nc, add=T,asp=1)
box()
![Page 14: Introdução ao pacote sp](https://reader036.fdocumentos.tips/reader036/viewer/2022070411/56814865550346895db572a1/html5/thumbnails/14.jpg)
SpatialPolygonsDataFrame – plot
spplot(nc[c("SID74", "SID79")])
rrt <- nc$SID74/nc$BIR74
brks <- quantile(rrt, seq(0,1,1/7))
dens <- (2:length(brks))*15
plot(nc, density=dens[findInterval(rrt, brks, all.inside=TRUE)])
box()
![Page 15: Introdução ao pacote sp](https://reader036.fdocumentos.tips/reader036/viewer/2022070411/56814865550346895db572a1/html5/thumbnails/15.jpg)
S4 – objetos
getSlots("SpatialPoints")slotNames(xy.sp)slot(xy.sp,"bbox")xy.sp@bbox
getSlots("Line")getSlots("Lines")getSlots("SpatialLines")sapply(slot(Sl, "lines"), function(x) slot(x, "ID"))
![Page 16: Introdução ao pacote sp](https://reader036.fdocumentos.tips/reader036/viewer/2022070411/56814865550346895db572a1/html5/thumbnails/16.jpg)
Grids e Pixelsgt = GridTopology(cellcentre.offset = c(1, 1),
cellsize = c(1, 1), cells.dim = c(3, 4))
grd = SpatialGrid(gt)
summary(grd)
gridparameters(grd)
plot(grd)
pts = expand.grid(x = 1:3, y = 1:4)
grd.pts = SpatialPixels(SpatialPoints(pts))
summary(grd.pts)
grd = as(grd.pts, "SpatialGrid")
summary(grd)
![Page 17: Introdução ao pacote sp](https://reader036.fdocumentos.tips/reader036/viewer/2022070411/56814865550346895db572a1/html5/thumbnails/17.jpg)
Grids e Pixelsattr = expand.grid(xc = 1:3, yc = 1:3)
grd.attr = data.frame(attr, z1 = 1:9, z2 = 9:1)
coordinates(grd.attr) = ~xc + yc
gridded(grd.attr)
gridded(grd.attr) = TRUE
gridded(grd.attr)
summary(grd.attr)
![Page 18: Introdução ao pacote sp](https://reader036.fdocumentos.tips/reader036/viewer/2022070411/56814865550346895db572a1/html5/thumbnails/18.jpg)
Pontos ou Matrizes?
fullgrid(grd); fullgrid(grd.pts); fullgrid(grd.attr)
fullgrid(grd.pts) = TRUE
fullgrid(grd.attr) = TRUE
fullgrid(grd.pts)
fullgrid(grd.attr)
fullgrid(grd.attr) = FALSE
image(grd.attr[1:5, "z1"])
fullgrid(grd.attr) = TRUE
image(grd.attr[1])
image(grd.attr["z2"])
![Page 19: Introdução ao pacote sp](https://reader036.fdocumentos.tips/reader036/viewer/2022070411/56814865550346895db572a1/html5/thumbnails/19.jpg)
SpatialGridsrequire(splancs)
data(bodmin)
b.xy <- coordinates(bodmin[1:2])
r = apply(bodmin$poly, 2, range)
(r[2,]-r[1,])/0.2
grd1 <- GridTopology(cellcentre.offset=c(-5.2, -11.5), cellsize=c(0.2, 0.2), cells.dim=c(75,100))
(r[2,]-r[1,])/0.1
grd1 <- GridTopology(cellcentre.offset=c(-5.2, -11.5), cellsize=c(0.1, 0.1), cells.dim=c(150,200))
![Page 20: Introdução ao pacote sp](https://reader036.fdocumentos.tips/reader036/viewer/2022070411/56814865550346895db572a1/html5/thumbnails/20.jpg)
SpatialGridsk100 <- spkernel2d(b.xy, bodmin$poly, h0=1, grd1)
k150 <- spkernel2d(b.xy, bodmin$poly, h0=1.5, grd1)
k200 <- spkernel2d(b.xy, bodmin$poly, h0=2, grd1)
k250 <- spkernel2d(b.xy, bodmin$poly, h0=2.5, grd1)
df <- data.frame(k100, k150, k200, k250)
kernels <- SpatialGridDataFrame(grd1, data=df)
spplot(kernels, col.regions=terrain.colors(16), cut=15)
image(kernels[1])
contour(kernels[1],add=T, nlev=5)
![Page 21: Introdução ao pacote sp](https://reader036.fdocumentos.tips/reader036/viewer/2022070411/56814865550346895db572a1/html5/thumbnails/21.jpg)
Introdução ao pacote spPedro Ribeiro de AndradeDSA/CCST/INPESão José dos Campos, 2012
Apresentação baseada em:Pebesma & Bivand. S Classes and Methods for Spatial Data: the sp Package
![Page 22: Introdução ao pacote sp](https://reader036.fdocumentos.tips/reader036/viewer/2022070411/56814865550346895db572a1/html5/thumbnails/22.jpg)
Projeções: rgdal com PROJ.4
require(rgdal)
data(state)
states <- data.frame(state.x77, state.center)
states <- states[states$x > -121,]
coordinates(states) <- c("x", "y")
proj4string(states) <- CRS("+proj=longlat +ellps=clrk66")
summary(states)
state.merc <- spTransform(states, CRS=CRS("+proj=merc +ellps=GRS80"))
summary(state.merc)