[mapserver-users] Query feature using openlayers and mapserver

P Kishor punk.kish at gmail.com
Thu Oct 29 05:44:09 PDT 2009


On Thu, Oct 29, 2009 at 1:13 AM, Aypes <apestgas at yahoo.com.hk> wrote:
>
> Dear all,
>
> I might open a topic in a wrong forum, because I do not know whether I
> should ask this question in Mapserver or OpenLayers forum. If I did, I feel
> sorry about that.
>
> I want to query the layer with using openlayers. However, I do not know how
> to 'call' the templates from mapfile to the html file.I have found topics
> about query using mapserver and openlayers, but I have still no idea how to
> code in html file. The query function is fine when using mapserver alone.
>
> In the mapfile, I have put metadata into web like this:
> WEB
>        HEADER "/ms4w/Apache/htdocs/web_header.html"
>        FOOTER "/ms4w/Apache/htdocs/web_footer.html"
>        EMPTY  "empty.html"
>        Metadata
>        "wms_title" "WMS"
>        "wms_onlineresource"
> "http://localhost/cgi-bin/mapserv.exe?map=c:/ms4w/Apache/htdocs/test.map&"
>        "wms_srs"       "EPSG:4269"
>        "wms_feature_info_mime_type" "text/html"
>        END
>        template "/ms4w/Apache/htdocs/web.html"
>        imagepath "/ms4w/Apache/htdocs/tmp/"
>        imageurl "/tmp/"
> END
> PROJECTION
>        "init=epsg:4269"
> END
>
> And in the layer, data from postgresql postgis (with attribute ID) was used
> with metadata:
> Name "Abc"
> metadata
>                "wms_title" "Abc"
>                "wms_srs" "EPSG:4269"
> End
> .....
>
> I also have put header footer in layer and template in the class
> Class
>        Name "abc"
>        Template "/ms4w/Apache/htdocs/query.html"
>             ......
> END
>
> The code in html file:
> var layer = new OpenLayers.Layer.MapServer( "ABC WMS",
>
> "http://localhost/cgi-bin/mapserv.exe?map=c:/ms4w/Apache/htdocs/test.map&",
>        {layers: 'Abc'} );
> I tried {layers: 'Abc', mode: 'query', params ='ID'}  but it did not work.
>
> I found an example of query: http://www.co.sweet.wy.us/mapserver/map.html
> I want to query the layer by a click instead of dragging and a new window
> come out to display information.
> How can I make this in html file?
>
> Furthermore, I find many examples using new OpenLayers.Layer.WMS. But when I
> use it, it displays nothing. I can only use new OpenLayers.Layer.MapServer
> to display map. What is the problem?
>


Here is how I did it, and it worked for me (test for typing errors)

Step 1: In the map file

Step 1a: WEB

    WEB
    ,,

        # WMS server settings
        METADATA
            ..
            "wms_feature_info_mime_type"    "text/html"
        END
    END # WEB

Step 1b: LAYER to be queried

    LAYER
        NAME 'layer_name'
        ..

        METADATA
           ..
            'wms_include_items' "FiEld_NaMe"   # field name is possibly
                                               # case-sensitive in the case of
                                               # shape files
        END # METADATA

        DUMP true
        HEADER "/absolute/path/to/template_header.html"
        TEMPLATE "/absolute/path/to/template_body.html"
        FOOTER "/absolute/path/to/template_footer.html"
    END # LAYER

Step 2: In your HTML templates

Step 2a: in template_header.html

<!-- MapServer Template -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/transitional.dtd">
<html>
  <head>
    <title>MapServer Template Sample</title>
  </head>

Step 2b: in template_body.html

  <body>
    <b>FiEld_NaMe:</b> [FiEld_NaMe]
  </body>

Step 2c: in template_footer.html

</html>

Step 3: In your HTML file with OpenLayers code

        map.addControl(
            new OpenLayers.Control.WMSGetFeatureInfo(
                function() {
                    return {
                        url: "/url/to/mapserv",
                        maxFeatures: 1,
                        title: 'Identify features by clicking',
                        queryVisible: true,
                        layers: [layer_name],
                        vendorParams: {
                            map: "/absolute/path/to/mapfile"
                        },
                        eventListeners: {
                            getfeatureinfo: function(event) {
                                map.addPopup(
                                    new OpenLayers.Popup.FramedCloud(
                                        "chicken",
                                        map.getLonLatFromPixel(event.xy),
                                        null,
                                        event.text,
                                        null,
                                        true
                                    ),
                                    true
                                ); // map.addPopup
                            } // getfeatureinfo
                        } // eventListeners
                    } // return
                }
            )
        ).activate();



-- 
Puneet Kishor http://www.punkish.org
Carbon Model http://carbonmodel.org
Charter Member, Open Source Geospatial Foundation http://www.osgeo.org
Science Commons Fellow, http://sciencecommons.org/about/whoweare/kishor
Nelson Institute, UW-Madison http://www.nelson.wisc.edu
-----------------------------------------------------------------------
Assertions are politics; backing up assertions with evidence is science
=======================================================================
Sent from Madison, WI, United States



More information about the MapServer-users mailing list