-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
147 lines (123 loc) · 5 KB
/
script.js
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
var CLIENT_ID = '945188122034-r8gqdcfbq87tepa0lgki5r2rlo5h7bav.apps.googleusercontent.com';
//Google Map
var map;
//Generate Systematic Grid Over a Country
//EE Spcript to Generate Systematic Grid Over a Country
var generateGrid = function() {
//Initialize EE
ee.initialize();
map.overlayMapTypes.clear();
//Import Topography Model image and Country Boundaries feature collection.
var srtm = ee.Image("USGS/SRTMGL1_003");
var gaul = ee.FeatureCollection(GAULSRC).sort('adm0_name');
//Define the country, projection and distance between plots (in meters) based on user input
var country = $('#selectedCountry').find(":selected").text();
var distanceBetweenPlots = parseInt($('#distanceBetweenPlots').val().replace(/,/g, ""), 10);
var projection = $('#projection').val();
//Filter country of Interest based on selection above
country = gaul.filterMetadata('adm0_name', 'equals', country).geometry();
//Stack the interesting bands
var dtm = ee.Algorithms.Terrain(srtm);
var lonlat = ee.Image.pixelLonLat();
var stack = (dtm).addBands(lonlat);
//Sample the stack of bands (image) over the country
var points = stack.sample(country, distanceBetweenPlots, ee.Projection(projection));
//Give the features a geometry so that they can be merged with the shapefiles
var points = points.map(
function(plot) {
// Keep this list of properties.
var keepProperties = ['latitude',
'longitude',
'elevation',
'slope',
'aspect',
];
var centroid = ee.Geometry.Point([plot.get("longitude"), plot.get("latitude")]);
// Select the properties from the feature, overwrite the geometry.
return ee.Feature(centroid).copyProperties(plot, keepProperties)
}
);
//Create the map layer to push to the Google Map
var eeMapConfig = points.getMap();
var eeTileSource = new ee.layers.EarthEngineTileSource(
'https://earthengine.googleapis.com/map',
eeMapConfig.mapid, eeMapConfig.token);
var overlay = new ee.layers.ImageOverlay(eeTileSource);
//Extract information for the points and export a link to download the CSV
var downloadlink = ee.FeatureCollection(points)
.getDownloadURL(
'csv', [
'system:index',
'latitude',
'longitude',
'elevation',
'slope',
'aspect'
]
);
// Push the generated layer to the Google Map
map.overlayMapTypes.push(overlay);
//Enable Download button for the CSV file
$('#downloadbtlink').attr('href', downloadlink);
$('#downloadbt').removeAttr('disabled');
$('#downloadbt').html(DOWNLOADMESSAGE);
var newBoundries = country.bounds().coordinates().getInfo();
centerMap(newBoundries)
};
function runEE() {
$('#downloadbt').prop('disabled', true);
$('#downloadbt').html(GENERATINGMESSAGE);
ee.data.authenticateViaOauth(CLIENT_ID, generateGrid, null, null, onImmediateFailed);
};
function centerMap(bound) {
var ne = new google.maps.LatLng(bound[0][2][1], bound[0][2][0]);
var sw = new google.maps.LatLng(bound[0][0][1], bound[0][0][0]);
var bounds = new google.maps.LatLngBounds(sw, ne);
map.fitBounds(bounds);
}
//Function used to get the countries names from the GeoTable
var listCountries = function() {
ee.initialize;
var gaul = ee.FeatureCollection(GAULSRC).sort('adm0_name');
var countriesArray = gaul.aggregate_array('adm0_name').getInfo();
$.each(countriesArray, function(val, text) {
$('#selectedCountry').append($('<option></option>').val(val).html(text))
});
$("#selectedCountry option[value='loadingmessage']").remove();
$('#selectedCountry').removeAttr('disabled');
}
$(document).ready(function() {
// Create the base Google Map.
map = new google.maps.Map($('#map').get(0), {
center: {
lat: 0,
lng: 0
},
zoom: 3,
streetViewControl: false,
fullscreenControl: false
});
$('#downloadbt').html(DOWNLOADMESSAGE);
$("#selectedCountry").append('<option value="loadingmessage">' + LOADINCOUNTRIESMESSAGE + '</option>');
//Get the countries list from the GeoTable TODO: Check for alternatives
ee.data.authenticateViaOauth(CLIENT_ID, listCountries, null, null, onImmediateFailed);
});
///////////////////////////////////////////////////////////////////////////////
// Static helpers and constants. //
///////////////////////////////////////////////////////////////////////////////
// Shows a button prompting the user to log in.
var onImmediateFailed = function() {
$('.g-sign-in').removeClass('hidden');
$('.output').text('(Log in to see the result.)');
$('.g-sign-in .button').click(function() {
ee.data.authenticateViaPopup(function() {
// If the login succeeds, hide the login button and run the analysis.
$('.g-sign-in').addClass('hidden');
});
});
};
const DOWNLOADMESSAGE = 'Download CSV';
const GENERATINGMESSAGE = '<i class="fa fa-spinner fa-spin"></i> Generating Grid';
const LOADINCOUNTRIESMESSAGE = 'Loading countries list';
const PROJECTION = 'EPSG:4826';
const GAULSRC = "users/marceloarvore/FAO-GAUL-2015_2014_0";