2017-04-11 58 views
0

我正在将许多栅格转换为多边形。但在很多情况下,我看到了意想不到的子几何,我似乎无法摆脱它们。将栅格转换为多边形时不需要的子几何图形

这是与R v3.3.3和光栅包v2.5-8。

这是一个应该重现我遇到的问题的例子。您可以下载我使用的光栅here

# first, read in raster and coarsen to something more manageable 

library(raster) 
library(rgeos) 
env <- raster('adefi.tif') 
env2 <-aggregate(env, 8) 

# Reclassify such that cells are either 1 or NA 
env2[!is.na(env2)] <- 1 

# this is what the raster now looks like: 
plot(env2) 

enter image description here

# Now I convert to polygon, choosing to dissolve 
p <- rasterToPolygons(env2, dissolve=T) 

plot(p) 

enter image description here

# I find that I can't get rid of these subgeometries 
p <- gUnaryUnion(p) # identical result 
gIsValid(p) # returns TRUE 

我不知道问题出在哪里?它是如何光栅包转换成小区多边形?或者是rgeos包如何将这些细胞多边形溶解在一起? 是否有解决方法?

回答

0

它看起来像一个投影问题。这个工作对我来说:

library(raster) 
library(rgeos) 

env <- raster(file.path(fp, "adefi.tif")) 
env2 <- aggregate(env, 8) 
env2[is.na(env2) == F] <- 1 
# Project Raster 
proj_env2 <- projectRaster(env2, crs = CRS("+init=epsg:3577")) 
p <- rasterToPolygons(proj_env2, dissolve = T) 
plot(p) 

不知道为什么,因为EPSG需要重投影:3577看起来是一样的原始投影,但我通常使用proj4string()或spTransform(),以确保一切确认投影将排队。