OpenLayers 4:使用ol.format.WFS时,WFS图层未显示在地图上

时间:2017-06-16 10:06:14

标签: javascript openlayers geoserver

我正在尝试使用Openlayers 4显示WFS图层。如果我查询服务器要求格式为json并使用ol.format.GeoJSON,则图层会在地图中正确显示。但是,请求gml3(并使用ol.format.WFS)查询服务器不会显示该层。我检查了vectorSource,它确实加载了功能,但它们没有显示。

任何人都知道这可能是什么? Bellow是我的代码和xml响应的片段。

loadLayer: function() {
      var wfsFormat = new ol.format.WFS();
      var self = this;
      this._vectorSource = new ol.source.Vector({
        format: wfsFormat,
        url: function(extent) {
          var url = self.serviceUrl +                  '&request=GetFeature&typeName=cite:dl_3&outputFormat=gml3&srsname=EPSG:3857' +
              '&bbox=' + extent.join(',') + ',EPSG:3857';
          return url;
      },
        strategy: ol.loadingstrategy.bbox,
        projection: 'EPSG:3857',
      });

      this.inherited(arguments);
    }

这个加载器位于一个名为WFSLayer的类中。贝娄是主要代码:

var wfsLayer = WFSLayer({
            serviceUrl: wfsServiceUrl,
        });
        wfsLayer.loadLayer();

        var wfsVector = new ol.layer.Vector({
            source: wfsLayer.getVectorSource(),
            style: new ol.style.Style({
                stroke: new ol.style.Stroke({
                    color: 'rgba(0, 0, 255, 1.0)',
                    width: 2,
                }),
            }),
        });

        var raster = new ol.layer.Tile({
            source: new ol.source.XYZ({
                attributions: 'Tiles © <a href="https://services.arcgisonline.com/ArcGIS/' +
                'rest/services/World_Topo_Map/MapServer">ArcGIS</a>',
                url: 'https://server.arcgisonline.com/ArcGIS/rest/services/' +
                'World_Topo_Map/MapServer/tile/{z}/{y}/{x}',
            }),
        });

        var map = new ol.Map({
            layers: [raster, wfsVector],
            target: document.getElementById('map'),
            view: new ol.View({
                center: [705208.0811999999, 6839435.416199997],
                zoom: 8,
                projection: 'EPSG:3857',
            }),
        });

这是xml响应的片段:

<wfs:FeatureCollection xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:wfs="http://www.opengis.net/wfs" xmlns:gml="http://www.opengis.net/gml" xmlns:cite="http://www.opengeospatial.net/cite" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" numberOfFeatures="10070" timeStamp="2017-06-15T15:06:48Z" xsi:schemaLocation="http://www.opengeospatial.net/cite http://geoserver:8080/geoserver/wfs?service=WFS&version=1.1.0&request=DescribeFeatureType&typeName=cite%3Adl_3 http://www.opengis.net/wfs http://geoserver:8080/geoserver/schemas/wfs/1.1.0/wfs.xsd">
<gml:featureMembers>
<cite:dl_3 gml:id="dl_3.1">
<cite:the_geom>
<gml:MultiLineString srsName="http://www.opengis.net/gml/srs/epsg.xml#3857" srsDimension="3">
<gml:lineStringMember>
<gml:LineString>
<gml:posList>
498498.93497989 6786346.91870299 43.80370511 498497.04800763 6786352.73097019 43.68967084 498495.16103228 6786358.54324726 43.83243658 498498.89640871 6786359.76200893 43.70402609 498499.33127574 6786359.90391212 43.65502487 498501.21824812 6786354.09163816 43.72265914 498503.10521781 6786348.27936783 43.7882934 498502.63037784 6786348.12447027 43.82089473 498498.93497989 6786346.91870299 43.80370511
</gml:posList>
</gml:LineString>
</gml:lineStringMember>
</gml:MultiLineString>
</cite:the_geom>
</cite:dl_3>
<cite:dl_3 gml:id="dl_3.2">
<cite:the_geom>
<gml:MultiLineString srsName="http://www.opengis.net/gml/srs/epsg.xml#3857" srsDimension="3">
<gml:lineStringMember>
<gml:LineString>
<gml:posList>
498509.86707833 6786343.314062 43.70141855 498508.02631507 6786349.10070888 43.75378441 498506.18554938 6786354.88735662 43.69115027 498509.61544357 6786355.98400546 43.67794078 498510.10837676 6786356.14156283 43.62683941 498511.94913995 6786350.35491354 43.70037356 498513.78990048 6786344.56826831 43.7912077 498513.44929737 6786344.45934742 43.78340864 498509.86707833 6786343.314062 43.70141855
</gml:posList>
</gml:LineString>
</gml:lineStringMember>
</gml:MultiLineString>
</cite:the_geom>
</cite:dl_3>
<cite:dl_3 gml:id="dl_3.3">
<cite:the_geom>
<gml:MultiLineString srsName="http://www.opengis.net/gml/srs/epsg.xml#3857" srsDimension="3">
<gml:lineStringMember>
<gml:LineString>
<gml:posList>
498513.37376869 6786275.58223307 43.95293282 498510.96125125 6786281.14230307 43.92010051 498508.54873049 6786286.70237625 43.89516819 498511.70049349 6786288.07686893 43.8335572 498514.11293161 6786282.51679254 43.84838951 498516.5253664 6786276.95671993 43.89402182 498513.70936863 6786275.72873202 43.95213165 498513.37376869 6786275.58223307 43.95293282
</gml:posList>
</gml:LineString>
</gml:lineStringMember>
</gml:MultiLineString>
</cite:the_geom>
</cite:dl_3>
<cite:dl_3 gml:id="dl_3.4">
<cite:the_geom>
<gml:MultiLineString srsName="http://www.opengis.net/gml/srs/epsg.xml#3857" srsDimension="3">
<gml:lineStringMember>
<gml:LineString>
<gml:posList>
498547.56469744 6786240.41906434 43.91062195 498546.95224789 6786240.21669251 43.91602369 498547.4916903 6786238.48738942 43.91663392 498548.12052306 6786238.67374152 43.91073226 498547.56469744 6786240.41906434 43.91062195
</gml:posList>
</gml:LineString>
</gml:lineStringMember>
</gml:MultiLineString>
</cite:the_geom>
</cite:dl_3>
<cite:dl_3 gml:id="dl_3.20">
<cite:the_geom>
<gml:MultiLineString srsName="http://www.opengis.net/gml/srs/epsg.xml#3857" srsDimension="3">
<gml:lineStringMember>
<gml:LineString>
<gml:posList>
498835.21795003 6785163.91809574 46.00415766 498829.6138097 6785161.26036719 46.02577907
</gml:posList>
</gml:LineString>
</gml:lineStringMember>
</gml:MultiLineString>
</cite:the_geom>
</cite:dl_3>

1 个答案:

答案 0 :(得分:0)

我明白了。万一有人出现同样的问题。问题是我在使用带有3D图层的GML3。 Openlayers没有正确地从XML获取srsDimension属性,因此坐标将不正确。截至目前,您可以使用GML2(或之前处理XML)。我把它作为issue添加到git。