With the development of digital maps such as OpenStreetMaps and Google Earth or Google Maps and other GIS systems many of the softwares need to processing geospatial data which are stores in databases. In the new version of the SQL Server 2008 R2 some new features to working with geographic data were introduced. First of all developers are able to work with two new datatypes which where implemented as a .NET common language runtime (CLR) data type:
These types support methods and properties that allow for the creation, comparison, analysis, and retrieval of spatial data. The difference between then is the geometry data type (called planar) supported by SQL Server conforms to the Open Geospatial Consortium (OGC) Simple Features for SQL Specification while the geography data type (geodetic) stores ellipsoidal (round-earth) data, such as GPS latitude and longitude coordinates.
After short theoretical introduction it`s time for implementation. First of all we need to create in our test database. Let's our table stores information about provinces in Poland and its SQL code be like this:
CREATE TABLE [dbo].[Provinces](
[ProvinceID] [int] IDENTITY(1,1) NOT NULL,
[CountryID] [int] NOT NULL,
[ProvinceName] [nvarchar](255) NOT NULL,
[ProvinceNameLocal] [nvarchar](255) NULL,
[geom] [geography] NOT NULL,
[Population] [int] NULL,
CONSTRAINT [PK_Province] PRIMARY KEY CLUSTERED ([ProvinceID] ASC)) ON [PRIMARY]
As can see the Provinces table stores information about Primary Key, CountryID (Foreign Key), province full name as well as local name, population and province border which is the most important in this context. Initially, the table is of course empty but can be populated with data by using several inserts available here (may looks complicated).
After data were inserted now we can preview them by using simple query in Sql Server Managment Studio by typing SELECT ProvinceName,Geom FROM Provinces and than switch to Spatial results tab (figure 1.).
Geospatial data were visualized but only in external tools and we want to be able to embed such map in our own solution. We don`t have to write rendering engine by our own but we can use already done one. Now it`s time to open the SQL Server Business Intelligence Development Studio (it`s located in the Sql Server 2008 R2 folder in Start) and create new project of type Report Server Project. After type any name for new solution next step is to establish connection with database to create DataSource. In new window type custom name for new instance of the dataset and choose datasource You added before.
- Right click on the Report folder and from context menu DON`T select Add New Report. This time choose Add New Item and than select Report file. This because the designer don`t supports spatial data.
- On the new report designer, open Toolbox and drag MAP control on the report and after new wizard displays choose SQL Server spatial query radio.
- Now select second option "Add a new dataset with SQL Server spatial data"
- By clicking Edit you can create a custon connection string to the database in which our table witch geospatial data is located (figure 2.)
|Figure 2. New DataSource|
- In the command window type the same SQL query which we use last time (SELECT [ProvinceID] ,[CountryID],[ProvinceName],[ProvinceNameLocal],[geom],[Population] FROM [dbo].Provinces) (figure 3.)
- In the next window you can see visualized spatial data (figure 4) . In this step optionally You can embed You map in Bing Maps context as well as control edge accuracy by changing quality (in performance case use lower resolution)
- Getting Started with the geography Data Type http://msdn.microsoft.com/en-us/library/bb895266.aspx
- Working with Spatial Data http://msdn.microsoft.com/en-us/library/bb933876.aspx