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?

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jun 11, 2014 at 3:36
4
  • 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? Commented Jun 11, 2014 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. Commented Jun 11, 2014 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. Commented Jun 11, 2014 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. Commented Jun 11, 2014 at 7:08

2 Answers 2

3

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.

answered Jun 11, 2014 at 7:14
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.

Andre Silva
10.5k12 gold badges57 silver badges109 bronze badges
answered Jul 24, 2016 at 22:54

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.