MapInfo Pro Developers User Group

 View Only
  • 1.  Run MapInfo Raster Clip operation through Python

    Posted 03-14-2022 11:31
    Hi All,

    I have written some Python code for the RasterProcessing.Clip function and wondered if you could help with getting the Clip Region to take the form Region Polygon Selection, as currently I have only been able to get the Clip Region as Region All Polygons within the tab file. Any ideas @Anshul Goel?

    #Raster Clip Operation using Tab File Input

    import string

    import clr

    import os

    import sys

     

    from os.path import abspath, join, dirname, exists

     

    sys.path.insert(0,"C:\\Program Files\\MapInfo\\Professional19\\Raster")

    clr.AddReference("MapInfo.RasterEngine.IO")

    clr.AddReference("MapInfo.RasterEngine.Common")

    clr.AddReference("MapInfo.RasterEngine.Operations")

     

    from MapInfo.RasterEngine.Operations import RasterProcessing

    from MapInfo.RasterEngine.Common import RasterInputDetails, RasterApiOptions, RasterClipExtent

    from MapInfo.RasterEngine.IO import DriverIDExtensions, DriverID

     

    #Input and output file path

    rasterInput = r"raster_filepath_in"

    rasterOutput = r"raster_filepath_out"

    outputRasterDriverID = DriverIDExtensions.GetString(DriverID.ASC)

    tabInput = r"tab_filepath"

     

    retainInsideRegion = True

     

    apiOptions = RasterApiOptions(RasterClipExtent())

     

    RasterProcessing.Clip(rasterInput, tabInput, rasterOutput, outputRasterDriverID, retainInsideRegion, apiOptions)


    Any help would be much appreciated,

    Thanks,
    Luke



    ------------------------------
    Luke Lynx
    Knowledge Community Shared Account
    ------------------------------


  • 2.  RE: Run MapInfo Raster Clip operation through Python
    Best Answer

    Employee
    Posted 03-15-2022 06:28
    Hello Luke,

    You can query the Polygon file using select statement. Using query you will be able to select sub polygons which you need as input to Clip operation. Once query is successful you will need to save that query into new table and provide this table path into Clip operation.

    Below is the sample code which can be used for this purpose:
    ### Description: This script show how to query a table in MapInfo Pro and save it as Table.
    import os
    try:
        table=pro.Catalog.OpenTable("D:\\RasterData\\VectorData\\RasterizeRefFiles\\AUSTRALA_CLR.TAB")
        tempfilePath="D:\\RasterData\\VectorData\\RasterizeRefFiles\\AUSTRALA_CLR1.TAB"
        queryname="myquery"
        if table:
            do("select * from {0} where {1} like \"%{2}%\" into {3} noselect".format(table.Alias,"State","QLD",queryname))
            do("Commit table {0} As \"{1}\" TYPE NATIVE Charset \"WindowsLatin1\"".format(queryname,tempfilePath))
    except Exception as e:
        print("Error: {}".format(e))​
    The table used here (Australia_CLR.Tab) contains column by the name of "State" and we are querying for value "QLD". This will help us select the feature associated with QLD value. Next we will save this query as a Table (tempfilePath). Now we can pass this table to Clip operation.

    Hope this helps.

    Thanks
    Anshul

    ------------------------------
    Anshul Goel
    Pitney Bowes Software Inc.
    Shelton CT
    ------------------------------



  • 3.  RE: Run MapInfo Raster Clip operation through Python

    Posted 03-15-2022 07:10
    Hi @Anshul Goel,

    Thanks for the help on this post, it has been very helpful, much appreciated.

    I was just wondering if there was a MapInfo Python developers cookbook/guide on the different functions/packages and the functionality/integration of Python in MapInfo/MapBasic/SDK Raster Operations etc as have struggled finding any resources on it (except from the help received on this community page).

    Thanks,
    Luke


    ------------------------------
    Luke Lynx
    Knowledge Community Shared Account
    ------------------------------



  • 4.  RE: Run MapInfo Raster Clip operation through Python

    Employee
    Posted 03-15-2022 09:30
    Hi Luke,

    Have you downloaded the Python Quick Start app from the MapInfo Marketplace?

    It's not as much a cook but a number of examples showing how to perform certain tasks through Python.

    ------------------------------
    Peter Horsbøll Møller
    Principal Presales Consultant | Distinguished Engineer
    Precisely | Trust in Data
    ------------------------------