|
Generation of Neighbourhood Files for Dorling Cartograms
and Generalization Filtering
1. Using R
The following R program calculates neighbourhoods starting with
a ArcInfo shapefile. There are some R packages required. Here the
code:
#################################################################
# INPUT
# shapefileName: the name of the shapefile without ".shp"
# shapefileDirectory: the name of the directory of the shapefile
# ids: the ID variable in the shapefile defining the sorting order
# of the boundary file and the data file (case sensitive!)
# OUTPUT
# The resulting neighbour file has the name shapefileName + ".fil"
shapefileName = "Polygone_SH"
shapefileDirectory = "."
idName = "BFS2009"
#################################################################
# library(ctv)
# install.views("Spatial")
library(spdep)
library(rgdal)
sh <- readOGR(".", shapefileName)
ids = sh[[idName]]
nb <- poly2nb(sh)
plot(sh, border="grey")
plot(nb, coordinates(sh), add=TRUE)
title(main=paste("Nachbarn im Shapefile",shapefileName, sep="\n"))
cards <- card(nb)
maxconts <- which(cards == max(cards))
if(length(maxconts) > 1) maxconts <- maxconts[1]
fg <- rep("grey", length(cards))
fg[maxconts] <- "red"
fg[nb[[maxconts]]] <- "green"
plot(sh, col=fg)
title(main="Region with largest number of contiguities")
plot(sh, border="grey")
plot(nb, coordinates(sh), add=TRUE)
title(main=paste("Neighbours in Shapefile",shapefileName, sep="\n"))
dsts <- nbdists(nb, coordinates(sh))
idw <- lapply(dsts, function(x) 1/(x / 10))
nbWeight <- function(nb, style, showNr) {
nbw <- nb2listw(nb, glist=idw, style=style, zero.policy=T)
cat(style, summary(unlist(nbw$weights)), "\n")
cat(style, summary(sapply(nbw$weights, sum)), " (Sum)\n")
if (showNr >= 0) cat(style, unlist(nbw$weights[showNr]), " - area", showNr, "\n" )
nbw
}
nbw <- nbWeight(nb, "W", 24)
nbw <- nbWeight(nb, "B", 24)
nbw <- nbWeight(nb, "C", 24)
nbw <- nbWeight(nb, "U", 24)
nbw <- nbWeight(nb, "S", 24)
expandNBleft <- function(x) {
rrr <- vector(mode="integer", length(unlist(nbw$neighbours)))
k = 0
for(i in 1:length(x)) {
for(j in 1:length(x[i][[1]])) {
k = k + 1
rrr[k] = i
}
}
rrr
}
w1 <- sort(ids)
w0 <- 1:length(w1)
w <- as.data.frame(cbind(w0,w1))
v1 <- expandNBleft(nbw$neighbours)
v2 <- unlist(nbw$neighbours)
v3 <- 100* unlist(nbw$weights)
nb.df <- as.data.frame(cbind(v1,v2,v3))
rr <- merge(nb.df, w, by.x = "v1", by.y = "w0")
rr <- merge(rr, w, by.x = "v2", by.y = "w0")
res <- as.data.frame(cbind(rr$w1.x, rr$w1.y,rr$v3))
summary(res)
options(scipen=12)
filFilename = paste(shapefileName, ".fil", sep="")
write.table(res, file=filFilename, col.names=F, row.names=F)
cat ("Resulting neighbour file:", filFilename, "\n")
#################################################################
NB: For the .wgt file: use e.g. population data.
2. Using ArcGIS and Excel
(mainly for internal documentation)
Step 1: ArcInfo-AML (ArcInfo required)
shp2fil.aml
Input: ArcInfo-Shapefile with an ID-Item; output: 3 text files
for the subsequent processing step.
Step 2: Excel-Macro (MS Excel required)
shp2fil.xls
The content of the 4 files from step 1 were pasted into the three
sheets "aat", "pat", "reg" and "nam".
The macro creates the .fil data in the sheet "fil".
|
 |
 |
 |
|