[GIS] Querying MapServer using WFS functionality

htmljavascriptmapserverwfs

I'm trying to display a shapefile using OpenLayers and Mapserver. Instead of overlaying it as a png, I want to return a vector, in this case a KML file. My HTML file (located in the same folder as mapserv on my server contains the following html/javascript:

<html>
  <head>
  <title>OpenLayers Example</title>
   <script src="http://openlayers.org/api/OpenLayers.js"></script>
   <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
    </head>
    <body>
      <div style="width:100%; height:100%" id="map"></div>
      <script type="text/javascript">

      var map = new OpenLayers.Map('map');

      var googleLayer = new OpenLayers.Layer.Google('Google', {type: google.maps.MapTypeId.SATELLITE});

       // Add WFS data
       var wfsLayer= new OpenLayers.Layer.Vector("WFS", {
           strategies: [new OpenLayers.Strategy.BBOX()],
           protocol: new OpenLayers.Protocol.WFS({
              url: "http://mysite.com/mapserv.cgi/wfs?&map=wfs.map&",
              featureType: "MLSOA",
              featureNS: "http://mysite.com/mapserv.cgi/wfs?&map=wfs.map&",
              srsName: "EPSG:27700",
              version: "1.0.0"  
          })
       });

//add layers
map.addLayers([googleLayer,wfsLayer]);

// Set initial center location
map.setCenter(new OpenLayers.LonLat(-0.13, 51.55).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject()),5);
</script>
</body>
</html>

The shapefile, MLSOA, is projected using EPSG:27700. My mapfile, is called wfs.map is as follows:

MAP # start of map file

NAME demo
STATUS ON
SIZE 420 300
EXTENT 82299.5 5002.78 655979.76 657599.55
UNITS METERS
SHAPEPATH "shapefiles"
IMAGECOLOR 255 255 255
  PROJECTION
    "init=epsg:27700"
  END

OUTPUTFORMAT
  NAME "kml"
  DRIVER KML
  MIMETYPE "application/vnd.google-earth.kml.xml"
  IMAGEMODE RGB
  EXTENSION "kml"
END

WEB
#TEMPLATE "template.html"
IMAGEPATH "/home/webapps/htdocs/output/"
IMAGEURL "/output/"
  METADATA
    "wfs_title"          "MLSOA"
    "wfs_version"           "1.0.0"
    "wfs_connectiontimeout" "60"
    "wfs_maxfeatures"       "10"
    "wfs_enable_request"   "*"
  END
END

  LAYER
     NAME "MLSOA"
     TYPE POLYGON
     STATUS DEFAULT
     CONNECTION "http://mysite.com/mapserv.cgi/wfs?&map=wfs.map&"
     CONNECTIONTYPE WFS
     CLASS
       STYLE
        COLOR 255 128 128
          OUTLINECOLOR 96 96 96
        END 
       END
     END
END

To check that this works, when I run:

http://mysite.com/mapserv.cgi/wfs?map=/home/urbmet/webapps/htdocs/wfs.map&SERVICE=WFS&REQUEST=GetCapabilities&VERSION=1.0.0

I get the following output.

<!-- MapServer version 6.0.1 OUTPUT=GIF OUTPUT=PNG OUTPUT=JPEG OUTPUT=KML SUPPORTS=PROJ SUPPORTS=AGG SUPPORTS=FREETYPE SUPPORTS=ICONV SUPPORTS=WMS_SERVER SUPPORTS=WMS_CLIENT SUPPORTS=WFS_SERVER SUPPORTS=WFS_CLIENT SUPPORTS=WCS_SERVER SUPPORTS=THREADS SUPPORTS=GEOS INPUT=POSTGIS INPUT=OGR INPUT=GDAL INPUT=SHAPEFILE -->

so I assume that it is working ok. However when I open my HTML file, the post query appears to run ok (as far as I understand):

<wfs:GetFeature xmlns:wfs="http://www.opengis.net/wfs" service="WFS" version="1.0.0" xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.0.0/WFS-transaction.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<wfs:Query typeName="feature:MLSOA" xmlns:feature="http://mysite.com/mapserv.cgi/wfs?&map=wfs.map&">
<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
<ogc:BBOX>
<ogc:PropertyName>the_geom</ogc:PropertyName>
<gml:Box xmlns:gml="http://www.opengis.net/gml" srsName="EPSG:4326">
<gml:coordinates decimal="." cs="," ts=" ">-55.50109375,43.134977942511 55.24109375,59.593781185019</gml:coordinates>
</gml:Box>
</ogc:BBOX>
</ogc:Filter>
</wfs:Query>
</wfs:GetFeature>

but the response from the server is as follows:

<?xml version='1.0' encoding="ISO-8859-1" ?>
<ServiceExceptionReport version="1.2.0" xmlns="http://www.opengis.net/ogc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/ogc http://schemas.opengis.net/wfs/1.0.0/OGC-exception.xsd">
  <ServiceException code="NoApplicableCode" locator="mapserv">
msWFSGetFeature(): WFS server error. FLTApplyFilterToLayer() failed
msWFSLayerWhichShapes(): WFS connection error. WFS request produced unexpected output (junk?) for layer MLSOA.
msBuildWFSLayerPostRequest(): WFS connection error. MapServer supports only WFS 1.0.0 or 0.0.14 (please verify the version metadata wfs_version).
msWFSLayerWhichShapes(): WFS connection error. WFS request produced unexpected output (junk?) for layer MLSOA.
msWFSLayerWhichShapes(): WFS connection error. WFS request produced unexpected output (junk?) for layer MLSOA.
msBuildWFSLayerPostRequest(): WFS connection error. MapServer supports only WFS 1.0.0 or 0.0.14 (please verify the version metadata wfs_version).
  </ServiceException>
</ServiceExceptionReport>

I'm not sure what to do to fix this error message. I've checked that Openlayers is sending a query with version: "1.0.0" encoded, but the other errors are not as clear. What can I do to fix this?

Best Answer

Your MAP file seems to indicate that your LAYER is coming from another WFS server using CONNECTIONTYPE WFS - are you sure you mean to do this?

If you just want to enable WFS for a layer using data on your server then just define a standard LAYER.

 LAYER
    NAME 'MLSOA'
    TYPE POLYGON
    DATA "C:/path/shapefile.shp"
    CLASS
      STYLE
       COLOR 255 128 128
       OUTLINECOLOR 96 96 96
      END 
    END
END

You'll need to request your WFS using the srsName of the OpenLayers map (EPSG:900913 by default - plus you will have to add this projection definition to your MAP file). Also use version 1.1.0 and add an srsName to your query string. Details and notes (of a not particularly pleasant process!) at:

http://geographika.co.uk/mapserver-openlayers-and-the-wfs-maze

Related Question