3

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.

8
  • How are you creating the selection polygon? Is this just an arbitrary area inside the West Midlands? (Obvious question - but there isn't any chance of overlapping with another county is there? And are the other counties returned neighbouring ones?) One other thing, you could try running the query in SQL Server to check you're getting back what you expect from the query. Commented Oct 25, 2012 at 17:25
  • My selection polygon is a rough box around the West Midlands created using a drawing control. The other returned counties in my cut down dataset are Tyneside and South Yorkshire, neither of which intersect my drawn polygon, but do intersect the bounding box being pulled through for feature CountyDay2.4 in the code above (386080,272232 to 475119,576159) so technically the spatial select is working, I just don't know where the new coordinates are coming from.
    – Pete
    Commented Oct 26, 2012 at 7:44
  • Isn't the bounding box for CountDay2.4 just the bounding box of what is being returned rather than the query that is being run? Might be worth profiling SQL Server and seeing what query is being run there and what is getting returned on the database side of things (i.e. is the county data ok). Also, what happens if you draw other areas - does the query sometimes work or do you always get extra results? This might highlight any systematic errors. Commented Oct 26, 2012 at 8:36
  • I've tried drawing round other areas, they all result in a very different set of coordinates being returned compared to the polygon I'm drawing. A couple more examples:
    – Pete
    Commented Oct 26, 2012 at 8:48
  • Draw round Preston (362859,413445 to 376130,431739) I get a box returned at (362859,212863 to 527085,576159) Likewise, drawing round Tyne and Wear (409543,545129 to 441987,576159) gives me a box at (409543,378558 to 475119,576159). The expanded boxes always relate to coordinates from other features, but the features chosen for bounds aren't consistent. I'll take a look in SQL and see what it thinks is going on.
    – Pete
    Commented Oct 26, 2012 at 8:54

2 Answers 2

1

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.

8
  • Thanks for the link and additional information. When I switch to WFS version 1.1.0 in my WFS protocol, I notice that the format of what is returned alters and I get no features returned. This is the case when i switch from 1.0.0 to 1.1.0 using my shapefile data. Do I need to change some other configuration settings somewhere? I've seen GML2 and GML3 formats mentioned but haven't had any luck utilising them. I'm seeing bounding boxes being replaced by envelopes that use lower / upper corner tags, rather than a string of coordinates, to define them.
    – Pete
    Commented Oct 30, 2012 at 13:02
  • Your protocol constructor should be this: dev.openlayers.org/docs/files/OpenLayers/Protocol/WFS/… Commented Oct 30, 2012 at 13:14
  • I've now got WFS version 1.1.0 working but am still having some issues. I have to have a reference to EPSG:4326 in my mapfile Web--MetaData--wfssrs otherwise I get a "Invalid GetFeature Request:Invalid SRS" error in my response. I've no other references to anything 4326 so not sure why this is needed. With 4326 added, I can see the XML response coming back, but no polygons appear on screen. I can even draw a polygon filter and see the response alter. However, as soon as I use SQL instead of shp, I get the same issue as my original posts, just a multisurface now due to using WFS 1.1.0
    – Pete
    Commented Oct 30, 2012 at 14:43
  • MULTILINESTRING types have issues in MapServer, so this may be the same with MULTIPOLYGONS. I'd recommend splitting them into single part features and seeing if the issue remains. Commented Oct 30, 2012 at 14:57
  • I've removed any multipart features and there is still no change. I've tweaked my layer definition in my aspx page so that I can now switch easily between shp and sql data by altering the mapfile specified in the URL. There are no differences in terms of projections etc specified in the map files, I've been through them line by line. Using a shapefile the intersect works perfectly but with the SQL connection it still produces a massively different polygon from what I've drawn before carrying out the intersect. I'm now officially out of ideas...
    – Pete
    Commented Oct 30, 2012 at 16:27
0

Having elimanated as many facotrs as possible, it turns out that I was having an issue with the plugin connection to SQL Server. Once I changed my mapfile to use the OGR connection instead of the plugin, my problems were mostly solved. However, I did find that some polygons were not displaying properly, and having looked at the response coming back through Firebug, found that I was getting the following:

OGR_MSSQLSpatial: 204 features read on layer 'COUNTYss'.
ODBC: SQLDisconnect()

appearing in the middle of my coordinates. If anyone else comes across this issue a colleague found that the debug settings in my mapfile were affecting the output. Once I set

CONFIG “CPL_DEBUG” “OFF”

rather than "ON", the polygons were then drawn correctly and the intersect issue I was having in my original question was also resolved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.