Skip to content
This repository was archived by the owner on Oct 7, 2019. It is now read-only.

Commit 919cd65

Browse files
committed
functioning prototype
1 parent ba3e4d6 commit 919cd65

File tree

1 file changed

+184
-121
lines changed

1 file changed

+184
-121
lines changed

project/static/js/map.js

+184-121
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,21 @@ function oauth(callback) {
4242

4343
$(document).on("ready", function() {
4444
$('.analysis-status').hide();
45+
$('#clear-button-item').hide();
46+
});
47+
48+
function resetAnalysis() {
49+
$('#analyze').prop("disabled", true);
50+
$('.analysis-status').hide();
51+
$('.analysis-status').empty();
52+
$('#clear-button-item').hide();
53+
drawnItems.clearLayers();
54+
drawInfo.update();
55+
}
56+
57+
$(document).on("click", "#clearCalcs", function() {
58+
console.log("clearing results");
59+
resetAnalysis();
4560
});
4661

4762
$("#about-btn").click(function() {
@@ -50,14 +65,13 @@ $("#about-btn").click(function() {
5065
return false;
5166
});
5267

53-
5468
/*******************************************************************************
5569
* create map with basemap and supplemental layers
5670
*/
5771
var map = L.map('map').setView([42.0170202, -73.9144284], 18);
5872

59-
//L.esri.basemapLayer('Imagery').addTo(map);
60-
//L.esri.Vector.basemap('Hybrid').addTo(map);
73+
L.esri.basemapLayer('Imagery').addTo(map);
74+
L.esri.Vector.basemap('Hybrid').addTo(map);
6175

6276
var layer_streams = L.esri.featureLayer({
6377
url: "https://services.arcgis.com/vT1c5Cjxbz2EbiZw/arcgis/rest/services/BardMicroHydro/FeatureServer/7",
@@ -126,8 +140,6 @@ drawInfo.onAdd = function(map) {
126140
drawInfo.addTo(map);
127141

128142

129-
var analyzeButton = '<div><button id="analyze" class="btn btn-primary btn-block" type="submit" disabled>Calculate</button></div>';
130-
131143
function makeAlert(msg, alertType) {
132144
var defaultMsg = null;
133145
if (alertType == 'info') {
@@ -149,6 +161,10 @@ function makeAlert(msg, alertType) {
149161
}
150162

151163
}
164+
165+
var analyzeButton = '<div><button id="analyze" class="btn btn-primary btn-block" type="submit" disabled>Calculate</button></div>';
166+
var clearButton = '<div><button id="clearCalcs" class="btn btn-primary btn-block" type="submit">Clear Results</button></div>';
167+
152168
/**
153169
* Analysis Control
154170
* Contains button and subsequent results outputs from the tool.
@@ -163,13 +179,14 @@ var analyzeControl = L.control.custom({
163179
content:
164180
'<li class="list-group-item" id="analyze-button-item">' + analyzeButton + '</li>' +
165181
'<li class="list-group-item analysis-status" id="msg-status"></li>' +
166-
'<li class="list-group-item analysis-status" id="msg-text"></li>',
182+
'<li class="list-group-item analysis-status" id="msg-text"></li>' +
183+
'<li class="list-group-item" id="clear-button-item">' + clearButton + '</li>',
167184
classes: 'list-group',
168185
id: "analyze-control",
169186
style: {
170187
margin: '10px',
171-
padding: '0px 0 0 0',
172-
cursor: 'pointer',
188+
padding: '0px 0 0 0'
189+
//cursor: 'pointer',
173190
}
174191
})
175192
.addTo(map);
@@ -359,147 +376,193 @@ HydroPowerClass.prototype.buildProfileGraphic = function() {
359376

360377
/**
361378
* instantiate the hydropower object
362-
*/
363-
var hp = HydroPowerClass();
379+
*/
380+
hp = HydroPowerClass();
364381

365382
/**
366383
* run the two GP tasks at once
367384
*/
368-
var runGP = function (hp) {
369-
370-
var elevProfileStatus = $.Deferred();
371-
var elevProfileService = L.esri.GP.service({
372-
url: "http://elevation.arcgis.com/arcgis/rest/services/Tools/ElevationSync/GPServer/Profile",
373-
useCors: true,
374-
token: arcgis_token
375-
});
376-
var elevProfileTask = elevProfileService.createTask();
377-
378-
var watershedStatus = $.Deferred();
379-
var watershedService = L.esri.GP.service({
380-
url: "http://hydro.arcgis.com/arcgis/rest/services/Tools/Hydrology/GPServer/Watershed",
381-
useCors: true,
382-
token: arcgis_token
383-
});
384-
var watershedTask = watershedService.createTask();
385-
385+
var runGP = function () {
386+
var e = $.Deferred();
387+
var w = $.Deferred();
388+
386389
/**
387390
* run the Elevation Profile service
388391
*/
389-
elevProfileTask.on('initialized', function() {
390-
// set input parameters
391-
elevProfileTask.setParam("DEMResolution ", "FINEST");
392-
elevProfileTask.setParam("ProfileIdField", "OID");
393-
elevProfileTask.setParam("MaximumSampleDistance", 50000);
394-
elevProfileTask.setParam("returnZ", true);
395-
// Input must be an L.LatLng, L.LatLngBounds, L.Marker or GeoJSON Point Line or Polygon object
396-
elevProfileTask.setParam("InputLineFeatures", drawnPolyline.toGeoJSON());
397-
// update status
398-
hp.status.profile = "Elevation initialized. Submitting Request...";
399-
console.log(hp.status.profile);
400-
// run the task
401-
elevProfileTask.run(function(error, result, response) {
402-
if (error) {
403-
hp.status.profile = "Elevation Profile: " + error.message + "(code:" + error.code + ")";
404-
$('#msg-status').html(makeAlert(hp.status.profile, 'danger'));
405-
console.log(error.details);
406-
elevProfileStatus.resolve();
407-
} else {
408-
// messages
409-
hp.status.profile = "Elevation Profile: Complete";
410-
console.log(hp.status.profile);
411-
console.log(result);
412-
// update the class
413-
hp.profile = result;
414-
// resolve callback
415-
elevProfileStatus.resolve();
416-
}
392+
//function runElevProfileGP() {
393+
var elevProfileService = L.esri.GP.service({
394+
url: "http://elevation.arcgis.com/arcgis/rest/services/Tools/ElevationSync/GPServer/Profile",
395+
useCors: true,
396+
token: arcgis_token
417397
});
418-
});
398+
var elevProfileTask = elevProfileService.createTask();
399+
elevProfileTask.on('initialized', function() {
400+
// set input parameters
401+
elevProfileTask.setParam("DEMResolution ", "FINEST");
402+
elevProfileTask.setParam("ProfileIdField", "OID");
403+
elevProfileTask.setParam("MaximumSampleDistance", 50000);
404+
elevProfileTask.setParam("returnZ", true);
405+
// Input must be an L.LatLng, L.LatLngBounds, L.Marker or GeoJSON Point Line or Polygon object
406+
elevProfileTask.setParam("InputLineFeatures", drawnPolyline.toGeoJSON());
407+
// update status
408+
console.log("Elevation initialized. Submitting Request...");
409+
// run the task
410+
elevProfileTask.run(function(error, result, response) {
411+
if (error) {
412+
msg = "Elevation Profile: " + error.message + "(code:" + error.code + ")";
413+
$('#msg-status').html(makeAlert(msg, 'danger'));
414+
console.log(error.details);
415+
e.resolve(error);
416+
} else {
417+
// messages
418+
msg = "Elevation Profile: Complete";
419+
console.log(msg);
420+
console.log(result);
421+
422+
// resolve callback
423+
e.resolve(result);
424+
}
425+
});
426+
});
427+
//}
419428

420429
/**
421430
* run the Watershed service
422-
*/
423-
watershedTask.on('initialized', function() {
424-
// InputPoints must be an L.LatLng, L.LatLngBounds, L.Marker or GeoJSON Point Line or Polygon object
425-
watershedTask.setParam("InputPoints", drawnPoint.toGeoJSON());
426-
watershedTask.setParam("SourceDatabase", "FINEST");
427-
watershedTask.setParam("PointIDField", "OID");
428-
watershedTask.setParam("SnapDistance", 500);
429-
watershedTask.setParam("Generalize", true);
430-
watershedTask.setParam("ReturnSnappedPoints", false);
431-
// output parameters (required for an async GP service)
432-
var outputWatershedArea, outputSnappedPoints;
433-
watershedTask.setOutputParam("WatershedArea");
434-
//watershedTask.setOutputParam("SnappedPoints");
435-
watershedTask.gpAsyncResultParam("WatershedArea", outputWatershedArea);
436-
//watershedTask.gpAsyncResultParam("SnappedPoints", outputSnappedPoints);
437-
hp.status.watershed = "Watershed initialized. Submitting Request...";
438-
console.log(hp.status.watershed);
439-
$('#analyze').prop("disabled", false);
431+
*/
432+
//function runWatershedGP () {
433+
var watershedService = L.esri.GP.service({
434+
url: "http://hydro.arcgis.com/arcgis/rest/services/Tools/Hydrology/GPServer/Watershed",
435+
useCors: true,
436+
token: arcgis_token
437+
});
438+
var watershedTask = watershedService.createTask();
439+
watershedTask.on('initialized', function() {
440+
// InputPoints must be an L.LatLng, L.LatLngBounds, L.Marker or GeoJSON Point Line or Polygon object
441+
watershedTask.setParam("InputPoints", drawnPoint.toGeoJSON());
442+
watershedTask.setParam("SourceDatabase", "FINEST");
443+
watershedTask.setParam("PointIDField", "OID");
444+
watershedTask.setParam("SnapDistance", 500);
445+
watershedTask.setParam("Generalize", true);
446+
watershedTask.setParam("ReturnSnappedPoints", false);
447+
// output parameters (required for an async GP service)
448+
var outputWatershedArea;
449+
watershedTask.setOutputParam("WatershedArea");
450+
watershedTask.gpAsyncResultParam("WatershedArea", outputWatershedArea);
451+
//var outputSnappedPoints;
452+
//watershedTask.setOutputParam("SnappedPoints");
453+
//watershedTask.gpAsyncResultParam("SnappedPoints", outputSnappedPoints);
454+
console.log("Watershed initialized. Submitting Request...");
455+
$('#analyze').prop("disabled", false);
456+
457+
watershedTask.run(function(error, result, response) {
458+
//console.log(response);
459+
if (error) {
460+
// messages
461+
msg = "Watershed: " + error.message + "(code:" + error.code + ")";
462+
console.log(msg);
463+
console.log(error);
464+
// update alert
465+
$('#msg-status').append(makeAlert(msg, 'danger'));
466+
// resolve callback
467+
w.resolve(error);
468+
} else {
469+
// messages
470+
msg = "Watershed: Complete";
471+
console.log(msg);
472+
console.log(result);
473+
// resolve callback
474+
w.resolve(result);
475+
}
476+
});
440477

441-
watershedTask.run(function(error, result, response) {
442-
//console.log(response);
443-
if (error) {
444-
// messages
445-
hp.status.watershed = "Watershed: " + error.message + "(code:" + error.code + ")";
446-
console.log(hp.status.watershed);
447-
console.log(error);
448-
// update alert
449-
$('#msg-status').append(makeAlert(hp.status.watershed, 'danger'));
450-
// resolve callback
451-
watershedStatus.resolve();
452-
} else {
453-
// messages
454-
hp.status.watershed = "Watershed: Complete";
455-
console.log(hp.status.watershed);
456-
console.log(result);
457-
// update the class
458-
hp.watershed = result;
459-
// resolve callback
460-
watershedStatus.resolve();
461-
}
462478
});
463-
});
464-
// return the deferral
465-
return $.Deferred(function (def) {
466-
$.when(elevProfileStatus, watershedStatus).done(function () {
467-
def.resolve();
479+
//}
480+
481+
/* when runGP runs, it first hits $.Deferred. The first argument is a function
482+
* that executes before anything else. That argument is a $.when function,
483+
* which runs the two functions defined above. When done, the results
484+
* are passed into a callback function that resolves the whole thing and
485+
* returns the two results objects from runGP
486+
*/
487+
var x = $.Deferred(function (def) {
488+
// elevProfileResult, watershedResult
489+
$.when(e, w).done(function (eR,wR) {
490+
def.resolve(eR,wR);
468491
});
469492
});
470-
493+
return x;
494+
471495
};
472496

473-
var analyzeGPResults = function() {
474-
//post process the GP service results with these two methods:
475-
hp.getArea();
497+
var analyzeGPResults = function(elevProfileResult,watershedResult) {
498+
console.log("Analyzing GP Results...");
499+
console.log(elevProfileResult);
500+
console.log(watershedResult);
501+
502+
/*post process the GP service results with these two methods:
503+
hp.profile = elevProfileResult;
476504
hp.calcHead();
477-
// add the results to our message area
478-
$('#msg-text').append('<h4>Area</h4><p>' + hp.area + ' km^2</p>');
479-
$('#msg-text').append('<h4>Head</h4><p>' + hp.head + ' m</p>');
480-
// use stored data to run calculation
505+
hp.watershed = watershedResult;
506+
hp.getArea();
481507
hp.calcPower(null,null);
482-
$('#msg-text').append('<h3>kW</h3><p>' + hp.power + ' m</p>');
483-
$('#msg-status').html(makeAlert(null,'success'));
484-
$('#analyze').prop("disabled", false);
508+
*/
509+
510+
// get the area value, which is buried in the service result object
511+
var area = watershedResult.WatershedArea.features[0].properties.AreaSqKm;
512+
$('#msg-text').append('<h4>Area</h4><p>' + _round(area,2) + ' km^2</p>');
513+
514+
//get the line from the result object
515+
var line = elevProfileResult.OutputProfile.features[0];
516+
// get the coords from the line
517+
var coords = line.geometry.coordinates;
518+
// get the z values from the first and last coordinate
519+
var firstZ = coords[0][2];
520+
var lastZ = coords[coords.length - 1][2];
521+
// save the difference
522+
var head = lastZ - firstZ;
523+
// check result. It must be a positive number
524+
525+
$('#msg-text').append('<h4>Head</h4><p>' + _round(head,2) + ' m</p>');
526+
527+
528+
// calculate power
529+
var Qavail = (area * 1.6);
530+
//where x is a range from 0.1 to 0.5 with default value of 0.3 (edited)
531+
var x = 0.3;
532+
var Qenv = (area * x);
533+
var Quseable = Qavail - Qenv;
534+
//Power in kW; where e is a variable with default value 0.7 (edited)
535+
var e = 0.7;
536+
var p = Quseable * head * (0.084) * e;
537+
var power = _round(p, 2);
538+
539+
if (head < 0) {
540+
$('#msg-status').html(makeAlert("The head calculation returned a negative value. Make sure the line was drawn downstream&rarr;upstream.", 'danger'));
541+
} else {
542+
$('#msg-status').html(makeAlert(null,'success'));
543+
}
544+
545+
$('#msg-text').append('<h3>Power Potential:</h3><h4>' + power + ' kW/year</h4>');
546+
$('#clear-button-item').show();
547+
$('#analyze').prop("disabled", true);
485548
};
486549

487550
/*******************************************************************************
488551
* Analyze Button - Action
489552
*/
490553

491554
$('#analyze').click(function() {
492-
/**
493-
* reset the messages
494-
*/
495-
$('.analysis-status').show();
496-
$('#msg-text').html();
497-
// set tthe status to
555+
556+
// reset the messages
557+
$('#analyze').prop("disabled", true);
558+
$('.analysis-status').hide();
559+
$('.analysis-status').empty();
560+
$('#clear-button-item').hide();
561+
// set the new status to
498562
$('#msg-status').html(makeAlert(null,'info'));
563+
$('.analysis-status').show();
499564

500-
/**
501-
* print geometries to console
502-
*/
565+
// print geometries to console
503566
console.log(drawnPolyline.toGeoJSON());
504567
console.log(drawnPoint.toGeoJSON());
505568

0 commit comments

Comments
 (0)