Better hillshading for map plots in R

后端 未结 1 1087
抹茶落季
抹茶落季 2021-02-03 13:38

I\'m working on improved hillshading for some topographical map plots. The basic hillshade workflow documented in image() is:

require(raster)
alt =         


        
1条回答
  •  [愿得一人]
    2021-02-03 14:05

    No experience with this, but why not give the gray undermap an alpha value that depends on slopes? Here's my try:

    # before
    require(raster)
    alt = getData('alt', country='CHE')
    slope = terrain(alt, opt='slope')
    aspect = terrain(alt, opt='aspect')
    hill = hillShade(slope, aspect, 40, 270)
    plot(hill, col=grey(0:100/100), legend=FALSE, main='Switzerland')
    plot(alt, col=rainbow(25, alpha=0.35), add=TRUE)
    

    enter image description here

    As you say, very dark.

    # after
    grayalphas <- seq(-1,1,by=.01)^2*100
    grayalphas[grayalphas==100] <- 99
    plot(hill, col=paste0(grey(0:100/100),sprintf("%.2d",grayalphas)), legend=FALSE, main='Switzerland')
    
    plot(alt, col=rainbow(25, alpha=0.35), add=TRUE)
    

    enter image description here

    I set the gray alphas to have a parabolic shape, with minimum where the gray value is .5 and max of 99 at gray values of 0 or 1. If you choose something like this, you'll want to tinker with levels, etc, but it is easy to implement. Plus you'll want to put more effort than I did into the alphas, as mine are strictly numeric and not hex.

    [Edit] I found a nifty function for adding alphas, addTrans() here in Sacha Epskamp's answer. This keeps the parabola, but it ranges from 0 in the middle to 255 on the extremes.

    grayalphas <- seq(-1,1,length=101)^2*255
    plot(hill, col=addTrans(grey(0:100/100),grayalphas), legend=FALSE, main='Switzerland')
    plot(alt, col=rainbow(25, alpha=0.35), add=TRUE)
    

    enter image description here

    0 讨论(0)
提交回复
热议问题