Last active
July 15, 2020 16:54
-
-
Save lbusett/8805a2712c2f9d139aee7471e62be42f to your computer and use it in GitHub Desktop.
Fix a kml exported from GEE so that holes are respected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Useful to fix holes on vector maps exported from GEE. Example of usage (after sourcing the function):
The new kml file should be readable by GDAL driver (eg in QGIS) and holes should be ok.