如何在OpenLayers中正确配置WFS层?

时间:2013-02-01 21:24:14

标签: gis openlayers

我正在尝试在OpenLayers地图上显示WFS图层。以下是 GETCAPABILITIES 响应的适当部分。

<ows:Operation name="GetFeature">
  <ows:DCP>
    <ows:HTTP>
      <ows:Get xlink:href="http://localhost/MapServer/Default.aspx"/>
      <ows:Post xlink:href="http://localhost/MapServer/Default.aspx"/>
    </ows:HTTP>
  </ows:DCP>
  <ows:Parameter name="resultType">
    <ows:Value>results</ows:Value>
    <ows:Value>hits</ows:Value>
  </ows:Parameter>
  <ows:Parameter name="outputFormat">
    <ows:Value>text/xml; subtype=gml/3.1.1</ows:Value>
  </ows:Parameter>
</ows:Operation>
<ows:Parameter name="srsName">
  <ows:Value>EPSG:4326</ows:Value>
</ows:Parameter>
<ows:Constraint name="DefaultMaxFeatures">
  <ows:Value>20000</ows:Value>
</ows:Constraint>

我想要显示的功能:

<wfs:FeatureType>
  <wfs:Name>Data_ACTIVE</wfs:Name>
  <wfs:Title/>
  <wfs:Abstract/>
  <ows:Keywords/>
  <wfs:DefaultSRS>EPSG:4326</wfs:DefaultSRS>
  <wfs:OutputFormats>
    <wfs:Format>text/xml; subtype=gml/3.1.1</wfs:Format>
  </wfs:OutputFormats>
  <ows:WGS84BoundingBox>
    <ows:LowerCorner>-102.01545715332 28.970645904541</ows:LowerCorner>
    <ows:UpperCorner>-71.021598815918 43.1671752929688</ows:UpperCorner>
  </ows:WGS84BoundingBox>
</wfs:FeatureType>

以下是我正在使用的代码:

$(document).ready(
    function () {
        // allow testing of specific renderers via "?renderer=Canvas", etc
        var renderer = OpenLayers.Util.getParameters(window.location.href).renderer;
        renderer = (renderer) ? [renderer] : OpenLayers.Layer.Vector.prototype.renderers;
        var geographic = new OpenLayers.Projection("EPSG:4326");
        var mercator = new OpenLayers.Projection("EPSG:900913");

        map = new OpenLayers.Map({
            div: "map",
            layers: [
                new OpenLayers.Layer.WMS("OpenLayers WMS",
                    "http://vmap0.tiles.osgeo.org/wms/vmap0",
                    { layers: "basic" }
                ),
                new OpenLayers.Layer.Vector("GML", {
                    strategies: [new OpenLayers.Strategy.Fixed()],
                    protocol: new OpenLayers.Protocol.WFS({
                            url: "http://localhost/MapServer/Default.aspx",
                            featureType: "Data_ACTIVE",
                            featureNS: "http://www.tstgis.org/gml",
                            version: "1.1.0",
                            geometryName: "line"
                        }),
                        renderers: renderer
                    })
            ],
            zoom: 15
        });

        var bb = new OpenLayers.Bounds(-179.821327209473, 12.1057098342161, -56.5289154052734, 78.1442901657839);
        map.zoomToExtent(bb);
});

我看到正在向服务器发送一个帖子请求,并且结果会返回,但根本不会显示。为什么图层不显示?任何帮助将不胜感激。

注意
此测试脚本是在localhost / mapserver下运行的HTML shell的一部分,因此它排除了熟悉的跨域问题。

1 个答案:

答案 0 :(得分:1)

我毫不怀疑这是OpenLayers的配置问题。根据我对OpenLayers的了解,我已经正确配置了它。事实证明,在我的例子中,设置featurePrefix选项至关重要。原因是每个FeatureMember节点的XML响应(GML)中的命名空间和前缀用于标识响应的适当读取器。默认情况下,featurePrefix设置为“feature”。如果响应的名称空间+前缀与OpenLayers中的配置不匹配,则功能不会添加到图层中,因此不会显示。在我的例子中,前缀设置为空字符串,因为服务器没有为响应添加前缀。

此外,设置geometryName至关重要。对于WFS版本1.0,geometryName的默认值为“the_geom”,对于更高版本,则为null。服务器使用geometryName来实际定位要素。在我的例子中,服务器上使用的geometryName是“msGeometry”。

这是工作代码。

$(document).ready(
function () {
    // allow testing of specific renderers via "?renderer=Canvas", etc
    var renderer = OpenLayers.Util.getParameters(window.location.href).renderer;
    renderer = (renderer) ? [renderer] : OpenLayers.Layer.Vector.prototype.renderers;
    var geographic = new OpenLayers.Projection("EPSG:4326");
    var mercator = new OpenLayers.Projection("EPSG:900913");

    map = new OpenLayers.Map({
        div: "map",
        layers: [
            new OpenLayers.Layer.WMS("OpenLayers WMS",
                "http://vmap0.tiles.osgeo.org/wms/vmap0",
                { layers: "basic" }
            ),
            new OpenLayers.Layer.Vector("GML", {
                strategies: [new OpenLayers.Strategy.Fixed()],
                protocol: new OpenLayers.Protocol.WFS({
                        url: "http://localhost/MapServer/Default.aspx",
                        featureType: "Data_ACTIVE",
                        version: "1.1.0",
                        geometryName: "msGeometry",
                        featurePrefix: ""
                    }),
                    renderers: renderer
                })
        ],
        zoom: 15
    });

    var bb = new OpenLayers.Bounds(-179.821327209473, 12.1057098342161, -56.5289154052734, 78.1442901657839);
    map.zoomToExtent(bb);

});