Skip to content

Instantly share code, notes, and snippets.

@KarthiAru
Last active December 19, 2016 12:41
Show Gist options
  • Save KarthiAru/73148ae994697249b41b1cedac2e090f to your computer and use it in GitHub Desktop.
Save KarthiAru/73148ae994697249b41b1cedac2e090f to your computer and use it in GitHub Desktop.
# Get the layers from .kml file
layers <- ogrListLayers('bbmp.kml')[2:198]
i<-1
# Get polygon from first layer
poly.data <- readOGR(dsn="bbmp.kml", layer=layers[1], encoding="utf-8", verbose = F)
n <- length(slot(poly.data, "polygons"))
poly.data <- spChFIDs(poly.data, as.character(i:(i+n-1)))
i <- i + n
# Combine remaining polygons with first polygon
timer_all <- Sys.time()
pb <- txtProgressBar(max=length(layers), style = 3)
options(warn=-1) # disable warnings
for (i in 2:length(layers)) {
temp.data <- readOGR(dsn="bbmp.kml", layer=layers[i], encoding="utf-8", verbose = F)
n <- length(slot(temp.data, "polygons"))
temp.data <- spChFIDs(temp.data, as.character(i:(i+n-1)))
i <- i + n
poly.data <- spRbind(poly.data,temp.data)
setTxtProgressBar(pb, i)
}
cat("***Process completed in",round(difftime(Sys.time(), timer_all, tz = "", units = "mins"),2),"min!***")
options(warn = 0) # re-enable warnings
close(pb)
# save combined polygons to a single .kml file
writeOGR(poly.data, dsn="blr.kml", layer="bangalore", driver="KML")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment