Question

Python script to modify output file (ECW image file)


Badge +10

How would I build a workspace with an ECW Writer transformer, followed by a Python script that modifies the ECW image file after it has been written?


9 replies

Badge +10

My progress so far is;

FeatureWriter (instead of Writer) to write the ECW (version 2) file

ListExploder so that count and name are now attributes

AttributeCreator to concatenate _dataset and name to generate the name of the ecw file

PythonCaller

Where I am stuck is the Python to use to explicitly set the values of Datum and Projection in the header of the (version 2) ECW file

Userlevel 1
Badge +10

My progress so far is;

FeatureWriter (instead of Writer) to write the ECW (version 2) file

ListExploder so that count and name are now attributes

AttributeCreator to concatenate _dataset and name to generate the name of the ecw file

PythonCaller

Where I am stuck is the Python to use to explicitly set the values of Datum and Projection in the header of the (version 2) ECW file

Hi @nicholas​. I think you're on the correct path if you want to use Python. You can also try using a shutdown script to do post-process your ECW using Python. I wondering why the header isn't being populated with the coordinate system info with FME? Have you tried doing the following as daveatsafe suggested here using an EPSG number to define your coordinate system?

Right-click on the output ECW feature type and choose Properties:Screen Shot 2022-06-09 at 3.05In the dialog that opens, set Use EPSG Code for Projection to Always:

Screen Shot 2022-06-09 at 3.05If this isn't working, would you mind sharing a simplified workspace (if you Save As Template, you can package up you source data with your workspace as a .fmwt file) to help demonstrate what's happening?

 

Badge +10

Hi @nampreetatsafe​, I want to write a version 2 ECW with a CRS of MGA2020-56_FME

The header of the ECW (that FME has written) has two values, one for datum and another for projection

datum: EPSG:1168

projection: SUTM56  

When I open this in QGIS, and examine the properties of the layer, QGIS does not recognise the CRS "Coordinate Reference System unknown"

I can manually change the value for projection to EPSG:7856

Then, QGIS does recognise the CRS properly as GDA2020 / MGA Zone 56

So, I am trying to write a python script that will explicitly write the value "EPSG:7856" to the header of the (version 2) ECW

I think that in python I need to;

open the ecw

seek the correct position in the file

write the value "EPSG:7856"

close the ecw

But I do not know how to write this properly

Badge +10

I do have a business need for the version 2 ECW.

When I choose version 2, the option to Use EPSG code for projection is not there

Badge +10

Hi @nampreetatsafe​ I have uploaded a fmwt here, as you suggested

Badge +10

Here is the code in my PythonCaller

 

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')

    # f = open('G:\\Geospatial\\GIS\\nick\\2022\\07\\ECW\\out\\SW_388000_8012000_1k.ecw', 'wb')

    # f.seek(74,0)

    # f.write('EPSG:7856')

    # f.close()

    print('finish')

    self.pyoutput(feature)

  def close(self):

    pass

 

I don't know how to make it work. I know I need to open, then seek, then write, then close, but how? How do I open the ECW file?

Badge +10

Hi @nampreetatsafe​ did you look at my fmwt?

Userlevel 2
Badge +10

Hi @nicholas​ if you want the EPSG code to be written out with the file, then you will need to use ECW Version 3 in the Writer parameters. Unfortunately, this setting is not available when using Version 2 in the Writer parameters. Additionally, even writing out with the writer set to Version 3, I can see that EPSG:7856 has not been verified to work with Erdas Products .

 

If you're looking to edit the ECW file manually, you could try reading the output ECW file in your workspace with the Text File Reader to read the file as raw text, and then use a StringReplacer to replace the EPSG:1168 string with EPSG:7856.

image

Badge +10

I think I have got it working

Code in PythonCaller

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')

    ecw_path = feature.getAttribute('_dataset')

    ecw_name = feature.getAttribute('name')

    file_name = ecw_path+'\\\\'+ecw_name+'.ecw'

    print ('file')

    print (file_name)

    with open(file_name,'r+b') as ecw_file:

    #  pass

      print('set datum to EPSG:1168')

      datum_string = 'EPSG:1168'

      datum_binary = datum_string.encode()

      ecw_file.seek(57,0)

      ecw_file.write(datum_binary)

      print('set projection to EPSG:7856')

      projection_string = 'EPSG:7856'

      projection_binary = projection_string.encode()

      ecw_file.seek(73,0)

      ecw_file.write(projection_binary)

    print ('finish')

    self.pyoutput(feature)

  def close(self):

    pass

Reply