taking advantage of a spatial database with mapinfo professional

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

Upload: peter-horsboll-moller

Post on 20-Jun-2015

291 views

Category:

Technology


0 download

DESCRIPTION

The MapInfo tab file is a great storage for your spatial data, but you can also find a number of advantages by using a spatial database such as SQL Server 2008, Oracle or PostGIS. In this session we will take a look at how you can take advantage of a spatial database with MapInfo Professional.

TRANSCRIPT

Page 1: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Welcome

Page 2: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Spatial Data Explosion 11A Taking Advantage of a Spatial Database with MI Pro

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

Every connection is a new opportunity™

Page 3: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Taking Advantage of a Spatial Database

with MapInfo ProThe MapInfo tab file is a great storage for your spatial data, but you can also find a number of advantages by using a spatial database such as SQL Server 2008, Oracle or PostGIS.

In this session we will take a look at how you can take advantage of a spatial database with MapInfo Professional.

Page 4: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

The tools

MapInfo Professional 12.0, no introduction required

EasyLoader, our tool for uploading tables to spatial databases

SQL Server 2008, the spatial database

SQL Server Management Studio, the management interface to the database

Page 5: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Creating a ODBC Data source

Data

Data

Page 6: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Creating a SQL Server data source

Data sources can be created manually or in a centralised way

They can be stored in files or in the registry

For SQL Server 2008 you should use the ”SQL Server Native Client 10.0” driver

For SQL Server 2012 and Windows7 you should use the ”SQL Server Native Client 11.0” driver

Page 7: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

The MapInfo Map Catalog

Page 8: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Map Catalog

The Map Catalog (MapInfo.MapInfo_MapCatalog) describes spatial tables in the database with values like: Kind of spatial table, for example. XY, SpatialWare, Oracle, SQL

Server, PostgreSQL/PostGIS, etc. Coordinate system Data bounds Symbol, default and per row Default view

• The Map Catalog must exist once in the databases holding spatial data that must be accessed by MapInfo Professional

Page 9: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Creating the Map Catalog

You can create the Map Catalog using EasyLoader, directly in the database using SQL or you can use the DBMS Catalog tool

The user creating the Map Catalog using EasyLoader needs a sysadmin server role.

If you create the Map Catalog using EasyLoader, make sure that you close all connections to the database before creating the Map Catalog. If not you might have issues using the Per Row Style option when making DBMS tables mappable.

Page 10: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Tables

Page 11: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Creating tables

You can create tables

• manually thru the Management Studio,

• thru MapInfo Professional via New Table or Save Copy As

• or upload them thru EasyLoader

If you use MapInfo Professional or EasyLoader a number of things will be made automatically for you, like

• adding the table to the MapCatalog

• creating spatial index

• and primary index enabling the Identity type

Page 12: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Creating tables – considerations

Avoid

• the unicode column types (nchar, nvarchar, ntext)

• the uniqueidentifier column type

• creating character string columns wider than 254

as MapInfo Professional can’t edit/read these

Consider naming your Primary key column MI_PRINX. MapInfo Professional will use this as the primary key in views

Consider adding Identity columns to all your table for the Primary Index column. This will automatically update the column with unique values when inserting new records (similar to AutoNumber in MS Access)

Page 13: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Using EasyLoader as a batch uploader

Samples on how to pass parameters to EasyLoader for doing batch uploads

Upload a single table “C:\Program\MapInfo\Professional\Tools\easyloader.exe”

/B dbo /E /F c:\ezload.log /G /K /P R

/S DSN=Memphis;UID=AdminUC;PWD=Admin

/Q /Y MI_STYLE

/T C:\Maps\Addresses.tab;Addresses

Upload multiple tables using a text file “C:\Program\MapInfo\Professional\Tools\easyloader.exe”

/B MAPS /E /F c:\ezload.log /G /K /P R

/S DSN=Memphis;UID=AdminUC;PWD=Admin

/Y MI_STYLE

/L C:\tabfiles.txt

Makes it easy to rerun the uploads!

Page 14: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Uploading thru EasyLoader

Page 15: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Opening tables into MapInfo Professional

Page 16: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Opening a remote table

Select File > Open

Click on Open DBMS Connection or select the open connection in the Files of Type dropdown list

Select the schema

Check the table(s) to open

Click OK

Page 17: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Reopening a remote table

It’s only the first time you open a DBMS table, that you need to specify which data to open

Afterwards you can reopen this table by opening the tab file

The tab file contains all the necessary information Table structure (for linked tables) Connection information Can also hold the Username and Password (if you add them)

If the table was opened as ”Linked”, you may want to refresh the data.

If the table was opened as ”Live”, MapInfo will read the latest data from the database

Page 18: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Opening a remote table

Page 19: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

MapInfo Pro: Linked vs Live tables

Page 20: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Linked

• Data is copied to the MapInfo table the when you create the linked table and when you refresh it

• Access to the database is only needed when saving or refreshing -> working offline and long transactions are built in so your data is locally available

• Speed is as fast as local data after download

• Data can be accessed read-only without a primary key

• Using a query to get just the data you need, will speed up download time. Queries are set when you create the linked table via the Row, Column or Expert dialogs

• Data is downloaded to the same location as the .TAB file

• Duplicate data

Page 21: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Live with/without cache

• First time opening a table may be faster – depending on the zoom of your map window

• One “view entire layer” or zoom out to the entire view will download the whole table

• Data must have a primary key

• You always need access to the database

Page 22: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Live without cache

• MapInfo Professional “always” reads directly from the database

• Every draw, browse, select click which can negatively affect performance.

• Data is reread from the server on every access

Page 23: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Live with Cache

• Uses a temporary cache Since 9.5.1

– MapInfo table stored in a temporary MapInfo table.

– The cache is progressive, as you pan, zoom out or have more than one map window of the same data, the cache grows

• Data is cached as used in the map, so subsequent access to the same data will be quicker

• The cache is cleared when the table closes

Page 24: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Conclusions

Only use LIVE WITHOUT CACHE when The data is highly volatile (changing minute to minute or even

sooner)

Use LIVE WITH CACHE when The table data is very large and it is not feasible to create a filter

query to download less data Your data changes regulary, for instance every day or during

the day

Use LINKED when Your data is static or only changes ones a day/week/month/year Performance is important You want to do advanced spatial analysis on the data in

MapInfo Pro

Page 25: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Conclusions

• If you have a small data size in your table, it will probably not matter if you choose Live or Linked

• Using a LIVE table ensures that users log onto the database before they can access the data

• If you use live tables we recommend that you access your data thru a workspace with zoom levels set on the live table to prevent downloading unnecessary data

• The support for MARS has improved the use of live with cache

Page 26: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Querying

Page 27: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

When is the query performed?

For Linked tables query is performed when the table is opened the first time the table is refreshed.

For Live with cache tables query is performed when the table is added to a map you zoom or pan in the map

For Live without cache query is performed when the table is added to a map you zoom or pan in the map you refresh the map …

Page 28: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Row filtering

You can specify which records to retrieve from the database by attributes

And by geography

Value of Current_Mapper and Selection are static and not updated when the table is refreshed.

“Selection” only available when a selection is active“Current_Mapper” only available when a map is active“Object” is only available for mappable tables

“Like” and “not like” only available for text columnsDo not use ”” around text values!

Page 29: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Querying - Expert

Write your own SQL statement

Syntax depends on the SQL dialect of the database

MapInfo Professional will “translate” certain expression when passing the query to the database: Object Within Selection Current_Mapper …

You can save and open queries using the two buttons on the right side of the dialog

Page 30: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Opening a remote table - queried

Page 31: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Editing tables from SQL Server

Page 32: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Editing

The remote table must contain a primary unique index In a view this column should be named MI_PRINX

You can edit the data with every tool available in MapInfo Professional

When editing coordinates of a XY-coordinate table, the coordinates are updated in the columns with the X and Y coordinates in the database table

When changing the style on a table that has ”per row style” enabled the new style will be stored in the column holding the style. On other tables the change of style will be dismissed when you refresh the table

Page 33: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Editing – by multiple users

If multiple users are editing the same table in SQL Server, they must not access this database table thru the same physical file on disk.

If they do only one user will be able to edit the table at a time – the others will be prevented from editing as soon as the first users starts editing.

They must access the database table thru their own (local) copy of a MapInfo table, can be linked as well as live

Page 34: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Saving

Click Save Table to save the pending edits to the DBMS table

When saving the edits to the database, MapInfo checks if the records you have changed, have been changed in the database since you accessed these. If so, a dialog appears asking you to solve the conflict

If you are using a linked table, MI Pro will ask if you want to refresh the table as well. I would recommend that you say yes

Saving changes offline (only for Linked tables) can be done by closing the table. MapInfo will ask how to store the pending edits: Save Changes to MapInfo Table Save Changed to Server Discard Changes

Page 35: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Conflict resolution in MapInfo Pro

If a record has been changed in the database when being saved, MapInfo will prompt the user with a Conflict Resolution dialog like this one

You can see the original server version, the current MapInfo version and the current server version

You can decide what to keep and what to change – column by column

• Here you can see that column KATEGORI has been changed, and how.

• You can also see that the geometry (object) has been changed. Harder to see how that has changed

Page 36: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Refreshing linked tables

Page 37: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Refreshing

Reloading data from the database can be done with Table > Maintenance > Refresh DBMS Table...

Refreshing a DBMS table Checks the database for changes Reapplies the query condition. Note that the query conditions

are static. If you have specified to filter using the current map or current

selection, this filter will always be applied to the table. To specify a different map extent, you have to reopen the table from the database.

MapBasic syntax is: Server Refresh name_of_table

Page 38: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Refreshing DBMS tables thru a workspace

!Workspace

!Version 950

!Charset WindowsLatin1

Dim nConn As Integer

nConn = Server_Connect( "ODBC", "DSN=GIS;UID=EditUser;PWD=Edit" )

Print “Connection opened as no: " + nConn

Print “Now opening tables..."

Open Table "D:\3. demo\dbms\Buildings.tab" Interactive

Map From Buildings

Print “Refreshing the DBMS tables..."

Server Refresh Buildings

Close Table Buildings

Print “We are done!"

Server nConn Disconnect

Undim nConn

Page 39: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Files versus database

Page 40: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Files versus database

The are a number of differences between MapInfo’s flat tab files and keeping the data in SQL Server Several applications can access the data, not only MapInfo

software Multiple user editing The entire database stuff: relations, views, security, triggers etc.

When data is kept in the flat MapInfo tab files, the application needs to handle things like timestamp on change keeping historic versions etc.

When the data is in the database, the database can manage these things.

Page 41: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Creating views in SQL Server

Page 42: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

What is a view?

Et view can be seen as a way to look at your data

With a view you can limit the number of columns and/or records you want to see, for instance to separate a table of roads into different road classes

With a view you can merge multiple columns to one column, for instance merge road name and house no to an address column

You can also use a view to enrich one table with data from another table, for isntance by transfering the postal area name from a postal table to a table with addresses. You often do this by joining these tables. This can be done thru alphanumerical or spatial attributes

A view can also be used to merge multiple tables into one.

Page 43: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Creating views in SQL Server – mappable

If the views must be mappable from within MapInfo Pro, you need to make the view mappable

You can also manually add the view to the MapInfo_MapCatalog – if you know all the parameters

Or you can copy the record of the base table in the Map Catalog, insert the copy and change the name of the table to the name of the view

Page 44: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Creating views

Page 45: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

A view that merges tables

The statement below should be run from a Query-vindue – not thru View > Create New:

Create View dbo.viewAddressesUnion As

SELECT *

FROM dbo.ADDRESSES_A

UNION ALL

SELECT *

FROM dbo.ADDRESSES_B

Add it to the MapCatalog if it is mappable

Page 46: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Creating Union Views

Page 47: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Introduction to Geometry type

Page 48: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Geometry vs Geography

The geometry data type supports planar, or Euclidean (flat-earth), data. The geometry data type conforms to the Open Geospatial Consortium (OGC) Simple Features for SQL Specification version 1.1.0.

In addition, SQL Server supports the geography data type, which stores ellipsoidal (round-earth) data, such as GPS latitude and longitude coordinates.

Read more:• http://msdn.microsoft.com/en-us/library/bb964711.aspx

Point

LineRegion

RegionPline

MultiPoint

Collection

Page 49: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Geometry OGC methodsSTArea

STAsBinary

STAsText

STBoundary

STBuffer

STCentroid

STContains

STConvexHull

STCrosses

STDifference

STDimension

STDisjoint

STDistance

STEndpoint

STEnvelope

• STEquals

• STExteriorRing

• STGeometryN

• STGeometryType

• STInteriorRingN

• STIntersection

• STIntersects

• STIsClosed

• STIsEmpty

• STIsRing

• STIsSimple

• STIsValid

• STLength

• STNumGeometries

• STNumInteriorRing

• STNumPoints

• STOverlaps

• STPointN

• STPointOnSurface

• STRelate

• STSrid

• STStartPoint

• STSymDifference

• STTouches

• STUnion

• STWithin

• STX

• STY

They are all case sensitive!!!

Page 50: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Invalid geometries

Find records with invalid geometries

Select * From dbo.MYTABLE

Where SP_Geometri.STIsValid() = 0

Fix records with invalide geometries – pure magic!? Or not!?

Update dbo.MYTABLE

Set SP_Geometri = SP_Geometri.MakeValid()

Page 51: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Coordinate systems – EPSG codes

Querying EPSG codes for a table, også known as SRID:

select distinct SP_GEOMETRY.STSrid from dbo.MYTABLE

If it isn’t ”right”, you can change it using this statement:

Update dbo.MYTABLE Set SP_GEOMETRY.STSrid = 25832

But do not change the EPSG code to one of a completely different coordinate system.

There does, however, exist coordinate systems with different EPSG codes!:

"UTM Zone 32 Euref89\p25832“

, 8, 115, 7, 9.0, 0, 0.9996, 500000, 0

"ETRS TM Zone 32, Northern Hemisphere (ETRS89)\p3044“

, 8, 115, 7, 9, 0, 0.9996, 500000, 0

Note that EasyLoader has its own projection file!!

Page 52: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Spatial SQL samples

Adding a column with the area

Select *, SP_GEOMETRY.STArea() As Area

From dbo.PLACES

Adding a column with the length

Select *, SP_GEOMETRY.STLength() As Length

From dbo.ROADS

Page 53: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Spatial SQL samples

Finding municipalities and their neighbours

Select t1.NAME, t2.NAME

From dbo.MUNICIPALITIES t1, dbo.MUNICIPALITIES t2

Where (t1.SP_GEOMETRY.STTouches(t2.SP_GEOMETRY) = 1

Or t1.SP_GEOMETRY.STIntersects(t2.SP_GEOMETRY) = 1)

And t1.MI_PRINX <> t2.MI_PRINX

Order By t1.NAME

Page 54: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Spatial SQL samples

Adding city name to only those roads, that intersects a citySelect R.*, P.PLACENAMEFROM dbo.ROADS AS R

CROSS JOIN dbo.PLACES AS PWHERE (R.SP_GEOMETRY.STIntersects(P.SP_GEOMETRY) = 1)

Adding city name to the roads, that intersects a city and NULL to the other roads

Select R.*, P.PLACENAMEFROM dbo.ROADS AS R

LEFT OUTER JOIN dbo.PLACES AS POn (R.SP_GEOMETRY.STIntersects(P.SP_GEOMETRY) = 1)

Page 55: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Some (spatial) join types of SQL Server

Inner JoinSELECT R.*, P.PLACENAMEFROM dbo.ROADS AS R INNER JOIN dbo.PLACES AS PON (R.SP_GEOMETRY.STIntersects(P.SP_GEOMETRY) = 1)

Left Outer JoinSELECT R.*, P.PLACENAMEFROM dbo.ROADS AS R LEFT OUTER JOIN dbo.PLACES AS PON (R.SP_GEOMETRY.STIntersects(P.SP_GEOMETRY) = 1)

Cross JoinSELECT R.*, P.PLACENAMEFROM dbo.ROADS AS R CROSS JOIN dbo.PLACES AS P

WHERE (R.SP_GEOMETRY.STIntersects(P.SP_GEOMETRY) = 1)

Page 56: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Triggers i SQL Server

Page 57: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

What are Triggers?

A trigger can be called when something happens in the database

It can be called when records are inserted, updated, deleted

It can be called when a user logs onto the database

A trigger can be used to stamp records with information on who changed them and when

A trigger can be used to copy changed records to a historic table

Page 58: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Temporary tables in triggers

SQL Server has two temporary tables, that can be accessed in a trigger:

inserted Contains the new records, that are to be inserted. This table

contains the changed records as they look after the change Exists on insert and on update

deleted Contains the records that have been deleted or updated. This table

contains the records as they look before they are changed/deleted Exists on update and on delete

Page 59: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Trigger statement

CREATE TRIGGER [schema_name.] trigger_name

ON { table | view }

[ WITH <dml_trigger_option> [ ,...n ] ]

{ FOR | AFTER | INSTEAD OF } { [INSERT] [,] [UPDATE] [,] [DELETE]}

