Skip to content

Instantly share code, notes, and snippets.

@valentinitnelav
Last active May 23, 2024 20:33
Show Gist options
  • Save valentinitnelav/0f89f9cad2c7bcae8f989f0cd90bb395 to your computer and use it in GitHub Desktop.
Save valentinitnelav/0f89f9cad2c7bcae8f989f0cd90bb395 to your computer and use it in GitHub Desktop.
# extract long-lat coordinates from a bunch of kmz files
# first unzips the KMZ-s then reads coordinates from each KML with getKMLcoordinates {maptools}
# (worth checking this as well: https://gist.github.com/holstius/6631918 )
library(maptools)
# list the kmz files in a given folder path
KMZs <- list.files(path="Data/kmz-files", pattern="*.kmz", full.names=FALSE)
# unzip each KMZ file and read coordinates with getKMLcoordinates()
# select only the matrix element of the list returned by getKMLcoordinates(),
# therefore mention the index [[1]]
LonLat <- sapply(KMZs,
function(x)
getKMLcoordinates(kmlfile = unzip(zipfile = paste0("Data/kmz-files/", x),
exdir = "Data/kmz-files/KML"),
ignoreAltitude = TRUE)[[1]])
# To get altitude from KML, then mention ignoreAltitude=FALSE
# delete the .kmz part from the column names in LonLat matrix
colnames(LonLat) <- gsub(pattern=".kmz", replacement="", x=colnames(LonLat))
# give names to rows
rownames(LonLat) <- c("Longitude", "Latitude")
# transpose the matrix for readability reasons
LonLat <- t(LonLat)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment