用C#编写arcgis engine程序,如何查询某个字段里的某项内容 40
一张云南省mxd地图中有某个图层,是各个县的范围,这个图层的属性表中有个字段是各个县的名字,如何用程序查询某个点在哪个县,并且得到县的名字???用空间过滤器时需要打开工作...
一张云南省mxd地图中有某个图层,是各个县的范围,这个图层的属性表中有个字段是各个县的名字,如何用程序查询某个点在哪个县,并且得到县的名字???
用空间过滤器时需要打开工作空间,或者设置空间参考吗? 展开
用空间过滤器时需要打开工作空间,或者设置空间参考吗? 展开
1个回答
展开全部
首先查询点坐标的位置要素,然后根据空间查询分析,进行查询县要素上的名称
具体代码如下:
IPoint point = new PointClass();
point.X = 94.82; //你要查询的点坐标位置
point.Y = 32.61; //你要查询的点坐标位置
ISpatialFilter spatialFilter = new SpatialFilterClass();
spatialFilter.Geometry = (point as IPoint) as IGeometry;//设置用于筛选几何对象
spatialFilter.GeometryField = "Shape"; //设置筛选几何字段
spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;//设置空间几何关系
IFeatureLayer featureLayer = this.axMapControl1.Map.get_Layer(0) as IFeatureLayer; //县的图层
IFeatureCursor featureCursor = null;
featureCursor = featureLayer.FeatureClass.Search(spatialFilter, false);
IFeature feature = null;
feature = featureCursor.NextFeature();
while (feature != null)
{
string strName = feature.get_Value(feature.Fields.FindField("NAME")).ToString(); //你要查询的县的名称
feature = featureCursor.NextFeature();
}
System.Runtime.InteropServices.Marshal.ReleaseComObject(featureCursor);
具体代码如下:
IPoint point = new PointClass();
point.X = 94.82; //你要查询的点坐标位置
point.Y = 32.61; //你要查询的点坐标位置
ISpatialFilter spatialFilter = new SpatialFilterClass();
spatialFilter.Geometry = (point as IPoint) as IGeometry;//设置用于筛选几何对象
spatialFilter.GeometryField = "Shape"; //设置筛选几何字段
spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;//设置空间几何关系
IFeatureLayer featureLayer = this.axMapControl1.Map.get_Layer(0) as IFeatureLayer; //县的图层
IFeatureCursor featureCursor = null;
featureCursor = featureLayer.FeatureClass.Search(spatialFilter, false);
IFeature feature = null;
feature = featureCursor.NextFeature();
while (feature != null)
{
string strName = feature.get_Value(feature.Fields.FindField("NAME")).ToString(); //你要查询的县的名称
feature = featureCursor.NextFeature();
}
System.Runtime.InteropServices.Marshal.ReleaseComObject(featureCursor);
更多追问追答
追问
按你的程序写了,感觉写的很有道理啊,但为什么总是执行不到while里面的内容呢?
追答
说明你的 feature 为null,你需要检查下条件有没有问题
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询