【问题标题】:Rasterizing coordinates for an irregular polygon changes original shape不规则多边形的栅格化坐标会改变原始形状
【发布时间】:2019-12-09 20:50:05
【问题描述】:

我正在尝试平滑 R 中不规则多边形的边缘,即将其尖角变成圆形边缘。我正在尝试使用smoothr::smooth 来执行此操作,但是此函数对来自包sfsp 的对象进行操作,而我所拥有的只是一组坐标。不知何故,将我的data.frame 变成SpatialPolygonsDataFrame 对象(来自包sp 的对象类)的结果是一个矩形,其极限是原始多边形的极限。有谁知道如何在保持原始多边形形状的同时将我的坐标集转换为与smoothr::smooth 兼容的类的对象?这是我所做的,部分遵循this 页面上的说明:

rm(list=ls()) # my compulsive habit of making sure R's memory is a clean slate

# Example dataset:
dd <- data.frame(
        Lon = c(18.95379, 18.82409, 18.58987, 18.80541, 18.92427, 19.00264),
        Lat = c(-32.42492, -32.32498, -31.89642, -31.73606, -32.16217, -32.37052)
        )

plot(0,0,
    xlim=c(18.5,19.1), ylim=c(-32.5,-31.6), 
    xlab="Longitude", ylab="Latitude"
    )
polygon(dd[,"Lon"],dd[,"Lat"], border="red")

# To make it smooth I plan on using
library(smoothr)
# But smoothr:: smooth works on objects from packages sf or sp so I need to  convert dd.
#convert to spatial points
library(sp)
coordinates(dd) = ~Lon + Lat
# convert to raster
library(raster)
rr <- raster::raster(dd)
#convert raster to polygons
sp = rasterToPolygons(rr, dissolve = T)
map(sp, add=T, col="green", fill=F)
# somehow my irregular polygon turned into a rectangle.
sps <- smooth(sp, method = "ksmooth", smoothness=5)
# this works, but of course is only rounding the corners of sp
map(sps, add=T, col="blue", fill=F)

红色是data.frame dd的原始多边形,绿色是对象sp,蓝色是spsps的平滑版本。函数smooth 完成了这项工作,问题在于将dd 转换为sp 兼容对象。我怀疑问题是由raster() 引起的,但我不确定为什么或如何解决它。 非常感谢。

【问题讨论】:

    标签: r gis r-raster smoothing rasterize


    【解决方案1】:

    这里我使用了sf,因为我个人觉得这更容易:

    library(sf)
    library(smoothr)
    
    # Example dataset:
    dd <- data.frame(
      Lon = c(18.95379, 18.82409, 18.58987, 18.80541, 18.92427, 19.00264),
      Lat = c(-32.42492, -32.32498, -31.89642, -31.73606, -32.16217, -32.37052)
    )
    
    # cast to polygon, use multipoint first though.   
    polygon <- as.matrix(dd) %>%
      sf::st_multipoint() %>%
      sf::st_cast("POLYGON")
    
    #  smooth polygon
    polygon_smoothed <- smoothr::smooth(polygon, method = "ksmooth", smoothness = 0.5)
    
    # plot to check
    plot(polygon, col = "red")
    plot(polygon_smoothed, col = "blue", add = T)
    

    【讨论】:

      【解决方案2】:

      我在这里找到了另一个解决方案:https://rstudio-pubs-static.s3.amazonaws.com/202536_7a122ff56e9f4062b6b012d9921afd80.html

      # Example dataset:
      dd <- data.frame(
              Lon = c(18.95379, 18.82409, 18.58987, 18.80541, 18.92427, 19.00264),
              Lat = c(-32.42492, -32.32498, -31.89642, -31.73606, -32.16217, -32.37052)
              )       
      plot(0,0,
          xlim=c(18.5,19.1), ylim=c(-32.5,-31.6), 
          xlab="Longitude", ylab="Latitude"
          )
      polygon(dd[,"Lon"],dd[,"Lat"], border="red")
      
      library(sp)
      p = Polygon(dd)
      p2 = Polygons(list(p),1) # I believe this aggregates polygons, so in this case it doesn't do anything.
      sp = SpatialPolygons(list(p2))
      sps <- smooth(sp, method = "ksmooth", smoothness=0.7)
      plot(sps, add=T, border="blue")
      

      【讨论】:

        猜你喜欢
        • 2011-12-10
        • 1970-01-01
        • 2020-11-01
        • 1970-01-01
        • 2021-02-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多