个人博客,已转移至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("导出成功", "系统提示");
}
}
有疑问可评论