MapInfo Pro Developers User Group

 View Only
  • 1.  Automating Contour Creation

    Posted 05-17-2021 10:59
    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
    ------------------------------


  • 2.  RE: Automating Contour Creation

    Posted 05-23-2021 19:38
    Edited by Duri Bradshaw 05-23-2021 19:38
    Hi Brandon,

    Have a look at this post, there is some sample code and links to more info
    https://community.precisely.com/communities/community-home/digestviewer/viewthread?MessageKey=d713632a-a019-4e9d-87b3-299de9b47446

    ------------------------------
    Duri Bradshaw
    Spatial IT Consultant
    Insight GIS
    ------------------------------



  • 3.  RE: Automating Contour Creation

    Employee
    Posted 05-24-2021 10:34
    We have some python examples that use the Raster SDK in our "Python Quick Start" which you can get from the MapInfo Marketplace.
    If you have 2019.3 you can simply type "python" in the Pro QuickSearch box on the title bar and it will list the app from the Marketplace.

    ------------------------------
    Bob Fortin
    Software Architect and Distinguished Engineer
    MapInfo Pro Development Team
    ------------------------------



  • 4.  RE: Automating Contour Creation

    Employee
    Posted 05-25-2021 03:46
    Edited by Anshul Goel 05-26-2021 06:25
    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
    ------------------------------



  • 5.  RE: Automating Contour Creation

    Posted 05-26-2021 10:20
    Thank you very much. I don't think there is a better way to get started than with this. I appreciate it.

    ------------------------------
    Brandon Shepherd
    Knowledge Community Shared Account
    Shelton CT
    ------------------------------



  • 6.  RE: Automating Contour Creation

    Posted 07-21-2021 19:33
    We undertook a similar task to this last year, transforming 1500 - 1km DEM tiles into 0.25m contours. We had a few attempts at doing it in MapInfo but ultimately went down the FME route. Contours were produced at 0.25m across all the elevation range -2m - 1300m but there was an option we didn't take advantage of to provide a contours translation table that would have allowed altering the contour interval for different elevation ranges. TAB output was fully styled to match our existing colour schemes and a labelling process was also run that produced 'acceptable' labels for 1:250/500 scale.

    If FME is an option for you I think we should be able to provide the FME workspace that was created for this. Processing took quite awhile, week or more, so if your area is a similar size (1500sq km) or larger you may want to rejig the FME workspace to take advantage of parallel processing.

    ------------------------------
    William Dean
    Cairns Regional Council
    Cairns QLD
    ------------------------------



  • 7.  RE: Automating Contour Creation

    Posted 01-13-2022 10:15
    Edited by Luke Lynx 01-14-2022 07:38
    Hi, 

    Thanks for the rundown of the code above. I have tried to use this to build a Python script which can access and run the raster operation "RasterAnalysis.Polygonise" for a given raster file and range of levels, I am presuming that these will be similar? 

    I currently have the following and am unsure how to fix it so that "manualLevels" is in the correct from for "Polygonise.Parameters" being "IList<Tuple<Double,Double,ContourStyle>>".

    from typing import List
    import clr 
    import os
    import sys

    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\\Professional19\\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 PolygoniseParameters, ContourStyle, ManualLevel, ContourOptions, InterpolationMethod, ContourLevels, FieldBandFilter

    srcRasterFilePath = "Raster_FilePath.asc"
    dstVectorFilePath = "RasterPolygonise_FilePath.tab"

    manualLevels= List[ManualLevel]()

    manualLevels.Add(ManualLevel(Double(0.00005),Double(0.025),ContourStyle(2,1,Color.Red)))
    manualLevels.Add(ManualLevel(Double(0.025),Double(0.05),ContourStyle(2,1,Color.Green)))
    manualLevels.Add(ManualLevel(Double(0.05),Double(0.075),ContourStyle(2,1,Color.Blue)))
    manualLevels.Add(ManualLevel(Double(0.075),Double(0.1),ContourStyle(2,1,Color.Red)))
    manualLevels.Add(ManualLevel(Double(0.1),Double(0.125),ContourStyle(2,1,Color.Green)))
    manualLevels.Add(ManualLevel(Double(0.125),Double(0.15),ContourStyle(2,1,Color.Blue)))
    manualLevels.Add(ManualLevel(Double(0.15),Double(0.3),ContourStyle(2,1,Color.Red)))
    manualLevels.Add(ManualLevel(Double(0.3),Double(0.5),ContourStyle(2,1,Color.Green)))
    manualLevels.Add(ManualLevel(Double(0.5),Double(5),ContourStyle(2,1,Color.Blue)))

    RasterAnalysis.Polygonise(srcRasterFilePath, dstVectorFilePath, PolygoniseParameters(manualLevels))

    Any help would be much appreciated,
    Thanks

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



  • 8.  RE: Automating Contour Creation

    Employee
    Posted 01-15-2022 06:16
    Hello Luke,

    I have created a separate post and posted sample code to run Polygonise operation using Python there.
    Please go to below link to find the post:
    https://community.precisely.com/communities/community-home/digestviewer/viewthread?GroupId=19&MessageKey=137f0240-761e-4784-bf4b-4794ef247b9a&CommunityKey=fe4d1ee2-f1c6-4307-87d2-00bea1344885&tab=digestviewer&ReturnUrl=%2fcommunities%2fcommunity-home%2fdigestviewer%3ftab%3ddigestviewer%26CommunityKey%3dfe4d1ee2-f1c6-4307-87d2-00bea1344885

    So for List Tuple you will need to define variable like below in Python:
    manualLevels=List[Tuple[Double,Double,ContourStyle]]()

    And then breaks can be added using below line:
    manualLevels.Add(Tuple.Create(Double(0.00005),Double(500),ContourStyle(2,1,Color.Red)))
     
    Hope this help. You can now define breaks based on your requirement.

    Let us know if you need any other help on the same.

    Thanks
    Anshul


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



  • 9.  RE: Automating Contour Creation

    Posted 01-17-2022 10:22
    Hi Anshul, 

    Thanks very much for your response it has been extremely helpful and I have now managed to sort my issues and everything runs.

    It is much appreciated, 
    Thanks :) 


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