MapPoint Forums

MapForums

Community of VE/MapPoint Users and Developers




Virtual Earth & SQL Server 2008 - Part 4: Integrating SQL Server 2008 and Virtual Earth (1/6)

This is a discussion on Virtual Earth & SQL Server 2008 - Part 4: Integrating SQL Server 2008 and Virtual Earth (1/6) within the Virtual Earth Blogs forums, part of the Blogs category; Integrating SQL Server 2008 and Virtual Earth So far we have given an overview about some of the important features ...


Go Back   MapPoint Forums > Blogs > Virtual Earth Blogs

Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 03-02-2008
Member
Green Belt
 
Join Date: Sep 2007
Posts: 50
Virtual Earth & SQL Server 2008 - Part 4: Integrating SQL Server 2008 and Virtual Earth (1/6)

Integrating SQL Server 2008 and Virtual Earth

So far we have given an overview about some of the important features in both SQL Server 2008 and Virtual Earth. Now we will bring the software and the service together.

We just learned that we have basically 3 options to add data from SQL Server 2008 to Virtual Earth:

  • Adding individual VEShape-objects
  • Importing data from an GeoRSS-feed into a VEShapeLayer
  • Adding Tile-Layers

We will have a look at all of these options but we will also have a look at how we can draw on Virtual Earth maps and insert the resulting VEShape-objects as geography data into SQL Server 2008.

Geocoding in SQL Server 2008

Wait a minute: geocoding in SQL Server 2008? Is that possible? Geocoding is not a feature of SQL Server 2008 but it is a feature of Virtual Earth so maybe we can bring the software and the service together in a way that they complement each other even more. The Virtual Earth Platform spans actually over 2 groups of services. The MapPoint Web Service and Virtual Earth itself. As an enterprise customer, you have always access to both services but everybody can sign-up to a free developer account for the MapPoint Web Service as well.


The key is that since SQL Server 2005 we have a Common Language Runtime (CLR) in the database. The CLR allows us to write .NET managed code for procedures or triggers and that allow us to make a calls to the MapPoint Web Service.

Let’s assume we have a table to record occurrences of “Foot and Mouth Disease”. The table is defined as follows:

CREATE TABLE [FMD](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Date] [date] NULL,
[City] [varchar](50) NULL,
[County] [varchar](50) NULL,
[Country] [varchar](50) NULL,
[Cattle] [int] NULL,
[Pigs] [int] NULL,
[Sheep] [int] NULL,
[Latitude] [float] NULL,
[Longitude] [float] NULL,
[Geom] [geography] NULL,
CONSTRAINT [PK_FMD] PRIMARY KEY CLUSTERED
(
[ID] ASC
))

Usually people will insert the date, city and county as well as the number of diseased cattle, pigs and sheep. They wouldn’t want to bother to geocode the data. On the other side it will help us a lot if we can have the geocodes and use them for some geographic analysis within SQL Server 2008

Thus the plan is to intercept the INSERT-statement with an INSTEAD OF trigger. We use the address information in the original statement as parameters in a SOAP-call to the MapPoint Web Service which returns us a latitude and longitude. With this information we can create a geography object of type point for SQL Server 2008 and then we execute an INSERT statement which adds these values to the original statement.



Before we start to develop our CLR trigger we first need to change a setting in the database to allow .NET managed code to access external data:

ALTER DATABASE VESQLWP SET TRUSTWORTHY ON

Now we can start coding. We start up the Visual Studio 2005 or higher and create a new database project. In the database project we create a new CLR trigger and we will name the class “Geocode”.



In the project properties we make sure that the assembly will have external access:



Now we add a web reference to the MapPoint Web Service and we name it MWS (https://mappoint-css.live.com/MwsSignup/Eval.aspx). Please note: If you haven’t done so yet sign up for a free developer account since you will need the credentials in a few minutes.



OK, we’re ready to go. You will find the complete code ready for download here


Please note: You will have to enter your Virtual Earth Platform User ID and Password in the file Geocode.vb)

Basically here is what happens:

...
******* initialise the MapPoint Web Service FindService (Geocoder)
*******
Dim findService As New FindServiceSoap
******* findService.Credentials = New Net.NetworkCredential(myUID, myPWD)

******* 'Determine the INSERT statement about to be executed
*******
Dim command As SqlCommand
******* Dim reader As SqlDataReader
******* Dim pipe As SqlPipe
******* Dim triggContext As SqlTriggerContext
******* triggContext = SqlContext.TriggerContext
******* pipe = SqlContext.Pipe

******* Select Case triggContext.TriggerAction
*********** Case TriggerAction.Insert
*************** Using connection As New SqlConnection("context connection=true")
******************* connection.Open()
******************* command = New SqlCommand("SELECT * FROM INSERTED;", connection)

******************* reader = command.ExecuteReader()
******************* reader.Read()

******************* 'Define Address Object for MapPoint Web Service Call
*******************
Dim myAddress As New Address
******************* myAddress.PrimaryCity = CType(reader(2), String)
******************* myAddress.Subdivision = CType(reader(3), String)
******************* myAddress.CountryRegion = CType(reader(4), String)

******************* 'Set Specification for Geocopder call
*******************
Dim findAddressSpec As New FindAddressSpecification
******************* findAddressSpec.InputAddress = myAddress
******************* findAddressSpec.DataSourceName = "MapPoint.EU"

*******************
'Call the Geocoder
*******************
Dim myFindResults As FindResults
******************* myFindResults = findService.FindAddress(findAddressSpec)

******************* 'Create Geography and replace original Statement
*******************
If myFindResults.Results.Length > 0 Then
***********************
command = New SqlCommand("INSERT into fmd (date, city, county, country, cattle, pigs, sheep, Latitude, Longitude, Geom) VALUES ('" + reader(1).ToString & "', '" & reader(2).ToString & "', '" & reader(3).ToString & "', '" & reader(4).ToString & "', " & reader(5).ToString & ", " & reader(6).ToString & ", " & reader(7).ToString & ", " & myFindResults.Results(0).FoundLocation.LatLong.Lat itude & ", " & myFindResults.Results(0).FoundLocation.LatLong.Lon gitude & ", " & "geography::STPointFromText('POINT(" & myFindResults.Results(0).FoundLocation.LatLong.Lat itude & " " & myFindResults.Results(0).FoundLocation.LatLong.Lon gitude & ")', 4326)" & ");", connection)
*********************** pipe.Send("SQL-Statement: " & command.CommandText)
******************* Else
***********************
command = New SqlCommand("INSERT into fmd (date, city, county, country, cattle, pigs, sheep, Latitude, Longitude, Geom) VALUES ('" + reader(1).ToString & "', '" & reader(2).ToString & "', '" & reader(3).ToString & "', '" & reader(4).ToString* & "', " & reader(5).ToString & ", " & reader(6).ToString & ", " & reader(7).ToString & ", null, null, null);", connection)
*********************** pipe.Send("No address found")
******************* End If
...

Now we compile the project and load the assembly into the database:

CREATE ASSEMBLY Geocode 
FROM 'C:\...\Visual Studio 2008\Projects\VE-SQL-01-Trigger\VE-SQL-01-Trigger\bin\VE-SQL-01-Trigger.dll'
WITH PERMISSION_SET = EXTERNAL_ACCESS;

Next we have to provide another assembly for XML Serialization. Why do we need to do this? Calling web service methods requires serializing all the types being used in xml. Outside SQL Server this serialization code is generated at runtime, compiled and loaded in the application. However, for security and reliability reasons, SQL Server does not allow you to dynamically load assemblies. The Visual Studio automatically recognized that we need this assembly and it compiled it in the project’s bin folder. However, SQL Server 2005 did not allow the automated provisioning, we have to load this assembly as follows:

CREATE ASSEMBLY [Geocode.XmlSerializers] 
FROM 'C:\...\Visual Studio 2008\Projects\VE-SQL-01-Trigger\VE-SQL-01-Trigger\bin\VE-SQL-01-Trigger.XmlSerializers.dll'
WITH PERMISSION_SET = SAFE;

And Finally we create the trigger on the database table

CREATE TRIGGER trig_Geocode 
ON FMD
INSTEAD OF INSERT
AS EXTERNAL
NAME [Geocode].[VE_SQL_01_Trigger.Triggers].[Geocode];

Let's insert a few records and check the results. Note: to easily review the geometry or geography data types use the method, STAsText().



(to be continued)

*



Click here to view the article.

Last edited by Eric Frost; 03-03-2008 at 08:20 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply


LinkBacks (?)
LinkBack to this Thread: http://www.mapforums.com/virtual-earth-sql-server-2008-part-4-integrating-sql-server-2008-virtual-earth-1-6-a-7222.html

Posted By For Type Date
Integrating SQL Server 2008 and Virtual Earth {Forum} - Launchwave This thread Refback 03-04-2008 02:55 PM

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads

Thread Thread Starter Forum Replies Last Post
Virtual Earth & SQL Server 2008 - Part 4: Integrating SQL Server 2008 and Virtual Earth (3/6) Johannes Kebeck's Blog Virtual Earth Blogs 0 03-02-2008 03:52 PM
Virtual Earth & SQL Server 2008 - Part 4: Integrating SQL Server 2008 and Virtual Earth (2/6) Johannes Kebeck's Blog Virtual Earth Blogs 0 03-02-2008 03:52 PM
Virtual Earth & SQL Server 2008 - Part 2: Spatial Data Management in SQL Server 2008 (3/3) Johannes Kebeck's Blog Virtual Earth Blogs 0 03-02-2008 01:40 PM
Virtual Earth & SQL Server 2008 - Part 2: Spatial Data Management in SQL Server 2008 (2/3) Johannes Kebeck's Blog Virtual Earth Blogs 0 03-02-2008 01:40 PM
Virtual Earth & SQL Server 2008 - Part 2: Spatial Data Management in SQL Server 2008 (1/3) Johannes Kebeck's Blog Virtual Earth Blogs 0 03-02-2008 01:40 PM


All times are GMT -5. The time now is 05:44 PM.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5
MP2K Magazine
Visitor Map

Flight Departures
Check Holiday Hypermarket to find flight departures to numerous exotic destinations. We link to details on flight departures and arrivals to help you find information fast.

City Break Holidays
Book city break holidays through Travel Counsellors. A personal travel advisor will help with your city break holiday plans.

Holidays Thailand
Holidays in Thailand are hugely diverse with wonderful luxury resorts and beaches but also some lovely forested mountains with interesting hill tribes. See dealchecker.co.uk.

Cancun Holidays
Cancun holidays offer beautiful scenery, calm waters and plenty of shopping. Get a taste of the good life while on holiday in Cancun.

Cheap Balearics Holidays
Cheap Balearics holidays are available if you know when to book and who to book with. The Balearics have many fascinating places to visit all year round. The partying never stops.

Cheap Travel
For cheap travel options, visit Travel.co.uk and explore the benefits of our comparison site.

Lanzarote Holidays
Visit the easternmost island of the archipelago! Book Lanzarote holidays at On The Beach!


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47