ArcEngine C# GIS开发入门作业 (四)Ex05——空间数据库查询选中要素高亮,并计算
ArcEngine C# GIS开发入门作业 (四)Ex05——空间数据库查询选中要素高亮,并计算
题目:
实现效果
注意事项先不重复了,前面几篇文章都有,直接上代码,这几天在实习要顺便写个实习的文章,这个作业就先还来不及行行注释了,为了保持作业的连续性,先把作业发完,后面再注释:
form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.ADF;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
namespace Ex05
{
public partial class Form1 : Form
{
#region class private members
private IMapControl3 m_mapControl = null;
private string m_mapDocumentName = string.Empty;
#endregion
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//get the MapControl
m_mapControl = (IMapControl3)axMapControl1.Object;
//disable the Save menu (since there is no document yet)
}
private void axMapControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
{
IPoint pPoint = new PointClass();
ITopologicalOperator pTopologicalOperator = pPoint as ITopologicalOperator;
pPoint.PutCoords(e.mapX, e.mapY);
IGeometry pGeometry = pTopologicalOperator.Buffer(0);
axMapControl1.Map.SelectByShape(pGeometry, null, false);
axMapControl1.Refresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
}
private void axMapControl1_OnMapReplaced(object sender, IMapControlEvents2_OnMapReplacedEvent e)
{
//get the current document name from the MapControl
m_mapDocumentName = m_mapControl.DocumentFilename;
}
private void QueryFilter_Click(object sender, EventArgs e)
{
if (axMapControl1.Map.LayerCount != 0)
{
IFeatureLayer pGeoLayer = this.axMapControl1.get_Layer(2) as IFeatureLayer;
ITable pTable = pGeoLayer.FeatureClass as ITable;
IFeatureCursor pCursor;
IQueryFilter pQueryFilter = new QueryFilter();
pQueryFilter.WhereClause = "LANDLOCKED = \'Y\'";
pCursor = (IFeatureCursor)pTable.Search(pQueryFilter, true);
IFeature pFea = pCursor.NextFeature();
double area = 0;
IArea pAea = pFea.Shape as IArea;
if (pFea != null)
{
if (pFea.Shape.GeometryType == esriGeometryType.esriGeometryPolygon)
{
area = area + pAea.Area;
pFea = pCursor.NextFeature();
}
}
int i = 1;
while (pFea != null)
{
i++;
if (pFea.Shape.GeometryType == esriGeometryType.esriGeometryPolygon)
{
area = area + pAea.Area;
pFea = pCursor.NextFeature();
}
}
MessageBox.Show(i + " countries with LANDLOCKED = Y \n " + "The sum of acreage:" + area + " Square Meter");
}
}
private void SpatialFliter_Click(object sender, EventArgs e)
{
ISelection pSelection = axMapControl1.Map.FeatureSelection;
IEnumFeatureSetup pEnumFeatureSetup = pSelection as IEnumFeatureSetup;
pEnumFeatureSetup.AllFields = true;
IEnumFeature pEnumFeature = pSelection as IEnumFeature;
IFeature pFeature = pEnumFeature.Next();
if (pFeature != null)
{
IFeatureLayer pFL = (IFeatureLayer)axMapControl1.get_Layer(0);
IFeatureClass pFC = pFL.FeatureClass;
ISpatialFilter pSF = new SpatialFilterClass();
pSF.WhereClause = "Population>2000000";
pSF.Geometry = pFeature.Shape;
pSF.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains;
IFeatureCursor pCursor;
pCursor = pFC.Search(pSF, true);
MessageBox.Show(pFC.FeatureCount(pSF) + " cities with population > 2000000");
}
else MessageBox.Show("未选中Country");
}
private void SelectCities_Click(object sender, EventArgs e)
{
ISelection pSelection = axMapControl1.Map.FeatureSelection;
IEnumFeatureSetup pEnumFeatureSetup = pSelection as IEnumFeatureSetup;
pEnumFeatureSetup.AllFields = true;
IEnumFeature pEnumFeature = pSelection as IEnumFeature;
IFeature pFeature = pEnumFeature.Next();
if (pFeature != null)
{
IFeatureLayer pFL = (IFeatureLayer)axMapControl1.get_Layer(0);
IFeatureClass pFC = pFL.FeatureClass;
ISpatialFilter pSF = new SpatialFilterClass();
pSF.WhereClause = "Population>2000000";
pSF.Geometry = pFeature.Shape;
pSF.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains;
IFeatureCursor pCursor;
pCursor = pFC.Search(pSF, true);
IFeatureSelection pFS = pFL as IFeatureSelection;
pFS.SelectFeatures(pSF, esriSelectionResultEnum.esriSelectionResultAdd, false);
axMapControl1.ActiveView.Refresh();
}
else MessageBox.Show("未选中Country");
}
}
}
program.cs
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS;
namespace Ex05
{
static class Program
{
private static LicenseInitializer m_AOLicenseInitializer = new 王晓佳_1609040125_Ex05.LicenseInitializer();
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop);
//ArcGIS的控件需要这行代码的绑定访问
if (!RuntimeManager.Bind(ProductCode.Engine))
{
if (!RuntimeManager.Bind(ProductCode.Desktop))
{
MessageBox.Show("Unable to bind to ArcGIS runtime. Application will be shut down.");
return;
}
}
//ESRI License Initializer generated code.
m_AOLicenseInitializer.InitializeApplication(new esriLicenseProductCode[] { esriLicenseProductCode.esriLicenseProductCodeEngine },
new esriLicenseExtensionCode[] { });
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
//ESRI License Initializer generated code.
//Do not make any call to ArcObjects after ShutDownApplication()
m_AOLicenseInitializer.ShutdownApplication();
}
}
}