Skip to content

Instantly share code, notes, and snippets.

@lbusett
Last active July 15, 2020 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lbusett/8805a2712c2f9d139aee7471e62be42f to your computer and use it in GitHub Desktop.
Save lbusett/8805a2712c2f9d139aee7471e62be42f to your computer and use it in GitHub Desktop.
Fix a kml exported from GEE so that holes are respected
fix_kml <- function(infile, outfile) {
intext <- readLines(con <- file(infile))
out <- list()
ll = 1
indout <- 1
# cycle on lines of input
while(ll <= length(intext)){
curline <- intext[ll]
# If a "hole block" is starting, check if there is only
# one ring. If not, enclose all rings in
# <innerBoundaryIs>" - "</innerBoundaryIs>" blocks
if(length(grep("<innerBoundaryIs>", curline)) != 0) {
if (length(grep("</innerBoundaryIs>", intext[ll+4])) == 0) {
while(length(grep("</innerBoundaryIs>", intext[ll+1])) == 0) {
out[indout] <- "<innerBoundaryIs>"
out[indout + 1] <- intext[ll+1]
out[indout + 2] <- intext[ll+2]
out[indout + 3] <- intext[ll+3]
out[indout + 4] <- "</innerBoundaryIs>"
ll <- ll + 3
indout <- indout + 5
}
indout <- indout + 1
ll = ll + 2
} else {
out[indout] <- curline
indout <- indout + 1
ll = ll +1
}
} else {
# Otherwise, simply copy the line in the output
out[indout] <- curline
indout <- indout + 1
ll = ll +1
}
}
outarr <- unlist(out)
outfileConn<-file(outfile)
writeLines(outarr, outfileConn)
close(outfileConn)
}
@lbusett
Copy link
Author

lbusett commented Jul 15, 2020

Useful to fix holes on vector maps exported from GEE. Example of usage (after sourcing the function):

infile_kml  <- "C:/Users/LB_laptop/Downloads/FireCCI_RD_36KWE_20190623_20190708.kml"
outfile_kml <- "C:/Users/LB_laptop/Downloads/FireCCInew.kml"
fix_kml(infile_kml  , outfile_kml )

The new kml file should be readable by GDAL driver (eg in QGIS) and holes should be ok.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment