SelectByQuery.cs

This example demonstrates how to select shapes with certain attributes using expression. The query string is: [type] = "residential" AND [osm_id] > 40000000. Here is a screenshot with the results of the code.

´╗┐using System;
using System.Windows.Forms;
using AxMapWinGIS;
using MapWinGIS;
using System.IO;

public partial class MapExamples
{
    // <summary>
    // Selects shapes with certain attributes.
    // </summary>
    public void SelectByQuery(AxMap axMap1, string dataPath)
    {
        string filename = dataPath + "landuse.shp";
        Shapefile sf = new Shapefile();
        if (sf.Open(filename, null))
        {
            // showing labels for [name] field
            sf.Labels.Generate("[type]", tkLabelPositioning.lpCentroid, true);
            
            int layerHandle = axMap1.AddLayer(sf, true);

            string error = "";
            object result = null;
            
            // the text values must be placed in quotes; we need to shield them with \ sign in C#
            // fields are must be placed in square brackets
            string query = "[type] = \"residential\" AND [osm_id] > 40000000";

            if (sf.Table.Query(query, ref result, ref error))
            {
                int[] shapes = result as int[];
                if (shapes != null)
                {
                    for (int i = 0; i < shapes.Length; i++)
                    {
                        sf.set_ShapeSelected(shapes[i], true);
                    }
                }
                axMap1.ZoomToSelected(layerHandle);
                MessageBox.Show("Objects selected: " + sf.NumSelected);
            }
            else
            {
                MessageBox.Show("No shapes agree with the condition.");
            }
        }
    }
}
 All Classes Files Functions Enumerations Properties