R & Shapefile, a short script

„shape2pdf“ … mein Beitrag zum R-Shapefile-Contest
See also: http://www.geoobserver.de/shape2pdf/shape2pdf-readme.pdf
https://web.archive.org/web/20160910135536/http://www.geoobserver.de:80/shape2pdf/shape2pdf_readme.pdf

Maybe the shortest script in this contest. Short but effective. It is helpful for simply getting annoying tasks done. With just a few lines of code! It generates n thematic maps from n data columns from a shapefile into a PDF. Columns with not available values (NA) are sorted out. Code and data have to be in the same folder (here in „.“). The output-PDF is also generated in this folder. The example-data are from the OpenData-Server from the city Halle (http://www.daten.halle.de/). They were adjusted with QGIS. All columns with „*_t“ contain values as strings. Columns with „*_n“ contain numerical values. This points out differences in classifications.
The code should be adjusted and optimized on demand, e. g. by classifications. Code and data can be found at http://www.geoobserver.de/shape2pdf/shape2pdf.rar. Have fun testing it.

Vielleicht das kürzeste R-Skript im Contest? Kurz aber wirkungsvoll. Es hilft, “lästige“, wiederkehrende Aufgaben einfach und schnell zu erledigen.Uns das bei minimalem Code! Hier wird die Generierung von n thematischen Karten aus den n Sachdatenspalten eines Shapefiles in ein PDF unterstützt. Spalten mit unerlaubten Werten („NA“) werden vorher aussortiert. Das Skript und die Daten (Shapefile) stehen im gleichen Verzeichnis (hier „.“), das PDF wird auch dort generiert.
Die Daten stammen aus den Offenen Daten der Stadt Halle (http://www.daten.halle.de/) und wurden mit QGIS bearbeitet. Alle Spalten mit „*_t“ enthalten die Werte als Strings, „*_n“ als numerische Werte, um die Unterschiede bei der Klassifizierung zu zeigen.
Das Skript sollte bei Bedarf noch angepasst und optimiert werden, z. B. bzgl. der Klassifizierung.
Skript und Daten stehen unter http://www.geoobserver.de/shape2pdf/shape2pdf.rar zur Verfügung, viel Spaß beim testen.

# ----------------------------------------------
# shape2pdf.r           (last edit: 27.07.2016)
# by mike elstermann @geoobserver_ alias geoObserver
# ----------------------------------------------

setwd(".")
require(maptools)
library(rworldmap)

pdf("_my_shapefile_output.pdf", width=8.26,height=11.69) # open the output PDF DIN-A4 H
inFile <- 'halle_flat_2013.shp'          # open the shapefile

sPDF <- readShapePoly(inFile)
df <- sPDF@data                          # via reading structur (str)
columns_all <- names(df)
# or define the Shape-Columns: columns <- c("SHAPE_AREA", "sum_n") via user-definition

df <- df[,colSums(is.na(df)) < nrow(df)] # remove columns with only "NA"
columns_not_na <- names(df)

for (column in columns_not_na) {
      cat("\n##### creating MAP with column:", column, ". . .")
      par(pin=c(7,10))
      plot(sPDF,axes=TRUE)
      mapParams <- mapCountryData( sPDF
                                   , nameColumnToPlot=column
                                   , mapTitle=paste("column: ",column)
                                   , addLegend=FALSE
                                   , catMethod="quantiles" # "categorical" #"quantiles"
                                   , add=TRUE
                                   )
      do.call( addMapLegendBoxes, c( mapParams, title=paste("column: ",column)
                                     #, legendLabels="all"
                                     #, legendWidth=1.0
                                     #, labelFontSize=0.5
                                     #, legendMar=6.5
                                     #, horizontal=TRUE
                                     #, tcl=-.5
      )) 
      grid()
}
dev.off() # close the output PDF
rm(df, columns_all, columns_not_na, inFile, mapParams, sPDF, column) #remove all vars ()

4 Gedanken zu „R & Shapefile, a short script

  1. Pingback: Results from the R Shapefile Contest! - AriLamstein.comAriLamstein.com

  2. Pingback: Results: R Shapefile Contest | Open Data Science

  3. Pingback: Buchtipp: „Spatial Statistics for Data Science: Theory and Practice with R“ | #geoObserver

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert