John Trengrove

home posts about

Visualising my location activity

05 Apr 2015

I've talked about Moves before, it is an app to track your location using your smartphone. I wanted to show the use of this data. As an example I created the heatmap above which shows the number of steps I was taking per hour over one week.

To export the data from Moves you can use this JSON exporter. After exporting I selected one week of data using the following jq command.

cat moves-export.json | jq '.[2], .[3], .[4], .[5], .[6], .[7], .[8]' > one-week.json

Note that you will have to do some cleaning of the data to get it into a proper object.

I then wrote a small script to calculate how many steps I took each hour.

var moment = require('moment');
var data = require('./one-week.json');

var t = {};

data.forEach(function(d) {
  var segments = d.segments;
  segments.forEach(function(s) {
    var activities = s.activities;
    activities.forEach(function(a) {
      if (a.activity == "wlk") {
        var timeStamp = moment.utc(
          a.startTime.replace(/[0-9]{4}Z/,''),
          "YYYYMMDDTHH").unix().toString();
        if (a.steps) {
          t[timeStamp] = (t[timeStamp]) ? t[timeStamp] + a.steps : a.steps;
        }
      }
    });
  });
});

console.log(JSON.stringify(t));

To display the date I used the cal-heatmap library. Most of the effort was in converting the data to the right format in the previous script. The cal-heatmap library is really useful and would be a good part of any quantified self dashboard.

var calendar = new CalHeatMap();
calendar.init({
  data: "/data/moves.json",
  start: new Date(2014, 9, 9),
  domain : "day",
  subDomain : "hour",
  itemName: ["step","steps"],
  range : 7,
});