(四)点空间查询
IFeatureLayer pFeatureLayer = this.axMapControl1.get_Layer(1) as IFeatureLayer;
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
IPoint pPoint = new PointClass ();
pPoint.PutCoords(83.140965992759618, 49.669371563063862);//pPoint的坐标
ISpatialFilter pSpatialFilter = new SpatialFilterClass();
pSpatialFilter.Geometry = pPoint;//原图形
pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;//查询条件intersect
IFeatureCursor pFeatureCursor = pFeatureClass.Search(pSpatialFilter ,false );//将查询结果赋给指针
IFeature pFeature = pFeatureCursor.NextFeature();//指针后移所指向的要素,赋给pFeature
while (pFeature != null)
{
this.axMapControl1.FlashShape(pFeature .Shape ,3,300,null );
pFeature = pFeatureCursor.NextFeature();
}
(五)面空间查询
//查询与一个面intersect的点有哪些
listView1.Items.Clear();
listView1.Columns.Clear();
IFeatureLayer pFeatureLayer = this.axMapControl1.get_Layer(0) as IFeatureLayer;//Layer0是点图层,1是面图层
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
// 获取字段名
int iFieldCount = pFeatureClass.Fields.FieldCount;
for (int i = 0; i < iFieldCount; i++)
{
string strFieldName = pFeatureClass.Fields.get_Field(i).Name;
listView1.Columns.Add(strFieldName);
}
IEnvelope pEnv = new EnvelopeClass ();
pEnv.PutCoords(83.140965992759618 - 10, 49.669371563063862 - 10, 83.140965992759618 + 10, 49.669371563063862 + 10);
//pPoint.PutCoords(83.140965992759618,49.669371563063862);pPoint的坐标
ISpatialFilter pSpatialFilter = new SpatialFilterClass();
pSpatialFilter.Geometry = pEnv ;//原图形
pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;//查询条件intersect
IFeatureCursor pFeatureCursor = pFeatureClass.Search(pSpatialFilter, false);//将查询结果赋给指针
IFeature pFeature = pFeatureCursor.NextFeature();//指针后移所指向的要素,赋给pFeature
while (pFeature != null)
{
ListViewItem pListViewItem = new ListViewItem();
pListViewItem.Text = pFeature.get_Value(0).ToString();
//添加字段值
for (int i = 1; i < iFieldCount; i++)
{
//添加字段值
string strFieldValue = pFeature.get_Value(i).ToString();
ListViewItem.ListViewSubItem pListViewSbuItem = new ListViewItem.ListViewSubItem();
pListViewSbuItem.Text = strFieldValue;
pListViewItem.SubItems.Add(pListViewSbuItem);
}
listView1.Items.Add(pListViewItem);
this.axMapControl1.FlashShape(pFeature.Shape, 3, 300, null);
pFeature = pFeatureCursor.NextFeature();
}
(六)点空间查询和面空间查询
private bool boolPointQuery = false ;
private bool boolEnvQuery = false;
private void btnSpatialQuery_Click(object sender, EventArgs e)
{
this.boolEnvQuery = true;
this.boolPointQuery = false;
listView1.Items.Clear();
listView1.Columns.Clear();
}
private void btnPointSpatial_Click(object sender, EventArgs e)
{
this.boolPointQuery = true;
this.boolEnvQuery = false;
listView1.Items.Clear();
listView1.Columns.Clear();
}
private void axMapControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
{
listView1.Items.Clear();
listView1.Columns.Clear();
//点空间查询,单个图层
if (this.boolPointQuery == true)
{
IFeatureLayer pFeatureLayer = this.axMapControl1.get_Layer(1) as IFeatureLayer;
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
//Create a point to use for the search;
ESRI.ArcGIS.Geometry.IPoint pPoint = new ESRI.ArcGIS.Geometry.PointClass();
pPoint.PutCoords(e.mapX, e.mapY);//49.669371563063862);
//pPoint.PutCoords(83.140965992759618, 55.4932623480468);//49.669371563063862);
//Getting a reference;
ISpatialFilter pPpatialFilter = new SpatialFilterClass();
//Setting the Geometry,GeometryField and SpatialRel parameters;
pPpatialFilter.Geometry = pPoint;
pPpatialFilter.GeometryField = pFeatureClass.ShapeFieldName;
pPpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects ;//.esriSpatialRelWithin ;//.esriSpatialRelIntersects;//.esriSpatialRelWithin ;//.esriSpatialRelIntersects;
// 获取字段名
int iFieldCount = pFeatureClass.Fields.FieldCount;
for (int i = 0; i < iFieldCount; i++)
{
string strFieldName = pFeatureClass.Fields.get_Field(i).Name;
listView1.Columns.Add(strFieldName);
}
//添加要素
try
{
//Execute the spatiafilter;
IFeatureCursor pFeatureCursor = pFeatureClass.Search(pPpatialFilter, false);
IFeature pFeature = pFeatureCursor.NextFeature();
while (pFeature != null)
{
ListViewItem pListViewItem = new ListViewItem();
pListViewItem.Text = pFeature.get_Value(0).ToString();
//添加字段值
for (int i = 1; i < iFieldCount; i++)
{
//添加字段值
string strFieldValue = pFeature.get_Value(i).ToString();
ListViewItem.ListViewSubItem pListViewSbuItem = new ListViewItem.ListViewSubItem();
pListViewSbuItem.Text = strFieldValue;
pListViewItem.SubItems.Add(pListViewSbuItem);
}
listView1.Items.Add(pListViewItem);
//闪烁查询到的要素
this.axMapControl1.FlashShape(pFeature.Shape, 2, 300, Type.Missing);
//下一个要素
pFeature = pFeatureCursor.NextFeature();
}
}
catch (Exception ax)
{
MessageBox.Show(ax.ToString());
}
}
//多边形空间查询,多个图层
if (this.boolEnvQuery == true)
{//用于查询的矩形(相当于点的缓冲区,这样比生成缓冲区节省资源),
//envelope的大小根据实际情况设定,以方便使用为准
IEnvelope pEnv = new EnvelopeClass();
pEnv.PutCoords(e.mapX - 2, e.mapY - 2, e.mapX + 2, e.mapY + 2);
IGeometry pGeometry = pEnv as IGeometry;
ISpatialFilter pSpatialFilter = new SpatialFilterClass();
pSpatialFilter.Geometry = pGeometry;
pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;//.esriSpatialRelContains;
//没有必要将地图上的高亮选择清除,因为下面对每个图层的选择都用esriSelectionResultNew,会自动清除上次的高亮显示,
//而不同图层之间的高亮选择不会有任何影响,因为IFeatureSelection接口的一切属性和方法都是针对一个图层
// this.axMapControl1.Map.ClearSelection();
IFeatureLayer pFeatureLayer;
IFeatureSelection pFeatureSelection;
ISelectionSet pSelectionSet;
IEnumIDs pEnumIDs;
IFeature pFeature;
pFeatureLayer = this.axMapControl1.get_Layer(0) as IFeatureLayer;
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
// 获取字段名到ListView中
int iFieldCount = pFeatureClass.Fields.FieldCount;
for (int i = 0; i < iFieldCount; i++)
{
string strFieldName = pFeatureClass.Fields.get_Field(i).Name;
listView1.Columns.Add(strFieldName);
}
try
{
//循环显示所有图层上选中的要素
for (int i = 0; i < this.axMapControl1.LayerCount; i++)
{
pFeatureLayer = this.axMapControl1.get_Layer(i) as IFeatureLayer;
if (pFeatureLayer.Visible && pFeatureLayer.Selectable)
{
pFeatureSelection = pFeatureLayer as IFeatureSelection;
//选择之前先清除,这是个好习惯(除非是用Add方式)
pFeatureSelection.Clear();
pFeatureSelection.SelectFeatures(pSpatialFilter,esriSelectionResultEnum.esriSelectionResultNew, true);
pSelectionSet = pFeatureSelection.SelectionSet;
//如果选择集内有Feature
if (pSelectionSet.Count > 0)
{
//通过pEnumIDs获得该SelectionSet中的Feature的id值,再用FeatureClass.GetFeature()方法获得该Feature对象
//这里为了学习新接口而使用了IEnumIDs,为了获得SelectionSet中的Feature,可以使用其Search()方法
//获得ICursor,再使用循环获得Feature,如下注释选定行
// pSelectionSet.Search(null,false,out pCursor);
// pRow = pCursor.NextRow();
// if(pRow!=null)
// {
// }
pEnumIDs = pSelectionSet.IDs;
long id = pEnumIDs.Next();
while (id != -1)
{
pFeature = pFeatureLayer.FeatureClass.GetFeature((int)id);
ListViewItem pListViewItem = new ListViewItem();
pListViewItem.Text = pFeature.get_Value(0).ToString();
for (int j = 1; j < pFeature.Fields.FieldCount; j++)
{
//添加字段值
string strFieldValue = pFeature.get_Value(j).ToString();
ListViewItem.ListViewSubItem pListViewSbuItem = newListViewItem.ListViewSubItem();
pListViewSbuItem.Text = strFieldValue;
pListViewItem.SubItems.Add(pListViewSbuItem);
}
listView1.Items.Add(pListViewItem);
this.axMapControl1.FlashShape(pFeature.Shape, 2, 300, Type.Missing);
//MessageBox.Show("选中的Feature ID为:" + id.ToString());
id = pEnumIDs.Next();
}
pFeatureSelection.Clear();
//MessageBox.Show("选中的Feature ID为:" + id.ToString());
}
}
}
}
catch (Exception ax)
{
MessageBox.Show(ax.ToString());
}
}
}