AE中画点线面查询小结(C#)

简介:  如皋项目开发基本结束,终于有时间总结整理下老。      首先从最基本的查询开始,先写空间查询部分。理下思路:选择绘图工具(点、线、框、面)-->操作地图-->高亮显示地物-->列出查询结果对话框-->单击某条记录,高亮居中显示-->地图清屏(1)选择绘图工具,操作地图。
 如皋项目开发基本结束,终于有时间总结整理下老。
      首先从最基本的查询开始,先写空间查询部分。理下思路:选择绘图工具(点、线、框、面)-->操作地图-->高亮显示地物-->列出查询结果对话框-->单击某条记录,高亮居中显示-->地图清屏
(1)选择绘图工具,操作地图。
      之前做server api for flex用到Tool 、Symbol、GraphicsLayer等object,定义绘图工具的代码示例如下:
<esri:Draw id="myDrawToolbar" map="{myMap}" graphicsLayer="{myGraphicsLayer}"  markerSymbol="{sms}" lineSymbol="{sls}" fillSymbol="{sfs}" />
跟这相比,AE开发相同他之处是同样要定义颜色IRgbColor、符号ISimpleMarkerSymbol;存在的差异有用IElement(IMarkerElement、ILineElement,ect)作为添加的对象,容器则是IGraphicsContainer。示例代码如下:
               
     IGraphicsContainer pGraphicsContainer;
                    IRubberBand pRubberBand;
                    IRgbColor pRgbColor = new RgbColorClass();
                    pRgbColor.Red = 255;
                    pRgbColor.Green = 0;
                    pRgbColor.Blue = 0;
                    ISimpleMarkerSymbol pSimpleMarkerSymbol = new SimpleMarkerSymbolClass();
                    pSimpleMarkerSymbol.Color = pRgbColor;
                    pSimpleMarkerSymbol.Size = 3;
                    IMarkerElement pMarkerElement;
                    IElement pMElement;
                    IPoint pPoint;
                    pMarkerElement = new MarkerElementClass();
                    pMarkerElement.Symbol = pSimpleMarkerSymbol as ISimpleMarkerSymbol;
                    pMElement = pMarkerElement as IElement;
                    pRubberBand = new RubberPointClass();
                    pPoint = pRubberBand.TrackNew(axMapControl1.ActiveView.ScreenDisplay, null) as IPoint;
                    pMElement.Geometry = pPoint;
                    pGraphicsContainer = axMapControl1.ActiveView as IGraphicsContainer;
                    pGraphicsContainer.AddElement(pMElement, 0);
                    axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);


(2)高亮显示地物。
AE中画点线面查询小结(C) - 一缕阳光 - 一缕阳光
 
        高亮显示地物,用到IMap的SelectByShape方法;
        C#定义: public  void  SelectByShape (       IGeometry   Shape,       ISelectionEnvironment   env,      bool   justOne   );
根据参数的要求,需要有IGeometry对象,这个来自于,上面地图操作中绘制的图形(点、线、面、框)。一般基于点查询会设置一定范围的矩形进行查询,代码如下:
                   
 IEnvelope pEnvelope = new EnvelopeClass();
                    pEnvelope.XMin = pPoint.X - 10;
                    pEnvelope.XMax = pPoint.X + 10;
                    pEnvelope.YMin = pPoint.Y - 10;
                    pEnvelope.YMax = pPoint.Y + 10;
                    pGeometry = pEnvelope as IGeometry;   


下面根据IGeometry进行查询高亮显示, SelectByShape 方法只针对IMap中可选图层,所以可以首先控制图层是否可选,然后再进行调用此方法。
补充下 ,设置某类图层不可选的代码:
       
 public bool UnselectableDLG(ref AxMapControl axMapControl)
        {
            ILayer pLayer = null;
            ICompositeLayer pCompositeLayer = null;
            IFeatureLayer pFeatureLayer = null;
            int iLayerCount;
            try
            {
                iLayerCount = axMapControl.ActiveView.FocusMap.LayerCount;
                if (iLayerCount > 0)
                {
                    for (int i = 0; i < iLayerCount; i++)
                    {
                        pLayer = axMapControl.ActiveView.FocusMap.get_Layer(i);
                        if (pLayer is IGroupLayer)
                        {
                            pCompositeLayer = pLayer as ICompositeLayer;
                            for (int j = 0; j < pCompositeLayer.Count; j++)
                            {
                                if (pCompositeLayer.get_Layer(j) is IFeatureLayer)
                                {
                                    pFeatureLayer = pCompositeLayer.get_Layer(j) as IFeatureLayer;
                                    if (pFeatureLayer.Name.Contains("DLG"))
                                    {
                                        pFeatureLayer.Selectable = false;
                                    }
                                }
                            }
                        }
                        else if (pLayer is IFeatureLayer)
                        {
                            pFeatureLayer = pLayer as IFeatureLayer;
                            if (pFeatureLayer.Name.Contains("DLG"))
                            {
                                pFeatureLayer.Selectable = false;
                            }
                        }
                    }
                    return true;
                }
                else
                {
                    if (pLayer != null)
                        Marshal.ReleaseComObject(pLayer);
                    if (pFeatureLayer != null)
                        Marshal.ReleaseComObject(pFeatureLayer);
                    if (pCompositeLayer != null)
                        Marshal.ReleaseComObject(pCompositeLayer);
                    return false;
                }
                if (pLayer != null)
                    Marshal.ReleaseComObject(pLayer);
                if (pFeatureLayer != null)
                    Marshal.ReleaseComObject(pFeatureLayer);
                if (pCompositeLayer != null)
                    Marshal.ReleaseComObject(pCompositeLayer);
                return false;
            }
            catch (System.Exception ex)
            {
                if (pLayer != null)
                    Marshal.ReleaseComObject(pLayer);
                if (pFeatureLayer != null)
                    Marshal.ReleaseComObject(pFeatureLayer);
                if (pCompositeLayer != null)
                    Marshal.ReleaseComObject(pCompositeLayer);
                return false;
            }
        }


