VERIFIED SOLUTION i
X

Applying filters to a WFS layer using the Wfs.Client namespace

UPDATED: July 30, 2018


Filters enable the user to be selective with features are returned from a WFS server, based on a set of criteria. Scalar filters, like the one below, allow queries to be conducted against specific properties of a feature.

A filter is similar to a SQL statement that includes a WHERE clause.  
Select * from US_COUNTY where County = 'York' AND State = 'SC'       
       <ogc:Filter>
            <ogc:And>
               <ogc:PropertyIsEqualTo>
                  <ogc:PropertyName>ns1:County</ogc:PropertyName>
                  <ogc:Literal>York</ogc:Literal>
               </ogc:PropertyIsEqualTo>
               <ogc:PropertyIsEqualTo>
                  <ogc:PropertyName>ns1:State</ogc:PropertyName>
                  <ogc:Literal>SC</ogc:Literal>
               </ogc:PropertyIsEqualTo>
            </ogc:And>
         </ogc:Filter>

The request uses two 'PropertyIsEqualTo' filters combined with the 'And' filter (which performs the binary AND operation). Capturing the XML request generated by this code shows the structure of the filter created:

The following sample code demonstrates the use of a filter in a WFS request.  This sample code is part of a console application that performs a request, adds the resulting feature to a map, and exports the map to a file.
using MapInfo.Wfs.Client
using MapInfo.Data
using MapInfo.Engine
using MapInfo.Mapping
using System.Drawing

        static void Main(string[] args)
        {
             //The URL of the WFS - Be sure to replace the following URL with your WFS URL
             string URL = "http://IP-address/wfs/GetFeature.ashx";

            //The file to which the map will be exported
            string exportFileName = "WfsClient_cs.gif";
            
            //Do a GetCapabilities and verify the specified URL is correct
            try
            {
                // Get the WFS capabilities of the WFS server using the HTTP GET method.
                WfsCapabilities Capabilities = WfsClient.GetCapabilities(RequestMethod.GET, URL);
            }
            catch
            {
                MessageBox.Show("Please check if " + URL + " is a valid WFS URL");
                return;
            }
                                             
            // Create the AND filter containing to attribute filters
            IFilter filter1 = new PropertyIsEqualTo(new PropertyName("miwfs","County"), new Literal("York"));
            IFilter filter2 = new PropertyIsEqualTo(new PropertyName("miwfs", "State"), new Literal("SC"));
            And andFilter = new And(new PropertyIsEqualTo[] { (PropertyIsEqualTo)filter1, (PropertyIsEqualTo)filter2 });
            IList queries = new Query[] { new Query(new TypeName("miwfs", "US_COUNTY"), null, andFilter) };
            
            // We only understand GML3 from our server
            string outputFormat = "GML3";
                        
            // Use the HTTP POST method to send the GetFeature request; -1 returns all features
            MultiFeatureCollection mfc = WfsClient.GetFeature(URL, queries ,outputFormat, -1);

            // Get all features from the USA feature type
            IFeatureCollection fc = mfc[0];

            // Iterate over the Usa MultiFeatureCollection and add each 
            // IFeatureCollection to a MemTable, etc...
            TableInfoMemTable memTableInfo = new TableInfoMemTable("myMemTable");
            foreach (Column c in fc.Columns)
            {
                memTableInfo.Columns.Add(c);
            }
            Table memTable = Session.Current.Catalog.CreateTable(memTableInfo);
            memTable.InsertFeatures(fc);

            // Create a layer from the MemTable
            FeatureLayer featureLayer = new FeatureLayer(memTable);

            // Create the map and add the layer
            Map map = Session.Current.MapFactory.CreateEmptyMap(new Size(500, 500));
            map.Layers.Add(featureLayer);
            map.SetView(featureLayer);

            // Export the map to a file
            using (MapExport mx = new MapExport(map))
            {
                mx.Format = ExportFormat.Gif;
                mx.Export(exportFileName);
            }

            // Clean up the map
            Session.Current.MapFactory.Remove(map);
        }




 

Environment Details

All Platform

Downloads

  • No Downloads