Show WFS layer with XML query

View on GitHub

Load a WFS feature table using an XML query.

Image of show WFS layer with XML query

Use case

QueryParameters objects can't represent all possible queries that can be made against a WFS feature service. For example, query parameters don't support wildcard searches. You can provide queries as raw XML strings, allowing you to access query functionality not available with QueryParameters.

How to use the sample

Run the sample and view the data loaded from the WFS feature table.

How it works

  1. Create a WfsFeatureTable and a FeatureLayer to visualize the table.
  2. Set the feature request mode to FeatureRequestMode.manualCache.
  3. Call populateFromServiceWithXml() to populate the table with only those features returned by the XML query.

Relevant API

  • FeatureLayer
  • WfsFeatureTable
  • WfsFeatureTable.axisOrder
  • WfsFeatureTable.populateFromServiceWithXml

About the data

This service shows trees in downtown Seattle and the surrounding area. An XML-encoded GetFeature request is used to limit results to only trees of the genus Tilia.

For additional information, see the underlying service on ArcGIS Online.

Tags

feature, OGC, query, service, web, WFS, XML

Sample Code

show_wfs_layer_with_xml_query.dart
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2024 Esri
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import 'package:arcgis_maps/arcgis_maps.dart';
import 'package:arcgis_maps_sdk_flutter_samples/utils/sample_state_support.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class ShowWfsLayerWithXmlQuery extends StatefulWidget {
  const ShowWfsLayerWithXmlQuery({super.key});

  @override
  State<ShowWfsLayerWithXmlQuery> createState() =>
      _ShowWfsLayerWithXmlQueryState();
}

class _ShowWfsLayerWithXmlQueryState extends State<ShowWfsLayerWithXmlQuery>
    with SampleStateSupport {
  // Create a controller for the map view.
  final _mapViewController = ArcGISMapView.createController();
  // A flag for when the map view is ready and controls can be used.
  var _ready = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          Column(
            children: [
              Expanded(
                // Add a map view to the widget tree and set a controller.
                child: ArcGISMapView(
                  controllerProvider: () => _mapViewController,
                  onMapViewReady: onMapViewReady,
                ),
              ),
            ],
          ),
          // Display a progress indicator and prevent interaction until state is ready.
          Visibility(
            visible: !_ready,
            child: const SizedBox.expand(
              child: ColoredBox(
                color: Colors.white30,
                child: Center(child: CircularProgressIndicator()),
              ),
            ),
          ),
        ],
      ),
    );
  }

  void onMapViewReady() async {
    // Create a map with the ArcGIS Navigation basemap style and set to the map view.
    final map = ArcGISMap.withBasemapStyle(BasemapStyle.arcGISNavigation);
    _mapViewController.arcGISMap = map;

    // Load the WFS layer with the XML query.
    await loadWfsLayerWithXmlQuery();

    // Set the ready state variable to true to enable the sample UI.
    setState(() => _ready = true);
  }

  Future<void> loadWfsLayerWithXmlQuery() async {
    const wfsFeatureTableUri =
        'https://dservices2.arcgis.com/ZQgQTuoyBrtmoGdP/arcgis/services/Seattle_Downtown_Features/WFSServer?service=wfs&amp;request=getcapabilities';

    // Create the WFS feature table from URI and name.
    final statesTable = WfsFeatureTable.withUriAndTableName(
      uri: Uri.parse(wfsFeatureTableUri),
      tableName: 'Seattle_Downtown_Features:Trees',
    )
      // Set the feature request mode and axis order.
      ..axisOrder = OgcAxisOrder.noSwap
      ..featureRequestMode = FeatureRequestMode.manualCache;

    // Create the feature layer from the feature table.
    final featureLayer = FeatureLayer.withFeatureTable(statesTable);
    await featureLayer.load();

    // Add the feature layer to the map.
    _mapViewController.arcGISMap?.operationalLayers.add(featureLayer);

    // Load the query string from the assets folder.
    final xmlQuery = await rootBundle.loadString('assets/wfs_query.xml');

    // Populate the features with the query string.
    await statesTable.populateFromServiceWithXml(
      xmlRequest: xmlQuery,
      clearCache: true,
    );

    // Zoom to the full extent of the feature layer.
    _mapViewController.setViewpointGeometry(featureLayer.fullExtent!);
  }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.