Hey Brandon,
Please refer the below sample code for running contour operation using Python. This should give you some idea on getting started.
Manual Intervals:
#Polyline contours
#Create Manual spaced contours, with a natural range (defined by grid)
#Contour styles will be defined in the manual levels
from typing import List
import clr
import sys
from os.path import abspath, join, dirname, exists
from System import UInt32
from System.Drawing import Color
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.IO import DriverIDExtensions, DriverID
from MapInfo.RasterEngine.Operations import RasterAnalysis
from MapInfo.RasterEngine.Common import ManualLevel, ContourOptions, InterpolationMethod, ContourLevels, FieldBandFilter,ContourStyle
cwd = dirname(__file__)
inputFile = abspath(join(cwd, "..\\RasterData\\GRD\\Elevation.grd"))
outputFile = abspath(join(cwd, "..\\RasterData\\GRD\\Elevation_contour.tab"))
polygonContouring=False;
manualLevels= List[ManualLevel]()
manualLevels.Add(ManualLevel(100.0,ContourStyle(2,1,Color.Red)))
manualLevels.Add(ManualLevel(200.0,ContourStyle(2,1,Color.Green)))
manualLevels.Add(ManualLevel(300.0,ContourStyle(2,1,Color.Blue)))
options=ContourOptions()
options.InterpolationMethod=InterpolationMethod.Default
options.Levels=ContourLevels(manualLevels)
fieldBandFilter=FieldBandFilter(UInt32(0),UInt32(0))
RasterAnalysis.Contour(inputFile, outputFile, 0, options, polygonContouring, fieldBandFilter)
if exists(outputFile):
print('Contour Successful!')
table = pro.Catalog.OpenTable(outputFile)
if table:
do('map from {}'.format(table.Alias))
Fixed Intervals:
#Polyline contours
#Create ConstantMinMax spaced (80 unit increments) contours starting from the data min, with a natural range (defined by grid)
#A major line will be created every 10th line increment and have the MajorStyle applied
#All minor lines have MinorStyle applied
from typing import List
import clr
import sys
from os.path import abspath, join, dirname, exists
from System import UInt32
from System.Drawing import Color
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.IO import DriverIDExtensions, DriverID
from MapInfo.RasterEngine.Operations import RasterAnalysis
from MapInfo.RasterEngine.Common import ManualLevel, ContourOptions, InterpolationMethod, ContourLevels, FieldBandFilter,ContourStyle,ContourLevelType
cwd = dirname(__file__)
inputFile = abspath(join(cwd, "..\\RasterData\\GRD\\SeattleElevation.grd"))
outputFile = abspath(join(cwd, "..\\RasterData\\GRD\\Elevation_contour.tab"))
polygonContouring=False;
contourStyles=[
10,
ContourStyle(3,2,Color.Red),
ContourStyle(1,1,Color.Blue)
]
options=ContourOptions(InterpolationMethod.Default,ContourLevels(ContourLevelType.ConstantMinMax,UInt32(80)),contourStyles)
fieldBandFilter=FieldBandFilter(UInt32(0),UInt32(0))
RasterAnalysis.Contour(inputFile, outputFile, 0, options, polygonContouring, fieldBandFilter)
if exists(outputFile):
print('Contour Successful!')
table = pro.Catalog.OpenTable(outputFile)
if table:
do('map from {}'.format(table.Alias))
Hope it helps.
Thanks
Anshul
------------------------------
Anshul Goel
Pitney Bowes Software Inc.
Shelton CT
------------------------------
Original Message:
Sent: 05-17-2021 10:59
From: Brandon Shepherd
Subject: Automating Contour Creation
Hello,
I am looking for a way to automate contour creation with MapInfo, MapBasic, or Python. It's common practice for us to create contour tables of different intervals at certain depths, such as 1' for 0-50, 5' for 55-100, 10' for 110-500 and so on. The current Raster contour tool only allows us to create one interval set at a time. The result is we need to then append the tables and delete the extra files. Over the course of a project we need to repeat the process hundreds of times. I attempted to use the custom intervals option in the contours tool, but it proved very tedious for designating hundreds of contours
I've created a few MapBasic tools and added them into the ribbon, but I am yet to grasp even the basics of Python integration and accessing the Raster toolbox. Any help getting started will be appreciated.
Thank you,
Brandon
------------------------------
Brandon Shepherd
Knowledge Community Shared Account
Shelton CT
------------------------------