Skip to main content
Question

Dynamic band interpretation

  • September 20, 2018
  • 2 replies
  • 21 views

jdh
Contributor
Forum|alt.badge.img+37
  • Contributor
  • 2002 replies

I have a scenario where I need to convert the bit depth of a raster from 16 bit to 8 bit, or vice versa, but the type should remain the same. ie Gray16->Gray8, Int16 -> Int8, Red8 -> Red16, etc.

Given that the RasterBandInterpretationCoercer does not accept attribute values for the Destination Interpretation Type parameter, is there an alternate method to testFilter for all allowed types, and 14 hardcoded RasterBandInterpretationCoercers?

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

2 replies

daveatsafe
Safer
Forum|alt.badge.img+19
  • Safer
  • 1637 replies
  • September 20, 2018

Hi @jdh,

The RasterBandInterpretationCoercer uses the function @ReinterpretRaster. If we run this function in the FMEFunctionCaller transformer, we gain the ability to set the output raster type from a feature attribute.

We can use the RasterBandPropertyExtractor to determine the band type, then map that to its output type in a new attribute for the function.

I am attaching a workspace to illustrate the process.

changerastertype.fmw


takashi
Celebrity
  • 7843 replies
  • September 21, 2018

Hi @jdh, I think you can implement that with Python. Example:

import fmeobjects
class FeatureProcessor(object):
    def __init__(self):
        interpretationPairs = {
            (fmeobjects.FME_INTERPRETATION_UINT8, fmeobjects.FME_INTERPRETATION_UINT16),
            (fmeobjects.FME_INTERPRETATION_INT8, fmeobjects.FME_INTERPRETATION_INT16),
            (fmeobjects.FME_INTERPRETATION_GRAY8, fmeobjects.FME_INTERPRETATION_GRAY16),
            (fmeobjects.FME_INTERPRETATION_RED8, fmeobjects.FME_INTERPRETATION_RED16),
            (fmeobjects.FME_INTERPRETATION_GREEN8, fmeobjects.FME_INTERPRETATION_GREEN16),
            (fmeobjects.FME_INTERPRETATION_BLUE8, fmeobjects.FME_INTERPRETATION_BLUE16),
            (fmeobjects.FME_INTERPRETATION_ALPHA8, fmeobjects.FME_INTERPRETATION_ALPHA16),
        }
        self.interpretations = {k : v for k, v in interpretationPairs}
        self.interpretations.update({v : k for k, v in interpretationPairs})
        self.rasterTools = fmeobjects.FMERasterTools()
        
    def input(self, feature):
        # Retrieve raster object from the input feature.
        raster = feature.getGeometry()
        if not isinstance(raster, fmeobjects.FMERaster):
            feature.setAttribute('fme_rejection_code', 'UNSUPPORTED_GEOMETRY_TYPE')
            self.pyoutput(feature)
        
        # Collect band objects and remove them from the raster.
        bands = []
        for i in range(raster.getNumBands()):
            bands.append(raster.getBand(0))
            raster.removeBand(0)
            
        # Update interpretation for each band and append them to the raster.
        for b in bands:
            before = b.getProperties().getInterpretation()
            after = self.interpretations.get(before, None)
            if after != None:
                b = self.rasterTools.convertBandInterpretation(after, b)
            raster.appendBand(b)

        # Reset raster to the feature.
        feature.setGeometry(raster)
        self.pyoutput(feature)
        
    def close(self):
        pass