[R-sig-Geo] Polygon edge smoothing

Sébastien Durand v8extra at gmail.com
Mon Dec 6 02:55:22 CET 2010


Here is what I found as a temporary solution:

Here is my example: 


library(akima)
library(maptools)
library(zoo)
gpclibPermit()
library(raster)
data(akima)

# define de dimension of grid
dimgrid <- 50

# Interpolate to regular grid
ak.li <- interp(akima$x, akima$y, akima$z, xo=seq(min(akima$x),
max(akima$x), length = dimgrid),yo=seq(min(akima$y), max(akima$y),
length = dimgrid),linear = TRUE, extrap=FALSE, duplicate = "mean")

# Show interpolation
image(ak.li)
points(akima)
with(akima, text(x, y, formatC(z,dig=2), adj = -0.1))

r <- raster(ak.li)
plot(r)

pol <- rasterToPolygons(r, fun=function(x){x>20 & x<25})

# Show the polygons
plot(pol, add=T, col='red')

### new code
digits <- 6
for (i in 1:length(pol at polygons)) {
	for (j in 1:length(pol at polygons[[i]]@Polygons)) {
			pol at polygons[[i]]@Polygons[[j]]@coords <-
			round(pol at polygons[[i]]@Polygons[[j]]@coords, digits)
	}
}


# Attempt to merge similar adjacent into one polygon
union = unionSpatialPolygons(pol, ID=rep(1, times=length(pol at polygons)))

# Show
plot(union, add=TRUE, col="white")

# Nombre de vertices
crds=union at polygons[[1]]@Polygons[[1]]@coords
tmp=length(crds[,1])

nexCoordX= rollmean(c(crds[tmp,1], crds[,1], crds[1,1]), k=2)
nexCoordY= rollmean(c(crds[tmp,2], crds[,2], crds[1,2]), k=2)
lines(nexCoordX, nexCoordY, col="yellow", lwd=2)


If you have any knowledge of a function that would do something similar, I would be happy to ear about it!

Thanks a lot!

Sébastien


More information about the R-sig-Geo mailing list