【解答】从ACCESS读取数据到ArcGIS个人数据库并创建Feature Class(point)
2009-08-01 10:40
661 查看
以下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<double> myList = new List<double>();
//数据库连接
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();
}
}
}
--------------------
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<double> myList = new List<double>();
//数据库连接
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();
}
}
}
相关文章推荐
- 【说明】从ACCESS读取数据到ArcGIS个人数据库并创建Feature Class(point)
- 【题目】从ACCESS读取数据到ArcGIS个人数据库并创建Feature Class(point)
- ArcGIS 网络分析[1.3] 在个人地理数据库中创建网络数据集/并简单试验最佳路径
- 使用Apache POI创建Excel,并从数据库中读取数据写入到Excel文件中
- ArcGIS 10.0 ArcGIS 9.3.1数据生成实验--个人地理数据库
- 创建一个sms.db数据库俩面在创建一个message表,插入数据然后在读取数据
- ArcGIS flex api创建的空间数据存入数据库
- ArcGIS官方提供的使用ConvertFeatureClass方法高效导出数据的代码示例
- 使用Apache POI创建Excel,并从数据库中读取数据写入到Excel文件中
- 从数据库中读取数据创建菜单
- 使用 SQL 语句创建 School 数据库(包括参数)、 TblClass 表、 TblStudent 表。 并向其中填入数据。
- GridView读取数据XLS ACCESS MDB 并写入到数据库
- 创建一个sms.db数据库俩面在创建一个message表,插入数据然后在读取数据
- 读取数据库(SQL 、Access)、数据类型转换(Convert.Tostring)、数据库链接
- 使用Apache POI创建Excel,并从数据库中读取数据写入到Excel文件中
- java小程序从数据库中(表)中读取数据然后创建文件夹生成txt文件
- C#创建数据库和表并把读取到的csv数据存入新建表中
- SQL 数据的导入导出,对远程(MSsql,OracleAccess,)数据库的操作以及读取Excel,txt文件中的数据
- 在C# + Access + ADOX环境下创建数据库和数据表
- ArcGIS 网络分析[1] 利用自定义点线数据(shp或数据库)创建网络数据集【小白向】