MapInfo Pro

 View Only
  • 1.  Sample code to run MapInfo raster Polygonise operation through Python

    Employee
    Posted 01-15-2022 06:03
    Hi All,

    There was a query in forum on running Raster Polygonise operation through Python, so thought of posting sample code for the same. Hope this helps.

    Polygonise Same Value Cells:
    #Polygonise Operation
    #Create Same Value Cells Polygonisation
    #Value of the cells will be compared, connected and same cells will be converted
    
    from typing import List
    import clr 
    import os
    import sys
    
    clr.AddReference('System.Collections')
    from System.Drawing import Color
    from os.path import abspath, join, dirname, exists
    from System import UInt32, Double
    from System.Collections.Generic import List
    
    sys.path.insert(0,"C:\\Program Files\\MapInfo\\Professional\\Raster")
    clr.AddReference("MapInfo.RasterEngine.IO")
    clr.AddReference("MapInfo.RasterEngine.Common")
    clr.AddReference("MapInfo.RasterEngine.Operations")
    clr.AddReference("System.Drawing")
    
    from MapInfo.RasterEngine.IO import DriverIDExtensions, DriverID
    from MapInfo.RasterEngine.Operations import RasterAnalysis
    from MapInfo.RasterEngine.Common import RasterInputDetails, PolygoniseParameters, PolygoniseType
    
    srcRasterFilePath = "D:\\RasterData\\MRR\\Seattle_100_mrr.mrr"
    dstVectorFilePath = "D:\\RasterData\\MRR\\RasterPolygonise_SameValueCells.tab"
    
    rasterInput=RasterInputDetails(srcRasterFilePath)
    
    
    polygoniseParameter=PolygoniseParameters(PolygoniseType.SameValueCells)
    polygoniseParameter.FillColorFromSourceRaster=False
    polygoniseParameter.OutlineColorFromSourceRaster=False
    
    RasterAnalysis.Polygonise(rasterInput, dstVectorFilePath, polygoniseParameter)​


    Polygonise Raster Extents:

    #Polygonise Operation
    #Create Raster Extent Polygonisation
    #Creates one polygon around the extent of the raster
    
    from typing import List
    import clr 
    import os
    import sys
    
    clr.AddReference("System.Drawing")
    clr.AddReference('System.Collections')
    from System.Drawing import Color
    from os.path import abspath, join, dirname, exists
    from System import UInt32, Double
    from System.Collections.Generic import List
    
    sys.path.insert(0,"C:\\Program Files\\MapInfo\\Professional\\Raster")
    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 RasterAnalysis
    from MapInfo.RasterEngine.Common import RasterInputDetails, PolygoniseParameters, PolygoniseType
    
    srcRasterFilePath = "D:\\RasterData\\MRR\\Seattle_100_mrr.mrr"
    dstVectorFilePath = "D:\\RasterData\\MRR\\RasterPolygonise_rasterextents.tab"
    
    rasterInput=RasterInputDetails(srcRasterFilePath)
    
    polygoniseParameter=PolygoniseParameters(PolygoniseType.RasterExtent)
    polygoniseParameter.FillColorFromSourceRaster=False
    polygoniseParameter.OutlineColorFromSourceRaster=False
    
    RasterAnalysis.Polygonise(rasterInput, dstVectorFilePath, polygoniseParameter)


    Polygonise Valid Invalid:

    #Polygonise Operation
    #Run Valid Invalid Polygonisation
    #Creates polygon as per the all connected valid and invalid cells ignoring the cell values
    
    from typing import List
    import clr 
    import os
    import sys
    
    clr.AddReference("System.Drawing")
    clr.AddReference('System.Collections')
    from System.Drawing import Color
    from os.path import abspath, join, dirname, exists
    from System import UInt32, Double
    from System.Collections.Generic import List
    
    sys.path.insert(0,"C:\\Program Files\\MapInfo\\Professional\\Raster")
    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 RasterAnalysis
    from MapInfo.RasterEngine.Common import RasterInputDetails, PolygoniseParameters, PolygoniseType
    
    srcRasterFilePath = "D:\\RasterData\\MRR\\Seattle_100_mrr.mrr"
    dstVectorFilePath = "D:\\RasterData\\MRR\\RasterPolygonise_validinvalid.tab"
    
    rasterInput=RasterInputDetails(srcRasterFilePath)
    
    polygoniseParameter=PolygoniseParameters(PolygoniseType.ValidInvalid)
    polygoniseParameter.FillColorFromSourceRaster=False
    polygoniseParameter.OutlineColorFromSourceRaster=False
    
    RasterAnalysis.Polygonise(rasterInput, dstVectorFilePath, polygoniseParameter)


    Polygonise User Defined Breaks:

    #Polygonise Operation
    #Creates user defined breaks
    #Contour styles will be defined in the manual levels
    
    import string
    import clr 
    import os
    import sys
    
    clr.AddReference("System.Drawing")
    clr.AddReference('System.Collections')
    from System.Drawing import Color
    from os.path import abspath, join, dirname, exists
    from System import Double, Tuple
    from System.Collections.Generic import List
    
    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 RasterAnalysis
    from MapInfo.RasterEngine.Common import RasterInputDetails, PolygoniseParameters, PolygoniseType, ContourStyle
    
    #Input and output file path
    srcRasterFilePath = "D:\\RasterData\\MRR\\Seattle_100_mrr.mrr"
    dstVectorFilePath = "D:\\RasterData\\MRR\\RasterPolygonise_custom.tab"
    
    rasterInput=RasterInputDetails(srcRasterFilePath)
    
    manualLevels=List[Tuple[Double,Double,ContourStyle]]()
    manualLevels.Add(Tuple.Create(Double(0.00005),Double(500),ContourStyle(2,1,Color.Red)))
    manualLevels.Add(Tuple.Create(Double(500),Double(1000),ContourStyle(2,1,Color.Green)))
    manualLevels.Add(Tuple.Create(Double(1000),Double(1500),ContourStyle(2,1,Color.Blue)))
    manualLevels.Add(Tuple.Create(Double(1500),Double(2000),ContourStyle(2,1,Color.Red)))
    manualLevels.Add(Tuple.Create(Double(2000),Double(2500),ContourStyle(2,1,Color.Green)))
    manualLevels.Add(Tuple.Create(Double(2500),Double(3000),ContourStyle(2,1,Color.Blue)))
    manualLevels.Add(Tuple.Create(Double(3000),Double(3500),ContourStyle(2,1,Color.Red)))
    manualLevels.Add(Tuple.Create(Double(3500),Double(4000),ContourStyle(2,1,Color.Green)))
    manualLevels.Add(Tuple.Create(Double(4000),Double(4500),ContourStyle(2,1,Color.Blue)))
    
    polygoniseParameter=PolygoniseParameters(manualLevels)
    polygoniseParameter.FillColorFromSourceRaster=True
    polygoniseParameter.OutlineColorFromSourceRaster=True
    
    RasterAnalysis.Polygonise(rasterInput, dstVectorFilePath, polygoniseParameter)


    Hope this is useful.

    Thanks
    Anshul



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


  • 2.  RE: Sample code to run MapInfo raster Polygonise operation through Python

    Posted 03-08-2022 06:27
    Hi @Anshul Goel

    Thanks for this informative answer, it has been very useful. Just a follow up question: 

    When you polygonise the raster file, is it also possible to set (or alter) the projection of this outputted tab file?

    Any help would be much appreciated.
    Thanks,
    Luke​

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



  • 3.  RE: Sample code to run MapInfo raster Polygonise operation through Python

    Employee
    Posted 03-09-2022 12:40
    Hello @Luke Lynx,
    ​Currently we don't provide option to set output projection for polygonise operation.

    But we can run Mapbasic command through Python to change the projection of the output tab file and save it as a new tab.
    Once Polygonise output tab is generated, you can add below lines of code to change the projection:
    RasterAnalysis.Polygonise(rasterInput, dstVectorFilePath, polygoniseParameter)
    
    #Sample code to call Save Copy As command on output Vector
    #After Polygonise output is created
    
    outputTabNewProj="D:\\RasterData\\MRR\\RasterPolygonise_reproject.tab"
    
    #Opening polygonise output tab and providing Alias name
    polygoniseOutput=pro.Catalog.OpenTable(dstVectorFilePath,"polygon_output")
    
    #Calling mapbasic command to create a new table and provide new projection
    do("Commit Table {0}  As \"{1}\" TYPE NATIVE Charset \"WindowsLatin1\" CoordSys Earth Projection 8, 74, \"m\", -177, 0, 0.9996, 500000, 0 Bounds (-7745844.29605, -9997964.94315)(8745844.29605,9997964.94315)".format(polygoniseOutput.Alias,outputTabNewProj))
    
    polygoniseOutput.Close()
    new_table = pro.Catalog.OpenTable(outputTabNewProj)


    Hope this help.

    Thanks
    Anshul



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



  • 4.  RE: Sample code to run MapInfo raster Polygonise operation through Python

    Posted 03-11-2022 09:30
    Hi @Anshul Goel,

    Thanks for the response, this has been of great help.

    Much appreciated.
    Thanks,

    Luke​

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