1

I have imported some shape files into SQL Server 2008. Now i want to use OpenLayers with my MVC 4 application to render those shape files from SQL Server Database.

Any help in this regard?

  • Welcome to GIS SE! Would you be able to edit your Question to include details of what you have already tried with OpenLayers, and where you are stuck, please? – PolyGeo Jun 11 '14 at 4:40
  • I am new to open layers. I have imported my shape files into SQL server by using SHP-to-SQL tool. Now i have the shape file in SQL server. I want to render those shape files in my view and query some data. I just dont know how to move forword from here. – Mustafa Sattar Jun 11 '14 at 5:18
  • There is a possible duplicate of A good tutorial for Openlayers available to help you get started. Once you have reviewed/tried some/all of the suggestions there I think you will be in a better position to edit your Question into something that is more focussed. – PolyGeo Jun 11 '14 at 5:52
  • I'm voting to keep it open, since the linked answer doesn't show how to actually create a WMS service, or some other service/protocol which OpenLayers can use. – Devdatta Tengshe Jun 11 '14 at 7:08
4

The database, only stores the data, and you need some way of getting that data in a format which OpenLayers will understand.

The easiest way of doing this would be to have a WMS server which can connect to your database, and serve out the data as a WMS service which you can then consume in OpenLayers.

I'll suggest that you look into using GeoServer. Using an extension, you can connect to spatial data in MS SQL from Geoserver. Please see this documentation page for more details.

| improve this answer | |
1

I have done this but with Postgres. You could create an action to get the json from the controller to the html. Openlayers can create a layer using json data or geojson, using something like this:

   var xhttp;
   xhttp = new XMLHttpRequest();
   xhttp.onreadystatechange = function ()
   {
       if (xhttp.readyState == 4 && xhttp.status == 200)
       {
           return xhttp;
       }
   };
   xhttp.open("GET", 'g_estados/GetEstadosJson', false);
   xhttp.send();

   var data = JSON.parse(xhttp.responseText);

   var format = new ol.format.WKT();
   var features = new Array();

   for (i = 0, len = data.length; i < len; i++)
   {
       var fea = format.readFeature(data[i].wkt, {
           dataProjection: 'EPSG:3857',
           featureProjection: 'EPSG:3857'
       });

       fea.setId(data[i]['id']);
       features.push(fea);
   }

   var layer = new ol.layer.Vector({
       id: 'test',
       name: 'test',
       source: new ol.source.Vector({
           useSpatialIndex: true,
           format: 'wkt',
           features: features
       }),
       visible: true
   });
   map.addLayer(layer);

and an Action like this:

   public JsonResult GetEstadosJson()
   {
       IEnumerable<g_estado> data = db.g_estado.ToList();

       return new JsonResult()
       {
           Data = data,
           MaxJsonLength = 86753090,
           JsonRequestBehavior = JsonRequestBehavior.AllowGet
       };
   }

MaxJsonLenght limits the size of data returned in the action, so make sure there is not a lot of data to return, it could produce 2 kind of errors, limit exceeded or out of memory if this get too long.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.