Getting Started With OpenLayers 3 And Spatial Data

Recently, I got an opportunity to work with OpenLayers and thought it worth sharing. In this article I am going to give you a jump start to visualize spatial data using OpenLayers. In the demo I will be using SQL Server 2008 and OpenLayers 3.

Before start working with OpenLayers and spatial data, let me explain these two buzzwords in short so that you can jump start.


It’s an open source project for all your mapping needs, which is being update frequently by its community and their (developer) work is really appreciated to bring all GIS mapping needs all together.

It provides tiles and vector layers so that we can work with existing GIS server and map providers like google and bing maps. And using vector layer we can add any geometry in map (which may exist in SQL Server , ORACLE or other databases which support Spatial data types).

Since its open source you can customize and implement as per your need. However it provide great flexibility to extend its feature.

You can read more about OpenLayers.

Spatial Data Types

Definition from msdn:

There are two types of spatial data. The geometry data type supports planar, or Euclidean (flat-earth), data. The geometry data type both conforms to the Open Geospatial Consortium (OGC) Simple Features for SQL Specification version 1.1.0 and is compliant with SQL MM (ISO standard).

In addition, SQL Server supports the geography data type, which stores ellipsoidal (round-earth) data, such as GPS latitude and longitude coordinates.

Actually there are 2 types of spatial data: 1. Represent geometry in 2 dimensions, and another is geography which is ellipsoidal representation of data.

Here is how we differentiate those:



It is a vast topic to describe here, but you can read more on msdn.

Now in this article I will explain how you can visualize your raw spatial data on map using openlayer3.

In this demo I will be using SQL Server 2008 and OpenLayers 3, from SQL Server 2008 there is support for spatial data types. Here is my table for this demo:

support for spatial data types

As you can see I can choose either geography or geometry. For jump start I will be using simple geometry type. Geometry include different types in it, as below



So we can use any of the following:

  • Point
  • LineString
  • Plygon
  • Multipolygon
  • MultilineString
  • Multipoint

In this demo we will be working with Point, LineString and Polygon and see the results on map.

Note: While working on Spatial data SRID (which represent Spatial Reference Systems) is important.

As per Wikipedia “A spatial reference system (SRS) or coordinate reference system(CRS) is a coordinate-based local, regional or global system used to locate geographical entities. A spatial reference system defines a specific map projection, as well as transformations between differentspatial reference systems.

Note: I am not going to use SRID for this example as it is not required in current scenario but while working with real application you must consider the same.

Step 1: Create a simple table in SQL as above screen. Table name is TEST_SPATIAL (script attached).

Step 2: Now we will insert some spatial data in table.

Since SQL Server have lot of function to work with Spatila data, I am going to use STGeomFromText function to get spatial data from text format(known as WKT(well known text))

Inserting Spatial data

Inserting point:

  1. DECLARE @data nvarchar(max) = 'POINT (1 10)'-- Point  
  2. DECLARE @g geometry;  
  3. SET @g = geometry::STGeomFromText(@data, 0);  
  5. insert into dbo.TEST_SPATIAL  
  6. values(@g)  
Same as we can insert LineString as ‘LINESTRING (4 4 4 4, 9 0 4 4)’ and Polygon as ‘POLYGON ((0 0, 30 0, 30 30, 0 30, 0 0))’.

Now you will see your spatila data in binary format as below:

spatila data in binary

You can also see spatial result view as :

spatial result

Step 3: Now we have our spatial data ready. In order to place data in map, .NET fretwork provide library Microsoft.SqlServer.Types to work with, which is the same library we are using in SQL (using common language runtime).

The purpose of this library is to manipulate and work with Spatial data in CLR compatible language like C#. Since we just want to represent our data in map without any manipulation we will not use this for now.

Referencing OpenLayers3 in ASP.NET project

In order to reference OpenLayers3 in any ASP.NET website (or web application) we just required the following files:


  1. ol.js: This file contains core of OpenLayers.
  2. ol.css: This file contains styles for map.

You can find those file on OpenLayers website or reference, where debug file is for development purpose.

I would also like to mention that there are helper libraries Proj4.js for front end, and proj4net for backend (.net), which can make you GIS development easy.

For this demonstration I am not going to use any of above as I will be using smart feature of OpenLayers which is now available in openlayer3. With the help of this we can read WKT(well known text) from database. STAsText() function provide wkt format of a geometry.

Here is my simple function to query spatial data from SQL in wkt:

  1. public List < string > GetSpatialData()  
  2. {  
  3.     List < string > spatialDataList = new List < string > ();  
  4.     dbConnection.Open();  
  5.     var command = new SqlCommand("select [GEOMETRY].STAsText() from TEST_SPATIAL where GEOMETRY IS NOT NULL", dbConnection);  
  6.     var rdr = command.ExecuteReader();  
  7.     while (rdr.Read())  
  8.     {  
  9.         spatialDataList.Add(Convert.ToString(rdr[0]));  
  10.     }  
  11.     dbConnection.Close();  
  12.     return spatialDataList;  
  13. }  
