Add WFS layer

View on GitHub

Display a layer from a WFS service, requesting only features for the current extent.

Image of add WFS layer

Use case

WFS is an open standard with functionality similar to ArcGIS feature services. ArcGIS Maps SDK support for WFS allows you to interoperate with open systems, which are often used in inter-agency efforts, like those for disaster relief.

How to use the sample

Pan and zoom to see features within the current map extent.

How it works

  1. Create a WfsFeatureTable with a URL.
  2. Create a FeatureLayer from the feature table and add it to the map.
  3. Listen for the ArcGISMapViewController.onNavigatonChanged event to detect when the navigation is complete.
  4. When the user is finished navigating, use WfsFeatureTable.populateFromService to load the table with data for the current visible extent.

Relevant API

  • ArcGISMapView
  • ArcGISMapViewController.onNavigationChanged
  • FeatureLayer
  • WfsFeatureTable
  • WfsFeatureTable.populateFromService

About the data

This service shows building footprints for downtown Seattle. For additional information, see the underlying service on ArcGIS Online.

Tags

browse, catalog, feature, interaction cache, layers, OGC, service, web, WFS

Sample Code

add_wfs_layer.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// 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';

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

  @override
  State<AddWfsLayer> createState() => _AddWfsLayerState();
}

class _AddWfsLayerState extends State<AddWfsLayer> 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;
  // Reference to the WFS feature table.
  late WfsFeatureTable _featureTable;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        top: false,
        child: 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);
    // Set the map to _mapViewController.
    _mapViewController.arcGISMap = map;

    // Load the WFS layer.
    await loadWfsLayerFromURL();

    // Set the initial viewpoint to Seattle Downtown using latitude, longitude and scale.
    final initialViewPoint = Viewpoint.withLatLongScale(
      latitude: 47.617207,
      longitude: -122.341581,
      scale: 5000,
    );
    //Set the initial Viewpoint on _mapViewController.
    _mapViewController.setViewpoint(initialViewPoint);

    // Add a listener for map navigation events.
    _mapViewController.onNavigationChanged.listen((isNavigating) {
      if (!isNavigating) {
        loadFeatures();
      }
    });

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

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

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

    // Create the feature layer from the feature table.
    final featureLayer = FeatureLayer.withFeatureTable(_featureTable)
      // Apply a renderer.
      ..renderer = SimpleRenderer(
        symbol: SimpleLineSymbol(
          color: Colors.red,
          width: 3,
        ),
      );
    // Wait for the feature layer to load.
    await featureLayer.load();

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

  // Call this function to load the features in the initial viewpoint.
  Future<void> loadFeatures() async {
    // Show the loading indicator.
    setState(() => _ready = false);

    // Get the current extent.
    final currentExtent = _mapViewController.visibleArea;

    // Create a query based on the current visible extent.
    final visibleExtentQuery = QueryParameters()
      ..geometry = currentExtent
      ..spatialRelationship = SpatialRelationship.intersects;

    // Populate the table with the query, leaving existing table entries intact.
    await _featureTable.populateFromService(
      parameters: visibleExtentQuery,
      clearCache: false,
      outFields: [],
    );
    // Hide the loading indicator.
    setState(() => _ready = true);
  }
}

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