地图查询实质就是对每个可选图层进行查询。直接上code:
地图查询://FeatureSelection of all selectable featurelayers(Array of LayerSelection)
        
public List<string> MapSelection(IMap pMap,IGeometry pGeometry)
        {
            List<string> lstRecord = null;
            string temp=string.Empty;
            ILayer pLayer = null;
            ICompositeLayer pCompositeLayer = null;
            IFeatureLayer pFeatureLayer = null;
            int iLayerCount;
            try
            {
                lstRecord = new List<string>();
                iLayerCount = pMap.LayerCount;
                if (iLayerCount>0)
                {
                    for (int i = 0; i < iLayerCount; i++)
                    {
                        pLayer = pMap.get_Layer(i);
                        if (pLayer is IGroupLayer)
                        {
                            pCompositeLayer = pLayer as ICompositeLayer;
                            for (int j = 0; j < pCompositeLayer.Count; j++)
                            {
                                if (pCompositeLayer.get_Layer(j) is IFeatureLayer)
                                {
                                    pFeatureLayer = pCompositeLayer.get_Layer(j) as IFeatureLayer;
                                    if (pFeatureLayer.Selectable)
                                    {
                                        temp = LayerSelection(pFeatureLayer, pGeometry);
                                        if (!temp.StartsWith("err:"))
                                            lstRecord.Add(temp);
                                        else
                                        {
                                            lstRecord.Add(temp + "@图层:" + pFeatureLayer.Name);
                                            return lstRecord;
                                        }                                        
                                    }
                                }
                            }
                        }
                        else if (pLayer is IFeatureLayer)
                        {
                            pFeatureLayer = pLayer as IFeatureLayer;
                            if (pFeatureLayer.Selectable)
                            {
                                temp = LayerSelection(pFeatureLayer, pGeometry);
                                if (!temp.StartsWith("err:"))
                                    lstRecord.Add(temp);
                                else
                                {
                                    lstRecord.Add(temp + "@图层:" + pFeatureLayer.Name);
                                    return lstRecord;
                                }
                            }
                        }
                    }

                }else
                {
                    temp = "err:没有图层.";
                    lstRecord.Add(temp);
                    return lstRecord;
                }
                if (pLayer != null) 
                    Marshal.ReleaseComObject(pLayer);
                if (pFeatureLayer != null)
                    Marshal.ReleaseComObject(pFeatureLayer);
                if (pCompositeLayer != null)
                    Marshal.ReleaseComObject(pCompositeLayer);
                return lstRecord;
            }
            catch (System.Exception ex)
            {
                if (pLayer != null)
                    Marshal.ReleaseComObject(pLayer);
                if (pFeatureLayer != null)
                    Marshal.ReleaseComObject(pFeatureLayer);
                if (pCompositeLayer != null)
                    Marshal.ReleaseComObject(pCompositeLayer);
                temp = "err:"+ex.Message;
                lstRecord.Add(temp);
                return lstRecord;
            }
        }


