ArcObjects拾遗之二——创建Shapefile
2012-05-04 10:18 贼寇在何方 阅读(3425) 评论(1) 编辑 收藏 举报Shapefile(shp)是ESRI开发的一种空间数据格式,一般由后缀为shp(图形数据)、shx(图形索引数据)、dbf(属性数据)、prj(投影信息)等一系列同名的文件构成,用于存储矢量图形数据。其中,shp、shx、dbf三个文件是一个完整的Shapefile数据必须包含的。
系统环境和第一篇的一样,看代码:
创建Shapefile
using System; using ESRI.ArcGIS.DataSourcesFile; using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.Geometry; // using ESRI.ArcGIS.Version; // 省略若干 static IFeatureClass CreateShapefile(string folder, string file, IFields fields) { // 创建Shapefile工作空间 var factory = new ShapefileWorkspaceFactory(); var workspace = factory.OpenFromFile(folder, 0); // 取得要素工作空间 var featureWorkspace = workspace as IFeatureWorkspace; // 创建要素类 var featureClass = featureWorkspace.CreateFeatureClass( file, // 名称,不用带后缀 fields, // 要素类的字段,其中必须包含Geometry字段 null, // 指定要素类包含何种要素,一般null即可 null, // 指定何种类型将被实例化为要素类扩展的,一般null即可 esriFeatureType.esriFTSimple, // 要素类型 "Shape",// 几何字段的名称,按惯例叫Shape即可 "" // 允许应用程序控制RDBMS中的表结构,一般用空字符串即可 ); return featureClass; }
要素类(FeatureClass) ?
A FeatureClass is an ObjectClass whose objects are features, that is, a feature class is a collection of spatial entities, modeled as objects with properties and behavior. All of the features in a feature class share the same attribute schema (they have the same set of named fields). The row objects handed out by a feature class support the IRow, IObject, and IFeature interfaces.
A feature class has a distinguished field of type Geometry, referred to as the shape field. The shape field stores the geometry (referred to as the shape property) for the features in the FeatureClass.
以上是官方的解释,我的理解就是,Shapefile在内存中就当一个FeatureClass好了。
构造字段(IField)
一些字段存放图形数据,另一些字段存放属性数据。创建要素类必须要有一个图形数据的Geometry字段,必须给定空间参考:
static IField CreateGeometryField(esriSRProjCSType projection) { // 创建几何定义 var geometryDef = new GeometryDef(); var geometryDefEdit = (IGeometryDefEdit)geometryDef; geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint; // 创建空间参考 var spatialReferenceFactory = new SpatialReferenceEnvironment(); var spatialReference = spatialReferenceFactory.CreateProjectedCoordinateSystem((int)projection); geometryDefEdit.SpatialReference_2 = spatialReference; // 创建字段 var geometryField = new Field(); var geometryFieldEdit = (IFieldEdit)geometryField; geometryFieldEdit.Name_2 = "Shape";// 字段名按惯例叫Shape即可,与CreateFeatureClass中必须一致 geometryFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;// 字段类型Geometry geometryFieldEdit.GeometryDef_2 = geometryDef; return geometryField; }
而一般的字段,就不用那么麻烦了:
static IField CreateStringField(string name, int length) { var field = new Field(); var fieldEdit = (IFieldEdit)field; fieldEdit.Name_2 = name; fieldEdit.Type_2 = esriFieldType.esriFieldTypeString; fieldEdit.Length_2 = length; return field; }
还需要一个IFields,容纳所有的字段
static void Main(string[] args) { try { Initialize(); // 创建Geometry字段 var geometryField = CreateGeometryField(esriSRProjCSType.esriSRProjCS_WGS1984UTM_50N); // 创建字段集合 var fields = new Fields(); var fieldsEdit = (IFieldsEdit)fields; fieldsEdit.AddField(geometryField); // 创建Shapefile var shapefile = CreateShapefile(AppDomain.CurrentDomain.BaseDirectory, "shapefile", fields); Console.WriteLine("完成"); } catch(Exception e) { Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); } Console.ReadKey(); }
参考链接
ArcGIS Resource Center :IFeatureWorkspace.CreateFeatureClass Method
FeatureLayer,FeatureDataset,FeatureClass,Feature几个概念一点点总结,欢迎指教