-
Notifications
You must be signed in to change notification settings - Fork 1
/
downLoadImagery
167 lines (139 loc) · 5.94 KB
/
downLoadImagery
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//downLoadImagery_Beta20150213
//Set the start date and enddate for the full time period you want images
var startdate = new Date("October 1, 2013");
var enddate = new Date("January 30, 2015");
//Set the length and type of temporal units for each picture (e.g., 1 picture for every 1 month, 1 picture for every 15 days)
var intervalType = 'month';
var intervalLength = 1;
//Set a base name for the image names
var imageBaseName = "Romero";
//--------------------
/////CHOOSE DATA TYPE
var imageType = 'NDVI'
//var imageType = 'Raw'
//var imageType = 'Wetness'
//var imageType = 'NDWI'
var imageType = 'Veg'
//---------------------
//CHOOSE REGION
var West = -119.74170684814453 //Longitude
var North = 34.05607276338369 //Latitude
var East = -119.66754913330078 //Longitude
var South = 34.00969645770158 //Latitude
var coords1 = '[[' + West + "," + North + '], ['+ West + "," + South + '], ['+ East + "," + South + '], ['+ East + "," + North + ']]';
//print(coords1);
//----------------------
// IMAGERY SOURCES - LANDSAT
// Code will use:
// -Landsat 5 if interval end date is before May 8 2012
// -Landsat 7 if interval end date is after May 9 2012 and interval start is after april 7 2013
// -Landsat 8 if interval start date is after April 7 2013
var landsat8startDate = new Date("April 7, 2013");
var landsat5endDate = new Date("May 8, 2012");
//----------------------
//A date function to increment dates by the temporal units given above
function dateAdd(date, interval, units) {
var ret = new Date(date); //don't change original date
switch(interval.toLowerCase()) {
case 'year' : ret.setFullYear(ret.getFullYear() + units); break;
case 'quarter': ret.setMonth(ret.getMonth() + 3*units); break;
case 'month' : ret.setMonth(ret.getMonth() + units); break;
case 'week' : ret.setDate(ret.getDate() + 7*units); break;
case 'day' : ret.setDate(ret.getDate() + units); break;
case 'hour' : ret.setTime(ret.getTime() + units*3600000); break;
case 'minute' : ret.setTime(ret.getTime() + units*60000); break;
case 'second' : ret.setTime(ret.getTime() + units*1000); break;
default : ret = undefined; break;
}
return ret;
}
//a formatting function to convert a date to a text string for a filename (e.g. "2014_06_01")
function dateFormat(date) {
var fYear = date.getFullYear();
var tempMonth = date.getMonth() + 1
var fMonth;
if (tempMonth<10) {
fMonth = "0"+tempMonth;
} else {
fMonth = tempMonth.toString();
}
var tempDate = date.getDate()
var fDate;
if (tempDate<10) {
fDate = "0"+tempDate;
} else {
fDate = tempDate.toString();
}
var fDateString = fYear.toString()+"_"+fMonth+"_"+fDate;
return fDateString;
}
//Establish variables for the loop
var i;
var intervalEndDate = startdate;
print("Generating image URLs...")
//Loop to iterate through the dates. Loop continues while the end of the interval is less than the end date for the full period
for (i=0;intervalEndDate < enddate;i=i+intervalLength) {
var intervalStartDate = dateAdd(startdate,intervalType,i);
var intervalEndDate = dateAdd(startdate,intervalType,i+intervalLength);
//print('START DATE ' + intervalStartDate.toString());
//print('END DATE ' + intervalEndDate.toString());
//Choose landsat 5, landsat 7, or landsat 8 based on interval range
if (intervalStartDate > landsat8startDate) {
var landsatNum = "LC8";
} else if (intervalEndDate < landsat5endDate) {
var landsatNum = "LT5";
} else {
var landsatNum = "LE7";
}
//print(landsatNum);
//get image collection. Note that Landsat 8 collection starts April 7th, 2013
switch(imageType) {
case 'NDVI':
var landsat8 = ee.ImageCollection('LANDSAT/' + landsatNum +'_L1T_8DAY_NDVI');
break;
case 'Raw':
var landsat8 = ee.ImageCollection('LANDSAT/' + landsatNum +'_L1T');
break;
case 'Wetness':
var landsat8 = ee.ImageCollection('LANDSAT/' + landsatNum +'_L1T');
break;
case 'NDWI':
var landsat8 = ee.ImageCollection('LANDSAT/' + landsatNum +'_L1T_8DAY_NDWI');
break;
case 'Veg':
var landsat8 = ee.ImageCollection('LANDSAT/' + landsatNum +'_L1T');
break;
}
//Filter the image collection to a smaller image collection based on the interval start and end date
var intervalImageCollection = landsat8.filterDate(intervalStartDate, intervalEndDate);
//Reduce the interval image collection to a single image using a median reducer
var median1 = intervalImageCollection.median();
//Set the bands to visualize the image
//var map1 = median1.visualize({'bands': ['B4', 'B3', 'B2'], 'min': 5000, 'max': 18000});
switch(imageType) {
case 'NDVI':
var NDVI_PALETTE = ['663300', 'CC7A00', 'B8B800', '009900'];
var map1 = median1.visualize({'bands': "NDVI", 'palette': NDVI_PALETTE});
break;
case 'Wetness':
var map1 = median1.visualize({'bands': ['B7', 'B5', 'B1'], 'min': 5000, 'max': 18000});
break;
case 'Raw':
var map1 = median1.visualize({'bands': ['B4', 'B3', 'B2'], 'min': 5000, 'max': 18000});
break;
case 'NDWI':
var NDWI_PALETTE = ['FFFFCC', '0066FF'];
var map1 = median1.visualize({'bands': "NDWI", 'palette': NDWI_PALETTE});
break;
case 'Veg':
var map1 = median1.visualize({'bands': ['B5', 'B6', 'B2'], 'min': 5000, 'max': 18000});
break;
}
//Generate file name for download
var imageName = imageBaseName+"_"+ imageType + "_" +dateFormat(intervalStartDate)+"_to_"+dateFormat(intervalEndDate);
//Generate a URL to download the image
var path = map1.getDownloadURL({'name':imageName, 'scale': 30, 'region': coords1, 'crs':'EPSG:4326', 'filePerBand': 'False'});
// gonzales zoom [[-121.493, 36.497], [-121.493, 36.464], [-121.44, 36.464], [-121.44, 36.497]]
// whole region '[[-121.59, 36.6], [-121.59, 36.4], [-121.38, 36.4], [-121.38, 36.6]]'
print(path);
}