Skip to content

Instantly share code, notes, and snippets.

@kokkytos
Last active December 1, 2019 18:05
Show Gist options
  • Save kokkytos/59421f6b62df85fecba05a0ffd0cfba1 to your computer and use it in GitHub Desktop.
Save kokkytos/59421f6b62df85fecba05a0ffd0cfba1 to your computer and use it in GitHub Desktop.
[Λήψη δορυφορικών εικόνων στην R μέσω του GIBS API] #GIBS
library(gdalUtils)
rGIBS<-function(date=Sys.Date(), EPSGCODE,PRODUCT,projwin, outputdir=file.path(Sys.getenv("HOME"),"gibs_downloads"),...){
dir.create(outputdir, showWarnings = FALSE)
setwd(outputdir)
#generate xml
makeXML <- function(date, EPSGCODE,PRODUCT){
myxml<- sprintf('<GDAL_WMS><Service name="TiledWMS"><ServerUrl>https://gibs.earthdata.nasa.gov/twms/epsg%s/best/twms.cgi?</ServerUrl><TiledGroupName>%s tileset</TiledGroupName><Change key="${time}">%s</Change></Service></GDAL_WMS>',EPSGCODE, PRODUCT, date)
tmp <- tempfile(fileext=".xml")
fileConn<-file(tmp)
writeLines(myxml, fileConn)
close(fileConn)
return(tmp)
}
infile<-makeXML(date,EPSGCODE,PRODUCT)
outfile<-gsub(" ","_",(sprintf('%s_%s_EPSG%s.tif',PRODUCT,date,EPSGCODE)))
gdal_translate(
infile,
outfile,
projwin=projwin,
verbose = T,
...
)
#delete temp file
unlink(infile)
message(sprintf("Image %s was saved in %s", outfile, outputdir))
return (file.path(outputdir,outfile))
}
PRODUCT<-'VIIRS SNPP DayNightBand ENCC'
EPSGCODE<-4326
date<-'2017-08-15'
projwin <- c(19.07, 42.16, 28.39, 34.5)
image<-rGIBS(date=date,EPSGCODE=EPSGCODE,PRODUCT=PRODUCT, projwin=projwin, of = "GTiff")
plotRGB(stack(image))
dates<-seq(as.Date("2017/11/08"), as.Date("2017/11/09"), "days")
myresults<-lapply(
dates,
rGIBS,
EPSGCODE=EPSGCODE,
PRODUCT=PRODUCT,
projwin=projwin,
of = "GTiff"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment