C#+AE 實現點選查詢屬性功能
實現效果如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.ADF;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Display;
namespace _0920tryTreeList
{
public partial class Form1 : Form
{
//需要建立一個公共變量表格
// public
// public DataTable dt2 = new DataTable();
// DataRow dr2;
public Form1()
{
InitializeComponent();
//第二張表格
//dt2.Columns.Add("Name");
//dt2.Columns.Add("Value");
//this.dataGridView2.DataSource = pDataTable;
//this.dataGridView2.DataSource = dt2;
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "*.mxd|*.mxd";
ofd.ShowDialog();
string fp = ofd.FileName;
axMapControl1.LoadMxFile(fp,0,Type.Missing);
}
bool bu = false;
private void button2_Click(object sender, EventArgs e)
{
bu = true;
}
//定義子節點的單擊事件也用到的公共變數
// public IFeature pFeature = null;
private void axMapControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
{
try
{
if (bu)
{
DataTable pDataTable = new DataTable();
DataRow pDataRow = null;
pDataTable.Columns.Add("ID");
pDataTable.Columns.Add("Name");
pDataTable.Columns.Add("ParentID");
pDataTable.Columns.Add("Value");
for (int i = 0; i < axMapControl1.Map.LayerCount;i++ )
{
pDataRow=pDataTable.NewRow();
string lyrName = axMapControl1.Map.get_Layer(i).Name;
pDataRow["ID"] = lyrName;
pDataRow["Name"] = lyrName;
pDataRow["ParentID"] = -1;
pDataTable.Rows.Add(pDataRow);
//開始點選查詢
IMap pMap;
pMap = axMapControl1.Map as IMap;
//獲取點圖層
IFeatureLayer pFeatureLayer;
pFeatureLayer = pMap.get_Layer(i) as IFeatureLayer;
IFeatureClass pFeatureClass;
pFeatureClass = pFeatureLayer.FeatureClass;
//獲取滑鼠點選點
IPoint pPoint;
pPoint = new PointClass();
pPoint.PutCoords(e.mapX, e.mapY);
IGeometry pGeometry;
//定義緩衝區
double db = 2;
ITopologicalOperator pTop;
pTop = pPoint as ITopologicalOperator;
pGeometry = pTop.Buffer(db);
//選取
pMap.SelectByShape(pGeometry, null, false);
pMap.ClearSelection();
//空間過濾運算
ISpatialFilter pSpatialFilter = new SpatialFilterClass();
pSpatialFilter.Geometry = pGeometry;
//設定為不同的要素型別的圖層
switch (pFeatureClass.ShapeType)
{
case esriGeometryType.esriGeometryPoint:
pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains;
break;
case esriGeometryType.esriGeometryPolyline:
pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelCrosses;
break;
case esriGeometryType.esriGeometryPolygon:
pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
break;
}
pSpatialFilter.GeometryField = pFeatureClass.ShapeFieldName;
//指標
IFeatureCursor pFeatureCursor;
pFeatureCursor = pFeatureClass.Search(pSpatialFilter, false);
IFeature pFeature;
pFeature = pFeatureCursor.NextFeature();
//開始遍歷
while (pFeature != null)
{
//獲取要素的欄位名和欄位值
int n = pFeature.Fields.FieldCount;
string sName;
string sValue;
//這句話的物件需要隨著地圖的改變而改變。可以是ID,FID 等帶有唯一標識身份的 東西。
int index = pFeature.Fields.FindField("ObjectID");
if (index == -1)
return;
IField pField = pFeature.Fields.get_Field(index);
sName = pField.Name;
sValue = pFeature.get_Value(index).ToString();
pDataRow = pDataTable.NewRow();
//之所以這樣賦值是為了保證ID的唯一性;
pDataRow["ID"] = lyrName + sValue;
pDataRow["Name"] = sValue;
pDataRow["ParentID"] = lyrName;
pDataTable.Rows.Add(pDataRow);
pFeature = pFeatureCursor.NextFeature();
}
//這個是師兄交的,指標等佔記憶體的東西在用完之後一定要釋放;!!!
System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatureCursor);
}
treeList1.DataSource = pDataTable;
treeList1.ParentFieldName="ParentID";
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
//換地圖清除資料來源
private void axMapControl1_OnMapReplaced(object sender, IMapControlEvents2_OnMapReplacedEvent e)
{
treeList1.DataSource = null;
// dataGridView1.DataSource = null;
}
//************//出現的問題是:值不在預期範圍內
private void treeList1_FocusedNodeChanged_1(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
{
try
{
int layerIndex;
if (e.Node.HasChildren)
{
return;
}
if (e.Node.ParentNode != null) //***********//這個存在bug,若節點超過兩級則出錯
{
//建立一個新的表作為屬性表
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add("Name");
dt.Columns.Add("Value");
//迴圈圖層
for (int i = 0; i < this.axMapControl1.LayerCount; i++)
{
//如果父節點名稱和圖層名相同,獲取索引
if (e.Node.ParentNode.GetValue(0).ToString() == this.axMapControl1.get_Layer(i).Name)
{
layerIndex = i;
IFeature pFeature;
pFeature = (this.axMapControl1.get_Layer(layerIndex) as IFeatureLayer).FeatureClass.GetFeature(int.Parse(this.treeList1.FocusedNode.GetValue(0).ToString())); ;
if (pFeature != null)
{
//迴圈欄位集,賦值給表dt
int n = pFeature.Fields.FieldCount;
for (int k = 0; k < n-1; k++)
{
IField pField = pFeature.Fields.get_Field(k);
string fieldName = pField.Name;
var a= pFeature.get_Value(k);
string fieldValue = pFeature.get_Value(k).ToString();
//賦值給表
dr = dt.NewRow();
dr["Name"]=fieldName;
dr["Value"]=fieldValue;
dt.Rows.Add(dr);
}
gridControl1.DataSource = dt;
}
else
return;
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}