获取选中地物的Geometry对象,及把Geometry对象显示在地图上

简介:


下面是Map 3D开发时帮助调试检查用的工具类,也是经常会用的的功能。贴在这里共享备查。


复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OSGeo.MapGuide;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.Gis.Map.Platform.Interop;
using Autodesk.Gis.Map.Platform.Utils;

namespace TestSolution
{
     public  class MapHelper
    {
         ///   <summary>
        
///  Get the geometry objects of selected features from map
        
///   </summary>
        
///   <returns></returns>
         static  public IList<MgGeometry> GetSelectedFeatureGeometry()
        {
            IList<MgGeometry> list =  new List<MgGeometry>();

            PromptSelectionResult selResult =
                Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.GetSelection();

             if (selResult.Status == PromptStatus.OK)
            {
                SelectionSet selSet = selResult.Value;
                 // Get all the features in SelectionSet
                MgSelectionBase selectionBase = AcMapFeatureEntityService.GetSelection(selSet);

                 foreach (MgLayerBase layer  in selectionBase.GetLayers())
                {
                    MgFeatureReader featureReader = selectionBase.GetSelectedFeatures(layer, layer.FeatureClassName,  false);

                     // Get the name of Geometry Property
                     string gepPropertyName = layer.GetClassDefinition().DefaultGeometryPropertyName;

                     if ( string.IsNullOrEmpty(gepPropertyName))
                    {
                        Util.PrintLn( " No geometry property exists in layer  " + layer.Name);
                         continue;
                    }

                     while (featureReader.ReadNext())
                    {
                        MgByteReader byteReader = featureReader.GetGeometry(gepPropertyName);
                        MgAgfReaderWriter agfReader =  new MgAgfReaderWriter();
                        MgGeometry geomtry = agfReader.Read(byteReader);

                        list.Add(geomtry);
                    }

                    featureReader.Close();

                }

               
                
            }
             return list;

        }


         ///   <summary>
        
///  Add a gemetry to a layer so that it is displayed on map
        
///   </summary>
        
///   <param name="layer"></param>
        
///   <param name="geometry"></param>
        
///   <returns></returns>
         static  public  bool AddFeature(MgLayerBase layer, MgGeometry geometry)
        {
             if (layer ==  null )
                 return  false;

             if (geometry ==  null)
                 return  false;

            MgClassDefinition classDef = layer.GetClassDefinition();
             string featClassName = layer.FeatureClassName;

            MgPropertyDefinitionCollection propDefs = classDef.GetProperties();
            MgGeometricPropertyDefinition geoPropDef = propDefs.GetItem(classDef.DefaultGeometryPropertyName)
                                                                     as MgGeometricPropertyDefinition;
             // Default Geometry Type of current layer. 
             int layerGeometryType = geoPropDef.GeometryTypes;
             // Geometry Type converted from acdbEntity.
             int geoType = geometry.GetGeometryType();

             // if (!GeometryTypeValidate(layerGeometryType, geoType))
            
// {
            
//     Util.PrintLn("Geometry type of the entity is invalid to the targeting layer.");
            
//     return false;
            
// }

            MgPropertyCollection props =  new MgPropertyCollection();
            MgAgfReaderWriter writer =  new MgAgfReaderWriter();
            MgByteReader byteReader = writer.Write(geometry);
            props.Add( new MgGeometryProperty(classDef.DefaultGeometryPropertyName,byteReader));

             if (props !=  null)
            {
                MgInsertFeatures insertFeat =  new MgInsertFeatures(featClassName, props);
                MgFeatureCommandCollection featCommands =  new MgFeatureCommandCollection();
                featCommands.Add(insertFeat);



                 try
                {
                    layer.UpdateFeatures(featCommands);
                }
                 catch (System.Exception e)
                {
                    Util.PrintLn( " Failed to add a Feature. ");
                    Util.PrintLn(e.Message);
                     return  false;
                }
                 return  true;
            }
             else
            {
                Util.PrintLn( " Operation cancelled ");
                 return  false;
            }
        }


         ///   <summary>
        
///  Validate whether the Geometry Type of Layer and MgGeometry to be added in match.
        
///   </summary>
        
///   <param name="layerGeoType"> Geometry Type of Layer </param>
        
///   <param name="geoType"> Geometry Type of the MgGeometry convert from Entity </param>
        
///   <returns> true if match </returns>
         private  bool GeometryTypeValidate( int layerGeoType,  int geoType)
        {
             bool match =  false;
             if (geoType == MgGeometryType.Point || geoType == MgGeometryType.MultiPoint)
            {
                 if ((layerGeoType & MgFeatureGeometricType.Point) !=  0)
                    match =  true;
            }
             else  if (geoType == MgGeometryType.LineString || geoType == MgGeometryType.MultiLineString)
            {
                 if ((layerGeoType & MgFeatureGeometricType.Curve) !=  0)
                    match =  true;
            }
             else  if (geoType == MgGeometryType.Polygon || geoType == MgGeometryType.CurvePolygon
                    || geoType == MgGeometryType.MultiPolygon)
            {
                 if ((layerGeoType & MgFeatureGeometricType.Surface) !=  0)
                    match =  true;
            }

             return match;
        }


    }
}
复制代码
作者: 峻祁连
邮箱:junqilian@163.com 
出处: http://junqilian.cnblogs.com 
转载请保留此信息。



本文转自峻祁连. Moving to Cloud/Mobile博客园博客,原文链接:http://www.cnblogs.com/junqilian/archive/2011/10/12/2208838.html ,如需转载请自行联系原作者
相关文章
|
6月前
|
定位技术
97Echarts - 地理坐标/地图(Draw Polygon on Map)
97Echarts - 地理坐标/地图(Draw Polygon on Map)
25 0
|
4月前
|
定位技术
ArcGIS中ArcMap栅格图层0值设置为NoData值的简便方法
ArcGIS中ArcMap栅格图层0值设置为NoData值的简便方法
|
4月前
|
定位技术
ArcGIS中ArcMap创建渔网Create Fishnet:生成指定大小的格网矢量文件
ArcGIS中ArcMap创建渔网Create Fishnet:生成指定大小的格网矢量文件
|
4月前
|
并行计算 定位技术
ArcGIS中ArcMap分割栅格Split Raster工具没有结果的解决
ArcGIS中ArcMap分割栅格Split Raster工具没有结果的解决
|
4月前
[Qt5] 矩形、圆和多边形ROI区域的交互(List View列表视图,halcon实现)
[Qt5] 矩形、圆和多边形ROI区域的交互(List View列表视图,halcon实现)
56 0
|
6月前
cesium中绘制立方体、设置材质、操作相机及获取鼠标经纬度和高度的方法
cesium中绘制立方体、设置材质、操作相机及获取鼠标经纬度和高度的方法
105 0
|
7月前
|
JSON 数据格式
使用Fiona创建Shapefile矢量数据
使用Fiona写入Shapefile数据,主要是构建一个Schema,然后将空间对象转为GeoJSON的形式进行写入。 这个Schema是一个字典结构,定义了Geometry的类型,属性字段的名称及其类型。
65 0
|
9月前
ArcGIS快速将栅格的0值设置为NoData的方法
本文介绍在ArcMap软件中,将栅格图层中的0值或其他指定数值作为NoData值的方法~
442 1
ArcGIS快速将栅格的0值设置为NoData的方法
|
数据库
ArcGIS创建矢量要素并绘制其空间范围的方法
本文介绍在ArcGIS下属ArcMap软件中,新建点、线、面等矢量要素图层,并对新建图层的空间范围加以划定的方法~
646 1
ArcGIS创建矢量要素并绘制其空间范围的方法
MFC绘制二维图形【1】—— 使用映射模式函数自定义坐标系
MFC绘制二维图形【1】—— 使用映射模式函数自定义坐标系
258 0
MFC绘制二维图形【1】—— 使用映射模式函数自定义坐标系