200 likes | 210 Views
Learn about the sp package in R and its capabilities for working with spatial data. Explore various packages for spatial data analysis and visualization.
E N D
Aula 2:Introdução ao sp Pedro Ribeiro de Andrade CCST/INPE São José dos Campos, 2016 Apresentação baseada em: Pebesma & Bivand. S Classes and Methods for Spatial Data: the sp Package
mais de 8000 pacotes no CRAN outros pacotes fora do CRAN Pacotes install.packages() require() base datasetsgrDevices graphicsgrid methods splines stats stats4 tcltktools utils KernSmooth MASS Matrix boot class cluster codetools foreign lattice mgcv nlme nnet rpart spatial survival
SpatialPoints xc = 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)
SpatialPointsDataFrame df = data.frame(ID=paste(1:10), z1 = round(5 + rnorm(10), 2), 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")]
SpatialPointsDataFrame – plot require(lattice) trellis.par.set(sp.theme()) data(meuse) coordinates(meuse)=~x+y spplot(meuse) spplot(meuse[,"zinc"], scales=list(draw=T)) spplot(meuse[1:100,"zinc"], do.log = T) spplot(meuse[,"zinc"], do.log = T, cuts = 3, legendEntries = c("low", "intermediate", "high")) spplot(meuse[,c("cadmium", "copper")], do.log = T) bubble(meuse,"cadmium", maxsize = 1.5, key.entries = 2^(-1:4))
SpatialLines l1 = 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"))
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)
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))) Sr4 = Polygon(cbind(c(5, 6, 6, 5, 5), c(4, 4, 3, 3, 4)), hole = TRUE) Srs1 = Polygons(list(Sr1), "s1") Srs2 = Polygons(list(Sr2), "s2") Srs3 = Polygons(list(Sr3, Sr4), "s34") SpP = SpatialPolygons(list(Srs1, Srs2, Srs3), 1:3) plot(SpP) plot(SpP, col=c("red","blue","green"))
SpatialPolygonsDataFrame attr = data.frame(a = 1:3, b = 3:1, row.names = c("s34", "s2", "s1")) SrDf = SpatialPolygonsDataFrame(SpP, attr) as(SrDf, "data.frame") summary(SrDf) plot(SrDf) spplot(SrDf) spplot(SrDf[c("s1","s2"),])
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))
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()
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()
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"))
Grids e Pixels gt = 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)
Grids e Pixels attr = 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)
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"])
SpatialGrids require(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))
SpatialGrids k100 <- 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)