[GIS] OpenLayers spatial filter polygon problem

filteropenlayers-2wfs

I've got a dataset of English Counties (currently cut down for testing) that I'm serving from SQL 2008 using MapServer and OpenLayers. The features in my dataset are appearing in the right location on my base map. I'm now trying to carry out a selection \ filter using a polygon that the user draws on screen. Set up is as follows:

County Layer

 var CountyLayerDay2 = new OpenLayers.Layer.Vector
            ("CountyDay2", {
                isBaseLayer: false,
                projection: new OpenLayers.Projection("EPSG:27700"),
                displayProjection: new OpenLayers.Projection("EPSG:27700"),
                strategies: [new OpenLayers.Strategy.Fixed()],
                protocol: new OpenLayers.Protocol.WFS({
                    url: "http://localhost/cgi-bin/mapserv.exe?map=C:/DevProjects/fgsv2/mapfile.map",
                    version: "1.0.0",
                    featureType: "CountyDay2",
                    featurePrefix: "ms",
                    featureNS: "http://mapserver.gis.umn.edu/mapserver"

My polygon drawing layer is then added as follows:

 var polyLayer = new OpenLayers.Layer.Vector
            ("Polygon",{projection: new OpenLayers.Projection("EPSG:27700"),
                        displayProjection: new OpenLayers.Projection("EPSG:27700")
                       });      

When the user draws a polygon, it is being reported as being in the right place with regards to the coordinates reported in the "Post" tab of Firebug (user has drawn round the West Midlands):

<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="ms:CountyDay2" xmlns:ms="http://mapserver.gis.umn.edu/mapserver">
<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
<ogc:Intersects>
<ogc:PropertyName>the_geom</ogc:PropertyName>
<gml:Polygon xmlns:gml="http://www.opengis.net/gml" srsName="EPSG:4326">
<gml:outerBoundaryIs>
<gml:LinearRing>
<gml:coordinates decimal="." cs="," ts=" ">-2.4628747596580483,52.856622995202905 -1.4081872598048397,52.77694589865457 -1.2983239785701848,52.01271002627193 -2.3749841346702887,52.32267900571302 -2.4628747596580483,52.856622995202905</gml:coordinates>
</gml:LinearRing>
</gml:outerBoundaryIs>
</gml:Polygon>
</ogc:Intersects>
</ogc:Filter>
</wfs:Query>
</wfs:GetFeature>

When I look on the "Response" tab I get some strange results. The initial gml:boundedby tag looks ok as the coordinates sit in the right place in terms of British National Grid coordinates as they correspond to the bounding box of the West Midlands county. However, when the response get to the gml:boundedby in the features, the coordinates hav expanded and I end up with a multi part polygon being returned as other counties are also selected:

 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.opengis.net/wfs     http://schemas.opengis.net/wfs/1.0.0/WFS-basic.xsd 
                   http://mapserver.gis.umn.edu/mapserver http://localhost/cgi-bin/mapserv.exe?map=C:/DevProjects/fgsv2/mapfile.map&amp;SERVICE=WFS&amp;VERSION=1.0.0&amp;REQUEST=DescribeFeatureType&amp;TYPENAME=ms:CountyDay2&amp;OUTPUTFORMAT=XMLSCHEMA">
      <gml:boundedBy>
        <gml:Box srsName="EPSG:27700">
        <gml:coordinates>386080.398996,272232.103705 439261.802108,307214.500253</gml:coordinates>
    </gml:Box>
  </gml:boundedBy>
<gml:featureMember>
  <ms:CountyDay2 fid="CountyDay2.4">
    <gml:boundedBy>
        <gml:Box srsName="EPSG:27700">
            <gml:coordinates>386080.398996,272232.103705 475119.498244,576159.994876</gml:coordinates>
        </gml:Box>
    </gml:boundedBy>
    <ms:msGeometry>
    <gml:MultiPolygon srsName="EPSG:27700">
    <gml:polygonMember>
      <gml:Polygon>
        <gml:outerBoundaryIs>

Can anyone spot what I've done wrong? My first concern was the conversion from lat long to British National Grid between the "Post" and "Response" tabs but it looks to work initially. I'm new to OpenLayers etc so may have set my layers up wrong. I've searched this site and been through the OpenLayers examples and can't find anything similar to what I need to do. Any pointers would be appreciated….

EDIT
I'm thinking that it might be a coordinate conversion problem from when the orginal user drawn polygon is created using SRSName:4326 and is then converted to SRSName:27700. I've tweaked a couple of things in my map setup to make sure everything is set up in SRSName: 27700, so the polygon drawn by the user is now pretty much the only thing that references SRSName:4326.

Best Answer

For some reason the projection code sent in the WFS protocol does not seem to have any effect. I had to add it as a querystring parameter for the projections to work correctly.

http://localhost/mapserver/?map=C:/PathToMapFile/mymap.map&SERVICE=WFS&VERSION=1.1.0&srsName=EPSG:900913

Also I had to use WFS version 1.1.0 to allow reprojections. More details at http://geographika.co.uk/mapserver-openlayers-and-the-wfs-maze - I kept trying everything until eventually MapServer and OpenLayers got along.