river level plugin calculator
  

Using the environment agency river level API

As a keen rower in York, our river level can vary dramatically. Therefore I've developed a plugin which pulls the river level from the environment agency and gives the club information on the river level's impact on whether they can row. This is a walkthrough of how this was done step by step and links to see it in action and the end result.

- 4 minutes read time

As a keen rower in York, our river level can vary dramatically. Therefore I’ve developed a plugin which pulls the river level from the environment agency and gives the club information on the river level’s impact on whether they can row.

The issue

I created a small link to the environment API to gather river level data and display on the club’s website. You can read more about why here.

The key bits of code

You can see the full code [here][codepen] which also shows the how we formatted the information.

Calling the API

var environmentJSON;
$.getJSON("[API link goes here]", function(json){
    environmentJSON = json;
    });

If you’re wondering which key I used, it was this one.

Checking if the river is rising or falling

Inside we can do our first check on whether the level is rising or not.

// collect current reading
  var currentReading = json["items"][0]["value"];

  // collect previous reading
    var previousReading = json["items"][1]["value"];

  // decides whether the river is falling, rising or level

  if (currentReading > previousReading) {
    var riverStatus = 'rising';
  } else if (currentReading < previousReading) {
    var riverStatus = 'falling';
  } else {
    var riverStatus = 'static';
  }

Then we can play back the results.

 document.getElementById('currentRiverStatus').innerHTML = '<p>'
 + 'The latest river level is ' + currentReading
 + ' m and was taken at  ' + json["items"][0]["dateTime"].substring(11,16)
 + '. ' + riverPosition + ' and is currently ' + riverStatus + '.'
 + '</p>' + '<p> Current recommendation: ' + crewBoats + '</p>';

Checking the height against the rules

Here we use a long but simple if / else statement to set variables of where the river is in relation to the bank and also what crews can go out on the water. (Note only some of the if statement is copied here).

  if (currentReading > 4.17) {
    var riverPosition = 'River is in the boathouse';
    var crewBoats = 'No boats should be out on the water.';
  } else if (currentReading > 3.68) {
    var riverPosition = 'The river is 2 steps from the road';
    var crewBoats = 'No boats should be out on the water.';
  } else if (currentReading > 2.91) {
    var riverPosition = 'The river is 7 steps from the road';
    var crewBoats = 'Only senior boats during the day can be out on the water.';
  } else if (currentReading > 2.75) {
    var riverPosition = 'The river is 11 steps above the tow-path';
  } else if (currentReading > 1.06) {
    var riverPosition = 'The river is level just over the towpath';
  } else if (currentReading > 0.95) {
    var riverPosition = 'The river is level with the towpath';
    var crewBoats = 'No boat restrictions at this time.';
  } else if (currentReading < 0.21) {
    var riverPosition = 'The river is over 4 steps below the towpath';
    var crewBoats = 'No boat restrictions at this time.';
  }

Outcome

Here’s a screenshot of it from the YCRC website.

Screenshot of the calculator live on the site

So what’s next?

Now the concept has been proved by showing the codepen results on the site, the next steps are to create a wordpress plugin, so that not only can it be easily amended by the club going forward, but it can also be used by other clubs.

Expect future posts when I tackle that!

The Archives

Latest Posts

How I designed my first 3d model

3d modelling can feel like a daunting ask, especially when you see some of the amzing designs online. But it doesn't have to be. Here's how I made a case for a pi zero and add on HAT.

- 6 minutes read time

Has a 3d printer been worth it?

I can't believe it's been two years with my Prusa mini. An amazing introduciton into 3d printing it's included plenty of ups and a few downs. Images, learns and fun reflections.

- 4 minutes read time

The Archives