[ NOT FOR REPLICATION ]

AS { sql_statement [ ; ]

[ ,...n ]

| EXTERNAL NAME <method specifier [ ; ] > }

Page 60: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Create a timestamp trigger on update

CREATE TRIGGER dbo.trg_MY_TABLE_AfterUpdate

ON dbo. MY_TABLE

AFTER UPDATE

AS

Begin

Update dbo.MY_TABLE

Set DATE_CHANGED = CURRENT_TIMESTAMP,

USER_CHANGED = SYSTEM_USER

Where MI_PRINX In (Select MI_PRINX From inserted);

End

GO

Page 61: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Create a timestamp trigger after insert

CREATE TRIGGER dbo.trg_MY_TABLE_AfterInsert

ON dbo. MY_TABLE

AFTER INSERT

AS

Begin

Update dbo.MY_TABLE

Set DATE_CREATED = CURRENT_TIMESTAMP,

USER_CREATED = SYSTEM_USER

Where MI_PRINX In (Select MI_PRINX From inserted);

End

GO

Page 62: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Create a ”history” trigger

CREATE TRIGGER dbo.trgCopyToHistory ON dbo.ADM_KOMMUNE_2007 AFTER DELETE,UPDATEAS Begin Insert Into dbo.ADM_KOMMUNE_2007_history (OBJECTID, NAME, OBJECTTYPE, ADM_CODE

, MI_PRINX_ORIGINAL, SP_GEOMETRY, MI_STYLE) Select OBJECTID, NAME, OBJECTTYPE, ADM_CODE

, MI_PRINX, SP_GEOMETRY, MI_STYLEFrom deleted

EndGo

Page 63: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Trigger updating X and Y columns

Create TRIGGER [dbo].[trg_MYTABLE_AfterUpdate]    ON  [dbo].[MYTABLE]    AFTER INSERT, UPDATE AS BEGIN

UPDATE DBO.MYTABLE SET X = SP_GEOMETRY.STX,

Y = SP_GEOMETRY.STY WHERE MI_PRINX IN (SELECT MI_PRINX FROM inserted)

END

Page 64: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Creating triggers

Page 65: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Using MapBasic to access SQL Server

Page 66: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Process

Create connectionnConn = Server_Connect("ODBC“, "DSN=Memphis")

Do somethingServer Create MapServer Create StyleServer Create TableServer Link TableRegister TableServer RefreshServer Set MapServer_ExecuteCommit Table…

Close ConnectionServer nConn Disconnect

Page 67: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Server_Execute

You can use the Server_Execute function to send SQL statements to the SQL Server to be executed

Make sure that you have an connection to the database open

Print Server_Execute(1, ”Drop table dbo.PLACES”)

Print Server_Execute(1, ”Delete From MapInfo.MapInfo_MapCatalog

Where OWNERNAME = ’dbo’

and TABLENAME = ’PLACES’”)

Do remember to replace the ” and ’ in the MapBasic window when copied from a PowerPoint

Page 68: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

MapBasic applications - FindDBMS

Find DBMS is a small utility showing how a search can be done using a spatial database on the back end

The tool searches a predefined table in the database using the connection #1. This could be changed to be a specific data source.

The tool lets you enter some value to search for. When you hit Enter, the SQL Select is sent to the database and the matching values are returned and shown in a listbox.

When you click on any of the returned values, the matching record is highlighted in the map and the map is zoomed to the record

Note that you should have the table searched open in a map. The name of this table is specified in the config file: FIND DBMS.ini

Page 69: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Find DBMS

Page 70: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Find DBMS

Page 71: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

MapBasic applications - TableRefresh

TableRefresh is a small utility that will search a file structure for tables

The tables found will be opened and if they are either a Linked or a WFS table, the application can refresh them if this has been specified

The tool is controlled by the configuration file

Page 72: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Final Words

We have now looked at how to

•access a spatial database from MapInfo Professional

•upload data to the spatial database using EasyLoader

•open tables from a spatial database from MapInfo Professional

•create views in a spatial database

•implement some triggers in the spatial database

•take advantage of the spatial database from MapBasic

Page 73: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

Thank you!

Peter Horsbøll Møller

[email protected]

Any Questions?

Page 74: Taking Advantage of a Spatial Database with MapInfo Professional

MapInfo User Conference 2014: GIS Gets Personal #MapInfoUC

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