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();
}
}
}
需求:读取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();
}
}
}
相关文章推荐
- Android传感器
- [tyvj1520]树的直径(dfs||树形dp)
- .NET MVC session消失问题
- 用js下载文件
- VTD算法的几句话总结
- CRT连接缓慢方法关闭UseDNS
- 前端开发工程师必读书籍有哪些值得推荐?
- 抽象类和接口异同
- 性能瓶颈分析方法
- jquery的extend方法
- SharedPreference.Editor的apply和commit方法异同
- Activiti框架学习笔记总结
- Delphi COM编程技术一(COM编程基础知识)
- Android采用双缓冲实现画板
- Linux目录结构讲解
- iOS应用程序状态图
- mycat启动后,用Navicat Premium 连接报 "2013"
- MongoDB 常用操作
- ResultSetHandler接口的实现类
- COM与DCOM的区别与联系