Solved

Create a heatmap in FME using Python

  • 21 January 2016
  • 2 replies
  • 8 views

Userlevel 5
Badge +30

Hello all users,

I saw at Internet a library in Python that create a heatmap http://jjguy.com/heatmap/.

In my opinion doenst have a problem to import this module into FME. Somebody works in FME to create heatmap using the transformer PythonCreator?

Thanks in Advance,

icon

Best answer by david_r 21 January 2016, 16:02

View original

2 replies

Userlevel 5

Hi

I've never used it, but it shouldn't be too difficult. You'd probably want to install a full version of Python and define it as the FME interpreter before installing the heatmap module. You could then read your data using FME and send it into a PythonCaller that converts it into a heatmap object.

In your PythonCaller you will probably want to use the class definition to process your data. That way you can initialize the heatmap module and any global settings in the __init__ method, populate the data in the input method, and finally write out the heatmap itself in the close method.

Here's an (untested!) example based on the first sample shown on the website:

import fme
import fmeobjects
import heatmap


class FeatureProcessor(object):
    def __init__(self):
        # This method is called once before the first feature arrives
        
        # Initialize heatmap object
        self.hm = heatmap.Heatmap()
        # Initialize the list that is going to hold our heatmap points
        self.hm_points = []
        
    def input(self,feature):
        # This method is called for each feature that is sent into the PythonCaller
        
        # Get X,Y coordinates of our incoming feature (first vertex)
        vertex = feature.getCoordinate(0)
        # Store X,Y coordinate tuple to our list
        self.hm_points.append((vertex[0], vertex[1]))
        # Next line is optional, only necessary if you connect 
        # anything after this PythonCaller
        self.pyoutput(feature)
        
    def close(self):
        # This method is called once after the last feature has passed through
        
        # Generate the heatmap
        img = self.hm.heatmap(self.hm_points)
        # Save the heatmap to filename defined in published parameter HEATMAP_FILENAME
        img.save(FME_MacroValues['HEATMAP_FILENAME'])

David

Userlevel 5
Badge +30

Hi @david_r very thanks your help.

I will make theses configurations that you said me.

Thanks your attention

Reply