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 execution.

using System.Windows.Forms;
using AxMapWinGIS;
using MapWinGIS;
namespace Examples
{
public partial class MapExamples
{
// <summary>
// Selects shapes with certain attributes.
// </summary>
public void SelectByQuery(AxMap axMap1, string dataPath)
{
axMap1.Projection = tkMapProjection.PROJECTION_GOOGLE_MERCATOR;
string filename = dataPath + "landuse.shp";
var sf = new Shapefile();
if (sf.Open(filename, null))
{
int layerHandle = axMap1.AddLayer(sf, true);
sf = axMap1.get_Shapefile(layerHandle); // in case a copy of shapefile was created by GlobalSettings.ReprojectLayersOnAdding
// showing labels for [name] field
sf.Labels.Generate("[type]", tkLabelPositioning.lpCentroid, 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.");
}
}
}
}
}
tkLabelPositioning
The available positioning of the label relative to the parent shape.
Definition: Enumerations.cs:835
tkMapProjection
Commonly used map projections to be set in Form Designer (see AxMap.Projection property).
Definition: Enumerations.cs:1741
Map component for visualization of vector, raster or grid data.
Definition: AxMap.cs:56
Provides a functionality for accessing and editing ESRI shapefiles.
Definition: Shapefile.cs:72
Labels Labels
Gets or sets the instance of the Labels class associated with the shapefile.
Definition: Shapefile.cs:184
int Generate(string Expression, tkLabelPositioning Method, bool LargestPartOnly)
Generates labels for each shape of the parent shapefile.
Definition: Labels.cs:490
tkMapProjection Projection
Sets projection of the map. It providers 2 most commonly used coordinate system/projections to be eas...
Definition: AxMap.cs:2709
bool ZoomToSelected(int layerHandle)
Zoomes map to display selected shapes of the specified shapefile.
Definition: AxMap.cs:482
int AddLayer(object Object, bool visible)
Adds a layer to the map.
Definition: AxMap.cs:1342
Shapefile get_Shapefile(int layerHandle)
Gets shapefile object associated with the layer.
Definition: AxMap.cs:1546