Friday, August 3, 2012

Example Python Code from ERDAS IMAGINE's Upcoming Spatial Modeler


The chief Python developer, Fengliang provided me an example Python script that will run a resolution merge (pan sharpen) in the upcoming Spatial Modeler. This example model (below) runs outside of the ERDAS IMAGINE User Interface, indeed ERDAS IMAGINE is not running at all when the Py scipt is launched. ERDAS IMAGINE is installed and licensed, but not running.

The Py script finds all the ERDAS IMAGINE Spatial Modeler functions, and runs the model. This model runs 100% in the background. The ERDAS IMAGINE UI never launches. If the user wants to, they can run the model, start the UI and display the results as a real-time preview in the Viewer. Note, the preview is realtime modeling to screen, like ERDAS ER Mapper Algorithms. When using preview, a new file is never actually created. (But you can create one when you need to!) 

This example Py script, and others will be provided when the software is released.

-------------------------------------------

def Sharpen( model, band, summary, panband ):
      return model.CastToFloat( model.Multiply( model.Divide(band, summary), panband))

# import erdas python module for spatial modeler
from erdas import modeler

# create a process
m = modeler.Model();

# create input operators
pan = m.RasterInput( "E:/Demo/Brovey/le7039035000009250_pan.img", "Float", "Nearest Neighbor");
multispectral = m.RasterInput("E:/Demo/Brovey/le7039035000009250_multi.img", "Float", "Nearest Neighbor");

# add three bands together
r = m.SelectBand(multispectral, '4:4');
g = m.SelectBand(multispectral, '3:3');
b = m.SelectBand(multispectral, '2:2');
sum = m.Add(1, r, g, b)

# calculate R, G, B bands for stacklayer
stack = m.StackLayers( Sharpen( m, r, sum, pan ), Sharpen( m, g, sum, pan ), Sharpen( m, b, sum, pan ) )

# to output operator
m.RasterOutput(stack, "E:/Demo/Brovey/output/brovey-output-python-6.img", "u8", Thematicity=modeler.Thematicity.continuous)

# finalize the process
m.Execute();

2 comments:

Rutger said...

Thats look great Paul!

Will it be possible to transfer data back and forth from for example Numpy arrays so you can also use other Python modules?

Anonymous said...

Yes, this is really looking good.
Whats about putting such python script beyond a new button of the ribbon interface? This should also be available with variable inputs. Thanks Wim