terça-feira, 10 de outubro de 2017

Generating Heatmap using FME

In this topic I describe how can use the Software FME to connect in the API from Strava and generate a Heatmap.

Last week I posted on twitter a short video demonstrating the heatmap generation using FME:





 The figure bellow shows this process:

Scenario:

 After a hard day of work, nothing better than running out.

 To monitor my Run i use the app from Strava. This app monitors the route by storing the informations: latitude and longitude, time, date, elevation, distance in KM, average speed, etc.

Once i finish my activity, the app salved my informations and this activity has a ID.

And now, how can i read the information my activity???

Do not P-A-N-I-C!

The Strava has a rich and documented API that enables us to access your activity.

To request your data using the API you need a access_token , that is possible to be obtained in Strava API Reference

https://strava.github.io/api/#access



The next Step it's in Software FME Desktop.

We build a Workspace to prepare to read the data originated by Strava and generate a Heatmap in python language.


Main points in Workspace:


  1. Use the transformer Creator to start your Workspace
  2. The  transformer HttpCaller is responsible to makes request to API Strava, using a URL and return in JSON all information your activitie respecting the id_activity amd access_token : https://www.strava.com/api/v3/activities/id_activity/streams/time,latlng,distance,altitude?access_token=xxxxxxxxxxxxxxxxxx.
Example JSON:


      3. Extract the informations lat and long from JSON and configure the transformer PytonCaller to receive these informations and generate the Heatmap.

      First we need to install a free library Heatmap into folder to site - packages in FME:

                                       fmepython27\lib\site-packages

     Installing the library heatmap using your IDE. I used the Pycharm to test this:

                                                        pip install heatmap

  Another action important to do is import a library responsible to work with different image file formats, the library PIL in the same folder to site - packages in FME.

                                                       pip install Pillow

    4. PythonCaller = Where the Magic happens
        
         Generating Heatmap:



     This sript in Python generate a KML with Heatmap and you can extract from this KML the Heatmap created



Conclusion: You can explore all possibilites with python using the transformers PytonCaller and PythonCreator.



See you on the next Post :)