Thanks for the response, this has been of great help.
Much appreciated.
Original Message:
Sent: 03-09-2022 12:40
From: Anshul Goel
Subject: Sample code to run MapInfo raster Polygonise operation through Python
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 createdoutputTabNewProj="D:\\RasterData\\MRR\\RasterPolygonise_reproject.tab"#Opening polygonise output tab and providing Alias namepolygoniseOutput=pro.Catalog.OpenTable(dstVectorFilePath,"polygon_output")#Calling mapbasic command to create a new table and provide new projectiondo("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
Original Message:
Sent: 03-08-2022 06:26
From: Luke Lynx
Subject: Sample code to run MapInfo raster Polygonise operation through Python
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
Original Message:
Sent: 01-15-2022 06:03
From: Anshul Goel
Subject: Sample code to run MapInfo raster Polygonise operation through Python
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 convertedfrom typing import Listimport clr import osimport sysclr.AddReference('System.Collections')from System.Drawing import Colorfrom os.path import abspath, join, dirname, existsfrom System import UInt32, Doublefrom System.Collections.Generic import Listsys.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, DriverIDfrom MapInfo.RasterEngine.Operations import RasterAnalysisfrom MapInfo.RasterEngine.Common import RasterInputDetails, PolygoniseParameters, PolygoniseTypesrcRasterFilePath = "D:\\RasterData\\MRR\\Seattle_100_mrr.mrr"dstVectorFilePath = "D:\\RasterData\\MRR\\RasterPolygonise_SameValueCells.tab"rasterInput=RasterInputDetails(srcRasterFilePath)polygoniseParameter=PolygoniseParameters(PolygoniseType.SameValueCells)polygoniseParameter.FillColorFromSourceRaster=FalsepolygoniseParameter.OutlineColorFromSourceRaster=FalseRasterAnalysis.Polygonise(rasterInput, dstVectorFilePath, polygoniseParameter)
Polygonise Raster Extents:
#Polygonise Operation#Create Raster Extent Polygonisation#Creates one polygon around the extent of the rasterfrom typing import Listimport clr import osimport sysclr.AddReference("System.Drawing")clr.AddReference('System.Collections')from System.Drawing import Colorfrom os.path import abspath, join, dirname, existsfrom System import UInt32, Doublefrom System.Collections.Generic import Listsys.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, DriverIDfrom MapInfo.RasterEngine.Operations import RasterAnalysisfrom MapInfo.RasterEngine.Common import RasterInputDetails, PolygoniseParameters, PolygoniseTypesrcRasterFilePath = "D:\\RasterData\\MRR\\Seattle_100_mrr.mrr"dstVectorFilePath = "D:\\RasterData\\MRR\\RasterPolygonise_rasterextents.tab"rasterInput=RasterInputDetails(srcRasterFilePath)polygoniseParameter=PolygoniseParameters(PolygoniseType.RasterExtent)polygoniseParameter.FillColorFromSourceRaster=FalsepolygoniseParameter.OutlineColorFromSourceRaster=FalseRasterAnalysis.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 valuesfrom typing import Listimport clr import osimport sysclr.AddReference("System.Drawing")clr.AddReference('System.Collections')from System.Drawing import Colorfrom os.path import abspath, join, dirname, existsfrom System import UInt32, Doublefrom System.Collections.Generic import Listsys.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, DriverIDfrom MapInfo.RasterEngine.Operations import RasterAnalysisfrom MapInfo.RasterEngine.Common import RasterInputDetails, PolygoniseParameters, PolygoniseTypesrcRasterFilePath = "D:\\RasterData\\MRR\\Seattle_100_mrr.mrr"dstVectorFilePath = "D:\\RasterData\\MRR\\RasterPolygonise_validinvalid.tab"rasterInput=RasterInputDetails(srcRasterFilePath)polygoniseParameter=PolygoniseParameters(PolygoniseType.ValidInvalid)polygoniseParameter.FillColorFromSourceRaster=FalsepolygoniseParameter.OutlineColorFromSourceRaster=FalseRasterAnalysis.Polygonise(rasterInput, dstVectorFilePath, polygoniseParameter)
Polygonise User Defined Breaks:
#Polygonise Operation#Creates user defined breaks#Contour styles will be defined in the manual levelsimport stringimport clr import osimport sysclr.AddReference("System.Drawing")clr.AddReference('System.Collections')from System.Drawing import Colorfrom os.path import abspath, join, dirname, existsfrom System import Double, Tuplefrom System.Collections.Generic import Listsys.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 RasterAnalysisfrom MapInfo.RasterEngine.Common import RasterInputDetails, PolygoniseParameters, PolygoniseType, ContourStyle#Input and output file pathsrcRasterFilePath = "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=TruepolygoniseParameter.OutlineColorFromSourceRaster=TrueRasterAnalysis.Polygonise(rasterInput, dstVectorFilePath, polygoniseParameter)
Hope this is useful.
Thanks
Anshul
------------------------------
Anshul Goel
Pitney Bowes Software Inc.
Shelton CT
------------------------------