創建字段IFields
從ACCESS讀取數據到ArcGIS個人數據庫並創建Feature Class(point)
以下VS2005控制臺應用程序代碼不能直接運行,需要添加License,方法見下一篇BLOG
--------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Geometry;
namespace ReadingAccessData
{
class Program
{
static void Main(string[] args)
{
List myList = new List();
//數據庫連接
OleDbConnection thisConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source = D:\\wsl\\TestDB.mdb ");
thisConnection.Open();
OleDbCommand thisCommond = thisConnection.CreateCommand();
thisCommond.CommandText = "select ID,X,Y,M from GeoXY";
OleDbDataReader thisReader = thisCommond.ExecuteReader();
while (thisReader.Read())
{
//Console.WriteLine("\t{0}\t{1}\t{2}\t{3}",
// thisReader["ID"], thisReader["X"],
//thisReader["Y"], thisReader["M"]);
myList.Add(Convert.ToDouble(thisReader["ID"]));
myList.Add(Convert.ToDouble(thisReader["X"]));
myList.Add(Convert.ToDouble(thisReader["Y"]));
myList.Add(Convert.ToDouble(thisReader["M"]));
}
thisReader.Close();
thisConnection.Close();
Console.WriteLine("read table GeoXY from TestDB.mdb successfully");
//把讀取的數據存放與雙精度二維數組 propertyValue
int mCount = (int)myList.Count / 4;
double[,] propertyValue = new double[mCount, 4];
for (int i = 0; i < mCount;i++ )
{
for (int j = 0; j < 4; j++)
{
propertyValue[i, j] = (double)myList[i * 4 + j];
}
}
//
string[] strFields;
ESRI.ArcGIS.Geodatabase.esriFieldType[] typeFields;
strFields = new string[] { "New ID", "X", "Y", "New M" };
typeFields = new esriFieldType[4];
typeFields[0] = esriFieldType.esriFieldTypeInteger;
typeFields[1] = esriFieldType.esriFieldTypeDouble;
typeFields[2] = esriFieldType.esriFieldTypeDouble;
typeFields[3] = esriFieldType.esriFieldTypeInteger;
try
{
////個人GDB路徑
string database = "D:\\wsl\\NewDB.mdb";
//new Feature Classes文件名
string shpName = "GeoXY";
//打開數據庫,創建工作空間
IPropertySet propertySet = new PropertySetClass();
propertySet.SetProperty("DATABASE", database);
IWorkspaceFactory workspaceFactory = new AccessWorkspaceFactoryClass();
IFeatureWorkspace pWorkspace =
workspaceFactory.Open(propertySet, 0) as IFeatureWorkspace;
IFields pFields = new FieldsClass();
IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;
IField pField = new FieldClass();
IFieldEdit pFieldEdit = pField as IFieldEdit;
pFieldEdit.Name_2 = "OBJECTID";
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID;
pFieldsEdit.AddField(pField);
pField = new FieldClass();
pFieldEdit = pField as IFieldEdit;
pFieldEdit.Name_2 = "Shape";
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
IGeometryDef pGeometryDef = new GeometryDefClass();
IGeometryDefEdit pGeometryDefEdit = pGeometryDef as IGeometryDefEdit;
pGeometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;
//SpatialReference;
ISpatialReferenceFactory pSpaRefFac = new SpatialReferenceEnvironmentClass();
IGeographicCoordinateSystem pGeoCoor =
pSpaRefFac.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984);
//IGeographicCoordinateSystem pGeoCoor = pSpaRefFac.CreateGeographicCoordinateSystem(4326);
ISpatialReference pSpatialReference = pGeoCoor ;
pSpatialReference.SetDomain(-180, 180, -90, 90);
pGeometryDefEdit.SpatialReference_2 = pSpatialReference;
pFieldEdit.GeometryDef_2 = pGeometryDef;
pFieldsEdit.AddField(pField);
//添加其他的屬性字段
if (strFields != null)
{
for (int i = 0; i < strFields.Length; i++)
{
pField = new FieldClass();
pFieldEdit = pField as IFieldEdit;
pFieldEdit.Name_2 = strFields[i];
pFieldEdit.Type_2 = typeFields[i];
pFieldsEdit.AddField(pField);
}
}
IFeatureClass pFeatureClass =
(pWorkspace as IFeatureWorkspace).CreateFeatureClass(shpName,
pFields, null, null,
esriFeatureType.esriFTSimple,
"Shape", "");
IFeatureCursor ipFC = pFeatureClass.Insert(true);
IFeatureBuffer ipFeatureBuffer = pFeatureClass.CreateFeatureBuffer();
//將數組中數據逐行加入到新生成的點層中。
for (int i = 0; i < mCount; i++)
{
IFeature ipFeature = ipFeatureBuffer as IFeature;
IPoint ipPoint = new PointClass();
ipPoint.PutCoords(Convert.ToDouble(propertyValue[i, 1]),
Convert.ToDouble(propertyValue[i, 2]));//指定每個點的經度,緯度
ipFeatureBuffer.Shape = ipPoint;
for (int j = 0; j < 4; j++)
{
ipFeature.set_Value(j + 2, propertyValue[i, j]);
}
ipFC.InsertFeature(ipFeatureBuffer);
}
ipFC.Flush();
}
catch (Exception ex)
{
string s = ex.Message + "\r\n" + ex.StackTrace;
}
Console.Write("Program finished,press enter ,"+
"then, run ArcMap and add data from your personal database ");
Console.ReadLine();
}
}
}
創建字段IFields