using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.ADF;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Display;
namespace QueryFilters
{
public sealed partial class MainForm : Form
{
#region class private members
private IMapControl3 m_mapControl = null;
private string m_mapDocumentName = string.Empty;
#endregion
#region class constructor
public MainForm()
{
InitializeComponent();
}
#endregion
private void MainForm_Load(object sender, EventArgs e)
{
//get the MapControl
m_mapControl = (IMapControl3)axMapControl1.Object;
//disable the Save menu (since there is no document yet)
menuSaveDoc.Enabled = false;
}
#region Main Menu event handlers
private void menuNewDoc_Click(object sender, EventArgs e)
{
//execute New Document command
ICommand command = new CreateNewDocument();
command.OnCreate(m_mapControl.Object);
command.OnClick();
}
private void menuOpenDoc_Click(object sender, EventArgs e)
{
//execute Open Document command
ICommand command = new ControlsOpenDocCommandClass();
command.OnCreate(m_mapControl.Object);
command.OnClick();
}
private void menuSaveDoc_Click(object sender, EventArgs e)
{
//execute Save Document command
if (m_mapControl.CheckMxFile(m_mapDocumentName))
{
//create a new instance of a MapDocument
IMapDocument mapDoc = new MapDocumentClass();
mapDoc.Open(m_mapDocumentName, string.Empty);
//Make sure that the MapDocument is not readonly
if (mapDoc.get_IsReadOnly(m_mapDocumentName))
{
MessageBox.Show("Map document is read only!");
mapDoc.Close();
return;
}
//Replace its contents with the current map
mapDoc.ReplaceContents((IMxdContents)m_mapControl. Map);
//save the MapDocument in order to persist it
mapDoc.Save(mapDoc.UsesRelativePaths, false);
//close the MapDocument
mapDoc.Close();
}
}
private void menuSaveAs_Click(object sender, EventArgs e)
{
//execute SaveAs Document command
ICommand command = new ControlsSaveAsDocCommandClass();
command.OnCreate(m_mapControl.Object);
command.OnClick();
}
private void menuExitApp_Click(object sender, EventArgs e)
{
//exit the application
Application.Exit();
}
#endregion
//listen to MapReplaced event in order to
update the statusbar and the Save menu
private void axMapControl1_OnMapReplaced(
object sender, IMapControlEvents2_OnMapReplacedEvent e)
{
//get the current document name from the MapControl
m_mapDocumentName = m_mapControl.DocumentFilename;
if (m_mapDocumentName == string.Empty)
{
menuSaveDoc.Enabled = false;
statusBarXY.Text = string.Empty;
}
else
{
//enable the Save menu and write the doc name to the statusbar
menuSaveDoc.Enabled = true;
statusBarXY.Text = Path.GetFileName(m_mapDocumentName);
}
}
private void axMapControl1_OnMouseMove(
object sender, IMapControlEvents2_OnMouseMoveEvent e)
{
statusBarXY.Text = string.Format("{0}, {1} {2}",
e.mapX.ToString("#######.##"),
e.mapY.ToString("#######.##"),
axMapControl1.MapUnits.ToString().Substring(4));
}
private void btnLandlocked_Click(object sender, EventArgs e)
{
IFeatureLayer fLayer;
fLayer = axMapControl1.get_Layer(2) as IFeatureLayer;
IFeatureClass fClass;
fClass = fLayer.FeatureClass;
IQueryFilter qFilter = new QueryFilter();
IFeatureCursor fCursor;
qFilter.WhereClause = "LANDLOCKED = \'Y\'";
fCursor = fClass.Search(qFilter, true);
double dblArea = 0;
long lngCountries = 0;
lngCountries = fClass.FeatureCount(qFilter);
IFeature feat = fCursor.NextFeature();
while (feat != null)
{
dblArea += Convert.ToDouble(feat.get_Value(10));
// Area in square km is the 11th field
feat = fCursor.NextFeature();
}
MessageBox.Show(String.Format("Number of landlocked countries:
{0}\r\n Area: {1:#} sq km", lngCountries, dblArea));
}
private void btnSpatialFilter_Click(object sender, EventArgs e)
{
IFeatureSelection cntryLyr;
ISelectionSet cntrySel;
ICursor cntryCursor;
cntryLyr = axMapControl1.get_Layer(2) as IFeatureSelection;
cntrySel = cntryLyr.SelectionSet;
cntrySel.Search(null, true, out cntryCursor);
IFeatureCursor featCursor = cntryCursor as IFeatureCursor;
IFeature country;
country = featCursor.NextFeature();
if ((country == null))
{
MessageBox.Show("Please select a country");
return;
}
ISpatialFilter spatFilt;
spatFilt = new SpatialFilter();
IQueryFilter qFilt;
qFilt = spatFilt;
spatFilt.WhereClause = "Population > 2000000";
spatFilt.Geometry = country.Shape;
spatFilt.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains;
IFeatureLayer cityLyr;
IFeatureClass cityFClass;
cityLyr = axMapControl1.get_Layer(0) as IFeatureLayer;
cityFClass = cityLyr.FeatureClass;
IFeatureCursor cityCursor;
cityCursor = cityFClass.Search(spatFilt, true);
long lngCities = 0;
lngCities = cityFClass.FeatureCount(spatFilt);
MessageBox.Show("This country has" + "\r\n"
+ lngCities + " cities with population > 2,000,000");
}
private void btnSelectCities_Click(object sender, EventArgs e)
{
IMap map;
map = axMapControl1.ActiveView.FocusMap;
IFeatureSelection featSel;
featSel = m_layer as IFeatureSelection;
IQueryFilter qFilt = new QueryFilter();
qFilt.WhereClause = "Population > 10000000";
if (featSel != null)
featSel.Clear();
else
return;
featSel.SelectFeatures(qFilt,
esriSelectionResultEnum.esriSelectionResultNew, false);
MessageBox.Show(featSel.SelectionSet.Count + " features selected");
IRgbColor grn = new RgbColor();
grn.Green = 255;
featSel.SelectionColor = grn;
axMapControl1.ActiveView.Refresh();
}
private void axTOCControl1_OnMouseDown(object sender,
ITOCControlEvents_OnMouseDownEvent e)
{
IBasicMap map = null;
ILayer layer = null;
object other = null;
object index = null;
esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone;
axTOCControl1.HitTest(
e.x, e.y, ref item, ref map, ref layer, ref other, ref index);
if ((item == esriTOCControlItem.esriTOCControlItemLayer))
{
m_layer = layer;
}
}
}
}