R : Copyright 2005, The R Foundation for Statistical Computing Version 2.1.1 (2005-06-20), ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for a HTML browser interface to help. Type 'q()' to quit R. > ### *
> ### > attach(NULL, name = "CheckExEnv") > assign(".CheckExEnv", as.environment(2), pos = length(search())) # base > ## add some hooks to label plot pages for base and grid graphics > setHook("plot.new", ".newplot.hook") > setHook("persp", ".newplot.hook") > setHook("grid.newpage", ".gridplot.hook") > > assign("cleanEx", + function(env = .GlobalEnv) { + rm(list = ls(envir = env, all.names = TRUE), envir = env) + RNGkind("default", "default") + set.seed(1) + options(warn = 1) + delayedAssign("T", stop("T used instead of TRUE"), + assign.env = .CheckExEnv) + delayedAssign("F", stop("F used instead of FALSE"), + assign.env = .CheckExEnv) + sch <- search() + newitems <- sch[! sch %in% .oldSearch] + for(item in rev(newitems)) + eval(substitute(detach(item), list(item=item))) + missitems <- .oldSearch[! .oldSearch %in% sch] + if(length(missitems)) + warning("items ", paste(missitems, collapse=", "), + " have been removed from the search path") + }, + env = .CheckExEnv) > assign("..nameEx", "__{must remake R-ex/*.R}__", env = .CheckExEnv) # for now > assign("ptime", proc.time(), env = .CheckExEnv) > grDevices::postscript("maptools-Examples.ps") > assign("par.postscript", graphics::par(no.readonly = TRUE), env = .CheckExEnv) > options(contrasts = c(unordered = "contr.treatment", ordered = "contr.poly")) > options(warn = 1) > library('maptools') Loading required package: foreign > > assign(".oldSearch", search(), env = .CheckExEnv) > assign(".oldNS", loadedNamespaces(), env = .CheckExEnv) > cleanEx(); ..nameEx <- "Map2poly" > > ### * Map2poly > > flush(stderr()); flush(stdout()) > > ### Name: Map2poly > ### Title: Create polygon lists and bounding boxes from imported shapefiles > ### Aliases: Map2poly Map2poly1 Map2lines Map2points Map2bbs MapShapeIds > ### shape2poly shape2lines shape2points shape2bbs convert.pl > ### Keywords: spatial > > ### ** Examples > > try2 <- read.shape(system.file("shapes/columbus.shp", package="maptools")[1]) Shapefile Type: Polygon # of Shapes: 49 > mappolys <- Map2poly(try2, as.character(try2$att.data$NEIGNO), quiet=FALSE) > plot(mappolys) > title(main="Polygons for Columbus OH from maptools package") > mappolys <- Map2poly1(try2, as.character(try2$att.data$NEIGNO), raw=FALSE) > plot(mappolys) > title(main="Polygons for Columbus OH from maptools package") > try3 <- read.shape(system.file("shapes/baltim.shp", package="maptools")[1]) Shapefile Type: Point # of Shapes: 211 > baltpts <- Map2points(try3) > xylims <- attr(baltpts, "maplim") > plot(xylims$x, xylims$y, asp=1, type='n', xlab="", ylab="") > points(baltpts) > title(main="Baltimore points from maptools package") > try4 <- read.shape(system.file("shapes/fylk-val.shp", package="maptools")[1]) Shapefile Type: Line # of Shapes: 97 > fylk.val <- Map2lines(try4) > xylims <- attr(fylk.val, "maplim") > plot(xylims$x, xylims$y, asp=1, type='n', xlab="", ylab="") > for (i in 1:length(fylk.val)) lines(fylk.val[[i]]) > title(main="Norwegian river centrelines from maptools package") > > > > cleanEx(); ..nameEx <- "dotsInPolys" > > ### * dotsInPolys > > flush(stderr()); flush(stdout()) > > ### Name: dotsInPolys > ### Title: Put dots in polygons > ### Aliases: dotsInPolys > ### Keywords: spatial > > ### ** Examples > > x <- read.shape(system.file("shapes/sids.shp", package="maptools")[1]) Shapefile Type: Polygon # of Shapes: 100 > ncpolys <- Map2poly(x) > try1 <- dotsInPolys(ncpolys, as.integer(x$att.data$SID74)) Loading required package: splancs Spatial Point Pattern Analysis Code in S-Plus Version 2 - Spatial and Space-Time analysis > plot(ncpolys) > xx <- lapply(try1, function(x) {if (!is.null(x)) points(x, pch=18, col="red")}) > try2 <- dotsInPolys(ncpolys, as.integer(x$att.data$SID74), f=gridpts) > plot(ncpolys) > xx <- lapply(try2, function(x) {if (!is.null(x)) points(x, pch=18, col="red")}) > > > > cleanEx(); ..nameEx <- "get.Pcent" > > ### * get.Pcent > > flush(stderr()); flush(stdout()) > > ### Name: get.Pcent > ### Title: Polygon centroids > ### Aliases: get.Pcent > ### Keywords: spatial > > ### ** Examples > > x <- read.shape(system.file("shapes/sids.shp", package="maptools")[1]) Shapefile Type: Polygon # of Shapes: 100 > get.Pcent(x) [,1] [,2] [1,] -81.49826 36.43140 [2,] -81.12515 36.49101 [3,] -80.68575 36.41252 [4,] -76.02750 36.40728 [5,] -77.41056 36.42228 [6,] -76.99478 36.36145 [7,] -76.23435 36.40120 [8,] -76.70448 36.44423 [9,] -78.11043 36.39697 [10,] -80.23428 36.40034 [11,] -79.33477 36.39347 [12,] -79.77038 36.39600 [13,] -78.65647 36.30013 [14,] -78.97684 36.38870 [15,] -78.41127 36.36234 [16,] -77.65628 36.25305 [17,] -76.31460 36.31237 [18,] -81.15963 36.20160 [19,] -81.69129 36.22480 [20,] -76.45461 36.20488 [21,] -76.61648 36.14893 [22,] -81.92144 36.07198 [23,] -80.66411 36.15479 [24,] -78.28700 36.07763 [25,] -80.25333 36.12565 [26,] -79.78280 36.07292 [27,] -79.39793 36.03766 [28,] -76.98481 36.06190 [29,] -79.12174 36.05365 [30,] -78.87806 36.02871 [31,] -77.98667 35.96233 [32,] -82.16333 36.00596 [33,] -77.59819 35.90722 [34,] -81.54366 35.94707 [35,] -82.30771 35.89220 [36,] -77.10766 35.83635 [37,] -78.65277 35.78448 [38,] -82.70532 35.85323 [39,] -80.87108 35.80282 [40,] -80.54156 35.92497 [41,] -81.17401 35.91575 [42,] -80.20826 35.79001 [43,] -81.70216 35.74488 [44,] -76.58298 35.82505 [45,] -76.21982 35.80960 [46,] -82.04492 35.67533 [47,] -79.80373 35.70548 [48,] -79.25662 35.69640 [49,] -77.92273 35.69914 [50,] -80.51933 35.63685 [51,] -77.37790 35.58916 [52,] -81.21384 35.65801 [53,] -82.52827 35.60683 [54,] -78.36664 35.51347 [55,] -82.98101 35.55149 [56,] -75.80982 35.73548 [57,] -76.87100 35.49595 [58,] -83.48978 35.48066 [59,] -77.67889 35.48230 [60,] -79.17371 35.47106 [61,] -81.91787 35.39873 [62,] -78.00720 35.35737 [63,] -78.87091 35.36657 [64,] -81.54949 35.32960 [65,] -81.22072 35.48059 [66,] -83.13941 35.28256 [67,] -79.47839 35.30621 [68,] -80.82937 35.24492 [69,] -80.55087 35.38489 [70,] -79.90283 35.32721 [71,] -80.24922 35.31393 [72,] -82.47627 35.33470 [73,] -83.82959 35.34506 [74,] -77.64549 35.23284 [75,] -82.79516 35.19970 [76,] -81.17521 35.29235 [77,] -82.17027 35.27568 [78,] -83.42232 35.14511 [79,] -78.36993 34.98674 [80,] -76.76878 35.13564 [81,] -84.05976 35.13107 [82,] -78.82654 35.04381 [83,] -77.35634 35.01854 [84,] -80.53134 34.98631 [85,] -80.10407 34.97520 [86,] -79.23592 35.01168 [87,] -76.24704 35.53211 [88,] -77.93361 34.93246 [89,] -79.74231 35.00146 [90,] -83.74725 35.05376 [91,] -77.10434 35.13080 [92,] -79.47720 34.83949 [93,] -77.44294 34.73910 [94,] -79.10111 34.63874 [95,] -76.69562 34.81979 [96,] -78.56013 34.60932 [97,] -77.91628 34.52686 [98,] -78.65492 34.26323 [99,] -77.89578 34.26166 [100,] -78.25073 34.07663 > > > > cleanEx(); ..nameEx <- "holepolys" > > ### * holepolys > > flush(stderr()); flush(stdout()) > > ### Name: gpcholes > ### Title: Hisaji Ono's lake/hole problem > ### Aliases: gpcholes hole1pl hole2pl > ### Keywords: datasets > > ### ** Examples > > data(gpcholes) > plot(hole2pl, col="red", pbg="white", border="blue", forcefill=FALSE) > plot(hole1pl, col="red", pbg="white", border="blue", forcefill=FALSE) > > > > cleanEx(); ..nameEx <- "maptools" > > ### * maptools > > flush(stderr()); flush(stdout()) > > ### Name: maptools > ### Title: Report version information and changes > ### Aliases: maptools > ### Keywords: spatial > > ### ** Examples > > maptools(changes=TRUE) maptools, version 0.4-13, 2005-03-04 0.4-2 Introduced changes including warnings in Map2poly() and plot.polylist() 0.4-2 with regard to rind-direction and plot-order heuristics - Map2poly() 0.4-2 tries to determine which polygons and sub-polygons are wholly contained 0.4-2 within each other, to note the order in which they should be plotted in 0.4-2 plot.polygon() to avoid over-plotting when painting polygons, and, if the 0.4-2 raw= argument is FALSE, to correct possible wrong ring directions (lakes 0.4-2 are not usually found in the sea etc.). plot.polylist() then tries to use 0.4-2 this information to avoid over-painting polygons by painting enclosed 0.4-2 units after their enclosing units, and, if the forcefill= argument is 0.4-2 FALSE, will paint anti-clockwise rings (aka holes) with background colour 0.4-2 rather than fill colour. At the present, raw= and forcefill= defaults are 0.4-2 TRUE, and warnings are issued that this behaviour will change. The 0.4-2 underlying problem is that most shapefiles in circulation do not follow 0.4-2 ESRI specifications for polygons, many have at least some rings in the 0.4-2 wrong direction, over and above slivers and dangles. This is unlikely to 0.4-2 change, so defensive code is required. 0.4-3 Back out of a too-aggressive heuristic for guessing ring-directions and 0.4-3 plot-ordering 0.4-4 Corrected C code for reading SHP/SHX files to reduce danger of mismatched 0.4-4 PROTECT/UNPROTECT pairs, and hopefully resolve issues with crashes on 0.4-4 Windows platforms in some cases. 0.4-5 Corrected C code underlying the reading of DBF files to allow character 0.4-5 fields to be arbitrarily wide (bug report Trondheim diamonds with fields 0.4-5 over 200 characters wide - buffer overflow from unchecked max width 49); 0.4-5 Added access to CHANGELOG through maptools() function. 0.4-6 Allow axis labels to be passed through plot.polylist() (thanks to Lukas 0.4-6 Meier) 0.4-7 The dbf.read() and dbf.write() functions have been revised and added to 0.4-7 the user-visible namespace. New functions write.pointShape() and 0.4-7 write.polylistShape() using dbf.write() has been added to let data.frames 0.4-7 be written as point-type shapefiles, or as polygon-type shapefiles with 0.4-7 an S3 polylist object, respectively - all these functions should be 0.4-7 treated as alpha-quality until they have been in use for some time; 0.4-7 reports on software that reads ESRI-generated shapefiles but does not 0.4-7 read these files should be sent to the package maintainer. Minor changes 0.4-7 to plot.polylist() to painting of background with pbg= argument; 0.4-7 corrections to plot.Map() to make sure color= and type= are passed to 0.4-7 color.ramp() (thanks to Johannes SCHNITZLER), and to make sure rec= is 0.4-7 respected (thanks to Tony Olsen); dotsInPolys() added for dot density 0.4-7 maps (suggested by Johannes SCHNITZLER); symbolsInPolys() added to place 0.4-7 grids of points over polygons with chosen density and/or symbols 0.4-7 (suggested by Michael Wolf) - the two latter functions use functions from 0.4-7 the splancs package internally. 0.4-8 Functions dbf.read() and dbf.write() replaced by read.dbf() and 0.4-8 write.dbf() in the foreign >= 0.8 package; references now made to those 0.4-8 functions and package dependency on foreign (which is a recommended 0.4-8 package and so not a problem in the course of time). This version of 0.4-8 foreign is new, and so will take time to diffuse. 0.4-9 A bug in write.polylistShape() found by Greg Snow, provoked by not 0.4-9 checking that numeric attributes were integer, has been resolved - they 0.4-9 are now set to the required types. 0.4-10 Function write.linelistShape() added to output a list of lines to a 0.4-10 shapefile, as suggested by Patrick Giraudoux. 0.4-11 Adding checks for length() being zero 0.4-12 Changed condition in tests from 0-4.9 from any() to all(); the plotting 0.4-12 order heuristic in Map2poly is replaced with plotting only in strict 0.4-12 decreasing order of top-level (multi)polygon area (thanks to Paul Bivand 0.4-12 for reporting a malformed plot order from the previous heuristic, 0.4-12 involving one polygon disappearing). In multipolygons, the components 0.4-12 are plotted in decreasing area order. The multpolygons themselves are 0.4-12 assigned their area sum for plotting order. Ring directions are all set 0.4-12 to clockwise - very many shapefiles have been observed with quite 0.4-12 unrealistic ring orders, so respecting input ring orders seems worse 0.4-12 than imposing uniformity. The previous plot order assignment mechanism 0.4-12 is retained using Map2poly1. The subset.polylist() function uses the 0.4-12 decreasing area heuristic for assigning plot order. 0.4-13 subset.polylist() now accepts both old and new polylists (without and 0.4-13 with an area attribute) > > > > cleanEx(); ..nameEx <- "plot.Map" > > ### * plot.Map > > flush(stderr()); flush(stdout()) > > ### Name: plot.Map > ### Title: Plot a Map object > ### Aliases: plot.Map > ### Keywords: spatial > > ### ** Examples > > x <- read.shape(system.file("shapes/sids.shp", package="maptools")[1]) Shapefile Type: Polygon # of Shapes: 100 > plot(x) > nParts <- sapply(x$Shapes, function(x) attr(x, "nPart")) > table(nParts) nParts 1 2 3 94 4 2 > cols <- c("azure", "blue", "orange") > fgs <- cols[nParts] > plot(x, fg=fgs) > res <- plot(x, auxvar=x$att.data$BIR74) > str(res) List of 3 $ ramp : chr [1:5] "#FFCCCC" "#FF9999" "#FF6666" "#FF3333" ... $ col.class: int [1:100] 2 1 4 1 2 2 1 1 2 3 ... $ breaks : num [1:6] 248 855 1553 2718 4450 ... > res <- plot(x, auxvar=x$att.data$BIR74, type="e") > str(res) List of 3 $ ramp : chr [1:5] "#FFCCCC" "#FF9999" "#FF6666" "#FF3333" ... $ col.class: int [1:100] 1 1 1 1 1 1 1 1 1 1 ... $ breaks : num [1:6] 248 4268 8536 12804 17072 ... > > > > cleanEx(); ..nameEx <- "plot.polylist" > > ### * plot.polylist > > flush(stderr()); flush(stdout()) > > ### Name: plot.polylist > ### Title: Plot polygons > ### Aliases: plotpolys plot.polylist leglabs > ### Keywords: spatial > > ### ** Examples > > try2 <- read.shape(system.file("shapes/columbus.shp", package="maptools")[1]) Shapefile Type: Polygon # of Shapes: 49 > mappolys <- Map2poly(try2, as.character(try2$att.data$NEIGNO)) > brks <- round(quantile(try2$att.data$CRIME, probs=seq(0,1,0.2)), digits=2) > colours <- c("salmon1", "salmon2", "red3", "brown", "black") > plot(mappolys, col=colours[findInterval(try2$att.data$CRIME, brks, all.inside=TRUE)], forcefill=FALSE) > legend(x=c(5.8, 7.1), y=c(13, 14.5), legend=leglabs(brks), + fill=colours, bty="n") > invisible(title(main=paste("Columbus OH: residential burglaries and vehicle", "thefts per thousand households, 1980", sep="\n"))) > try3 <- read.shape(system.file("shapes/sids.shp", package="maptools")[1]) Shapefile Type: Polygon # of Shapes: 100 > mappolys <- Map2poly(try3, as.character(try3$att.data$FIPSNO)) > cols <- c("pink", "orange", "red") > np <- sapply(mappolys, function(x) attr(x, "nParts")) > plot(mappolys, col=cols[np], forcefill=FALSE) > invisible(title(main="White: one part, orange: two part, red: three part polygons")) > plot(mappolys, density=c(10, 20, 30)[np], angle=c(-45, 0, 45)[np], axes=FALSE, forcefill=FALSE) > > > > cleanEx(); ..nameEx <- "read.shape" > > ### * read.shape > > flush(stderr()); flush(stdout()) > > ### Name: read.shape > ### Title: Read shapefile into Map object > ### Aliases: read.shape getinfo.shape print.shapehead > ### Keywords: spatial > > ### ** Examples > > x <- read.shape(system.file("shapes/sids.shp", package="maptools")[1]) Shapefile Type: Polygon # of Shapes: 100 > length(x$Shapes) [1] 100 > unlist(lapply(x$att.data, class)) AREA PERIMETER CNTY_ CNTY_ID NAME FIPS FIPSNO CRESS_ID "numeric" "numeric" "integer" "integer" "factor" "factor" "integer" "integer" BIR74 SID74 NWBIR74 BIR79 SID79 NWBIR79 "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" > str(getinfo.shape(system.file("shapes/fylk-val.shp", package="maptools")[1])) List of 5 $ : chr "/CRANPkg/check/maptools.Rcheck/maptools/shapes/fylk-val.shp" $ : int 3 $ : int 97 $ : num [1:4] -4868 6456207 0 0 $ : num [1:4] 1084722 7841997 0 0 - attr(*, "class")= chr "shapehead" > > > > cleanEx(); ..nameEx <- "subset.polylist" > > ### * subset.polylist > > flush(stderr()); flush(stdout()) > > ### Name: subset.polylist > ### Title: Subset polygon list objects > ### Aliases: subset.polylist > ### Keywords: spatial > > ### ** Examples > > nc <- read.shape(system.file("shapes/sids.shp", package = "maptools")[1]) Shapefile Type: Polygon # of Shapes: 100 > mappolys <- Map2poly(nc, as.character(nc$att.data$FIPSNO)) > submap <- subset(mappolys, nc$att.data$SID74 > 0) > plot(mappolys, col="orange") Warning in plot.polylist(mappolys, col = "orange") : From next major release, default fill behaviour will change > plot(submap, add=TRUE, col="lightpink", forcefill=TRUE) Warning in plot.polylist(submap, add = TRUE, col = "lightpink", forcefill = TRUE) : From next major release, default fill behaviour will change > > > > cleanEx(); ..nameEx <- "symbolsInPolys" > > ### * symbolsInPolys > > flush(stderr()); flush(stdout()) > > ### Name: symbolsInPolys > ### Title: Place grids of points over polygons > ### Aliases: symbolsInPolys > ### Keywords: spatial > > ### ** Examples > > x <- read.shape(system.file("shapes/sids.shp", package="maptools")[1]) Shapefile Type: Polygon # of Shapes: 100 > ncpolys <- Map2poly(x) > np <- sapply(ncpolys, function(x) attr(x, "nPart")) > symbs <- c("-", "+", "*") > try1 <- symbolsInPolys(ncpolys, 70, symb=symbs[np]) Spatial Point Pattern Analysis Code in S-Plus Version 2 - Spatial and Space-Time analysis > plot(ncpolys) > xx <- lapply(try1, function(x) {if (!is.null(x)) points(x, pch=attr(x, "symb"))}) > > > > cleanEx(); ..nameEx <- "write.linelistShape" > > ### * write.linelistShape > > flush(stderr()); flush(stdout()) > > ### Name: write.linelistShape > ### Title: Write a arc-type shapefile > ### Aliases: write.linelistShape > ### Keywords: spatial > > ### ** Examples > > x <- 10 * 1:nrow(volcano) > y <- 10 * 1:ncol(volcano) > line.list <- contourLines(x, y, volcano) > vol.levels <- data.frame(alt=sapply(line.list, function(x) x[[1]])) > vol.ll <- lapply(line.list, function(x) cbind(x$x, x$y)) > tmpshp <- paste(tempdir(), "volcano", sep="/") > write.linelistShape(vol.ll, vol.levels, file=tmpshp) > try1 <- read.shape(tmpshp) Shapefile Type: Line # of Shapes: 20 > plot(try1) > > > > cleanEx(); ..nameEx <- "write.pointShape" > > ### * write.pointShape > > flush(stderr()); flush(stdout()) > > ### Name: write.pointShape > ### Title: Write a point-type shapefile > ### Aliases: write.pointShape > ### Keywords: spatial > > ### ** Examples > > balt_orig <- read.shape(system.file("shapes/baltim.shp", package="maptools")[1]) Shapefile Type: Point # of Shapes: 211 > plot(balt_orig) > balt_df <- balt_orig$att.data > balt_cheap <- balt_df[balt_df$PRICE < 40,] > file <- tempfile("") > write.pointShape(balt_cheap, file, coordinates=cbind(balt_cheap$X, balt_cheap$Y)) > getinfo.shape(paste(file, ".shp", sep="")) Shapefile Type: Point # of Shapes: 103 > balt_new <- read.shape(paste(file, ".shp", sep="")) Shapefile Type: Point # of Shapes: 103 > plot(balt_new, fg="red", glyph=1, add=TRUE) > > > > cleanEx(); ..nameEx <- "write.polylistShape" > > ### * write.polylistShape > > flush(stderr()); flush(stdout()) > > ### Name: write.polylistShape > ### Title: Write a polygon-type shapefile > ### Aliases: write.polylistShape > ### Keywords: spatial > > ### ** Examples > > col_orig <- read.shape(system.file("shapes/columbus.shp", package="maptools")[1]) Shapefile Type: Polygon # of Shapes: 49 > mappolys <- Map2poly(col_orig, as.character(col_orig$att.data$NEIGNO)) > plot(mappolys) > col_df <- col_orig$att.data > col_cheap <- subset(mappolys, col_df$HOVAL < 34) > col_df_cheap <- subset(col_df, col_df$HOVAL < 34) > file <- tempfile("") > write.polylistShape(col_cheap, col_df_cheap, file) > getinfo.shape(paste(file, ".shp", sep="")) Shapefile Type: Polygon # of Shapes: 25 > col_new <- read.shape(paste(file, ".shp", sep="")) Shapefile Type: Polygon # of Shapes: 25 > mappolys <- Map2poly(col_new, as.character(col_new$att.data$NEIGNO)) > plot(mappolys, border="red", add=TRUE) > > > > ### *