Implementing Search Functionality in OpenLayers and GeoServer

geoserveropenlayers-2postgis

I am using Openlayers,Geoserver & Postgis…. My postgis tables are having columns as id, name, POINT/POLYGON/LINE. Each table corresponds to a layer that I am showing in layerswitcher in OL. I am getting these layers via WMS from geoserver.

I want to implement search functionality so that whenever a user gives a string(name value of the table) in search box, he should be zoomed to that particular feature on the map.

Can anyone guide me how to achieve this functionality???
Any help is highly appreciated

The idea I got is:

Step 1: User has to check only one layer(the layer in which he want to search) in layerswitcher before searching . But how to get which layer has been selected??

Step 2: After knowing the selected layer , Using php search that corresponding table for the search string in names column , if it is present get the latitude and longitude values. But how to get these values and what if they are polygons/lines???

Step 3: how to send these latitude longitude values to JS so that I can zoom to that point???

I have completed 2 1/2 steps. I am using st_box2d and getting the bounds of the feature as "BOX(minx miny,maxx maxy)"…… where bounds contain BOX(minx miny,maxx maxy)

the code below is the ajax function for fetching bounds from db. Now how to use bounds variable to zoom to that feature.

 <?-- code for displaying map -->
 function searchfunction(form)
        {   
            httpObject = getHTTPObject();
            var searchvalue=form.searchvalue.value;
            if (httpObject != null) 
            {
                httpObject.open("GET","searchfeature.php?selectedlayer="+selLayer+"&searchvalue="+searchvalue,true);
                httpObject.send(null); 
                httpObject.onreadystatechange = function()
                {
                    if(httpObject.readyState == 4)
                    {
                        bounds=httpObject.responseText;
                    }
                }
            }
        }

Best Answer

You can pass query terms in the URL to an SQL View in GeoServer 2.1 and above. Create the view with an ilike clause and you should get the basic functionality. Have OpenLayers fetch the view with the supplied name as a parameter. Then, tell OpenLayers zoom to the extent of the result.

If you don't have GeoServer 2.1:

  1. Get the name of the feature with Javascript/OpenLayers,
  2. pass the name asynchronously to a php script on the server,
  3. the php script will query the database directly for the st_envelope or st_box2d of the feature with that name (select st_envelope(the_geom) from table where name ilike passed_parameter),
  4. the php script returns the extent/bounding box of the feature which is used in Javascript/OpenLayers to zoom the map.

You'll have to parse the st_envelope result into something that OpenLayers understands. You also might want to add a little buffer to the zoom extent. You can add this manually or use st_expand instead of st_envelope.