Skip to main content

I want to use the PythonCaller to read a value from the header of an ECW file.

 

The value I am looking for is four characters long, starting at the position of 37.

 

I want to then print the value.

 

This is the code I have so far;

 

import fme

 

import fmeobjects

 

# Template Function interface:

 

# When using this function, make sure its name is set as the value of

 

# the 'Class or Function to Process Features' transformer parameter

 

def processFeature(feature):

 

 pass

 

 

 

# Template Class Interface:

 

# When using this class, make sure its name is set as the value of

 

# the 'Class or Function to Process Features' transformer parameter

 

class FeatureProcessor(object):

 

 def __init__(self):

 

  pass

 

 def input(self,feature):

 

  print ('start')

 

  n = feature.getAttribute('fme_dataset')

 

  print (n)

 

  f = open(n,'rb')

 

  f.seek(37,0)

 

  print(f.read(4))

 

  f.close()

 

  self.pyoutput(feature)

 

  print ('finish')

 

 def close(self):

 

  pass

 

 

 

But it does not return the value that I am expecting

 

I am making some progress

 

Here is new code

import fme

import fmeobjects

# Template Function interface:

# When using this function, make sure its name is set as the value of

# the 'Class or Function to Process Features' transformer parameter

def processFeature(feature):

  pass

 

# Template Class Interface:

# When using this class, make sure its name is set as the value of

# the 'Class or Function to Process Features' transformer parameter

class FeatureProcessor(object):

  def __init__(self):

    pass

  def input(self,feature):

    print ('start')

    file_name = feature.getAttribute('fme_dataset')

    print (file_name)

    ecw_file = open(file_name,'rb')

    ecw_file.seek(57,0)

    datum_string = ecw_file.read(9).decode('ascii')

    print('datum')

    print(datum_string)

    ecw_file.seek(73,0)

    projection_string = ecw_file.read(9).decode('ascii')

    print('projection')

    print(projection_string)

    ecw_file.close()

    print ('finish')

    self.pyoutput(feature)

  def close(self):

    pass

 


I am making some progress

 

Here is new code

import fme

import fmeobjects

# Template Function interface:

# When using this function, make sure its name is set as the value of

# the 'Class or Function to Process Features' transformer parameter

def processFeature(feature):

  pass

 

# Template Class Interface:

# When using this class, make sure its name is set as the value of

# the 'Class or Function to Process Features' transformer parameter

class FeatureProcessor(object):

  def __init__(self):

    pass

  def input(self,feature):

    print ('start')

    file_name = feature.getAttribute('fme_dataset')

    print (file_name)

    ecw_file = open(file_name,'rb')

    ecw_file.seek(57,0)

    datum_string = ecw_file.read(9).decode('ascii')

    print('datum')

    print(datum_string)

    ecw_file.seek(73,0)

    projection_string = ecw_file.read(9).decode('ascii')

    print('projection')

    print(projection_string)

    ecw_file.close()

    print ('finish')

    self.pyoutput(feature)

  def close(self):

    pass

 

Code improved with a "with" statement

import fme

import fmeobjects

# Template Function interface:

# When using this function, make sure its name is set as the value of

# the 'Class or Function to Process Features' transformer parameter

def processFeature(feature):

  pass

 

# Template Class Interface:

# When using this class, make sure its name is set as the value of

# the 'Class or Function to Process Features' transformer parameter

class FeatureProcessor(object):

  def __init__(self):

    pass

  def input(self,feature):

    print ('start')

    file_name = feature.getAttribute('fme_dataset')

    print ('file')

    print (file_name)

    with open(file_name,'rb') as ecw_file:

      print('datum')

      ecw_file.seek(57,0)

      datum_string = ecw_file.read(9).decode('ascii')

      print(datum_string)

      print('projection')

      ecw_file.seek(73,0)

      projection_string = ecw_file.read(9).decode('ascii')

      print(projection_string)

    print ('finish')

    self.pyoutput(feature)

  def close(self):

    pass


Your code only prints the character it read from the file, it does not send it back to the workspace. For this you can replace these two lines:

projection_string = ecw_file.read(9).decode('ascii')
print(projection_string)

With this single line:

feature.setAttribute('projection_string', ecw_file.read(9).decode('ascii'))

You'll have to manually expose the attribute "projection_string" in the PythonCaller.

 

PS, next time can you please post Python code using the Code Snippet formatting tool, it's much, much easier to read that way.


Reply