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 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;
                    }
                }
            }
        }
share|improve this question
2  
How fancy do want to get? Do you want to do fuzzy matching to catch misspellings? Full text searching? Or can you simply rely on the user to enter a (mostly) complete correct name? –  Sean Jun 17 '11 at 12:38
    
simply rely on the user to enter a (mostly) complete correct name –  Vikash Talanki Jun 17 '11 at 12:51
    
I just do this webgis youtube.com/watch?feature=player_embedded&v=gci2HkSpdz4. But do not write the search function for the object is the land .. Please help me! Please code search function in webgis use tool openlayer (geoserver) or asp.net C # ... Thanks!! –  user16236 Mar 16 '13 at 16:34
    
Hi! Welcome to the site. It sounds like you have questions that were not addressed by the answers to this one. Why not ask a new question? –  Jay Guarneri Mar 16 '13 at 17:15

2 Answers 2

up vote 4 down vote accepted

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.

share|improve this answer
    
I am using geoserver-2.0.3 and now it wud be difficult to me to switch to 2.1.x.... –  Vikash Talanki Jun 17 '11 at 14:15
    
my map projection is 900913 and the features I am storing in db are projection 4326. Now using "select st_box2d(the_geom) from table where name ilike passed_parameter" i am getting bounds in 4326 projection. How to convert them into 900913??? –  Vikash Talanki Jun 18 '11 at 17:39
    
st_transform(st_box2d(the_geom),900913) check the postgis docs to make sure that's correct syntax. –  Sean Jun 18 '11 at 22:59
    
Yeah its working...... But how to zoom to specific bounds after getting the bounds. I am really stuck up there –  Vikash Talanki Jun 19 '11 at 12:22
    
Read up on OpenLayers.Map.zoomToExtent in the OpenLayers docs. –  Sean Jun 20 '11 at 3:29

Are you looking for something like http://ian01.geog.psu.edu/geoserver/www/search/gazetteer.html , if so then you can follow the instructions at http://ian01.geog.psu.edu/geoserver_docs/apps/gaz/index.html to create your own version.

share|improve this answer
    
@iant, But I am not using GeoExt and wanted to do everything by just OL/Geoserver/postgis. –  Vikash Talanki Jun 17 '11 at 14:31
    
the requests and actual zoom to point code will be identical. All you have to do is write the code that GeoExt provides for you. –  iant Jun 17 '11 at 14:32
    
OK, I shall try this but what if the layer is a polygon or line layer –  Vikash Talanki Jun 17 '11 at 14:41
1  
    
@iant, I shall try the way you specified, But what about the Idea I got?? Its easy to get the lat lon values from the db but how to know which layer has been selected and how to send the lat lon values as parameter to zoom function to the JS??? any idea –  Vikash Talanki Jun 17 '11 at 15:07

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.