C# ArcEngine利用点数组生成SHP图层(Point Polygon)

个人博客,已转移至http://www.zgeo.work:8022/wordpress

只贴核心代码,稍微修改下就能运行,开发环境windows7 、ArcEngine10.2、VS2010。

引用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Carto;

using System.Windows.Forms;

class CreateGeometry

    {
        float[] data = { 116, 39, 117, 36, 118, 25 };
        public void createPoint()
        {
            string FeatureType = "Point";
            //打开一个要素类
            IFeatureClass pFeatureClass = GetIFeatureClass(FeatureType); ;
            IPoint pPoint = new PointClass();
            for (int i = 0; i < data.Length; i++)
            {
                pPoint.X = data[i];
                pPoint.Y = data[++i];
                IFeature pFeature = pFeatureClass.CreateFeature();
                pFeature.Shape = pPoint;
                pFeature.Store();
            }
            string path = GetSaveFileName();
            if (path != "")
            {
                string ExportFileShortName = System.IO.Path.GetFileNameWithoutExtension(path);
                string ExportFilePath = System.IO.Path.GetDirectoryName(path);
                SaveShpToFile(pFeatureClass, ExportFilePath, ExportFileShortName);
            }
            else
            {
                MessageBox.Show("无效目录!");
            }
        }
        public void createPolyline()
        {


        }
        public void createPolygon()
        {
            string FeatureType = "Polygon";
            //打开一个要素类
            IFeatureClass pFeatureClass = GetIFeatureClass(FeatureType);
            Ring ring1 = new RingClass();
            object missing = Type.Missing;
            IPoint pPoint = new PointClass();
            for (int i = 0; i < data.Length; i++)
            {
                pPoint.X = data[i];
                pPoint.Y = data[++i];
                ring1.AddPoint(pPoint, ref missing, ref missing);
            }
            IGeometryCollection pointPolygon = new PolygonClass();
            pointPolygon.AddGeometry(ring1 as IGeometry, ref missing, ref missing);
            IPolygon polyGonGeo = pointPolygon as IPolygon;
            IFeature pFeature = pFeatureClass.CreateFeature();
            pFeature.Shape = polyGonGeo;
            pFeature.Store();
            string path = GetSaveFileName();
            if (path != "")
            {
                string ExportFileShortName = System.IO.Path.GetFileNameWithoutExtension(path);
                string ExportFilePath = System.IO.Path.GetDirectoryName(path);
                SaveShpToFile(pFeatureClass, ExportFilePath, ExportFileShortName);
            }
        }
        public IFeatureClass GetIFeatureClass(string FeatureType)
        {
            IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
            IWorkspace pWorkSpace = pWorkspaceFactory.OpenFromFile(@"D:\Data", 0);
            //初始化要素工作空间
            IFeatureWorkspace pFeatWorkSpace = pWorkSpace as IFeatureWorkspace;
            IFeatureClass pFeatureClass = pFeatWorkSpace.OpenFeatureClass(FeatureType);
            return pFeatureClass;
        }
       
        public void SaveShpToFile(IFeatureClass pFeatureClass, string ExportFilePath, string ExportFileShortName)
        {
            //设置导出要素类的参数
            IFeatureClassName pOutFeatureClassName = new FeatureClassNameClass();
            IDataset pOutDataset = (IDataset)pFeatureClass;
            pOutFeatureClassName = (IFeatureClassName)pOutDataset.FullName;
            //创建一个输出shp文件的工作空间
            IWorkspaceFactory pShpWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
            IWorkspaceName pInWorkspaceName = new WorkspaceNameClass();
            pInWorkspaceName = pShpWorkspaceFactory.Create(ExportFilePath, ExportFileShortName, null, 0);

            //创建一个要素类
            IFeatureClassName pInFeatureClassName = new FeatureClassNameClass();
            IDatasetName pInDatasetClassName;
            pInDatasetClassName = (IDatasetName)pInFeatureClassName;
            pInDatasetClassName.Name = ExportFileShortName;//作为输出参数
            pInDatasetClassName.WorkspaceName = pInWorkspaceName;
            IFeatureDataConverter pShpToClsConverter = new FeatureDataConverterClass();
            pShpToClsConverter.ConvertFeatureClass(pOutFeatureClassName, null, null, pInFeatureClassName, null, null, "", 1000, 0);
            MessageBox.Show("导出成功", "系统提示");
        }

    }

有疑问可评论

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值