Skip to main content

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...

Popular posts from this blog

Full-Text Search with PDF in Microsoft SQL Server

Last week I get interesting task to develop. The task was to search input text in PDF file stored in database as FileStream. The task implementation took me some time so I decided to share it with other developers. Here we are going to use SQL Server 2008 R2 (x64 Developers Edition), external driver from Adobe, Full-Text Search technology and FileStream technology.Because this sems a little bit comlicated let`s make this topic clear and do it step by step. 1) Enable FileStream - this part is pretty easy, just check wheter You already have enabled filestream on Your SQL Server instance - if no simply enable it as in the picture below. Picture 1. Enable filestream in SQL Server instance. 2) Create SQL table to store files  - mainly ther will be PDF file stored but some others is also be allright. Out table DocumentFile will be created in dbo schema and contain one column primary key with default value as sequential GUID. Important this is out table contains FileSt...

Persisting Enum in database with Entity Framework

Problem statement We all want to write clean code and follow best coding practices. This all engineers 'North Star' goal which in many cases can not be easily achievable because of many potential difficulties with converting our ideas/good practices into working solutions.  One of an example I recently came across was about using ASP.NET Core and Entity Framework 5 to store Enum values in a relational database (like Azure SQL). Why is this a problem you might ask... and my answer here is that you want to work with Enum types in your code but persist an integer in your databases. You can think about in that way. Why we use data types at all when everything could be just a string which is getting converted into a desirable type when needed. This 'all-string' approach is of course a huge anti-pattern and a bad practice for many reasons with few being: degraded performance, increased storage space, increased code duplication.  Pre-requirements 1. Status enum type definition...

Using Newtonsoft serializer in CosmosDB client

Problem In some scenarios engineers might want to use a custom JSON serializer for documents stored in CosmosDB.  Solution In CosmosDBV3 .NET Core API, when creating an instance of  CosmosClient one of optional setting in  CosmosClientOptions is to specify an instance of a Serializer . This serializer must be JSON based and be of  CosmosSerializer type. This means that if a custom serializer is needed this should inherit from CosmosSerializer abstract class and override its two methods for serializing and deserializing of an object. The challenge is that both methods from  CosmosSerializer are stream based and therefore might be not as easy to implement as engineers used to assume - still not super complex.  For demonstration purpose as or my custom serializer I'm going to use Netwonsoft.JSON library. Firstly a new type is needed and this must inherit from  CosmosSerializer.  using  Microsoft.Azure.Cosmos; using  Newtonsoft.Json; usin...