the strength of a spatial database

51
Peter Horsbøll Møller Senior Systems Engineer Pitney Bowes Software

Upload: peter-horsboll-moller

Post on 15-Jan-2015

362 views

Category:

Technology


2 download

DESCRIPTION

A presentation I used at a session at the Insights Conference in June 2012 in New Orleans

TRANSCRIPT

Page 1: The strength of a spatial database

Peter Horsbøll MøllerSenior Systems Engineer

Pitney Bowes Software

Page 2: The strength of a spatial database

The Strength of a Spatial Database

Peter Horsbøll MøllerSenior Systems EngineerJune 15, 2012

Every connection is a new opportunity™

Page 3: The strength of a spatial database

The spatial database

The basis

The benefits

The next step

1

2

3

4

3

Agenda

Page 4: The strength of a spatial database

What is a spatial database?

• In my view a spatial database is a database that can handle alfanumerical as well as spatial data

• If you are working with MI Pro you probably use a spatial database, but maybe you aren’t aware: The tab-format is one example of a spatial database.

• But in this session we will look at ”larger” spatial databases, like SQL Server 2008, Oracle or PostgreSQL/PostGIS and not basic filbased databases

4

Page 5: The strength of a spatial database

The spatial database

The basis

The benefits

The next step

1

2

3

4

5

Agenda

Page 6: The strength of a spatial database

The basis

• We have MapInfo Professional 10.0 or even never

• We have a spatial database, such as• SQL Server 2008• Oracle• PostgreSQL/PostGIS

• My examples are based on SQL Server 2008

Page 7: The strength of a spatial database

The basis

• We have a connection between MapInfo Professional and the database using ODBC or OCI

• We have prepared the database with the MapInfo MapCatalog

Data

Data

Page 8: The strength of a spatial database

The basis

• We have uploaded our data with, for instance, EasyLoader

• So the tools are ready, the database is ready and the data is ready ....

• If not I’ll need another two hours to get you upon speed on that, too

Page 9: The strength of a spatial database

What now?

• Now it’s time to take advantage of the spatial database

Page 10: The strength of a spatial database

The spatial database

The basis

The benefits

The next step

1

2

3

4

10

Agenda

Page 11: The strength of a spatial database

What advantages?

There are plenty• Security (and data access)• Triggers• Views• Programmability• Multi user editing• ”Open access”

• ... just to mention some

Page 12: The strength of a spatial database

Security & Data access

Page 13: The strength of a spatial database

Security

• Only allow access to the data to those who should access it

• Access can be given as database user or as a Windows/OS user

Page 14: The strength of a spatial database

Security

Security can be handled at different levels:• On the database server• On the database• On a schema• On a table

• and as read, write or read-write access

Choose the one that fits your need

Page 15: The strength of a spatial database

Data Access

As a MapInfo Professional user you can access the database thru a linked table or thru a live table.

• Linked stores data in a tab file• Live always reads data from the database

Page 16: The strength of a spatial database

Data Access

Each type has it’s pros and cons:

• From a security perspective you might want to choose Live

• From a performance perspective you might want to choose linked

• But other things might influence this choice

Page 17: The strength of a spatial database

Data Access

• Use LIVE WITH CACHE when– Your data must be kept secure– 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 or year

– Performance is important– You want to do advanced spatial analysis on the data in

MapInfo Professional

Page 18: The strength of a spatial database

Data Access

Page 19: The strength of a spatial database

Triggers

Page 20: The strength of a spatial database

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 21: The strength of a spatial database

What are Triggers good for?

Triggers can be used for a veriaty of things:• 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• A trigger can do certain checks before data is interted

Page 22: The strength of a spatial database

Tables within a trigger

• SQL Server, for one, has two temporary tables, that can be accessed within 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 23: The strength of a spatial database

Create a timestamp trigger on update

CREATE TRIGGER dbo.trg_MY_TABLE_AfterUpdate ON dbo. MY_TABLEAFTER UPDATEAS Begin Update dbo.MY_TABLE Set DATE_CHANGED = CURRENT_TIMESTAMP,

USER_CHANGED = USER Where MI_PRINX In (Select MI_PRINX From inserted);EndGO

Page 24: The strength of a spatial database

Create a timestamp trigger after insert

CREATE TRIGGER dbo.trg_MY_TABLE_AfterInsert ON dbo. MY_TABLEAFTER INSERTAS Begin Update dbo.MY_TABLE Set DATE_CREATED = CURRENT_TIMESTAMP,

USER_CREATED = USER Where MI_PRINX In (Select MI_PRINX From inserted);EndGO

Page 25: The strength of a spatial database

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 26: The strength of a spatial database

Views

Page 27: The strength of a spatial database

What are views?

A view • can be seen as a way to look at your data• behaves very much like a table, but doesn’t hold any

data• Extracts data from one or more tables – or views

Page 28: The strength of a spatial database

What are views good for?

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

• merge multiple columns to one column, for instance merge road name and house no to an address column

• enrich one table with data from another table, for instance by transfering the postal area name from a postal table to a table with addresses

• merge multiple tables into one, for instance merge several tables with names to search for into one search table

Page 29: The strength of a spatial database

Please note!

• Do remember to add the views to the MapCatalog if you want to use these inside MapInfo Professional as mappable tables.

