0

My .NET/OpenLayers/GeoServer/PostGres-PostGIS application allows users to create custom queries to get data from my PostGIS tables.

The queries are called in .NET (not javascript) using a web method using the following SQL query:

SELECT "tract_name", ST_AsGeoJSON("geom") FROM a_postgis_table;

The query works fine when I run it in the PostGres SQL window.

How can I pass the PostGIS results, including the GeoJSON column, back to javascript/openlayers from the .NET web method so that OpenLayers can render the vector layer in the map? There has to something really simple I am missing here. Seems like this should be an extremely common thing that people need to do.

JGH
44.4k3 gold badges49 silver badges95 bronze badges
asked Nov 13, 2015 at 6:55
1
  • What .NET web framework are you using? Commented Nov 13, 2015 at 7:55

3 Answers 3

1

I think there are three parts to getting your solution.

  1. Creating a GeoJSON FeatureArray
  2. Returing the FeatureArray from .NET
  3. Consuming that on the client

1) GeoJSON FeatureArray. Easiest way (IMHO) is to write a Postgresql query to do that. There's a guide here on how to do that, but, assuming tract_name is unique your query could be something like this:

SELECT row_to_json(fc)
 FROM ( SELECT 'FeatureCollection' As type, array_to_json(array_agg(f)) As features
 FROM (SELECT 'Feature' As type
 , ST_AsGeoJSON(lg.geom)::json As geometry
 , row_to_json(lp) As properties
 FROM a_postgis_table As lg 
 INNER JOIN (SELECT tract_name FROM a_postgis_table) As lp 
 ON lg.tract_name = lp.tract_name ) As f ) As fc;

2) .NET service There are lots of ways to do this, but I have done this in the past using WCF / svc if I've got my terminology correct. Here's a little VB sketch:

 <OperationContract()> _
 <WebInvoke(Method:="POST", ResponseFormat:=WebMessageFormat.Json, 
BodyStyle:=WebMessageBodyStyle.Bare, UriTemplate:="GetTracts")> _
 Public Function GetTracts() As Stream
 ' Code to get your Postgresql Feature set into a string
 dim json as String = "" ' The sql output
 WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8"
 Dim ms As MemoryStream = New MemoryStream(Encoding.UTF8.GetBytes(json))
 Return ms
 End Function

3) Get it into OpenLayers ( works for v 2)

I assume you have created an empty vector layer in OpenLayers called "myTracts". You can do something like this:

 var request = OpenLayers.Request.POST({
 url: serviceUrl,
 data: '{}',
 headers: {
 // contentType: "text/plain"
 "Content-Type": "application/json; charset=utf-8"
 },
 callback: onGeoJsonLoadSuccess
 });
 function onGeoJsonLoadSuccess(data) {
// Assuming your server data are in lon/lat
 var geojson_format = new OpenLayers.Format.GeoJSON({
 'internalProjection': map.baseLayer.projection,
 'externalProjection': new OpenLayers.Projection("EPSG:4326")
 });
// You might want to empty the features first...
myTracts.addFeatures(geojson_format.read(data);
}
answered Nov 13, 2015 at 13:09
0

I am not familiar with .NET, but if you are able to modify the DOM from the .NET method you could write the data to a hidden div on which a change listener is registered. eg using jQuery:

$.("#data").bind('contentchanged', function() {
 //do something with data
)};

When the event fires, the function gets triggered and you can handle the data. Another way would be to write the data to a file and load it using AJAX. For both methods you need to modify the DOM, though, as you need to let JavaScript now what to load.

answered Nov 13, 2015 at 8:07
0

You can use JavascriptSerializer to convert your .Net list to JSON or GeoJson.

GeoJsonList refers to list in data

var serializer = new JavaScriptSerializer();
var serializedResult = serializer.Serialize(GeoJsonList);
answered Nov 13, 2015 at 8:42
1
  • No need to do that - ASP.NET does that for you if you do it properly. encosia.com/… Commented Nov 13, 2015 at 11:47

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.