niedziela, 7 sierpnia 2011

Visualize geospatial data from SQL SERVER 2008 R2

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:
  • geography 
  • geometry
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.). 
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.)

    Figure 3. New DataSet
  • 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) 
Figure 4. Visualized data in wizard
  • Next step allow You to control how Your maps will be looks like. You have three option to choose but in this article we use second option 'Color Analytical Map' - this option required to select dataset one more again in next step.
  • Last step allow developer to choose which field will be user for coloring maps fields. In this example on the map sum of the population for each province will be evaluated (by choosing from dropdown list 'Field to visualize') (figure 5.).
Now when You Run Your project You can see You visualized spatial data in the report. This report can be deployed onto report server and be accessed by Internet or embedded in Web application.
Figure 5. Setting visualize options
Read more...