In Default.aspx page first of all we required map object as follows:
  1. var map = new ol.Map(  
  2. {  
  3.     layers: [layer],  
  4.     target: 'map',  
  5.     view: new ol.View(  
  6.     {  
  7.         center: [0, 0],  
  8.         zoom: 2  
  9.     })  
  10. });  
This is the base object of map to be rendered. There are 3 keys layers ,target ,view.
  • Layer: In a laymen term layer is an another map rendering on existing map, which may contain geometries/objects to be displayed on map.
  • Target: this is a html dom element id in which map will be rendered.
  • View: This defines the initial position of map and its zoom level.

Apart from above there are several configurations which are not required in this demonstration. As there any many types of layer, I will use OSM (open street map) and Vector layer. We use Vector layer when we need to display geometries from our database.

We create object of OSM layer as follows:

  1. var osmLayer = new ol.layer.Tile({  
  2.    source: new ol.source.OSM()  
  3. });  
And will pass osmLayer object to map layer key as:
  1. var map = new ol.Map(  
  2. {  
  3.     layers: [osmLayer],  
  4.     target: 'map',  
  5.     view: new ol.View(  
  6.     {  
  7.         center: [0, 0],  
  8.         zoom: 2  
  9.     })  
  10. });  
And here is output:

See output

Next step is to add SQL spatial geometries to map. There are no of ways to render spatial data in openlayer3, we can use json object, we can manually create features (explained below) and add them on map or can read wkt formatted geometry (which we are going to see).

Features are the object we see on the map layer. Feature can be line, polygon point. In order to visualize those features we use style to add color to these geometries.

To visualize spatial data on map, first of all we will put all geometries in a JavaScript array for simplicity.
  1. var geometries = []; // javascript array   
  3. <% GettingStarted.GeoHelper helper = new GettingStarted.GeoHelper();  
  5.    foreach (var item in helper.GetSpatialData())  
  6.    { %>  
  8.       geometries.push('<%=item %>');  
  10. <% } %>  
Now will create object of wkt parser/formatter/reader and will create array of features.
  1. var wktReader = new ol.format.WKT();  
  2. var featureCollection = []; // features to render on map  
To visualize features on map we need to apply styling on features as per geometry type:
  1. for (var i = 0; i < geometries.length; i++)  
  2. {  
  3.     var feature = wktReader.readFeature(geometries[i]);  
  4.     feature.getGeometry().transform('EPSG:4326''EPSG:3857');  
  5.     if (feature.getGeometry().getType() == 'Polygon')  
  6.     {  
  7.         feature.setStyle(new  
  8.         {  
  9.             stroke: new  
  10.             {  
  11.                 color: 'blue',  
  12.                 width: 3  
  13.             }),  
  14.             fill: new  
  15.             {  
  16.                 color: 'rgba(0, 0, 255, 0.1)'  
  17.             })  
  18.         }));  
  19.         featureCollection.push(feature);  
  20.     }  
  21.     else if (feature.getGeometry().getType() == 'LineString')  
  22.     {  
  23.         feature.setStyle(new  
  24.         {  
  25.             stroke: new  
  26.             {  
  27.                 color: 'red',  
  28.                 width: 3  
  29.             })  
  30.         }));  
  31.         featureCollection.push(feature);  
  32.     }  
  33.     else if (feature.getGeometry().getType() == 'Point')  
  34.     {  
  35.         feature.setStyle(new  
  36.         {  
  37.             image: new /** @type {} */ (  
  38.             {  
  39.                 anchor: [0.5, 46],  
  40.                 anchorXUnits: 'fraction',  
  41.                 anchorYUnits: 'pixels',  
  42.                 opacity: 0.75,  
  43.                 src: 'Icons/marker.png'  
  44.             }))  
  45.         }));  
  46.         featureCollection.push(feature);  
  47.     }  
  48. }  
We use feature.setStyle(style) function to apply style. Also note we must transform all geometries to map transformation( projection) using:
  1. transform('EPSG:4326''EPSG:3857');  
Which we configure using SRID in database (as mentioned earlier).

Since we are having 3 types of geometries for we have applied style accordingly. You can use any styling as per your requirement.

Now we will create Vector source using source object and add featureCollection:
  1. var source = new ol.source.Vector({  
  2.    features: featureCollection   
  3. });  
Create layer using layer object and add source to it:
  1. var vectorLayer = new ol.layer.Vector({  
  2.    source: source  
  3. });  
And at last, add layer to map object:
  1. var map = new ol.Map({  
  2.    layers: [osmLayer, vectorLayer],  
  3.    target: 'map',  
  4.    view: new ol.View({  
  5.       center: [0, 0],  
  6.       zoom: 2  
  7.    })  
  8. });  


I would try to create specific geometry for a regions but it’s bit difficult to create coordinates for complex geometries.

Final Words: This was just a jump start demo with OpenLayers and Spatial data. For further reading you can visit where you can find API docs and source code, you can consider MSDN for Spatial data types.

Your ideas and suggestions will be appreciated.
Source code uploaded. 

Read more articles on Spatial Data: