Skip to content

Commit

Permalink
Landsat api request
Browse files Browse the repository at this point in the history
  • Loading branch information
pjosh committed Jul 19, 2017
1 parent 8fda5a2 commit 7934971
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
5 changes: 3 additions & 2 deletions app/assets/javascripts/map/templates/tabs/basemaps.handlebars
Expand Up @@ -22,17 +22,18 @@
<li class="maptype landsat2004" data-maptype="landsat2004">Landsat 2004</li>
<li class="maptype landsat2005" data-maptype="landsat2005">Landsat 2005</li>
<li class="maptype landsat2006" data-maptype="landsat2006">Landsat 2006</li>
<li class="maptype landsat2007" data-maptype="landsat2007">Landsat 2007</li>
</div>
<div>
<li class="maptype landsat2007" data-maptype="landsat2007">Landsat 2007</li>
<li class="maptype landsat2008" data-maptype="landsat2008">Landsat 2008</li>
<li class="maptype landsat2009" data-maptype="landsat2009">Landsat 2009</li>
<li class="maptype landsat2010" data-maptype="landsat2010">Landsat 2010</li>
<li class="maptype landsat2011" data-maptype="landsat2011">Landsat 2011</li>
<li class="maptype landsat2012" data-maptype="landsat2012">Landsat 2012</li>
<li class="maptype landsat2013" data-maptype="landsat2013">Landsat 2013</li>
<li class="maptype landsat2014" data-maptype="landsat2014">Landsat 2014</li>
<li class="maptype landsat2014" data-maptype="landsat2015">Landsat 2015</li>
<li class="maptype landsat2015" data-maptype="landsat2015">Landsat 2015</li>
<li class="maptype landsat2016" data-maptype="landsat2016">Landsat 2016</li>
</div>
</ul>

Expand Down
82 changes: 82 additions & 0 deletions app/assets/javascripts/services/LandsatService.js
@@ -0,0 +1,82 @@
define([
'Class',
'uri',
'bluebird',
'map/services/DataService'
], function(Class, UriTemplate, Promise, ds) {

'use strict';

var GET_REQUEST_LANDSAT_TILES_ID = 'LandsatService:getTiles';

var APIURL = window.gfw.config.GFW_API_HOST_PROD;

var APIURLS = {
'getTiles': '/landsat-tiles/{year}'
};

var LandsatService = Class.extend({
init: function() {
this.currentRequest = [];
},

getTiles: function(year) {
return new Promise(function(resolve, reject) {
var url = new UriTemplate(APIURLS.getTiles).fillFromObject({
year: year
});

this.defineRequest(GET_REQUEST_LANDSAT_TILES_ID,
url, { type: 'persist', duration: 1, unit: 'days' });

var requestConfig = {
resourceId: GET_REQUEST_LANDSAT_TILES_ID,
success: function(res, status) {
resolve(res.data, status);
},
error: function(errors) {
reject(errors);
}
};

this.abortRequest(GET_REQUEST_LANDSAT_TILES_ID);
this.currentRequest[GET_REQUEST_LANDSAT_TILES_ID] = ds.request(requestConfig);
}.bind(this));
},

defineRequest: function (id, url, cache) {
ds.define(id, {
cache: cache,
url: APIURL + url,
type: 'GET',
dataType: 'json',
contentType: 'application/json; charset=utf-8',
decoder: function ( data, status, xhr, success, error ) {
if ( status === "success" ) {
success( data, xhr );
} else if ( status === "fail" || status === "error" ) {
error( JSON.parse(xhr.responseText) );
} else if ( status === "abort") {

} else {
error( JSON.parse(xhr.responseText) );
}
}
});
},

/**
* Abort the current request if it exists.
*/
abortRequest: function(request) {
if (this.currentRequest && this.currentRequest[request]) {
this.currentRequest[request].abort();
this.currentRequest[request] = null;
}
}

});

return new LandsatService();

});

0 comments on commit 7934971

Please sign in to comment.