码迷,mamicode.com
首页 > 其他好文 > 详细

创建字段IFields

时间:2017-08-31 12:34:33      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:bsp   ini   ntc   led   指定   write   graph   line   select   

从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

标签:bsp   ini   ntc   led   指定   write   graph   line   select   

原文地址:http://www.cnblogs.com/marvelousone/p/7457436.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!