Skip to content

Polygon edge smoothing

3 messages · Sébastien Durand, Pierre Roudier, Alexandre Villers

#
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
#
Hi S?bastien,

Maybe you should have a look to the rgeos package. It offers bindings
for the GEOS C++ lib. Note that unlike the gpclib, it is really
open-source (beware of the gpclib licence terms - they are very
restricted). However, it is not (AFAIK) on CRAN yet. You have to
install it from r-forge:

install.packages("rgeos", repos="http://R-Forge.R-project.org")

The function you want is probably gSimplify(). I never used that
functionality so I unfortunately can't help you more on that!

HTH,

Pierre

2010/12/6 S?bastien Durand <v8extra at gmail.com>:
#
Le 06/12/2010 03:55, S?bastien Durand a ?crit :
Hey,

Hey,

You can also have a look at the spatstat package and the simplify.owin() 
function. Maptools package provides function to convert objects between 
sp and spatstat formats.
HTH

Alex