Exercise in grImport

红太狼 2022-09-25 08:22 197阅读 0赞

Last week I used grImport for the first time. I decided to try perform another exercise using it. The task was to add voivodeship division of Poland.

Standard R maps do not contain such a division. I have found it on r-forge in package mapoland based on ESRI shape files, but I wanted to import such a map from SVG file which can be found onWikipedia. As last time SVG file has to be converted do PS first. Here is a comparison of both maps showing that the import worked quite well.

woj.png_zoom_2_resize_320_2C304

And this is the code I have used:

library (grImport )

PostScriptTrace ( “Wojewodztwa.ps” )

voiv <- readPicture ( “Wojewodztwa.ps.xml” )

broken.voiv <- explodePaths (voiv )

#extracting voivodeship ‘PictureStrokes’

xpath <- ypath <- list ()

sel <- c ( 2, 4, 6, 8, 20, 22, 24, 28, 30, 32, 34, 36, 38, 41, 44, 46 )

for (i in seq (along = sel )) {

  1. xpath **\[\[**i **\]\] <-** broken.voiv **\[\[**sel **\[**i **\]\]\]**@paths **$**path@x
  2. ypath **\[\[**i **\]\] <-** broken.voiv **\[\[**sel **\[**i **\]\]\]**@paths **$**path@y

}

#adding Wolin island to zachodniopomorskie

xpath [[ 2 ]] <- c (broken.voiv [ 10 ]@paths $path@x [ c ( 39 : 1, 95 : 40 )],

  1. xpath **\[\[** 2 **\]\])**

ypath [[ 2 ]] <- c (broken.voiv [ 10 ]@paths $path@y [ c ( 39 : 1, 95 : 40 )],

  1. ypath **\[\[** 2 **\]\])**

library (mapoland )

pl <- getShape ( “voiv” )

#functions rescaling paths to mapoland map size

transx <- function(x ) {

  1. old **<-** c **(** min **(** sapply **(**xpath, min **))**, max **(** sapply **(**xpath, max **)))**
  2. new **<-** pl@bbox **\[** 1, **\]**
  3. **((**x **–** old **\[** 1 **\]) / (**old **\[** 2 **\] **old **\[** 1 **\])) \* (**new **\[** 2 **\] –**new **\[** 1 **\])**

+ new [ 1 ]

}

transy <- function(y ) {

  1. old **<-** c **(** min **(** sapply **(**ypath, min **))**, max **(** sapply **(**ypath, max **)))**
  2. new **<-** pl@bbox **\[** 2, **\]**
  3. **((**y **–** old **\[** 1 **\]) / (**old **\[** 2 **\] **old **\[** 1 **\])) \* (**new **\[** 2 **\] –**new **\[** 1 **\])**

+ new [ 1 ]

}

plot (pl, lwd = 4 )

for (i in seq (along = sel )) {

  1. lines **(**transx **(**xpath **\[\[**i **\]\])**, transy **(**ypath **\[\[**i **\]\])**, col **=** red **)**

}

发表评论

表情:
评论列表 (有 0 条评论,197人围观)

还没有评论,来说两句吧...

相关阅读