某个图层查询: //FeatureSelection of the single featurelayer(layername:oid1,oid2,...)
     
   public string LayerSelection(IFeatureLayer pFeatureLayer,IGeometry pGeometry)
        {
            string strRecords = pFeatureLayer.Name + ":";
            string displayField = string.Empty;
            IFeatureClass pFeatureClass = null;
            ISpatialFilter pSpatialFilter = null;
            IFeatureCursor pFeatureCursor = null;
            IFeature pFeature = null;
            try
            {
                pFeatureClass = pFeatureLayer.FeatureClass;
                pSpatialFilter = new SpatialFilterClass();
                pSpatialFilter.Geometry = pGeometry;
                pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
                pSpatialFilter.GeometryField = pFeatureClass.ShapeFieldName;
                pFeatureCursor = pFeatureClass.Search(pSpatialFilter, false);
                pFeature = pFeatureCursor.NextFeature();
                while (pFeature != null)
                {
                    strRecords += pFeature.get_Value(pFeature.Fields.FindField("OBJECTID")).ToString()+",";
                    pFeature = pFeatureCursor.NextFeature();
                }
                if (pFeature != null)
                    Marshal.ReleaseComObject(pFeature);
                if (pFeatureCursor != null)
                    Marshal.ReleaseComObject(pFeatureCursor);
                if(pSpatialFilter!=null)
                    Marshal.ReleaseComObject(pSpatialFilter);
                if (pFeatureClass != null)
                    Marshal.ReleaseComObject(pFeatureClass);
                strRecords = strRecords.Substring(0, strRecords.Length - 1);
                return strRecords;
            }
            catch (System.Exception ex)
            {
                if (pFeature != null)
                    Marshal.ReleaseComObject(pFeature);
                if (pFeatureCursor != null)
                    Marshal.ReleaseComObject(pFeatureCursor);
                if (pSpatialFilter != null)
                    Marshal.ReleaseComObject(pSpatialFilter);
                if (pFeatureClass != null)
                    Marshal.ReleaseComObject(pFeatureClass);
                strRecords = "err:" + ex.Message;
                return strRecords;
            }
        }


(3)在进行高亮显示的同时已经将地图信息进行统计 ,一般用树结构分图层展示,主要是树结构的创建与控制事件的实现。
创建树结构的代码:
          
  TreeNode pNode;
            TreeNode pParent;
            string[] temp;
            string[] detail;
            try
            {
                for (int i = 0; i < lstReturn.Count; i++)
                {
                    temp= lstReturn[i].Split(':');
                    pParent = new TreeNode(temp[0]);
                    treeResult.Nodes.Add(pParent);                    
                    if(temp.Length==2)
                    {
                        detail = temp[1].Split(',');
                        foreach (string obj in detail)
                        {
                            pNode = new TreeNode(obj);
                            pParent.Nodes.Add(pNode);
                        }
                    }                    
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("err:"+ex.Message);
            }

目录
相关文章
|
NoSQL C# 数据库
使用c#对MongoDB进行查询(1)
1.BsonDocument对象     在MongoDB.Bson命名空间下存在一个BsonDocument类,它是MongoDB的文档对象,代表着MongoDB中不规则数据一条条实体模型。可以使用BsonDocument对不规则数据进行操作,这个类型继承了IEnumberable类,也就是说又将...
2386 0
|
2月前
|
SQL 开发框架 .NET
C#进阶-LINQ表达式之GroupBy分组查询
本篇文章我们将演示LINQ扩展包基础语法里的GroupBy分组查询,并实现投影等实际操作中常用的类型转换手法。目前LINQ支持两种语法,我会在每个案例前先用大家熟知的SQL语句表达,再在后面用C#的两种LINQ语法分别实现。LINQ语法第一次接触难免感到陌生,最好的学习方式就是在项目中多去使用,相信会有很多感悟。
141 0
|
2月前
|
C# 索引
C# | 【完全开源】手机号码归属地查询,一秒内百万次查询
这个开源项目是一个.NET库,可以通过手机号码获取号码归属地信息,包括运营商、国家、省份、城市、邮政编码、区号等信息。 该库加载了一个包含46万条数据的“中国手机号归属地信息”数据集,并实现了高速查询。在我的7年老笔记本上执行一百万次查询耗时不足一秒。
187 0
|
12月前
|
开发框架 .NET 编译器
C# Lambda表达式和linq表达式 之 匿名对象查询接收
C# Lambda表达式和linq表达式 之 匿名对象查询接收
|
SQL C# uml
C# SQL添加数据,删除数据,修改数据,查询数据
C# SQL添加数据,删除数据,修改数据,查询数据
204 0
C# SQL添加数据,删除数据,修改数据,查询数据
|
SQL 关系型数据库 API
基于C#的ArcEngine二次开发37:循环查询过程的内存管理与性能优化(三)
基于C#的ArcEngine二次开发37:循环查询过程的内存管理与性能优化
基于C#的ArcEngine二次开发37:循环查询过程的内存管理与性能优化(三)
|
开发框架 .NET Serverless
C# Linq语言集成查询
C# Linq语言集成查询
|
SQL 存储 安全
基于C#的ArcEngine二次开发37:循环查询过程的内存管理与性能优化(二)
基于C#的ArcEngine二次开发37:循环查询过程的内存管理与性能优化
|
存储 C#
基于C#的ArcEngine二次开发37:循环查询过程的内存管理与性能优化(一)
基于C#的ArcEngine二次开发37:循环查询过程的内存管理与性能优化
|
C# 数据库 C++
手把手教你使用C#操作SQLite数据库,新建数据库,创建表,插入,查询,删除,运算符,like(持续更新)
有问题欢迎留言!!!  目录: 一、新建项目,添加引用 二、创建数据库 三、创建表 四、插入数据  五、查询数据  六、删除数据  七、运算符 八、like语句 我的环境配置:windows 64,VS,SQLite(点击下载),System.Data.SQLite.DLL(点击下载)。
6844 1