| 1 |
# Copyright (c) 2006-7 Patrick Hausmann and Roger Bivand |
| 2 |
|
| 3 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 4 |
sp2Mondrian <- function(SP, file, new_format=TRUE) { |
| 5 |
if (!inherits(SP, "SpatialPolygonsDataFrame")) |
| 6 |
stop("not a SpatialPolygonsDataFrame object") |
| 7 |
pls <- slot(SP, "polygons") |
| 8 |
n <- length(pls) |
| 9 |
id <- 1:n |
| 10 |
df <- as(SP, "data.frame") |
| 11 |
df <- data.frame("/P_SP_ID"=id, df, check.names=FALSE) |
| 12 |
|
| 13 |
if (!new_format) { |
| 14 |
con <- file(file, open="wt") |
| 15 |
write.table(df, file = con, |
| 16 |
quote = FALSE, |
| 17 |
row.names = FALSE, |
| 18 |
col.names = TRUE, sep="\t", dec=".") |
| 19 |
IDs <- sapply(pls, function(x) slot(x, "ID")) |
| 20 |
for (i in 1:n) { |
| 21 |
pl <- slot(pls[[i]], "Polygons") |
| 22 |
m <- length(pl) |
| 23 |
for (j in 1:m) { |
| 24 |
crds <- slot(pl[[j]], "coords") |
| 25 |
nc <- nrow(crds) |
| 26 |
lab <- paste(id[i], paste("/P", IDs[i], sep=""), |
| 27 |
nc, sep="\t") |
| 28 |
cat("\n", file = con) |
| 29 |
writeLines(lab, con = con) |
| 30 |
write.table(crds, file = con, |
| 31 |
append = TRUE, |
| 32 |
row.names = FALSE, |
| 33 |
col.names = FALSE, sep="\t", dec=".") |
| 34 |
} |
| 35 |
|
| 36 |
} |
| 37 |
close(con) |
| 38 |
} else { |
| 39 |
bnm <- basename(file) |
| 40 |
dnm <- dirname(file) |
| 41 |
MAP_file <- paste(dnm, paste("MAP_", bnm, sep=""), |
| 42 |
sep=.Platform$file.sep) |
| 43 |
|
| 44 |
con <- file(file, open="wt") |
| 45 |
write.table(df, file = con, |
| 46 |
quote = FALSE, |
| 47 |
row.names = FALSE, |
| 48 |
col.names = TRUE, sep="\t", dec=".") |
| 49 |
|
| 50 |
cat("\n", file = con) |
| 51 |
writeLines(MAP_file, con, sep="") |
| 52 |
|
| 53 |
close(con) |
| 54 |
con <- file(MAP_file, open="wt") |
| 55 |
for (i in 1:n) { |
| 56 |
pl <- slot(pls[[i]], "Polygons") |
| 57 |
m <- length(pl) |
| 58 |
for (j in 1:m) { |
| 59 |
crds <- slot(pl[[j]], "coords") |
| 60 |
nc <- nrow(crds) |
| 61 |
lab <- paste(id[i], "/P_SP_ID", nc, sep="\t") |
| 62 |
if (i > 1 || j > 1) cat("\n", file = con) |
| 63 |
writeLines(lab, con = con) |
| 64 |
write.table(crds, file = con, |
| 65 |
append = TRUE, |
| 66 |
row.names = FALSE, |
| 67 |
col.names = FALSE, sep="\t", dec=".") |
| 68 |
} |
| 69 |
|
| 70 |
} |
| 71 |
close(con) |
| 72 |
} |
| 73 |
invisible(NULL) |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
Shape2Mondrian <- function(shape, file, id, export.data = TRUE) { |
| 78 |
|
| 79 |
fshape <- shape |
| 80 |
xfile <- file |
| 81 |
idx <- paste("/P", id, sep="") |
| 82 |
|
| 83 |
# |
| 84 |
# Export "att.data" -- TRUE / FALSE |
| 85 |
# |
| 86 |
if (is.logical(export.data) && export.data) { |
| 87 |
y <- fshape$att.data |
| 88 |
names(y) <- sub(id, idx, names(y)) |
| 89 |
write.table(y, file = xfile, |
| 90 |
append = TRUE, |
| 91 |
row.names = FALSE, |
| 92 |
col.names = TRUE, sep="\t", dec=".") |
| 93 |
} |
| 94 |
|
| 95 |
for (i in 1:length(fshape$Shapes)) { |
| 96 |
|
| 97 |
xnrow <- nrow(fshape$Shapes[[i]]$verts) |
| 98 |
lab <- paste(i, idx, xnrow, sep="\t") |
| 99 |
|
| 100 |
write.table("\n", file = xfile, |
| 101 |
append = TRUE, eol = "", |
| 102 |
quote = FALSE, |
| 103 |
row.names = FALSE, |
| 104 |
col.names = FALSE) |
| 105 |
write.table(lab, file = xfile, |
| 106 |
append = TRUE, |
| 107 |
quote = FALSE, |
| 108 |
row.names = FALSE, |
| 109 |
col.names = FALSE, sep="\t") |
| 110 |
write.table(fshape$Shapes[[i]]$verts, file = xfile, |
| 111 |
append = TRUE, |
| 112 |
row.names = FALSE, |
| 113 |
col.names = FALSE, sep="\t", dec=".") |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 118 |
|
| 119 |
#library(maptools) |
| 120 |
#x <- read.shape(system.file("shapes/columbus.shp", package="maptools")[1]) |
| 121 |
#Shape2Mondrian(x, file="c:\\colombus.txt", "POLYID", export.data = TRUE) |
| 122 |
#xx <- readShapePoly(system.file("shapes/columbus.shp", package="maptools")[1]) |
| 123 |
#sp2Mondrian(xx, file="colombus1.txt") |
| 124 |
|