I have managed to extract MODIS land cover data from an HDF file and put it into a raster.
library(gdalUtils) #? gdal_translate?
library(raster)
sds <- get_subdatasets(".......myfileplath/file.hdf")
# Isolate the name of the first sds
name <- sds[5]
filename <- '2009test.tif'
gdal_translate(sds[5], dst_dataset = filename)
# Load the Geotiff created into R
r <- raster(filename)
I want to put it into a dataframe but reproject it from the original sinusoidal which is +proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs
, I think.
To a normal ellipsoid/WGS84 one that is compatible with other datasets I'm analyzing.
This is what I've tried and seemed to work:
sr <- '+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0'
projected_raster <- projectRaster(r, crs = sr)
However, when I then put my my landcover data in this new projection into a data frame all the landcover values have gone to NA.
This is what the dataframe looked like in a sinu projection (the 4's being a land cover classifications)
DF <- as.data.frame(r, xy=TRUE)
head(DF)
# x y X2009test
#1 -1111718.9 1111719 4
#2 -1111255.6 1111719 4
#3 -1110792.2 1111719 4
#4 -1110328.9 1111719 4
#5 -1109865.6 1111719 4
and with my reprojection it looks like this:
reprojected_DF <- as.data.frame(projected_raster, xy=TRUE)
head(reprojected_DF)
# x y X2009test
#1 -10.173076 10.01876 NA
#2 -10.168896 10.01876 NA
#3 -10.164716 10.01876 NA
#4 -10.160536 10.01876 NA
#5 -10.156356 10.01876 NA
#6 -10.152176 10.01876 NA
#7 -10.147996 10.01876 NA
Any advice on what I'm doing wrong or how I can get the land cover coordinates to reproject properly?
Cheers!!!!
Also I read there was a NASA MODIS reprojection tool but that that no long exists / is available. Anyone know anything about that?