【问题标题】:R: Add non-pch symbols to a map (polygon layer)R:将非 pch 符号添加到地图(多边形图层)
【发布时间】:2017-08-26 19:36:00
【问题描述】:

我在这里使用 R 作为 GIS 来创建地图。这是一个单色(黑色/白色)的场地平面图。但是,我想使用these open triangular symbols for coniferous forests 将森林与其他地区区分开来,这在德语世界的制图学中很常见。

经过一番研究,我找到了 TeachingDemos 包中的 my.symbols 函数。我知道可以编写一个函数,在xlim = c(-1,1)ylim = c(-1,1) 绘图中绘制所需的符号,然后使用TeachingDemos::my.symbols 将此符号与points 函数一起添加到绘图中。我的计划是给TeachingDemos::my.symbols 森林区域内的网格坐标。

我设法编写了一个绘制符号的函数:

nw <- function(){
  par(oma = c(0,0,0,0), mar = c(0,0,0,0))
  plot(c(-0.84, 0), c(-0.4, 0.67), xlim = c(-1,1), ylim = c(-1,1), type = "l", lwd = 2)
  segments(0, 0.67, 0.84, -0.4, lwd = 4)
}

但我没有设法以正确的方式将函数传递给my.symbols。我也没有设法仅在森林多边形中扩展网格,因此将其扩展到多边形层的整个边界框 (bbox) 并仅选择位于森林中的点。比如:

library(maptools)
nc1 <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1],
                     proj4string=CRS("+proj=longlat +datum=NAD27"))
cd <- c(100, 50)
grd <- GridTopology(cellcentre.offset = nc1@bbox[, 1],
                    cellsize = rep(diff(as.numeric(nc1@bbox["x",]))/100, 2),
                    cells.dim = cd)
grd.sp <- SpatialPixelsDataFrame(grd,
                               data = data.frame(id = 1:prod(cd)),
                               proj4string = CRS(proj4string(nc1)))
x11(10, 6)
plot(nc1)
points(coordinates(grd.sp[nc1[which(nc1$AREA > 0.15),],]))

【问题讨论】:

    标签: r plot maps gis geospatial


    【解决方案1】:

    在分隔的多边形中绘制特定图像

    您可以使用库sp 对特定多边形中的点进行采样。在我的例子中,我根据NAME选择了一个特定的多边形。
    然后你可以使用库emojifont 添加一些来自表情符号或字体的符号。例如,您可以在列表中找到树,例如 evergreen_tree。或者您可以重复绘制自己的 png 图像。

    在定界多边形中创建规则的点网格

    所以,首先为特定多边形创建网格:

    library(maptools)
    library(rgdal)
    library(sp)
    library(emojifont)
    
    nc1 <- rgdal::readOGR(system.file("shapes/sids.shp", package = "maptools")[1])
    
    # Do a point grid in a specific polygon
    grid.pt <- sp::spsample(nc1[which(nc1$NAME == "Alamance"),], n = 20, type = "regular")
    

    将表情符号绘制为重复的图像模式

    然后使用grid.pt 将表情符号绘制为文本:

    # plot the entire map with restricted limits
    plot(nc1, xlim = c(-79.6, -79.1), ylim = c(35.8, 36.3))
    # Highlight the selected polygon
    plot(nc1[which(nc1$NAME == "Alamance"),], border = "red", lwd = 3, add = TRUE)
    # Normal points
    points(grid.pt, pch = 20, col = "blue", cex = 0.1)
    # Add emojifont instead of points
    text(coordinates(grid.pt)[, 1], coordinates(grid.pt)[, 2],
      labels = emoji('evergreen_tree'), cex = 1.5,
      col = 'forestgreen', family = 'EmojiOne')
    

    将您自己的图像绘制为重复模式

    您也可以使用自己的 png 图像作为点。在此响应中,将图像下载到网络上作为可重现的示例:https://gis.stackexchange.com/questions/118286/is-it-possible-to-create-a-scatterplot-with-icons-svg-or-png-instead-of-points
    但是,您可以创建和使用自己的 png 图像。

    # Plot with your own image
    library(png)
    # Image downloaded as an example for reproducibility
    iconfile1 <- download.file('http://icons.iconarchive.com/icons/oxygen-icons.org/oxygen/256/Status-weather-clouds-icon.png',
                               destfile = 'icon1.png', mode = 'wb')
    icon1 <- png::readPNG('icon1.png')
    
    # Depending on the desired size of the image
    offset <- 0.02
    
    # plot the entire map with restricted limits
    plot(nc1, xlim = c(-79.8, -78.9), ylim = c(35.6, 36.5))
    # Plot all images at points locations
    for (i in 1:length(grid.pt)) {
      graphics::rasterImage(
        icon1, 
        coordinates(grid.pt)[i, 1] - offset,
        coordinates(grid.pt)[i, 2] - offset,
        coordinates(grid.pt)[i, 1] + offset,
        coordinates(grid.pt)[i, 2] + offset
      )
    }
    

    【讨论】:

    • 关于第二种解决方案,您仍然可以明确提及您的答案已经暗示的内容:可以创建自己的符号(正如我的问题中非常简单的符号),将它们导出为 png,然后使用它们.这种使用临时文件的解决方法不像普通代码那样优雅,但它已经很好了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-11
    • 2020-02-17
    • 1970-01-01
    • 1970-01-01
    • 2013-05-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多