Add a color scale with feet and meters to a contour map via Surfer automation

For those needing to showcase data in both metric and standard units, having a dual-labeled color scale can be a game-changer! Surfer's color scale doesn't natively support dual units, but this script offers a clever solution. By generating two contour maps—one in meters and another in feet—it adds aligned color scale bars for each and strategically hides the extra elements. The result? A seamless single map with a color scale displaying both meters and feet, offering clear, professional, dual-unit visuals. While it currently converts meters to feet, the script can be easily customized for other unit conversions.

Create a contour map with a color scale in two units in Surfer!

To run this script:

  1. In a Windows Explorer window, navigate to C:\Program Files\Golden Software\Surfer.
  2. Double click on Scripter.exe to launch Scripter.
  3. Copy and paste the script below into Scripter's code window (be sure to delete any existing lines first), or simply download the attached BAS file and open it in Scripter.
  4. Update the values in the USER-DEFINED VARIABLES section near the top of the script as needed. If you skip this step, the script will still run using the current values for these variables.
  5. Click Script | Run to run the script.

Sub Main
    
    'Clear any output in the Imediate window
    Debug.Clear

    'Initialize Surfer
    Dim SurferApp As Object
    Set SurferApp = CreateObject("Surfer.Application")
    SurferApp.Visible = True

    '====================================================== 
    'USER-DEFINED VARIABLES 
    '======================================================
    path2inputGrid = SurferApp.Path + "\samples\demogrid.grd"
    meters2feet = 3.28084 'conversion factor to convert meters to feet
    zMinMeters = 25 'the minimum contour value in meters
    zMaxMeters = 100 'the maximum contour value in meters
    zIntMeters = 4 'the contour interval in meters 
    zMinFeet = zMinMeters * meters2feet
    zMaxFeet = zMaxMeters * meters2feet
    zIntFeet = zIntMeters * meters2feet
    '======================================================

    'Create a new plot document
    Dim Plot As Object
    Set Plot = SurferApp.Documents.Add
    Plot.PageSetup.Orientation = srfLandscape

    'Create a contour map from original grid file (meters)
    Dim MapFrame As Object, ContourLayer As Object
    Set MapFrame = Plot.Shapes.AddContourMap(GridFileName:=path2inputGrid)
    MapFrame.Name = "Map (m)"
    Set ContourLayer = MapFrame.Overlays(1)

    'Edit the contour layer properties
    With ContourLayer
        .LevelMethod = SrfConLevelMethodSimple
        .SetSimpleLevels(Min:=zMinMeters, Max:=zMaxMeters, Interval:=zIntMeters)
        .FillContours = True
        .FillForegroundColorMap.LoadPreset("Rainbow3")
        .FillForegroundColorMap.SetDataLimits(DataMin:=zMinMeters, DataMax:=zMaxMeters)
        .ShowMajorLabels = False
        .ShowMinorLabels = False
    End With

    'Add color scale and edit properties
    Dim ColorScale As Object
    ContourLayer.ShowColorScale=True
    Set ColorScale = ContourLayer.ColorScale
    With ColorScale
        .Name = "Color Scale (m)"
        .Title = "Elevation"
        .TitlePosition = srfColorScaleTitlePositionTop
        .TitleAngle = 0
        .TitleOffsetHorizontal = -0.2
        .LabelFormat.Postfix = " m"
        .Left = 9
    End With

    'Convert grid file to feet
    Dim inGrid(1 To 1) As IGridMathInput
    Set inGrid(1) = SurferApp.NewGridMathInput(SurferApp.Path + "\Samples\demogrid.grd", "A")
    SurferApp.GridMath3("A*" + Str(meters2feet), inGrid, "C:\temp\demogrid_feet.grd")

    'Create a new contour map
    Dim MapFrame2 As Object, ContourLayer2 As Object
    Set MapFrame2 = Plot.Shapes.AddContourMap(GridFileName:= "C:\temp\demogrid_feet.grd")
    MapFrame2.Name = "Map (ft)"
    Set ContourLayer2 = MapFrame2.Overlays(1)

    'Edit contour layer to have the same layer properties as for the other layer
    With ContourLayer2
        .LevelMethod = SrfConLevelMethodSimple
        .SetSimpleLevels(Min:=zMinFeet, Max:=zMaxFeet, Interval:=zIntFeet)
        .FillForegroundColorMap.LoadPreset("Rainbow3")
        .FillForegroundColorMap.SetDataLimits (DataMin:=zMinFeet, DataMax:=zMaxFeet)
        .ShowMajorLabels = False
        .ShowMinorLabels = False
    End With

    'Add a color scale to the new layer and edit properties
    Dim ColorScale2 As Object
    ContourLayer2.ShowColorScale = True
    Set ColorScale2 = ContourLayer2.ColorScale
    With ColorScale2
        .Name = "Color Scale (ft)"
        .Title = ""
        .LabelFormat.Type = srfLabFixed
        .LabelFormat.NumDigits = 1
        .LabelFormat.Postfix = " ft"
        .Left = 8.4
    End With

    'Hide the second contour map and the second color scale bar's lines
    MapFrame2.Visible = False
    ContourLayer2.MajorLine.Style = "Invisible"
    ContourLayer2.MinorLine.Style = "Invisible"
    ColorScale2.FrameLine.Style = "Invisible"
End Sub

Related Articles and Help Pages:

Updated October 2024

Was this article helpful?
8 out of 10 found this helpful

Comments

0 comments

Please sign in to leave a comment.