library(magrittr)
library(raster)
## Loading required package: sp
## 
## Attaching package: 'raster'
## The following object is masked from 'package:magrittr':
## 
##     extract
library(rgdal)
## rgdal: version: 1.3-4, (SVN revision 766)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 2.2.3, released 2017/11/20
##  Path to GDAL shared files: /usr/share/gdal/2.2
##  GDAL binary built with GEOS: TRUE 
##  Loaded PROJ.4 runtime: Rel. 4.9.3, 15 August 2016, [PJ_VERSION: 493]
##  Path to PROJ.4 shared files: (autodetected)
##  Linking to sp version: 1.3-1
library(gdalUtils)
library(mapview)

setwd("/home/michael/Dropbox/BGU/Allan_Just/p_08_MODIS_projection_in_Earth_Engine/")

# Original HDF
s = get_subdatasets("MOD11A1.A2014129.h12v04.006.2016203053220.hdf")
r1 = s[1] %>% readGDAL %>% raster
## HDF4_EOS:EOS_GRID:MOD11A1.A2014129.h12v04.006.2016203053220.hdf:MODIS_Grid_Daily_1km_LST:LST_Day_1km has GDAL driver HDF4Image 
## and has 1200 rows and 1200 columns
p1 = proj4string(r1)

# EE export
r2 = raster("SRTMonMODISprojection2.tif")
e2 = extent(r2)

# Map *before* proj4string change
mapview(r2)
# proj4string change!
proj4string(r2) = p1

# Map *after* proj4string change
mapview(r2)
# Crop original HDF
r1 = crop(r1, e2)

# Check alignment
extent(r1)
## class       : Extent 
## xmin        : -5895191 
## xmax        : -5781216 
## ymin        : 4660926 
## ymax        : 4736909
extent(r2)
## class       : Extent 
## xmin        : -5895191 
## xmax        : -5781216 
## ymin        : 4660926 
## ymax        : 4736909
dim(r1)
## [1]  82 123   1
dim(r2)
## [1]  82 123   1
## EE Code

# var elevation = ee.Image("USGS/SRTMGL1_003");
# var lst = ee.Image('MODIS/006/MOD11A1/2014_05_09')
# .select('LST_Day_1km');
# 
# var pal = ['#9e0142','#d53e4f','#f46d43','#fdae61','#fee08b','#ffffbf','#e6f598','#abdda4','#66c2a5','#3288bd','#5e4fa2'];
# 
# // Get the elevation cover data at MODIS scale and projection.
# 
# var modisProjection = lst.projection();
# print(modisProjection);
# print(modisProjection.nominalScale());
# 
# var elevationMean = elevation
# // Force the next reprojection to aggregate instead of resampling.
# .reduceResolution({
#   reducer: ee.Reducer.mean(),
#   maxPixels: 1500
# })
# // Request the data at the scale and projection of the MODIS image.
# .reproject("SR-ORG:6974", null, 926.6254330555);
# 
# 
# // Print
# print(elevationMean);
# 
# // Create a geometry representing an export region.
# var pol = ee.Geometry.Polygon([ [ [ -72.214370000058082, 41.190339999919971 ], [ -72.214370000049158, 43.30923999991181 ], [ -69.64939, 43.30924 ], [ -69.64939, 41.19034 ], [ -72.214370000058082, 41.190339999919971 ] ] ]);
# //pol = pol.buffer(-20000);
# 
# // Clip
# pol = pol.buffer(-80000);
# 
# // Map
# Map.setCenter(-71.046464, 42.358930, 8);
# Map.addLayer(
#   elevationMean, 
#   {bands: ["elevation"], min: 0, max: 100, palette: pal}, 
#   "Elev"
# );
# //Map.addLayer(pol, {color: '000000'}, 'Polygon');
# 
# // Export the image, specifying scale and region.
# Export.image.toDrive({
#   image: elevationMean,
#   description: 'SRTMonMODISprojection2',
#   dimensions: 1200,
#   scale: 926.6254330555, 
#   crs: 'SR-ORG:6974',
#   region: pol
# });