• You can do this thru the Table > Maintenance > Make DBMS table mappable option within MapInfo Professional

Page 30: The strength of a spatial database

Nice user interface!

Page 31: The strength of a spatial database

Filtering view

Create View [dbo].[viewROAD_MOTORWAYS] AsSELECT VEJNAVN, VEJKODE, VEJKLASSE, RUTENR

, MI_STYLE, MI_PRINX, SP_GEOMETRYFROM [UKGIS].[dbo].[KEY_ROAD]Where VEJKLASSE = 1

Page 32: The strength of a spatial database

Column merging view

Create View [dbo].[viewADDRESSES] AsSelect VEJNAVN

+ ' ' + Cast(HUS_NR as VarChar) + HUSBOGSTAV + ', ' + Cast(POSTNR As VarChar) + ' ' + POSTDIST As SEARCHVALUE

, MI_PRINX, SP_GEOMETRYFrom MAPS.KEY_ADDRESS

Page 33: The strength of a spatial database

Union view

Create View [dbo].[viewSearch] WITH SCHEMABINDING AsSelect VEJNAVN

+ ' ' + Cast(HUS_NR as VarChar) + HUSBOGSTAV + ', ' + Cast(POSTNR As VarChar) + ' ' + POSTDIST As SEARCHVALUE, MI_PRINX, SP_GEOMETRY

From MAPS.KEY_ADDRESSUnion AllSelect MAS_MATRNR + ' ' + ELAVNAVN As SEARCHVALUE

, (1000000 + MI_PRINX) As MI_PRINX

, SP_GEOMETRYFrom MAPS.KEY_CADASTRE

Page 34: The strength of a spatial database

Views

Page 35: The strength of a spatial database

Programmability

Page 36: The strength of a spatial database

Programma-what?

• With stored procedures and user defined functions you can build part of your business logic into the database

• A stored procedure can be compared to a batch job• A user defined function can take some parametes, does

some calculations and returns calculated value

• And you can use MapBasic or a .NET programming language to program application that take advantage of your spatial database

Page 37: The strength of a spatial database

What’s the benefit of this?

• Being able to encapsulation for instance certain calculations into a stored procedure or user defined functions, helps doing the calculation in the same way each time

• Makes it easier to reuse an already existing function/calculation

Page 38: The strength of a spatial database

Create an address function

Create FUNCTION GetAddress ( @road varchar(250), @houseno int, @letter varchar(1), @zipcode int, @ziparea varchar(250))RETURNS varchar(250)ASBEGIN

DECLARE @address varchar(250)Set @address = @road

+ ' ' + Cast(@houseno as VarChar) + @letter+ ', ' + Cast(@zipcode As VarChar) + ' ' + @ziparea

RETURN @addressEND

Page 39: The strength of a spatial database

Multi user editing

Page 40: The strength of a spatial database

Multi user editing, really?

• With a normal tab file only one user can edit this dataset at a given time. If another user tries to, he will get told that it’s not possible

• This issue is caused by file locking – MI Pro stores the changes in a set of transaction file and only one of these can exist at a time

• If each user connects to the database thru their own local tab file, the database and MI Pro can handle that multiple users edit the same dataset at the same time

• So yes, multi user editing will be possible

46

Page 41: The strength of a spatial database

How are conflicts handled?

A conflict might arrise if two users edit the same record at ”the same time”

When MI Pro is updating a record in the database, it compares the current database record with the record when it was read from the database

• If they are the same, the update continues• If they aren’t the same, someone else changed the

record in the database and a conflict resolution dialog appears

47

Page 42: The strength of a spatial database

”Open Access”

Page 43: The strength of a spatial database

What do you mean?

• Data is stored in a database that a lot of tools and applications can access

• Even non-GIS applications can read at least the non-GIS part of the GIS data

• Even if the applications can’t view or work with the spatial data, it can still use the spatial part of the database to extract results

Page 44: The strength of a spatial database

And what should the be good for?

• You can design and create spatial views in the database, that returns a non spatial dataset

• Eventhough your query does use spatial data to find the result, the spatial data might not be necessary for the end user (GIS without the Map)

Page 45: The strength of a spatial database

Build data forms using MS Access

• You could use MS Access to build easy to use data entry forms for your spatial tables.

• These could be used for your non-GIS users

Page 46: The strength of a spatial database

Business systems

• If your business system can access the database, it can use the sptial data or the spatial queries without really understanding spatial data and spatial relationships

• Here the programmability part might also come in handy

Page 47: The strength of a spatial database

The spatial database

The basis

The benefits

The next step

1

2

3

4

53

Agenda

Page 48: The strength of a spatial database

So what do we do now?

Page 49: The strength of a spatial database

Let’s get going

• There are a number of good reasons for moving towards a spatial database ...

• ... but there are also a few pitfalls

• You do not need to be more than a handfull users to harvest some of the benefits

• All in all the benefits are bigger than the few pitfalls

• In Denmark there is a ”rush” towards spatial databases, especially in the public sector

Page 50: The strength of a spatial database

How do we get going?

• There are a number of ”free” database, that you could use to get familiar with the concept

• Maybe you already have a spatial database in-house without knowing it – or using it

• Start uploading a number of your dataset

• Consider and test the different data access types in MapInfo Professional

56

Page 51: The strength of a spatial database

Thank You

Peter Horsbøll Møller

[email protected]