vivirbr
vivirbr

Reputation: 133

How to create polygons based on lines delimitation like Feature to Polygon

is there a way to create polygons based on line delimitation? For example, if I merge a lot of different shapefiles I will see new "polygons" created from the delimitation of their lines, but I am interested in create a ID for those new polygons. The same functionality as Feature to Polygon in ArcGIS.

enter image description here

Upvotes: 0

Views: 191

Answers (1)

James Thomas Durant
James Thomas Durant

Reputation: 305

Try this:

library(sp)
library(rgdal)
library(rgeos)
library(raster)
library(tmap)

id1 <- readOGR(dsn = "./ID1.kml", "ID1.kml")
id2 <- readOGR(dsn = "./ID2.kml", "ID2.kml")
id3 <- readOGR(dsn = "./ID3.kml", "ID3.kml")

x <- rbind(id1, id2, id3, makeUniqueIDs = TRUE)

plot(x)

id4 <- crop(id1, id2)
id4@data$Name <- "iD4"
id5 <- crop(id1, id3)
id5@data$Name <- "iD5"

id6 <- gDifference(id1, id2)
id6 <- SpatialPolygonsDataFrame(id6, data.frame(Name = "ID6", Description = ""))

id7 <- gDifference(id1, id3)
id7 <- SpatialPolygonsDataFrame(id7, data.frame(Name = "ID7", Description = ""))

x1 <- rbind(id1, id2, id3, id4, id5, id6, id7,  makeUniqueIDs = TRUE)

tm_shape(x1) + tm_polygons(col = "Name")

enter image description here

Upvotes: 2

Related Questions