Examples of MapBasic Programs - MapInfo_Pro - 2023

MapInfo Pro Help

Product type
Software
Portfolio
Locate
Product family
MapInfo
Product
MapInfo > MapInfo Pro
Version
2023
Language
English
Product name
MapInfo Pro
Title
MapInfo Pro Help
First publish date
1985
Last updated
2023-09-12
Published on
2023-09-12T16:39:16.995549

This section shows examples of MapBasic programs you can use to enhance your maps.

Converting a Table of Coordinates to a Table of Lines

The following steps can be used to create lines for each record in a table. You must have a column for each of the necessary coordinates. It is suggested that you make any changes on a copy of your table. If there are already objects in a Map window for this table, they will be lost.

  1. On the HOME tab, in the Windows group, click Tool Windows, and click MapBasic from the list, to open the MapBasic window.
  2. Type the following command in the window:

    update point_table set obj=createline(Start_X, Start_Y,End_X,End_Y)

Where point_table is the copy of your original table, Start_X is the column containing the first X coordinate, Start_Y is the column containing the first Y coordinate, End_X is the column containing the second X coordinate, and End_Y is the column containing the second Y coordinate. These columns will be the names that you have already specified in your table.

MapInfo Pro will go row by row through your table and create a line object based on the information in that row. You will not see a change in the table until you redraw the window or open a new Map window for it.

Troubleshooting (Converting a Table of Coordinates to a Table of Lines)

Before you proceed with the update make sure that the table in which the lines will be stored is mappable. To make the table mappable, on the TABLE tab, in the Edit group, click Maintenance, and Modify Structure.

If your map is projected, the line objects may not be created properly. To work around this situation, see the Set Coordsys statement in the MapBasic Reference to change the MapBasic coordinate system.

Creating Circles Around Points Using the MapBasic Window

The following createcircle command can be used to convert a table of points to a table of circular regions. This command is similar to creating buffers on a point layer. It differs, however, from buffering in that it actually converts the point objects into circles. It is suggested that you make any changes on a copy of your table. If there are already objects in a Map window for this table, they will be lost.

  1. On the HOME tab, in the Windows group, click Tool Windows, and click MapBasic from the list, to open the MapBasic window.
  2. Type the following line in the window (all on one line):

    update tablename set

    obj=createcircle(centroidx(obj),centroidy(obj),radius)

    Where tablename is the name of the copy of the table, and radius is the radius of the circle in current system units.

The table will then be automatically updated.

Troubleshooting (Creating Circles Around Points Using the MapBasic Window)

This command modifies the object column of your table. The command replaces the point object with circular objects. If you wish to undo this operation, make sure the Map window is active and on the TABLE tab, in the Contents group, click Revert to revert the table to the last time it was saved.

The final parameter in the MapBasic command, 10 in this example, is the radius of the newly created buffers, in miles. This figure is adjustable. If you perform the command and find that the radius is too small or too large, perform the command again, with a different radius.

If your map is projected, the circular objects may not be created properly.

If you wish to convert a table of circular objects back to a table of points, run the MapBasic command:

Update tablename set obj=Buffer(obj,smoothness,distance,units)

Using the ObjectInfo Function to Describe Graphical Object Types

The Objectinfo command is used to return the type of graphic object associated with each row. Each object type is represented by a small integer. MapInfo Pro has 12 types of objects. They are listed in the Objectinfo function section in the MapBasic Reference.

To determine the different types of objects in a single layer, do the following:

  1. On the HOME tab, in the Windows group, click Tool Windows, and click MapBasic from the list, to open the MapBasic window.
  2. Enter the following SQL Select command. Where obj refers to the object associated with the row of the table, 1 is the code to return the object type, and tablename is your table.

    Select ObjectInfo (obj,1) from tablename

  3. On the HOME tab, in the Windows group, click New Document, and click Browser from the list, to open a Browser window.
  4. Choose to browse selection. A list of the number codes for each object type appear.

Selecting All Records of a Specific Object Type

  1. On the HOME tab, in the Windows group, click Tool Windows, and click MapBasic from the list, to open the MapBasic window.
  2. Type the following SQL Select statement. Where tablename is your table and objecttype is the type of object you wish to select. To determine the type of an object, double-click the object. A dialog box displays with the region type listed.

    Select * from tablename where str$(obj)="objecttype"

  3. Choose to browse or map the selection of these objects. You can also search for other types of objects including regions, arcs, lines, ellipses, rectangles, points, and polylines.

Selecting Streets by Color Using SQL Select

To select all streets from a StreetPro file by the width, pattern, and color of the line segment, do the following:

  1. With your street file open, on the HOME tab, in the Windows group, click Tool Windows, and click MapBasic from the list, to open the MapBasic window.
  2. To select thin red lines, type the following:

    Str$(ObjectInfo(obj,2))="Pen (2, 2, 16711680)"

