jps services
  • Home Page
  • GIS Training Courses
    • QGIS Training Courses
    • ArcGIS Training Courses
    • MapInfo Pro Training Courses >
      • MapBasic Training Course
  • About
  • Blog

MapBasic code to add cartographic and thematic legends in MapInfo Professional using the Atlas program

26/10/2017

0 Comments

 
So far in previous blogs we have used MapBasic to create an Atlas program which can iterate through the regions of a specified layer in order to create individual customised maps. The Atlas program could be further enhanced if the user could have cartographic and thematic legends automatically created as required. Additionally the thematic layer should also be made available for the user to include in the printouts.
In the following screen shot a workspace has just been loaded into MapInfo Pro. As you can see the Legend Designer window is in focus. (As seen by the blue band at the top of its window).   
Picture
The Atlas program requires the map window to be in focus in order to create a cartographic or thematic legend for each selected region. In the next screen shot a Note message informs the user of the need to change the focus if they require a cartographic or thematic legend. If they ignore this message a map or maps can still be created but without a legend being included in the output.
Here is a screen shot of the Note message.
Picture
Assuming that the focus is on the map window, output can be obtained like the following screen shots which are based on the Canadian map provided in the MapInfo Professional trial data. Note that in this map the capital and highways data is not particularly informative.  It is recommended that you save the layers with more meaningful names as is shown in further examples.
Picture
Here we have saved the capital and highway layers with more meaningful names:-
Picture
The cartographic legend can also be incorporated into maps which have a mask element as in the following screen shots:-

Picture
Picture
Having created cartographic legends automatically from a workspace the next stage is to also be able to automatically create output from thematic maps with an appropriate legend.

Here is another workspace of the Canadian regions which has a thematic theme and legend.

Picture
The Atlas program has MapBasic code to check whether a thematic layer is present. In that case the program creates maps which reflect the theme and also includes a thematic legend as in the following screen shots.
Picture
Here is another example:-
Picture
As with the cartographic examples, maps with a thematic element can also can be enhanced with a mask. Here are some examples:-
Picture
And here is another example:-
Picture
The Atlas program needs several new modules in order to be able to create maps which have thematic layers or cartographic legends.

Here is the project code showing the respective modules.

Project file: Atlas.mbp
[Link]
Application=..\Atlas.mbx
Module=Library\ARRAYLib.mbo
Module=Library\DEBUGLib.mbo
Module=Library\ERRORLib.mbo
Module=Library\RIBBONLib.mbo
Module=AtlasRibbonInterface.mbo
Module=Functions.mbo
Module=End_Program.mbo
Module=AtlasAbout.mbo
Module=MLB.mbo
Module=LayoutPortrait.mbo
Module=LayoutLandscape.mbo
Module=MLBOKButton.mbo
Module=SelectAllFromMLB.mbo
Module=HighLightSelectedRegions.mbo
Module=PopulateRegionsArray.mbo
Module=InputAtlasDefaultSettings
Module=Atlas_dialog.mbo
Module=AddMaps.mbo
Module=CreateRibbonMask.mbo
Module=RibbonMaskSettings.mbo
Module=CreateMaskMaps.mbo
Module=ShadeInfo1.mbo
Module=SearchReplace.mbo
Module=ThematicName.mbo
Module=CartoLegend
In all we are adding four new modules, ShadeInfo1, SearchReplace, ThematicName and CartoLegend.

​Additionally, Atlas.def has also been updated.
''************************************************************************************
'**  Project Atlas
'**  
'**  Definition file
'**  
'**  Author: Joseph Short
'**  Date: 14 September 2017
'**  Version: AppVersion (see below)
'************************************************************************************
Include "MAPBASIC.DEF"
Include "ICONS.DEF"
Include "MENU.DEF"

