Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I must mention first of all that I am very new to this area... I am developing a GIS application. I created a simple map using geoserver with openlayers, it is all working through apache tomcat - and is connected to postgis/postgresql database. When I click on a point (the points represent weather stations - and stored in database) it is set to display the tablewith all the data like: fid, station_name etc. What I want to do is - to store station_name(I clicked on) in a variable and use for my needs. In this case I was trying to break response.responseText in parts, and get it from there but it never worked... response.responseText - returns all the information and creates the tables. I need to only get ONE value from database. Any ideas!? Any help greatly appriciated!!!

Here is the code: In this part it is set to read a click and make a response:

map.events.register('click', map, function (e) {

                document.getElementById('nodelist').innerHTML = "Loading... please wait...";
                var params = {
                    REQUEST: "GetFeatureInfo",
                    EXCEPTIONS: "application/vnd.ogc.se_xml",
                    BBOX: map.getExtent().toBBOX(),
                    SERVICE: "WMS",
                    INFO_FORMAT: 'text/html',
                    QUERY_LAYERS: map.layers[0].params.LAYERS,
                    FEATURE_COUNT: 50,
                    Layers: 'map_with_stations',
                    WIDTH: map.size.w,
                    HEIGHT: map.size.h,
                    format: format,
                    styles: map.layers[0].params.STYLES,
                    srs: map.layers[0].params.SRS};

                // handle the wms 1.3 vs wms 1.1 madness
                if(map.layers[0].params.VERSION == "1.3.0") {
                    params.version = "1.3.0";
                    params.j = parseInt(e.xy.x);
                    params.i = parseInt(e.xy.y);
                } else {
                    params.version = "1.1.1";
                    params.x = parseInt(e.xy.x);
                    params.y = parseInt(e.xy.y);
                }

                // merge filters
                if(map.layers[0].params.CQL_FILTER != null) {
                    params.cql_filter = map.layers[0].params.CQL_FILTER;
                } 
                if(map.layers[0].params.FILTER != null) {
                    params.filter = map.layers[0].params.FILTER;
                }
                if(map.layers[0].params.FEATUREID) {
                    params.featureid = map.layers[0].params.FEATUREID;
                }
                OpenLayers.loadURL("http://localhost:82/geoserver/wms", params, this, setHTML, setHTML);
                OpenLayers.Event.stop(e);
            });
        }

        // sets the HTML provided into the nodelist element
        function setHTML(response){

            document.getElementById('nodelist').innerHTML = response.responseText;



        };
share|improve this question
add comment

3 Answers 3

Although your method should work, since you are already using OpenLayers you should use OpenLayers.Control.WMSGetFeatureInfo , here is an example from a previous, but different question here in gis.stackexchange.com:

var queryableMapLayers = [];
var getFeatureControl = new OpenLayers.Control.WMSGetFeatureInfo({
  url: your_wms_url.here // Your WMS server url here,
  drillDown: false, // Or true if you want drill down (see the docs)
  hover: false, // Or true if you want but bear in mind this could get chatty
  layers: queryableMapLayers,
  eventListeners: {
    getfeatureinfo: function (event) {
      // Code here if you want to process the results
      // From openlayers docs:
      // Triggered when a GetFeatureInfo response is received.  The event                     
      // object has a text property with the body of the response (String), 
      // a features property with an array of the parsed features, an xy 
      // property with the position of the mouse click or hover event that 
      // triggered the request, and a request property with the request 
      // itself.  If drillDown is set to true and multiple requests were 
      // issued to collect feature info from all layers, text and request 
      // will only contain the response body and request object of the last request.
    },
    beforegetfeatureinfo: function(event) {
      // Code here to set the content of queryableMapLayers
      // The event object will contain xy of mouse click
    },
    nogetfeatureinfo: function(event) {
      // Code here if no queryable layers are found
    }
  } 
});

// Add the control to your map
map.addControl(getFeatureControl);

Also look at the OpenLayers.Control.WMSGetFeatureInfo.request method for a customized request(like a fixed location request, or one not coming from a click event)

share|improve this answer
    
Thanks for tour reply! Well yes it works fine - it returns tables with the information about layer (I have one - which was made from a shapefile and spatial database with points(weather stations)). But I dont need them tables, I just need to store a value of a station_name (I clicked on) in a variable (var station; for example). So from your answer I get that I have to write a function to store a value of station_name in a variable!? –  Andrei Ivanov Mar 13 '13 at 21:49
    
Yes, inside the getfeatureinfo event callback function(where I wrote the WMSGetFeatureInfo description from the openlayers docs) you have the event object which you can parse, and get the info you need. –  pacofvf Mar 13 '13 at 22:06
add comment

Also you could do

layers: [layer_1, layer_2],
vendorParams: {propertyName:['column_name1', 'column_name2', 'column_name3']}

replace 'layer_1' and 'layer_2' with wms layers you want to query and 'column_name1'...column_name3' with real column names of those tables.

After this getfeatureinfo will get only data from those fields.

share|improve this answer
add comment

You can customize what properties are returned by using GetFeatureInfo Templates of Geoserver.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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