ArcEngine批量添加XY数据

使用ArcGIS Desktop “添加XY数据”或者“创建XY事件图层”工具 可以导入Excel坐标数据,生成临时图层并添加至ArcMap、ArcGlobe或者ArcScene中。在ArcEngine中可以利用GP工具(MakeXYEventLayer)将某个 x,y 坐标表添加到地图、globe 或 scene 中。该数据表中必须包含两个字段:一个用于 x 坐标,一个用于 y 坐标。Excel表的表头最好字段放至第一行,否则会报错。原本想直接将MakeXYEventLayer创建事件图层添加至地图中,但是至今未想到方法。只有暂时生成要素类导出至磁盘中,再添加至地图中。如果高手们有其他方法,希望不吝赐教。 

实现思路: 
第一步:读取Excel数据表,提取表名、X字段、Y字段 
第二步:实例化数据管理工具MakeXYEventLayer 
第三步:实例化Geoprocessor,并执行数据管理工具MakeXYEventLayer 
第四步:使用复制要素工具将此事件图层导出为磁盘上的要素类 
第五步:加载要素至图层中 
主要代码如下:

GP执行MakeXYEventLayer工具并复制图层至文件地理数据库中

        /// <summary>

        /// 执行MakeXYEventLayer工具,并将创建的临时图层转换为要素类

        /// </summary>

        /// <param name="xyEventLayer">XY 事件图层tool</param>

        /// <param name="out_Layer_Path">要素输出路径</param>

        /// <returns></returns>

        private IFeatureClass ConvertExcelToFeatureClass(MakeXYEventLayer xyEventLayer, string out_Layer_Path)

        {

            try

            {

                Geoprocessor GP = new Geoprocessor();

                GP.OverwriteOutput = true;

                GP.Execute(xyEventLayer, null);//执行XY事件  

 

                //复制要素

                ESRI.ArcGIS.DataManagementTools.CopyFeatures cf = new ESRI.ArcGIS.DataManagementTools.CopyFeatures();

                cf.in_features = xyEventLayer.out_layer;

                cf.out_feature_class = out_Layer_Path + xyEventLayer.out_layer;

                IGeoProcessorResult gpResult = GP.Execute(cf, null) as IGeoProcessorResult;

                IFeatureClass _featureClass;

                IQueryFilter qf;//查询过滤

                IGPUtilities gpUtils = new GPUtilitiesClass();

                gpUtils.DecodeFeatureLayer(gpResult.GetOutput(0), out _featureClass, out qf);

                IFeatureCursor cursor = featureClass.Insert(true);

                return _featureClass;

            }

            catch (Exception ex)

            {

                MessageBox.Show("导入要素失败,原因:" + ex.ToString());

                return null;

            }

        }

 

调用方法,并将要素添加至地图中

 

                //比如Excel文件:大学校区.xls,表包括(sheet1,sheet2,sheet2)

                //tableName即表路径,该路径形式例如:D:\\DataFile\\大学校区.xls\\sheet1$

                tableName = m_fileName + "\\" + comtxt_excel.SelectedItem.ToString();

                x = comtxt_x.SelectedItem.ToString();//X坐标

                y = comtxt_y.SelectedItem.ToString();//Y坐标

                outName = txt_layername.Text;

                //添加XY事件

                MakeXYEventLayer MxyLayer = new MakeXYEventLayer(tableName, x, y, outName);

                string tempPath = GetAppDirectory() + "TempData\\Temp.gdb\\";//要素临时存储路径

                featureClass = ConvertExcelToFeatureClass(MxyLayer, tempPath);

                 if (featureClass != null)

                {

                    pFeatureLater = new FeatureLayerClass();

                    pFeatureLater.FeatureClass = featureClass;

                    pFeatureLater.Name = featureClass.AliasName;

                    axMapControl1.Map.AddLayer(pFeatureLater as ILayer);

                    axMapControl1.Extent = axMapControl1.FullExtent;

                }

你可能感兴趣的:(ArcEngine批量添加XY数据)