Declare Sub Main
Declare Sub End_Program
Declare Sub Endhandler
Declare Sub InputAtlasDefaultSettings
Declare Sub MLB
Declare Sub LayoutPortrait
Declare Sub LayoutLandscape
Declare Sub MLBOKButton
Declare Sub SelectAllFromMLB
Declare Sub HighLightSelectedRegions
Declare Sub PopulateRegionsArray
Declare Sub DlgHandler
Declare Sub Atlas_dialog
Declare Sub AddMaps
Declare Sub DialogHandler
Declare Sub AtlasAbout
Declare Sub RibbonMaskSettings
Declare Sub CreateMaskMaps
Declare Sub CreateRibbonMask
Declare Sub ShadeInfo1
Declare Sub ThematicName
Declare Sub LegendInfo
Declare Sub CartoLegend
Declare Sub WinFocusChangedHandler 
Declare Function LongDate(ByVal dDate As Date) As String
Declare Function SearchReplace(ByVal strInput as String, ByVal strReplace as String, ByVal strReplacement as String) as String

'Function to check whether a table is open or not
Declare Function TableIsOpen (ByVal sTabName As String) As Logical

' /////     Class "MBExtensions.MBDateAndTime"     /////
' Allows you to retreive information from a MapBasic Date (i.e. YYYYMMDD) or
' DateTime (i.e. YYYYMMDDHHMMSSFFF) string
 
' Convert the string to it's equivalent long date string representation
' (depends on Control Panel > Regional and Language Options)
Declare Method RegionalLongDate
    Class "MBExtensions.MBDateAndTime"
    Lib "MBExtensions.dll" (ByVal sDateString as string) As String

Define AppName "Ribbon Based Atlas Sample Code"
Define AppVersion "0.70"
Define MLBID 1001
Define SelectAllMLBID 1002 

'Defining Dialog Width and Height
'This allows you to specify with and height clauses in terms of characters (i.e., Width 30dW, Height 10dH).
Define dW *4 'Four dialog units equals one character in width
Define dH *8 'Eight dialog units equals one character in height

Global tTime as Time
Global dDate as Date
Global sTabFile, sTable, sColA, sColB, sRegionsArray(), sAddressArray(5), sPrintDate, sCopyrightText, sFont, sRegion, sRegionTitle, sPrinterName, sDocTitle, sPapersize, sOutputFolder, sThematicLayer, sShadeText, sTotalString,sFirstLine, sFirstPart, sSecondPart,sFirstPartThematic, sSecondPartThematic, s_title, sColumn,sRec1, sLegendText As String 
Global iRegionsSelectedArray(), iPaperChoice, iPapersize, iLogoWindowID, iMapWindowID, iInitialMapWindowID,iLayoutWindowID, iLayoutFontSize1, iLayoutFontSize2, iLayoutFontSize3, iLayoutFontSize4, iOrientation as Integer
Global fZoom, fMp, fPaper1, fPaper2, fPaperWidth, fPaperHeight, fHorizontalDistance, fVerticalDistance, fPos1, fPos2 As Float
Global lRegionObjectsSelected As Logical
Global l_create_mask, l_shadeInfo, is_thematic, is_legend, is_cartographic As Logical 'TRUE means include mask 
Global sMaskTable, sMaskTabFilePath As String
Global sPctTranslucencyArray(5) As String
Global fX1, fY1, fX2, fY2 As Float
Global oRectangle, oCutter, oMask, ObjRegion As Object
Global iPctTranslucency As Integer
Global pPenStyle As Pen
Global alAlias,alTable As Alias
Here is the CartoLegend code which creates the cartographic legend.

Read More
0 Comments

    Author

    Joe Short BSc has been involved with various mapping solutions for over twenty years.  If you are considering implementing a GIS  or have ArcGIS Pro, MapInfo Pro or QGIS training requirements, jps services would be happy to be of assistance to your organisation. 

    Archives

    April 2020
    March 2020
    October 2019
    September 2019
    August 2019
    July 2019
    March 2019
    November 2018
    October 2018
    August 2018
    July 2018
    November 2017
    October 2017
    September 2017
    July 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    May 2016
    February 2016
    September 2015
    August 2015
    April 2015
    February 2015
    November 2014
    October 2014
    July 2014
    June 2014
    May 2014
    March 2014
    February 2014
    December 2013
    November 2013
    October 2013
    September 2013
    August 2013
    June 2013
    May 2013
    April 2013
    February 2013
    December 2012
    October 2012
    September 2012
    August 2012
    July 2012

    Categories

    All
    Arc Gis
    Arcview
    Autodesk
    Cad
    Gis Training
    Local Government
    Mapbasic
    Mapinfo
    Quantum Gis
    Relational Databases
    Saga Gis

    RSS Feed