Finding the Angle from a Horizontal

The following procedure finds the angle (angle A in the next example) between a line that connects two given points and a horizontal line. This is done by creating a right triangle where the hypotenuse is the line between the two points and the adjacent is the line between the origin point and a point on the horizontal directly below or above the destination point. After you find the hypotenuse and adjacent distances, you can find the angle by taking the arc cosine of the adjacent divided by the hypotenuse.

Note: The angle will be calculated using a Cartesian coordinate system. For some projections, a calculated 90 degree angle may be obviously different from a "right angle" on the projected map.

The two points will be referred to as the origin and the destination. The origin is the point that is on the horizontal line. The destination is the other point. In the next example, Hartford is the origin. It lies on the imaginary horizontal line. Boston is the destination. The hypotenuse is the distance from Hartford to Boston; the adjacent is the distance from Hartford to the point where the horizontal intersects the vertical dotted line that passes through Boston.



  1. On the HOME tab, in the Windows group, click Tool Windows, and click MapBasic from the list, to open the MapBasic window.
  2. Double-click the origin point (Hartford). MapInfo Pro opens a Point Object window that shows the coordinate information. Write down the X and Y coordinates. These coordinates will be referred to as OriginX and OriginY.
  3. Double-click the destination point (Boston). MapInfo Pro opens a Point Object window that shows the coordinate information. Write down the X and Y coordinates. These coordinates will be referred to as DestX and DestY.
  4. In the MapBasic Window, type and run the following command. Substitute the values you find in step 2 and 3 for OriginX, OriginY, DestX and DestY. MapInfo Pro returns the distance between the origin and destination points in a Message window. The value is the hypotenuse of the triangle.

    Print Distance(OriginX, OriginY, DestX, DestY, "mi")

  5. In the MapBasic window, type and run the following command. Substitute the values you find in step 2 and 3 for OriginX, OriginY, DestX and DestY. MapInfo Pro returns the distance between the origin and a point on the horizontal directly below the destination point. MapInfo Pro returns the distance in a Message window. The value is the adjacent of the triangle.

    Print Distance(OriginX, OriginY, DestX, OriginY, "mi")

  6. In the MapBasic window, find the angle with the following MapBasic command. Substitute the values that you found in step 4 and 5 for adjacent and hypotenuse. MapInfo Pro returns the angle in degrees in a Message window.

    Print(ACOS(Adjacent/Hypotenuse)*57.2958)

If you would like to find the angle in radians, omit the multiplication at the end of the statement, leaving:

	Print(ACOS(Adjacent/Hypotenuse))

If, instead of two points, you have one line that has the destination and origin as endpoints, do the following instead of steps 2 and 3:

  1. Double-click the line object. MapInfo Pro opens a window that shows the coordinate information. Write down the Start Point and End Point X and Y coordinates.
  2. Determine whether the Start Point or the End Point is higher, that is, which point has a more northerly Y coordinate.

    If the destination is above the origin (like the example above), then the destination will have the coordinates of the most northerly point (determined in the previous step). The origin would then take the coordinates of the other endpoint of the line.

    If the origin is above the destination, then it takes the coordinates of the more northerly point.

  3. Continue with step 4.

Finding Floating Windows

All windows in MapInfo Pro may be docked or may be floating windows. These include the Map, Browser, and Layout windows, as well as special windows like the Info window, Statistics window, button pads, ruler, and Message window. You can move windows around and place them off to the side of the screen. Occasionally, one of these windows may be shifted out of view. Using MapBasic, you can bring these windows back to the center of your screen. This example uses the Info window. Other special windows are listed in the Set Window statement in the MapBasic Reference.

  1. On the HOME tab, in the Windows group, click Tool Windows, and click MapBasic from the list, to open the MapBasic window.
  2. Type the following code into the window:

    Set Window Info Position (1.5,2.0) Units "in"

The Info window displays in the center of your screen.

Setting the Default Zoom for a Map

When you open a map in MapInfo Pro, it displays in the default view every time, even though you probably changed the zoom when you last opened the map. You can change the default zoom for a map with the Set Map statement.

For example, if you open the standard world map that ships with MapInfo Pro, it displays with the whole world in view, by default. You frequently use the world map to view just the European countries and would like the map to display in that view every time you open it. To change the map's default zoom, do the following:

  1. On the HOME tab, in the Windows group, click Tool Windows, and click MapBasic from the list, to open the MapBasic window.
  2. Open the WORLD map.
  3. Set the zoom level and position of your map to view Europe only.
  4. In the MapBasic window, enter the following command:

    Set Map Layer 1 Default Zoom

  5. Close the WORLD table.
  6. Re-open the WORLD table. It opens in the zoom level that you set.