Solved

Using the FMe Objects API in Python to Raster

  • 26 February 2017
  • 2 replies
  • 6 views

Userlevel 4
Badge +30

Hello All,

Has someone been to use in the edition FME Deskto 2017 the API in Python to manipulate Raster files?

I need to create a example to share this news to my customer, but my Workspace has a Error when i write the code inside the transformer PythonCaller.

import fme
import fmeobjects


class FMERasterProperties(object):
    def __init__(self):
        pass
    def input(self,feature):
        begin = fmeobjects.FMERasterProperties()
        begin.getNumCols()
        
    def close(self):
        pass

Atttached my Workspace,   * the Reader can be any Raster file

icon

Best answer by takashi 26 February 2017, 04:52

View original

2 replies

Userlevel 2
Badge +17

Hi @danilo_inovacao, according to the API doc, constructor of the FMERasterProperties class requires multiple arguments, but you have passed no arguments to the constructor when creating a new instance of the class. It's the direct reason for the error.

If you intend to get the number of columns of the input raster, you can use an FMERasterProperties instance which is returned from the FMERaster.getProperties() method. e.g.

import fme
import fmeobjects

class FMERasterProperties(object):
    def __init__(self):
        pass
    def input(self, feature):
        raster = feature.getGeometry()
        if isinstance(raster, fmeobjects.FMERaster):
            properties = raster.getProperties()
            feature.setAttribute('_num_columns', properties.getNumCols())
        else:
            feature.setAttribute('_error', 'The geometry is not Raster.')
        self.pyoutput(feature)
        
    def close(self):
        pass

Although the API documentation has not been published on the Knowledge Center yet, you can find the doc with this path in your machine if you selected the option for installing SDK when installing FME 2017.0.

<FME 2017 HOME>/fmeobjects/python/apidoc/index.html

Userlevel 4
Badge +30

Hi @danilo_inovacao, according to the API doc, constructor of the FMERasterProperties class requires multiple arguments, but you have passed no arguments to the constructor when creating a new instance of the class. It's the direct reason for the error.

If you intend to get the number of columns of the input raster, you can use an FMERasterProperties instance which is returned from the FMERaster.getProperties() method. e.g.

import fme
import fmeobjects

class FMERasterProperties(object):
    def __init__(self):
        pass
    def input(self, feature):
        raster = feature.getGeometry()
        if isinstance(raster, fmeobjects.FMERaster):
            properties = raster.getProperties()
            feature.setAttribute('_num_columns', properties.getNumCols())
        else:
            feature.setAttribute('_error', 'The geometry is not Raster.')
        self.pyoutput(feature)
        
    def close(self):
        pass

Although the API documentation has not been published on the Knowledge Center yet, you can find the doc with this path in your machine if you selected the option for installing SDK when installing FME 2017.0.

<FME 2017 HOME>/fmeobjects/python/apidoc/index.html

Thanks @takashi your excellent explanation about this code.   :)

 

 

Reply