我是靠谱客的博主 拉长小熊猫,最近开发中收集的这篇文章主要介绍C#AE练习 (5)查询与选择 QueryFilter、SpatialFilter、Select Cities,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
要求:
编写程序,使用QueryFilter、SpatialFilter和FeatureCursor。
1、在Country图层中,以"LANDLOCKED = ‘Y’"为条件进行查询,并对查询结果汇总Country的个数及面积之和。
2、在Country中先选中一个Country(例如Brazil),而后以"Population > 2000000"为条件查询该Country中的所有Cities,统计其个数。
3、将2中满足条件的Cities置于被选中状态。
对象模型图:
作答:
.1.QueryFilter:
在Country图层中,以“LANDLOCKED = ‘Y’”为条件进行查询,并对查询结果汇总Country的个数及面积之和
//自定义方法FindLayer(),通过查询要素图层名来返回其相应的在地图中的索引值LayerIndex
public int FindLayer(string LayerName)
{
int i = 0;
for (; i < axMapControl1.LayerCount; i++)
{
if (axMapControl1.get_Layer(i).Name == LayerName)
break;
}
return i;
}
// QueryFilter
private void button1_Click(object sender, EventArgs e)
{
if (axMapControl1.Map.LayerCount != 0)
{
int LayerIndex = FindLayer("Country"); //自定义方法FindLayer()
IFeatureLayer pFL = axMapControl1.get_Layer(LayerIndex) as IFeatureLayer;
IFeatureClass pFC = pFL.FeatureClass;
//创建QueryFilter
//☆Search cursor查询要素图层FeatureLayer的要素类FeatureClass表中满足属性条件的结果(只是查询,没有返回结果)
IQueryFilter pQF = new QueryFilterClass();
pQF.WhereClause = "LANDLOCKED = 'Y'"; //☆设置属性查询条件,根据情况更改
//使用IFeatureClass的Search方法从QueryFilter返回查询结果FeatureCursor(查询结果用FeatureCursor返回了)
IFeatureCursor pFCur = pFC.Search(pQF, true);
//☆当FeatureCursor通过Search方法创建后,将建立一个游标(指针)指向首条记录的上面一位。要想获得第一条记录,就要采用NextFeature方法。
//☆继续调用NextFeature()方法,仍会移动游标(指针)到FeatureCursor的下一条记录,返回相应的Feature
IFeature pFea = pFCur.NextFeature(); //建立要素访问游标
int FieldIndex = pFC.FindField("shape_Area"); //面积字段索引值
int Count = 0; //查询的Country的个数
double SumArea = 0; //查询的Country的面积之和
while (pFea != null)
{
string str = pFea.get_Value(FieldIndex).ToString();
SumArea += double.Parse(str);
pFea = pFCur.NextFeature();
Count++;
}
SumArea *= 0.001 * 0.001; //转换为km²
MessageBox.Show("总面积:" + SumArea.ToString() +" km² n" + "总数:" + Count.ToString());
}
}
2.点选提示信息,SpatialFilter
//选择面图层并显示所选择的图层国家名称
private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
//-------------------点选-----------------------//
//创建鼠标点击的位置Point
IPoint pPoint = new PointClass();
pPoint.PutCoords(e.mapX, e.mapY);
//创建缓冲区
ITopologicalOperator pTO = pPoint as ITopologicalOperator;
IGeometry pGeometry = pTO.Buffer(0);
axMapControl1.Map.SelectByShape(pGeometry, null, false);
axMapControl1.Refresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
//--------------------------------------------
/---------------访问Map中FeatureLayer被选中的要素---------------///
int LayerIndex = FindLayer("Country");
IFeatureLayer pFL = axMapControl1.Map.get_Layer(LayerIndex) as IFeatureLayer;
IFeatureClass pFC = pFL.FeatureClass;
int index = pFC.FindField("CNTRY_NAME");
IFeatureSelection pFS = pFL as IFeatureSelection;
ISelectionSet pSS = pFS.SelectionSet;
ICursor pCursor;
pSS.Search(null, true,out pCursor);
IFeatureCursor pFCur = pCursor as IFeatureCursor;
IFeature pFea = pFCur.NextFeature();
while (pFea != null)
{
MessageBox.Show("你选中的国家为:" + pFea.get_Value(index).ToString());
pFea = pFCur.NextFeature();
}
}
//SpatialFilter
private void button2_Click(object sender, EventArgs e)
{
int LayerIndex = FindLayer("Cities");
IFeatureLayer pFL = axMapControl1.Map.get_Layer(LayerIndex) as IFeatureLayer;
IFeatureClass pFC = pFL.FeatureClass;
//☆获取选中要素的属性(IMap.FeatureSelection方法获取Feature的属性信息)
ISelection pSelection = axMapControl1.Map.FeatureSelection; //获取选择集
IEnumFeatureSetup pEnumFS = pSelection as IEnumFeatureSetup; //打开属性标签
pEnumFS.AllFields = true; //设置所有字段显示
IEnumFeature pEnumF = pSelection as IEnumFeature; //读取属性
IFeature pFea = pEnumF.Next();
if (pFea != null)
{
ISpatialFilter pSF = new SpatialFilterClass();
pSF.Geometry = pFea.Shape;
pSF.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains; //面中包含的点
pSF.WhereClause = "Population > 2000000";
int City_Count = pFC.FeatureCount(pSF);
/* (优化代码,统计个数可以直接用上面的pFC.FeatureCount)
IFeatureCursor pFCur = pFC.Search(pSF, true); //建立游标
IFeature pFea1 = pFCur.NextFeature();
int City_Count = 0;
while (pFea1 != null)
{
City_Count++;
pFea1 = pFCur.NextFeature();
}
*/
MessageBox.Show(City_Count.ToString() + " cities with population > 2000000");
}
else
{
MessageBox.Show("未选中Country!");
}
}
3.选中满足条件的Cities
private void button3_Click(object sender, EventArgs e)
{
int LayerIndex = FindLayer("Cities");
IFeatureLayer pFL = axMapControl1.Map.get_Layer(LayerIndex) as IFeatureLayer;
IFeatureClass pFC = pFL.FeatureClass;
----------------选中Country中的Cities-----------------/
//☆获取选中要素的属性(IMap.FeatureSelection方法获取Feature的属性信息)
ISelection pSelection = axMapControl1.Map.FeatureSelection; //获取选择集
IEnumFeatureSetup pEnumFS = pSelection as IEnumFeatureSetup; //打开属性标签
pEnumFS.AllFields = true; //设置所有字段显示
IEnumFeature pEnumF = pSelection as IEnumFeature; //读取属性
IFeature pFea = pEnumF.Next();
if (pFea != null)
{
axMapControl1.Map.ClearSelection(); //清空选择
IFeatureSelection pFS = pFL as IFeatureSelection;
ISpatialFilter pSF = new SpatialFilterClass();
pSF.Geometry = pFea.Shape;
pSF.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains;//面包含点
pSF.WhereClause = "Population > 2000000";
pFS.SelectFeatures(pSF, esriSelectionResultEnum.esriSelectionResultNew, false);
/* (优化代码,可以用上面的IFeatureSelectionn的SelectFeatures 替代 axMapControl1.Map.SelectFeaure)
IFeatureCursor pFCur = pFC.Search(pSF,true);
IFeature pFea1 = pFCur.NextFeature();
while (pFea1 != null)
{
axMapControl1.Map.SelectFeature(pFL, pFea1);
pFea1 = pFCur.NextFeature();
}
*/
}
axMapControl1.ActiveView.Refresh();
}
参考文章:
https://blog.csdn.net/m0_37768631/article/details/86034383
最后
以上就是拉长小熊猫为你收集整理的C#AE练习 (5)查询与选择 QueryFilter、SpatialFilter、Select Cities的全部内容,希望文章能够帮你解决C#AE练习 (5)查询与选择 QueryFilter、SpatialFilter、Select Cities所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复