您的位置:首页 > 其它

File GeoDatabase API学习

2016-04-26 09:39 288 查看
File GeoDatabase API学习(C#)

需求:读取GDB数据,读取(空间库、属性库)数据集名称、字段、字段别名、数据

现在获取到了所以表信息和字段信息。

获取字段信息有些问题没有解决,无法获取到别名

table.FieldDefs 属性不能调用

调用抛异常: 未经对象引用到对象的实例



在 Esri.FileGDB.FieldDef.get_NativeFieldDef() 位置 c:\jenkins\jobs\filegdb_api_1.4_vs12\workspace\fgdbapi_ba\src\esri.filegdbapi\fielddef.cpp:行号 44

   在 Esri.FileGDB.Table.get_FieldDefs() 位置 c:\jenkins\jobs\filegdb_api_1.4_vs12\workspace\fgdbapi_ba\src\esri.filegdbapi\table.cpp:行号 121

   在 QueryGDBTable.Form1.GetFieldInfos(String tbPath) 位置 e:\Supermap\GDB导入\FileGDB_API_VS2012_1_4\samplesC#\QueryGDBTable\Form1.cs:行号 162



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using Esri.FileGDB;

namespace QueryGDBTable
{
public partial class Form1 : Form
{
private Geodatabase geodatabase = null;
private readonly string featureDatasetType = "Feature Dataset";
private readonly string featureClassType = "Feature Class";
private readonly string tableType = "Table";
public Form1()
{
InitializeComponent();
}

private void btnOpen_Click(object sender, EventArgs e)
{
string path = this.txtPath.Text.Trim();
if (string.IsNullOrEmpty(path))
{
this.richTxtInfo.AppendText(System.DateTime.Now.ToString() + ":路径不能为空。\r\n");
return;
}
if (!path.ToUpper().EndsWith(".GDB"))
{
this.richTxtInfo.AppendText(System.DateTime.Now.ToString() + ":路径必须以‘.GDB’结尾。\r\n");
return;
}
try
{
if (geodatabase != null)
geodatabase.Close();
geodatabase = Geodatabase.Open(path);

this.richTxtInfo.AppendText(System.DateTime.Now.ToString() + ":打开" + path + "。\r\n");
}
catch (Exception ex)
{
this.richTxtInfo.AppendText(System.DateTime.Now.ToString() + ":打开失败。\r\n");
this.richTxtInfo.AppendText(System.DateTime.Now.ToString() + ":" + ex.Message + "。\r\n");
}
}

private void btnQuery_Click(object sender, EventArgs e)
{
if (geodatabase == null)
return;
List<string> tbs = GetTableList("\\");
foreach(string tbPath in tbs){
ConsoleMsg(tbPath);
string name= geodatabase.GetQueryName(tbPath);
ConsoleMsg("queryName:"+tbPath);
GetFieldInfos(tbPath);

}
}

/// <summary>
/// 获取表字段信息
/// </summary>
/// <returns></returns>
private List<string> GetFieldInfos(string tbPath)
{
try
{
try
{
string dsinfo = geodatabase.GetDatasetDocumentation(tbPath, tableType);
ConsoleMsg("dsinfo:" + dsinfo);
}
catch (Exception ex) {
ConsoleMsg(ex.Message);
}
Table tb = geodatabase.OpenTable(tbPath);
int fieldNum = tb.FieldInformation.Count;

List<string> fieldNames = new List<string>();

for(int i=0;i<fieldNum;i++){
string name = tb.FieldInformation.GetFieldName(i);
fieldNames.Add(name);
ConsoleMsg("Field:"+name);
}
//var fieldDefs = tb.FieldDefs;
//if (fieldDefs != null && fieldDefs.Length > 0)
//{
//}

}
catch (Exception ex) {
ConsoleMsg("error:"+ex.Message);
}
return null;
}

/// <summary>
/// 获取表path
/// </summary>
/// <param name="parPath"></param>
/// <returns></returns>
private List<string> GetTableList(string parPath)
{
if (geodatabase == null)
return null;
List<string> list = new List<string>();
try
{
string[] tables = GetChildDatasets(parPath, tableType);
if (tables != null && tables.Length > 0)
list.AddRange(tables);

string[] featureClasses = GetChildDatasets(parPath, featureClassType);
if (featureClasses != null && featureClasses.Length > 0)
list.AddRange(featureClasses);

var dses = GetChildDatasets(parPath, featureDatasetType);

if (dses != null && dses.Length > 0)
{
foreach (string path in dses)
{
List<string> childList = GetTableList(path);
if (childList != null && childList.Count > 0)
{
list.AddRange(childList);
}
}
}
}
catch (Exception ex)
{
this.richTxtInfo.AppendText(System.DateTime.Now.ToString() + ":" + ex.Message + "。\r\n");
}
return list;
}
/// <summary>
/// 获取子数据集
/// </summary>
/// <param name="parPath"></param>
/// <param name="childtype"></param>
/// <returns></returns>
private string[] GetChildDatasets(string parPath, string childtype)
{
try
{
string[] dses = geodatabase.GetChildDatasets(parPath, childtype);
if (dses != null && dses.Length > 0)
return dses;
}
catch (Exception ex)
{
ConsoleMsg(ex.Message);
}
return null;
}

/// <summary>
/// 输出信息
/// </summary>
/// <param name="msg"></param>
private void ConsoleMsg(string msg)
{
this.richTxtInfo.AppendText(System.DateTime.Now.ToString() + ":" + msg + "\r\n");
}

private void txtPath_DoubleClick(object sender, EventArgs e)
{
FolderBrowserDialog browserDialog = new FolderBrowserDialog();

if (browserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.txtPath.Text = browserDialog.SelectedPath;
}
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (geodatabase != null)
geodatabase.Close();

}
}
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: