25
MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC Welcome

Automating Repetitive Tasks with MapBasic

Embed Size (px)

DESCRIPTION

Time to look under the hood. Open up the MapBasic window to see how you can automate data processing and other tasks using MapInfo Professional. You might find some great time savers in this session. You do not need to be a programmer to benefit from these techniques.

Citation preview

Page 1: Automating Repetitive Tasks with MapBasic

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Welcome

Page 2: Automating Repetitive Tasks with MapBasic

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Hands on Lab 14D Automating Repetitive Tasks with MapBasic

Peter Horsbøll MøllerGIS/LI Pre-Sales SpecialistJune 13, 2014

Every connection is a new opportunity™

Page 3: Automating Repetitive Tasks with MapBasic

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Automating Repetitive Tasks

with MapBasic

Time to look under the hood.

Open up the MapBasic window to see how you can automate data processing and other tasks using MapInfo Professional.

You might find some great time savers in this session.

You do not need to be a programmer to benefit from these techniques.

Page 4: Automating Repetitive Tasks with MapBasic

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

MapBasic Documentation

MapBasic Help file: Help System (zip)

MapBasic Reference Guide: Reference

MapBasic User Guide: User Guide

Page 5: Automating Repetitive Tasks with MapBasic

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

MapBasic Help FileINTRODUCTION

How to read the Help File?

MapBasic Fundamentals

Page 6: Automating Repetitive Tasks with MapBasic

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

MapBasic Documentation

Page 7: Automating Repetitive Tasks with MapBasic

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

The Features of the MapBasic Window

The MapBasic window in MapInfo Professional:

•Can be opened via Options > Show MapBasic Window

•Records, when open, (most) actions performed by the user in MapInfo Professional

•Can also ”play back” actions/statements

• Put the cursor in the line and hit the Enter-key

• Highlight several lines and hit the Enter-key

•You can save statements from the window to a text file via the MapBasic menu

•You can clear the content in the window via the MapBasic menu

Page 8: Automating Repetitive Tasks with MapBasic

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Be adviced!

Please note that

•The MapBasic statements in the MapBasic window can be very specific. They can for example refer to a specific window, a specific table or similar

•The scenario might not be completely similar next time you want to run a MapBasic statement. Maybe you don’t have any tables open, maybe there are no open windows, maybe the active window isn’t a map window and so on

Page 9: Automating Repetitive Tasks with MapBasic

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Using the MapBasic window

Open the MapBasic window

Perform one or more actions using the interface in MapInfo Professional, that is a menu item, a button or similar

Notice that the statements are written to the MapBasic window

Now try to execute the statements one at a time via the MapBasic window

Remember to clean up before you repeat the actions via the MapBasic window: Close tables or revert changes to tables and so forth.

Page 10: Automating Repetitive Tasks with MapBasic

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Example

Page 11: Automating Repetitive Tasks with MapBasic

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Update Column

Normally you are limited to updating one column at a time thru the Update Column dialog.

The MapBasic statement Update Table, however, lets you update several columns at a time

Update table

Set column = expr [ , column = expr, ...]

[ Where RowID = idnum ]

Use the dialog to get the syntax for one column and then modify the statement to update several afterwards

Page 12: Automating Repetitive Tasks with MapBasic

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Exercise

Dataset

•Exercise_Data\CriminalActivity.TAB

•Update X with CentroidX(OBJ) thru the dialog

•Modify the Update Table statement to also update:

• Y with CentroidY(OBJ)

• CRIMEID with RowID

•Consider setting the Coordinate System with:

• Set CoordSys Table C

Page 13: Automating Repetitive Tasks with MapBasic

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Append Rows to table

With Append Rows to Table you can insert records from one table into another table

Using the dialog you can however not control which column is inserted into which column.

When using the dialog the order of the columns is important

You can get around this issue using the MapBasic window

Insert Into table [ ( columnlist ) ]

{ Values ( exprlist )

| Select columnlist From table }

Using columnlist you can specify columns with names like ID, X, Y, NAME instead of COL1, COL2, COL2, COL4

Page 14: Automating Repetitive Tasks with MapBasic

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Exercise

Dataset

•US\CITY_125.TAB

•US\STATECAP.TAB

•Save a copy of StateCap as USCities

•Append City_125 to USCities using Append Rows to Table

•Revert changes to USCities

•Modify the statement to use column names

•Modify the statement to set FIPS_Code to ”00” for the cities inserted

Page 15: Automating Repetitive Tasks with MapBasic

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Exercise

Modify your script to have the style of the state capitals as red circles using the statement:

Set Style Symbol(35,16711680,12,"MapInfo Symbols",0,0)

And the style of the cities as grey circles using this statement:Set Style Symbol (35,8421504,8,"MapInfo Symbols",0,0)

Use CreatePoint to recreate the points using the new style

You can set the style using Symbol Style and use this statement to get the symbol printed to the Message window:

Print CurrentSymbol()

