Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

Remome duplicate functions #825

Benjamin King994a8b9b4618
diff --git a/web/js/animation/gif.js b/web/js/animation/gif.js
index 5db04e0bc..76e8da2e7 100644
--- a/web/js/animation/gif.js
+++ b/web/js/animation/gif.js
@@ -6,12 +6,18 @@ import gifshot from 'gifshot';
import lodashFind from 'lodash/find';
import lodashEach from 'lodash/each';
import lodashRound from 'lodash/round';
-import lodashIsUndefined from 'lodash/isUndefined';
import lodashThrottle from 'lodash/throttle';
import lodashCapitalize from 'lodash/capitalize';
import canvg from 'canvg-browser';
import FileSaver from 'file-saver';
import { GA as googleAnalytics, GifResSelection } from 'worldview-components';
+import {
+ imageUtilGetLayerOpacities,
+ imageUtilCalculateResolution,
+ imageUtilGetLayers,
+ imageUtilGetCoordsFromPixelValues,
+ imageUtilGetConversionFactor
+} from '../image/util';
import util from '../util/util';
import wvui from '../ui/ui';
import uiIndicator from '../ui/indicator';
@@ -136,13 +142,17 @@ export function animationGif(models, config, ui) {
var update = function(selectedRes, lonlats) {
if (!animationCoordinates) return;
- var numDays, stateObj, dimensions;
+ var numDays, stateObj, dimensions, isGeoProjection;
+ isGeoProjection = (models.proj.selected.id === 'geographic');
resolution = selectedRes;
stateObj = animModel.rangeState;
numDays = util.getNumberOfDays(new Date(stateObj.startDate), new Date(stateObj.endDate), ui.anim.ui.getInterval());
- if (!resolution) resolution = calcRes(0);
+ if (!resolution) {
+ let currentZoom = Math.round(ui.map.selected.getView().getZoom());
+ resolution = imageUtilCalculateResolution(currentZoom, isGeoProjection, models.proj.selected.resolutions);
+ }
dimensions = getDimensions(lonlats, models.proj.selected.id, resolution);
imgWidth = dimensions[0];
imgHeight = dimensions[1];
@@ -157,7 +167,7 @@ export function animationGif(models, config, ui) {
};
var onSelectionChange = function(selectedRes) {
- update(selectedRes, getCoords());
+ update(selectedRes, imageUtilGetCoordsFromPixelValues(animationCoordinates, ui.map.selected));
};
var fileSizeValid = function() {
@@ -307,54 +317,6 @@ export function animationGif(models, config, ui) {
return { stampHeight: stampHeight, dateStamp: dateStamp };
};
- /*
- * Calculates resolution of frame based
- * on zoom and projection
- *
- *
- * @func calcRes
- * @private
- *
- * @returns {void}
- *
- */
- var calcRes = function(mode) { // return either multiplier or string resolution
- // geographic has 10 zoom levels from 0 to 9, polar projections have 8 from 0 to 7
- var str;
- var res;
- var isGeographic = models.proj.selected.id === 'geographic';
- // Map the zoom level from 0-9 / 0-7 to an index from 0-4
- var zoomRes = [40, 20, 4, 2, 1];
- var zoom = ui.map.selected.getView().getZoom();
- zoom = zoom < 0 ? 0 : zoom;
- if (isGeographic) {
- res = zoomRes[Math.floor(zoom / 2)];
- } else {
- res = zoomRes[Math.floor(((zoom + 2) / 2))];
- }
-
- if (mode === 0) { return res.toString(); } else {
- switch (res) {
- case 1:
- str = '250m';
- break;
- case 2:
- str = '500m';
- break;
- case 4:
- str = '1km';
- break;
- case 20:
- str = '5km';
- break;
- default:
- str = '10km';
- }
-
- return str;
- }
- };
-
/*
* checks if rotation, changed palettes, or graticules
* are active and ask to reset if any are active
@@ -414,29 +376,6 @@ export function animationGif(models, config, ui) {
self.createGIF();
};
- /*
- * Retieves avtive layers by day
- *
- * @method getLayersForDay
- * @private
- *
- * @param {array} array of layers
- *
- * @returns {array} array of layer ids
- *
- */
- var getLayers = function(products, proj) {
- var layers = [];
- lodashEach(products, function(layer) {
- if (layer.projections[proj].layer) {
- layers.push(layer.projections[proj].layer);
- } else {
- layers.push(layer.id);
- }
- });
- return layers;
- };
-
/*
* retrieves renderable layers
*
@@ -466,39 +405,6 @@ export function animationGif(models, config, ui) {
return layers;
};
- /*
- * Retieves opacities from palettes
- *
- * @method getOpacities
- * @private
- *
- * @param {array} array of layers
- *
- * @returns {array} array of opacities
- *
- */
- var getOpacities = function(products) {
- var opacities = [];
- lodashEach(products, function(product) {
- opacities.push((lodashIsUndefined(product.opacity)) ? 1 : product.opacity);
- });
- return opacities;
- };
-
- /*
- * Retieves coordinates from pixel
- *
- * @method getCoords
- * @private
- *
- * @returns {array} array of coords
- *
- */
- var getCoords = function() {
- return [ui.map.selected.getCoordinateFromPixel([Math.floor(animationCoordinates.x), Math.floor(animationCoordinates.y2)]),
- ui.map.selected.getCoordinateFromPixel([Math.floor(animationCoordinates.x2), Math.floor(animationCoordinates.y)])];
- };
-
/*
* Dimenions from zoom & projection
*
@@ -538,7 +444,7 @@ export function animationGif(models, config, ui) {
var j = 0;
var src;
var strDate;
- var lonlat = getCoords();
+ var lonlat = imageUtilGetCoordsFromPixelValues(animationCoordinates, ui.map.selected);
var layers;
var proj = models.proj.selected.id;
var opacities;
@@ -563,8 +469,8 @@ export function animationGif(models, config, ui) {
}
products = getProducts(current);
- layers = getLayers(products, proj);
- opacities = getOpacities(products);
+ layers = imageUtilGetLayers(products, proj);
+ opacities = imageUtilGetLayerOpacities(products);
url = util.format(host + '/' + path + '?{1}&extent={2}&epsg={3}&layers={4}&opacities={5}&worldfile=false&format=image/jpeg&width={6}&height={7}', 'TIME={1}', lonlat[0][0] + ',' + lonlat[0][1] + ',' + lonlat[1][0] + ',' + lonlat[1][1], epsg, layers.join(','), opacities.join(','), imgWidth, imgHeight);
src = util.format(url, strDate);
if (showDates) {
@@ -833,11 +739,6 @@ export function animationGif(models, config, ui) {
}
};
- var getConversionFactor = function(proj) {
- if (proj === 'geographic') return 0.002197;
- return 256;
- };
-
/*
* uses resolution and dimension to
* calculates size of selected area
@@ -853,7 +754,7 @@ export function animationGif(models, config, ui) {
var calulateFileSize = function(imgRes, lonlat1, lonlat2, numDays, imgWidth, imgHeight) {
var conversionFactor;
- conversionFactor = getConversionFactor(models.proj.selected.id);
+ conversionFactor = imageUtilGetConversionFactor(models.proj.selected.id);
resolution = imgRes;
imgWidth = Math.round((Math.abs(lonlat2[0] - lonlat1[0]) / conversionFactor) / Number(imgRes));
imgHeight = Math.round((Math.abs(lonlat2[1] - lonlat1[1]) / conversionFactor) / Number(imgRes));
@@ -995,7 +896,7 @@ export function animationGif(models, config, ui) {
},
onChange: function(e) {
onBoundingBoxChange(e, $dialog, $dlButton);
- update(resolution, getCoords());
+ update(resolution, imageUtilGetCoordsFromPixelValues(animationCoordinates, ui.map.selected));
},
onRelease: function() {
removeCrop();
@@ -1019,7 +920,8 @@ export function animationGif(models, config, ui) {
setIconFontSize($dlButton, starterWidth);
$dlButton.on('click', getGif);
});
- update(null, getCoords());
+
+ update(null, imageUtilGetCoordsFromPixelValues(animationCoordinates, ui.map.selected));
};
self.init();
return self;
diff --git a/web/js/image/panel.js b/web/js/image/panel.js
index 7b5000359..81be292f4 100644
--- a/web/js/image/panel.js
+++ b/web/js/image/panel.js
@@ -1,8 +1,6 @@
import $ from 'jquery';
import 'jquery-ui/button';
import 'jquery-ui/dialog';
-import lodashEach from 'lodash/each';
-import lodashIsUndefined from 'lodash/isUndefined';
import olProj from 'ol/proj';
import { GA as googleAnalytics, ImageResSelection } from 'worldview-components';
import util from '../util/util';
@@ -10,6 +8,14 @@ import wvui from '../ui/ui';
import React from 'react';
import ReactDOM from 'react-dom';
+import {
+ imageUtilCalculateResolution,
+ imageUtilGetLayerOpacities,
+ imageUtilGetCoordsFromPixelValues,
+ imageUtilGetLayers,
+ imageUtilGetConversionFactor
+} from './util';
+
const resolutionsGeo = {
values: [
{ value: '0.125', text: '30m' },
@@ -187,57 +193,12 @@ export function imagePanel (models, ui, config, dialogConfig) {
// for incremental zoom steps
var curZoom = Math.round(ui.map.selected.getView()
.getZoom());
- curZoom = curZoom < 0 ? 0 : curZoom;
// Don't do anything if the user hasn't changed zoom levels; we want to
// preserve their existing settings
if (curZoom !== lastZoom) {
lastZoom = curZoom;
- var nZoomLevels = models.proj.selected.resolutions.length;
- var curResolution = (curZoom >= nZoomLevels)
- ? models.proj.selected.resolutions[nZoomLevels - 1]
- : models.proj.selected.resolutions[curZoom];
-
- // Estimate the option value used by "wv-image-resolution"
- var resolutionEstimate = (models.proj.selected.id === 'geographic')
- ? curResolution / 0.002197265625 : curResolution / 256.0;
-
- // Find the closest match of resolution within the available values
- var possibleResolutions = (models.proj.selected.id === 'geographic')
- ? [0.125, 0.25, 0.5, 1, 2, 4, 20, 40]
- : [1, 2, 4, 20, 40];
- var bestDiff = Infinity;
- var bestIdx = -1;
- var currDiff = 0;
- for (var i = 0; i < possibleResolutions.length; i++) {
- currDiff = Math.abs(possibleResolutions[i] - resolutionEstimate);
- if (currDiff < bestDiff) {
- resolution = possibleResolutions[i];
- bestDiff = currDiff;
- bestIdx = i;
- }
- }
-
- // Bump up resolution in certain cases where default is too low
- if (bestIdx > 0) {
- if (models.proj.selected.id === 'geographic') {
- switch (curZoom) {
- case 3:
- case 4:
- case 6:
- case 7:
- resolution = possibleResolutions[bestIdx - 1];
- }
- } else {
- switch (curZoom) {
- case 1:
- case 2:
- case 4:
- case 5:
- resolution = possibleResolutions[bestIdx - 1];
- }
- }
- }
- imgRes = resolution;
+ let isGeoProjection = (models.proj.selected.id === 'geographic');
+ imgRes = imageUtilCalculateResolution(curZoom, isGeoProjection, models.proj.selected.resolutions);
updatePanel(getUpdatedProps());
}
@@ -267,7 +228,7 @@ export function imagePanel (models, ui, config, dialogConfig) {
y2 = px.y2;
crs = models.proj.selected.crs;
- lonlats = getCoordsFromPixelValues(x1, x2, y1, y2, map);
+ lonlats = imageUtilGetCoordsFromPixelValues(pixels, map);
geolonlat1 = olProj.transform(lonlats[0], crs, 'EPSG:4326');
geolonlat2 = olProj.transform(lonlats[1], crs, 'EPSG:4326');
@@ -319,27 +280,11 @@ export function imagePanel (models, ui, config, dialogConfig) {
width: x2 - x1
}).html(bottomLeftCoordinates);
};
- var getCoordsFromPixelValues = function(x1, x2, y1, y2, map) {
- return [
- map.getCoordinateFromPixel([Math.floor(x1), Math.floor(y2)]),
- map.getCoordinateFromPixel([Math.floor(x2), Math.floor(y1)])
- ];
- };
- var getLayerOpacities = function(products) {
- var opacities = [];
- lodashEach(products, function (product) {
- opacities.push((lodashIsUndefined(product.opacity)) ? 1 : product.opacity);
- });
- return opacities;
- };
- var getConversionFactor = function(proj) {
- if (proj === 'geographic') return 0.002197;
- return 256;
- };
+
var calulateFileSize = function(imgRes, lonlat1, lonlat2) {
var conversionFactor;
- conversionFactor = getConversionFactor(models.proj.selected.id);
+ conversionFactor = imageUtilGetConversionFactor(models.proj.selected.id);
resolution = imgRes;
imgWidth = Math.round((Math.abs(lonlat2[0] - lonlat1[0]) / conversionFactor) / Number(imgRes));
imgHeight = Math.round((Math.abs(lonlat2[1] - lonlat1[1]) / conversionFactor) / Number(imgRes));
@@ -356,7 +301,8 @@ export function imagePanel (models, ui, config, dialogConfig) {
reverse: true,
renderable: true
});
- dlURL = createDownloadURL(models.date.selected, lonlats, models.proj.selected.epsg, products, getLayerOpacities(products), url);
+
+ dlURL = createDownloadURL(models.date.selected, lonlats, models.proj.selected.epsg, products, imageUtilGetLayerOpacities(products), url);
googleAnalytics.event('Image Download', 'Click', 'Download');
util.metrics('lc=' + encodeURIComponent(dlURL + '&worldfile=' + imgWorldfile + '&format=' + imgFormat + '&width=' + imgWidth + '&height=' + imgHeight));
window.open(dlURL + '&worldfile=' + imgWorldfile + '&format=' + imgFormat + '&width=' + imgWidth + '&height=' + imgHeight, '_blank');
@@ -373,10 +319,15 @@ export function imagePanel (models, ui, config, dialogConfig) {
};
var createDownloadURL = function(time, lonlats, epsg, products, opacities, dlURL) {
- var layers;
- layers = getLayers(products, models.proj.selected.id);
+ var layers, jStart, jDate;
+ var dTime = time;
- dlURL += 'TIME=' + util.toISOStringSeconds(time);
+ layers = imageUtilGetLayers(products, models.proj.selected.id);
+ // Julian date, padded with two zeros (to ensure the julian date is always in DDD format).
+ jStart = util.parseDateUTC(dTime.getUTCFullYear() + '-01-01');
+ jDate = '00' + (1 + Math.ceil((dTime.getTime() - jStart) / 86400000));
+ dlURL += 'TIME=' + dTime.getUTCFullYear() + (jDate)
+ .substr((jDate.length) - 3);
dlURL += '&extent=' + lonlats[0][0] + ',' + lonlats[0][1] + ',' + lonlats[1][0] + ',' + lonlats[1][1];
dlURL += '&epsg=' + epsg;
@@ -385,17 +336,7 @@ export function imagePanel (models, ui, config, dialogConfig) {
return dlURL;
};
- var getLayers = function(products, s) {
- var layers = [];
- lodashEach(products, function(layer) {
- if (layer.projections[s].layer) {
- layers.push(layer.projections[s].layer);
- } else {
- layers.push(layer.id);
- }
- });
- return layers;
- };
+
init();
return self;
};
diff --git a/web/js/image/util.js b/web/js/image/util.js
new file mode 100644
index 000000000..e8cf94b00
--- /dev/null
+++ b/web/js/image/util.js
@@ -0,0 +1,126 @@
+import lodashIsUndefined from 'lodash/isUndefined';
+import lodashEach from 'lodash/each';
+
+const GEO_ESTIMATION_CONSTANT = 256.0;
+const POLAR_ESTIMATION_CONSTANT = 0.002197265625;
+
+/*
+ * Estimate appropriate Resolution based on zoom
+ * This is only run if user has not already selected
+ * a resolution
+ */
+export function imageUtilCalculateResolution(zoom, isGeoProjection, resolutions) {
+ var resolution;
+ var resolutionEstimate;
+ var nZoomLevels = resolutions.length;
+ var currentZoom = zoom < 0 ? 0 : zoom;
+ var curResolution = (currentZoom >= nZoomLevels)
+ ? resolutions[nZoomLevels - 1]
+ : resolutions[currentZoom];
+
+ // Estimate the option value used by "wv-image-resolution"
+ resolutionEstimate = imageUtilEstimateResolution(curResolution, isGeoProjection);
+
+ // Find the closest match of resolution within the available values
+ var possibleResolutions = (isGeoProjection)
+ ? [0.125, 0.25, 0.5, 1, 2, 4, 20, 40]
+ : [1, 2, 4, 20, 40];
+ var bestDiff = Infinity;
+ var bestIdx = -1;
+ var currDiff = 0;
+ for (var i = 0; i < possibleResolutions.length; i++) {
+ currDiff = Math.abs(possibleResolutions[i] - resolutionEstimate);
+ if (currDiff < bestDiff) {
+ resolution = possibleResolutions[i];
+ bestDiff = currDiff;
+ bestIdx = i;
+ }
+ }
+
+ // Bump up resolution in certain cases where default is too low
+ if (bestIdx > 0) {
+ if (isGeoProjection) {
+ switch (currentZoom) {
+ case 3:
+ case 4:
+ case 6:
+ case 7:
+ resolution = possibleResolutions[bestIdx - 1];
+ }
+ } else {
+ switch (currentZoom) {
+ case 1:
+ case 2:
+ case 4:
+ case 5:
+ resolution = possibleResolutions[bestIdx - 1];
+ }
+ }
+ }
+ return resolution.toString();
+};
+
+/*
+ * Retieves avtive layers by day
+ *
+ * @method getLayersForDay
+ * @private
+ *
+ * @param {array} array of layers
+ *
+ * @returns {array} array of layer ids
+ *
+ */
+export function imageUtilGetLayers (products, proj) {
+ var layers = [];
+ lodashEach(products, function(layer) {
+ if (layer.projections[proj].layer) {
+ layers.push(layer.projections[proj].layer);
+ } else {
+ layers.push(layer.id);
+ }
+ });
+ return layers;
+};
+/*
+ * Retieves opacities from palettes
+ *
+ * @method getOpacities
+ * @private
+ *
+ * @param {array} array of layers
+ *
+ * @returns {array} array of opacities
+ *
+ */
+export function imageUtilGetLayerOpacities (products) {
+ var opacities = [];
+ lodashEach(products, function(product) {
+ opacities.push((lodashIsUndefined(product.opacity)) ? 1 : product.opacity);
+ });
+ return opacities;
+};
+
+export function imageUtilEstimateResolution(resolution, isGeoProjection) {
+ return (isGeoProjection) ? resolution / POLAR_ESTIMATION_CONSTANT : resolution / GEO_ESTIMATION_CONSTANT;
+}
+export function imageUtilGetConversionFactor (proj) {
+ if (proj === 'geographic') return 0.002197;
+ return 256;
+};
+
+/*
+ * Retieves coordinates from pixel
+ *
+ * @method getCoords
+ * @private
+ *
+ * @returns {array} array of coords
+ *
+ */
+export function imageUtilGetCoordsFromPixelValues (pixels, map) {
+ return [
+ map.getCoordinateFromPixel([Math.floor(pixels.x), Math.floor(pixels.y2)]),
+ map.getCoordinateFromPixel([Math.floor(pixels.x2), Math.floor(pixels.y)])
+ ];
+};