Skip to content

Instantly share code, notes, and snippets.

@hakimabdi
Created October 23, 2015 16:12
Show Gist options
  • Save hakimabdi/a2ea2a4aecfd5e3c9198 to your computer and use it in GitHub Desktop.
Save hakimabdi/a2ea2a4aecfd5e3c9198 to your computer and use it in GitHub Desktop.
##############################################################################
# title : projHDF2GTiff.R
# purpose : Converts desired HDF layer to a GeoTIFF and projects it
# author : Abdulhakim Abdi (@HakimAbdi)
# input : HDF-EOS files
# output : Projected GeoTIFF files
##############################################################################
projHDF2GTiff = function(loc, hdfs, gtiffs, lyr, fromSRS, toSRS){
if("gdalUtils" %in% rownames(installed.packages()) == FALSE){
install.packages("gdalUtils", repos="http://r-forge.r-project.org")
require(gdalUtils)
} # install and load the gdalUtils package.
setwd(loc) # set the working directory to where the data is
suppressWarnings(dir.create(paste(loc,"Projected",sep="/"))) # create a directory to store projected files
for (i in 1:length(hdfs)){
gdal_translate(hdfs[i],gtiffs[i],sd_index=lyr) # extract the specified HDF layer and save it as a Geotiff
gdalwarp(gtiffs[i],paste(loc,"Projected",gtiffs[i],sep="/"),s_srs=fromSRS,t_srs=toSRS,srcnodata=-3000,dstnodata=-3000,overwrite = T) # project geotiffs
unlink(gtiffs[i]) # delete unprojected geotiffs to save space
}
}
@ndichistan
Copy link

Hi.. Thanks for this program.

I have tried to run your program on my data and I keep getting this error
"Error in projHDF2GTiff(loc = myloc, hdfs = hdfs1, gtiffs = gtiffs1, lyr = 1, :
unused arguments (srcnodata = s.nodata, dstnodata = d.nodata
)".
Could you take a look and see what I am doing wrong? I have attached my code and some of my data for your consideration.
sample data and code

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