Page 16: Automating Repetitive Tasks with MapBasic

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Creating Spatial Features

You can create points using Table > Create Points.

And you can create other basic spatial features using the MapBasic window and some MapBasic functions: CreatePoint(x, y) CreateLine(x1, y1, x2, y2) CreateCircle(x, y, radius) Buffer(OBJ, resolution, distance, units) * MBR(OBJ) Offset(OBJ, direction, distance, units) * OffsetXY(OBJ, offsetx, offsety, units) *

* Some of the functions above can be used in a Cartesian as well as a Spherical version

Page 17: Automating Repetitive Tasks with MapBasic

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Exercise

Dataset

•US\States.TAB

•US\US_HIWAY.TAB

•US\USCities.TAB (the table that we created in a previous exercise)

•Create buffers around the cities based on the population

•Set the size to (POP_1990 / 20000) miles – or pick your own size

•Set the style as blue border for cities and red for state capitals

Page 18: Automating Repetitive Tasks with MapBasic

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

From Points to Squares

Page 19: Automating Repetitive Tasks with MapBasic

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Exercise

Dataset

•US\States.TAB

•US\US_HIWAY.TAB

•US\USCities.TAB (the table that we created in a previous exercise)

•Add four columns (X1, Y1, X2, Y2) of type Float to the table USCities

•Update these with:• CentroidX(Offset(OBJ, 45, (POP_1990 / 20000), ”mi”))

• CentroidY(Offset(OBJ, 45, (POP_1990 / 20000), ”mi”))

• CentroidX(Offset(OBJ, 225, (POP_1990 / 20000), ”mi”))

• CentroidY(Offset(OBJ, 225, (POP_1990 / 20000), ”mi”))

•Create a square around each city using CreateLine and MBR

Page 20: Automating Repetitive Tasks with MapBasic

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Solutions

Set CoordSys Table CriminalActivity

Update CriminalActivity

Set X = CentroidX(obj)

, Y = CentroidY(obj)

, CrimeID = ROWID

Page 21: Automating Repetitive Tasks with MapBasic

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Solutions

Commit Table STATECAP As “…\US\USCities.tab" TYPE NATIVE Charset "WindowsLatin1" Interactive

Open Table “…\US\USCities.TAB" Interactive

Insert Into USCities ( Capital, State, Pop_1990, Num_HU_90) Select City, State, Tot_Pop, Tot_hu From CITY_125

Insert Into USCities ( Capital, State, Pop_1990, Num_HU_90) Select City, State, Tot_Pop, Tot_hu From CITY_125

Page 22: Automating Repetitive Tasks with MapBasic

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Solutions

Commit Table STATECAP As “…\US\USCities.tab" TYPE NATIVE Charset "WindowsLatin1" Interactive

Open Table “…\US\USCities.TAB" Interactive

Set Style Symbol(35,16711680,12,"MapInfo Symbols",0,0)

Update USCities

Set OBJ = CreatePoint(CentroidX(OBJ), CentroidY(OBJ))

Set Style Symbol (35,8421504,8,"MapInfo Symbols",0,0)

Insert Into USCities

( Capital, State, Pop_1990, Num_HU_90, FIPS_CODE, OBJ) Select City, State, Tot_Pop, Tot_hu, "00“

, CreatePoint(CentroidX(OBJ), CentroidY(OBJ))

From CITY_125

Page 23: Automating Repetitive Tasks with MapBasic

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Solutions

Set Style Brush(1, 16777215, 16777215)

Set Style BorderPen(1, 2, 16711680)

Select * From USCities Where FIPS_Code <> "00" Into __TO_UPDATE

Update __TO_UPDATE Set OBJ = Buffer(obj, 100, pop_1990 / 20000, "mi")

Set Style BorderPen(1, 2, 3175935)

Select * From USCities Where FIPS_Code = "00" Into __TO_UPDATE

Update __TO_UPDATE Set OBJ = Buffer(obj, 100, pop_1990 / 20000, "mi")

Page 24: Automating Repetitive Tasks with MapBasic

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Solutions

Alter Table "USCities"

(add X1 Float,Y1 Float,X2 Float,Y2 Float ) Interactive

Update USCities

Set X1 = CentroidX(Offset(OBJ,45,(POP_1990/20000),"mi"))

Update USCities

Set Y1 = CentroidY(Offset(OBJ,45,(POP_1990/20000),"mi"))

Update USCities

Set X2 = CentroidX(Offset(OBJ,225,(POP_1990/20000),"mi"))

Update USCities

Set Y2 = CentroidY(Offset(OBJ,225,(POP_1990/20000),"mi"))

Update USCities

Set OBJ = CreateLine(X1, Y1, X2, Y2)

Update USCities

Set OBJ = MBR(OBJ)

Page 25: Automating Repetitive Tasks with MapBasic

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Get the latestMapInfo User Conference news:Twitter via #MapInfoUCand follow us @MapInfo