3

With R I am looking to smooth my Polygon: inter2.csv

code:

inter1 <- read.table("c:/inter2.csv", header=TRUE)

# add a category (required for later rasterizing/polygonizing)
inter1 <- cbind(inter1, cat = rep(1L, nrow(inter1)), stringsAsFactors = FALSE)

# convert to spatial points
coordinates(inter1) <- ~long + lat

# gridify your set of points
gridded(inter1) <- TRUE

# convert to raster
r <- raster(inter1)

# convert raster to polygons
sp <- rasterToPolygons(r, dissolve = T)
plot(sp)
# addition transformation to distinguish well the set of polygons
polys <- slot(sp@polygons[[1]], "Polygons")

require(Hmisc) # for Bezier curve
output <- SpatialPolygons(
  Srl = lapply(1:length(polys),
               function(x){
                 p <- polys[[x]]

                 #applying bezier curve for smoothing polygon edges
                 px <- slot(polys[[x]], "coords")[,1]
                 py <- slot(polys[[x]], "coords")[,2]
                 bz <- bezier(px, py)
                 slot(p, "coords") <- as.matrix(cbind(bz$x,bz$y))                

                 # create Polygons object
                 poly <- Polygons(list(p), ID = x)
                 return(poly)
               }),
  proj4string = CRS("+init=epsg:4326")
)

# plot
plot(sp, border = "gray", lwd = 2) # polygonize result
plot(output, border = "red",  add = TRUE) # smoothed polygons

I get this result:

plot

It's some days I look for the problem without success, can someone help me please?

  • 2
    Have you looked at this post on smoothing polygons?: gis.stackexchange.com/a/24929/8104 – Aaron Oct 11 '14 at 20:29
  • yes, but I don't understand how to apply it on my example is that you can help me? – zina_GIS Oct 11 '14 at 20:39
  • 1
    i see that you have been using the code i've suggested here stackoverflow.com/questions/26087772/… indeed the problem was also reported there, and deals with bezier smoothing. i will tackle the problem and suggest some fix ASAP. – eblondel Oct 14 '14 at 19:56
2

I've updated the original code given here https://stackoverflow.com/questions/26087772/create-polygon-from-set-of-points-distributed/26089377#26089377

Instead of Bezier-based smoothing, you can used the smoothing suggested here https://gis.stackexchange.com/a/24929/8104 with the function spline.poly (as suggested by @aaron) which gives accurate results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.