栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

arcgis打开mdb数据库

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

arcgis打开mdb数据库

从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();

}

}

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/911154.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号