Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Subbasin NHD+ Table #2794

Merged
merged 7 commits into from Apr 24, 2018
Merged

Add Subbasin NHD+ Table #2794

merged 7 commits into from Apr 24, 2018

Conversation

arottersman
Copy link

@arottersman arottersman commented Apr 20, 2018

Overview

Similar to #2781 where we got HUC-12 details — get catchment details and geometries and display them in the new catchment table. Also clean up some map-timing bugs.

Connects #2730
Connects #2791

Demo

screen shot 2018-04-20 at 5 34 31 pm

screen shot 2018-04-20 at 5 34 21 pm

Testing Instructions

  • Pull and bundle
  • In subbasin view, select a huc-12 and confirm the catchment table is populated
  • Confirm if you select the same huc-12 the catchment details request doesn't execute again
  • Toggle between the various views in various states (eg Analyze toggle while viewing catchments, exit attenuated and then view again). Confirm there are no errors in the console and nothing breaks on the second viewing.

Add an endpoint similar to the subbasin huc12s endpoint
to fetch the id, area, shape, and streams for a set of
catchment ids.

GET
/mmw/modeling/subbasins/catchments/?catchment_comids=%255B%25224780661%2522%252C%25224780663%2522%252C%25224780665%2522%252C%25224780751%2522%252C%25224782293%2522%252C%25224782295%2522%252C%25224782297%2522%252C%25224782307%2522%255D

-- where `catchment_comids` is a urlencoded json array

Response:
[
    {
        "id": 423312 // comid
        "shape": { "type": "MultiPolygon", ...}, // catchment geojson
        "stream": { "type": "MultiLineString", ...}, // stream geojson
        "area": 4.3111 // area in hectares
    }, ...
]
@arottersman arottersman changed the title wip Arr/subbasin catchment table Add Subbasin NHD+ Table Apr 20, 2018
@rajadain
Copy link
Member

Looks like the HUC / Catchments detail table isn't being initialized correctly anymore:

image

It is initialized correctly on develop / staging:

image

There may be a missing .bootstrapTable() call somewhere. Will see if I can find it.

@arottersman
Copy link
Author

Hm, I noticed that intermittently before I moved that bootstrap init call to onRender. I'd wager we still need it onAttach and need to be a bit more clever about where we call it again on re-render.

Copy link
Member

@rajadain rajadain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 great work. This works really well on the whole.

Sending the list of catchments as a URL encoded JSON array is an interesting choice, but it works so I'm not going to challenge it right now 🚀 🐦 .

Code comments are minor suggestions, all optional. The only thing we should fix before merging is the .bootstrapTable() calls so that they always work.


fetchCatchmentsIfNeeded: function(comids) {
var catchments = this.get('catchments');
if (!catchments.isEmpty() || this.fetchCatchmentsPromise) { return; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer returning an immediately resolved promise rather than nothing:

return $.when();

if (!catchments.isEmpty() || this.fetchCatchmentsPromise) { return; }

var encodedComids = encodeURIComponent(JSON.stringify(comids));
this.fetchCatchmentsPromise = catchments.fetch({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, prefer returning this promise rather than losing the reference. This allows the possibility of usage like:

subbasins.fetchCatchmentsIfNeeded.then(function() { ... });

if (this.catchmentDetails.isEmpty()) {
self.listenToOnce(self.catchmentDetails, 'add', this.setupCatchmentDetails, this);
} else {
this.setupCatchmentDetails();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling render within initialize feels like a mistake ... but I'm not seeing any errors in the console. It is possible that this is the cause of the broken bootstrap table. Consider moving it to a custom function and calling that directly here, and also from within onRender.

@rajadain rajadain assigned arottersman and unassigned rajadain Apr 23, 2018
@arottersman
Copy link
Author

Great suggestions re: promises. I've pushed one fixup to address the fetching, and another for the table renders (I believe the issue is fixed now though it doesn't look all that pretty).

Sending the list of catchments as a URL encoded JSON array is an interesting choice, but it works so I'm not going to challenge it right now

Definitely with you on this — I wanted it to stay a GET request like its HUC-12 sibling, and for some reason didn't want to consider how the list should come in. Encoding a JSON array happens easily and predictably so it seems okay for now! 🚀 🐦

Copy link
Member

@rajadain rajadain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 this works great! The bootstrapTable is rendered correctly every time. I did add comment for a slight adjustment to the promises, but after that this should be ready to merge. Nice work!

@@ -825,14 +825,17 @@ var SubbasinDetailModel = Backbone.Model.extend({

fetchCatchmentsIfNeeded: function(comids) {
var catchments = this.get('catchments');
if (!catchments.isEmpty() || this.fetchCatchmentsPromise) { return; }
if (!catchments.isEmpty() || this.fetchCatchmentsPromise) {
return $.when();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I missed this earlier, but if this.fetchCatchmentsPromise exists, we should probably return that instead of $.when():

if (!catchments.isEmpty() || this.fetchCatchmentsPromise) {
  return this.fetchCatchmentsPromise || $.when();
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that's my bad. I'll fix before merging.

Alice Rottersman added 6 commits April 24, 2018 13:49
When entering the HUC-12-level view, fetch
the catchment details if they have not already
been fetched. This provides us the catchment area to
eventually calculate the rates, as well as the geometries.
Add template and view to display catchment loads for
an active HUC-12 in subbasin view.
The huc-12 totals table re-renders after the huc12 details
change. After every re-render the bootstrap table must be
reinitialized.
Fix a bug where if you switched to Analyze or Model views
while you were viewing a HUC-12 in subbasin mode, the shapes
would not get re-added to the map after coming back to the subbasin
view.
Use the helper function to clear the subbasin shapes
when exiting subbasin mode instead of clearing the collection.
The helper function removes the map listeners, which would otherwise
try to fire on removed layers.
The huc12 view should only be rendered if there's
an active huc12 to populate it with.
@arottersman
Copy link
Author

@azavea-bot rebuild

@arottersman arottersman merged commit 43c0465 into develop Apr 24, 2018
@arottersman arottersman deleted the arr/subbasin-catchment-table branch April 24, 2018 18:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants