从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;
ISpatialReferenceFactorypSpaRefFac = 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 + "rn"
+ ex.StackTrace;
}
Console.Write("Program
finished,press enter ,"+
"then, run ArcMap and add data
from your personal database ");
Console.ReadLine();
}
}
}



