Hexagon Geospatial
MENU

ERDAS IMAGINE

Discuss and share topics of interest using ERDAS IMAGINE the world’s leading geospatial data authoring system.
Showing results for 
Search instead for 
Do you mean 
Reply
Contributor
Posts: 74
Registered: ‎11-02-2015
Accepted Solution

Load an exisiting Model and change port values within Python

Hi there,

 

I spent some time to find a solution but finally I failed with a quite simple task:

 

The goal is to load an existing model within Python and adjust at least the filenames for one Raster Input and one Raster output operator.

 

Loading the model works fine:

 

 

from imagine import modeler
# Load a saved model
modelFile = 'e:/tmp/test.gmdx'
m = modeler.Solution.Load(modelFile)

But I have found no way to set the values for Filename port the Raster Input and the Raster Outpur operator directly nor by adding two "Port Input" operators.

 

 

Reading the values is described in the online help and also works fine here:

 

from imagine import modeler
# Load a saved model
modelFile = 'e:/tmp/test.gmdx'
m = modeler.Solution.Load(modelFile)
for p in m.GetPorts():
    print(p.name + ' = ' + str(p.data))

But I have no idea how to set the data value for the specific port. Any help is more than welcome

 

 

Cheers

Fritz

 

P.S. the used model should be attached to this post

 

 

 

Geography is what geographers do...
Highlighted
Frequent Contributor
Posts: 83
Registered: ‎01-20-2016

Re: Load an exisiting Model and change port values within Python

Hi Fritz,

 

I could address one Port by using index:

print(m.GetPorts()[0])

It gives me the result:

Port "InFile"
   name:             InFile
   displayName:      InFile
   description:      
   supportedTypes:   ['File']
   operator:         N/A
   input:            True
   output:           False
   private:          False
   hidden:           False
   optional:         False
   aliased:          True
   portType:         input
   data:             e:/demos/apollo_wps/change detection/subset_tm_00.img

 

then it is possible to change properties e.g.

m.GetPorts()[0].name = 'Infile2'

or

m.GetPorts()[0].data = 'E:/temp/test.img'

 

The result afterwards using print(m.GetPorts()[0]) is:

Port "Infile2"
   name:             Infile2
   displayName:      InFile
   description:      
   supportedTypes:   ['File']
   operator:         N/A
   input:            True
   output:           False
   private:          False
   hidden:           False
   optional:         False
   aliased:          True
   portType:         input
   data:             E:/temp/test.img

 

Hope this helps Smiley Happy

Ruth

 

 

 

 

Contributor
Posts: 74
Registered: ‎11-02-2015

Re: Load an exisiting Model and change port values within Python

Thanks Ruth,

 

due to your hints my script is working perfectly now. Great!

 

Cheers

Fritz

Geography is what geographers do...
Contributor
Posts: 74
Registered: ‎11-02-2015

Re: Load an exisiting Model and change port values within Python

[ Edited ]

Hi,

 

thanks to Ruth I now know how to get the array of all ports. In order to be a little bit more flexible I now pick the needed port by name. At this point I am able to set the filename used by this port: 

 

# Import Modeler module 
from imagine import modeler

# Load a saved model
modelFile = 'e:/tmp/test.gmdx'
m = modeler.Solution.Load(modelFile)

# Find a InFile port
InFile = next(port for port in m.GetPorts() if port.name == "InFile")
# Change Filename for InFile Port
InFile.data = 'e:/demos/apollo_wps/change detection/subset_tm_007.img'

# Find a OutFile port
OutFile = next(port for port in m.GetPorts() if port.name == "OutFile")
# Change Filename for OutFile Port
OutFile.data = 'E:/tmp/test007.img'

#Display Port Values
print ('InFile: ' + InFile.data + '\n' + 'OutFile: ' + OutFile.data)

 

I think this might be helpful for anybody out there.

 

Cheers

Fritz

Geography is what geographers do...
Do you need immediate support?
If you encounter a critical issue and need immediate assistance please submit a Service Request through our Support Portal.