MapInfo Pro

 View Only
  • 1.  Raster SDK Calculate method in Python

    Posted 08-04-2020 15:11
    Hi all,

    I'm currently struggling with the RasterProcessing.Calculate Method from the Raster SDK used in a Python script. Although most other .NET code is easily translated to Python, i keep stalling on this one.

    The first parameter (inputRasters) is of type MapInfo.RasterEngine.Common.RasterExpressionInputs[] but i'm having issues to define this inputRasters. Can anyone tell me what seems to be the issue/what would be the correct way to do this?

    Here's how i try do define this variable:
    inputRasters = RasterExpressionInputs('GRID_IN', 0, r"C:\Users\username\client\2020\data\QC_pythonTest.MRR")

    to use it in the Calculate method:
    RasterProcessing.Calculate(inputRasters, 0, GRID_OUT, outputRasterDriverID, expression, decimalSeparator, argumentSeparator)

    but I get this error: TypeError : no constructor matches given arguments

    Thanks!




    ------------------------------
    Julien Lebrun
    Korem Geospatial Software & Data
    Quebec QC
    ------------------------------


  • 2.  RE: Raster SDK Calculate method in Python

    Employee
    Posted 08-04-2020 17:43
    Hi Julien

    Could it be because the method expects a list of RasterExpressionInputs and you only pass in a single RasterExpressionInputs?

    Still new to Python, so I wonder if this would do the trick:
    inputRasters = [RasterExpressionInputs('GRID_IN', 0, r"C:\Users\username\client\2020\data\QC_pythonTest.MRR")]



    ------------------------------
    Peter Horsbøll Møller
    Distinguished Engineer
    Pitney Bowes Software & Data
    ------------------------------



  • 3.  RE: Raster SDK Calculate method in Python

    Posted 08-05-2020 08:19
    Edited by Julien Lebrun 08-05-2020 12:30
    Hi Peter,
    Thanks for your help, I was able to make it work with something very similar to what you suggested:
    inputRasters = [RasterExpressionInputs()]
    inputRasters[0].alias = 'GRID_IN'
    inputRasters[0].field = 3
    inputRasters[0].filePath = r"C:\Users\username\client\2020\data\QC_pythonTest.MRR"

    where [0] correspond to the index (i use only one raster as input but there could be more) and .alias/.field/.filePath are the different parameters needed for RasterExpressionInputs.

    When i try to validate the calculate method, i'm getting the following error:

    TypeError : No method matches given arguments for ValidateCalculatorExpression: (<class 'list'>, <class 'int'>, <class 'str'>, <class 'str'>, <class 'str'>, <class 'str'>, <class 'str'>, <class 'str'>, <class 'int'>)

    i'm wondering if the first parameter (inputRasters) has the correct type?

    Full code: 

    import clr, os
    clr.AddReference('MapInfo.RasterEngine.IO')
    clr.AddReference('MapInfo.RasterEngine.Operations')
    clr.AddReference('MapInfo.RasterEngine.Common')
    from MapInfo.RasterEngine.IO import DriverIDExtensions, DriverID
    from MapInfo.RasterEngine.Operations import RasterProcessing
    from MapInfo.RasterEngine.Common import RasterExpressionInputs, RasterFieldType
    
    
    inputRasters = [RasterExpressionInputs()]
    inputRasters[0].alias = 'GRID_IN'
    inputRasters[0].field = 3
    inputRasters[0].filePath = "C:\\Users\\username\\client\\2020\\data\\QC_pythonTest.mrr"
    
    GRID_OUT = 'C:\\Users\\username\\client\\2020\\data\\QC_pythonTestZero.mrr'
    primaryRasterIndex = 0
    expression = 'if GRID_IN>var1 then GRID_IN else null endif'
    outputRasterDriverID = DriverIDExtensions.GetString(DriverID.MRR)
    decimalSeparator = '.'
    argumentSeparator = ','
    variableNames = 'var1'
    variableValues = 0
    
    
    RasterProcessing.ValidateCalculatorExpression(inputRasters, primaryRasterIndex, GRID_OUT, outputRasterDriverID, expression, decimalSeparator, argumentSeparator, variableNames, variableValues)

    Thanks!



    ------------------------------
    Julien Lebrun
    Korem Geospatial Software & Data
    Quebec QC
    ------------------------------



  • 4.  RE: Raster SDK Calculate method in Python

    Posted 08-06-2020 15:56
    Does anyone know if all the functionalities of the Raster SDK work in Python? I'm curious because the Calculate method is not included in the quick start and the different parameter types also vary from the existing sample code.

    Any existing Python script using the calculate method?


    ------------------------------
    Julien Lebrun
    Korem Geospatial Software & Data
    Quebec QC
    ------------------------------



  • 5.  RE: Raster SDK Calculate method in Python

    Employee
    Posted 08-07-2020 02:54
    Edited by Anshul Goel 08-07-2020 03:11
    Hi Julien,

    Below are two examples in python for Calculate raster operation. First one is similar to code.

    ### Category: Raster Operation Calculator
    from os.path import abspath, join, dirname, exists
    
    clr.AddReference("MapInfo.RasterEngine.IO")
    clr.AddReference("MapInfo.RasterEngine.Common")
    clr.AddReference("MapInfo.RasterEngine.Operations")
    from MapInfo.RasterEngine.IO import DriverIDExtensions, DriverID
    from MapInfo.RasterEngine.Operations import RasterProcessing
    from MapInfo.RasterEngine.Common import RasterExpressionInputs, RasterFieldType, RasterCompressionType, RasterCompressionOptions
    from MapInfo.RasterEngine.Common import RasterCreationOptions, RasterApiOptions, RasterBandDataType, RasterResampleMethod
    
    cwd = dirname(__file__)
    
    inputFile1 = abspath(join(cwd, "..\\Data\\SeattleElevation.grd"))
    outputFile = abspath(join(cwd, "..\\Data\\SeattleElevationOut.mrr"))
    outputFileTab = abspath(join(cwd, "..\\Data\\SeattleElevationOut.tab"))
    
    inArray = [inputFile1]
    aliases = ['GRID_IN']
    expressions = ['if GRID_IN>var1 then GRID_IN else null endif']
    variableNames = ['var1']
    variableValues = [200]
    decimalSeparator = '.'
    argumentSeparator = ','
    useInfoFromGrid = 0
    
    creationOptions = RasterCreationOptions(RasterCompressionOptions(RasterCompressionType.LZMA, 5), -1)
    
    inputRasters = []
    for i in range(len(inArray)):
        rasterExpressionInputs = RasterExpressionInputs()
        rasterExpressionInputs.filePath = inArray[i]
        rasterExpressionInputs.alias = aliases[i]
        rasterExpressionInputs.field = 0
        inputRasters.append(rasterExpressionInputs)
    
    
    
    
    validExpression = False
    try:
        RasterProcessing.ValidateCalculatorExpression(inputRasters, 0, 
        expressions, decimalSeparator, argumentSeparator, variableNames, variableValues, True)
        print('valid expression')
        validExpression = True
    except:
        print('invalid expression')
    
    if validExpression:
        apiOptions = RasterApiOptions()
        apiOptions.CreationOptions = creationOptions
        outputRasterDriverID = DriverIDExtensions.GetString(DriverID.MRR)
        RasterProcessing.Calculate(
                        inputRasters,
                        useInfoFromGrid,
                        outputFile,
                        outputRasterDriverID,
                        expressions,
                        decimalSeparator,
                        argumentSeparator,
                        variableNames, variableValues,
                        True, RasterFieldType.Continuous,
                        False, RasterBandDataType.UndefinedData,
                        RasterResampleMethod.Bilinear,
                        True, False,
                        apiOptions,
                        None)
    
        if exists(outputFile) and exists(outputFileTab):
            print('calculated!')
            table = pro.Catalog.OpenTable(outputFileTab)
            if table:
                do('map from {}'.format(table.Alias))


    ### Category: Raster Operation Calculator
    from os.path import abspath, join, dirname, exists
    
    clr.AddReference("MapInfo.RasterEngine.IO")
    clr.AddReference("MapInfo.RasterEngine.Common")
    clr.AddReference("MapInfo.RasterEngine.Operations")
    from MapInfo.RasterEngine.IO import DriverIDExtensions, DriverID
    from MapInfo.RasterEngine.Operations import RasterProcessing
    from MapInfo.RasterEngine.Common import RasterExpressionInputs, RasterFieldType, RasterCompressionType, RasterCompressionOptions
    from MapInfo.RasterEngine.Common import RasterCreationOptions, RasterApiOptions, RasterBandDataType, RasterResampleMethod
    
    cwd = dirname(__file__)
    
    inputFile1 = abspath(join(cwd, "..\\Data\\SeattleElevation.grd"))
    inputFile2 = abspath(join(cwd, "..\\Data\\seattleelevation_shifted_reprojected.grd"))
    outputFile = abspath(join(cwd, "..\\Data\\SeattleElevationNonOverlappingSummed.mrr"))
    outputFileTab = abspath(join(cwd, "..\\Data\\SeattleElevationNonOverlappingSummed.tab"))
    
    inArray = [inputFile1, inputFile2]
    aliases = ['GRID1', 'GRID2']
    expressions = ['GRID1+GRID2']
    decimalSeparator = '.'
    argumentSeparator = ','
    useInfoFromGrid = 0
    
    creationOptions = RasterCreationOptions(RasterCompressionOptions(RasterCompressionType.LZMA, 5), -1)
    
    inputRasters = []
    for i in range(len(inArray)):
        rasterExpressionInputs = RasterExpressionInputs()
        rasterExpressionInputs.filePath = inArray[i]
        rasterExpressionInputs.alias = aliases[i]
        rasterExpressionInputs.field = 0
        inputRasters.append(rasterExpressionInputs)
    
    
    
    
    validExpression = False
    try:
        RasterProcessing.ValidateCalculatorExpression(inputRasters, 0, 
        expressions, decimalSeparator, argumentSeparator, None, None, True)
        print('valid expression')
        validExpression = True
    except:
        print('invalid expression')
    
    if validExpression:
        apiOptions = RasterApiOptions()
        apiOptions.CreationOptions = creationOptions
        outputRasterDriverID = DriverIDExtensions.GetString(DriverID.MRR)
        RasterProcessing.Calculate(
                        inputRasters,
                        useInfoFromGrid,
                        outputFile,
                        outputRasterDriverID,
                        expressions,
                        decimalSeparator,
                        argumentSeparator,
                        None, None,
                        True, RasterFieldType.Continuous,
                        False, RasterBandDataType.UndefinedData,
                        RasterResampleMethod.Bilinear,
                        True, False,
                        apiOptions,
                        None)
    
        if exists(outputFile) and exists(outputFileTab):
            print('calculated!')
            table = pro.Catalog.OpenTable(outputFileTab)
            if table:
                do('map from {}'.format(table.Alias))


    ------------------------------
    Anshul Goel
    Knowledge Community Shared Account
    Shelton CT
    ------------------------------



  • 6.  RE: Raster SDK Calculate method in Python

    Posted 08-07-2020 11:45
    Hi Anshul,
    Thank you very much for the help, it's very appreciated. I think my issue was with the way I was building the inputRasters parameter.

    Regards,
    Julien

    ------------------------------
    Julien Lebrun
    Korem Geospatial Software & Data
    Quebec QC
    ------------------------------