ado.net下的shape文件读取

shape文件是基本的GIS文件格式,闲来无事,探讨一下对其的读取源码

从理论上讲,传统的GIS数据读取分为图形读取与属性读取两个方面。

语言:C#
平台:ArcEngine

图形的读取是比较简单的
如下所示

if(openDialog.ShowDialog() == DialogResult.OK)
{
  string filename = openDialog.FileName;
  if(filename.EndsWith("shp") == true)
  {
   axMap2.Refresh();

   string dir = System.IO.Path.GetDirectoryName(filename);
   string file = System.IO.Path.GetFileName(filename);
   axMap1.AddShapeFile(dir,file);
   axMap2.AddShapeFile(dir,file);
  }

  axMap1.Refresh();
  
  tabControlMain.SelectedTab = tabMap;
}

比较麻烦的是属性的读取
从简易度上出发,没有参考AE/AO下的组件
而是选择使用DataGrid显示与Shape file同名的dbf

代码并不复杂,但是被一个问题困扰了一下午。
初时尝试采用系统自带的DBASE的ODBC,并不能读取所有的dbf。
于是百度,未果;google,未果;...最后只好上ESRI搜了。

看见一小段E文,说建议采用Foxpro的odbc试试。
于是装了一个Foxpro 6,换用Foxpro的odbc,大功告成~

下面是代码示例:

if(tableDialog.ShowDialog() == DialogResult.OK)
{
string FileName = tableDialog.FileName;//search for DBASE file with the same
name
string name = null;
FileInfo file = new FileInfo(FileName);
name = file.Name;//filename
string path = file.Directory.FullName + "\\"; //file path
string strConnection = "Driver={Microsoft Visual FoxPro Driver};SourceType=DB
F;SourceDB=" + path;[:)]注意这里
//string strConnection="Driver={Microsoft dBASE Driver (*.dbf)};Dbq=" + path;

string strSelect="SELECT * FROM " + name;
OdbcConnection thisConnection = new OdbcConnection(strConnection);
thisConnection.Open();

OdbcDataAdapter thisAdapter = new OdbcDataAdapter(strSelect,thisConnection);

DataSet thisDataSet = new DataSet();

try
{
  thisAdapter.Fill(thisDataSet,name);
}
catch(Exception exec)
{
  MessageBox.Show("Error: " + exec.Message);
  return;
}

thisConnection.Close();

Grid1.DataSource = thisDataSet.Tables[name];

Grid1.CaptionText = FileName;

tabControlMain.SelectedTab = tabTable;
}

回头看了一下,原来百度到的代码有的是用的foxpro,有的是用的系统自带的dbf。但是没
有人指出它们的差别,其实一个是鲁棒的,另一个就不这么样了。[:(]

于是彻底明白了一件旧事:为什么我们平时在excel里编辑完图形的dbf属性表,保存时总
会提示丢失了一些东西。因为excel处理这种较低版本的dbf时,类似于系统自带dbf的odb
c,不能处理某些字段。同样的问题似乎也发生于Mapinfo的属性表格处理中。而若是在Fo
xpro中就不会产生上述问题,因为dbf作为其主要格式,Foxpro中的新旧格式兼容性相信做
的比较好。(其实现象早就发现了,但是本质总是难以琢磨的)

一点个人看法,不足之处还望路过的大牛指出~


e-mail:shisong.zhu@gmail.com
GISer in China, for engineering
原文地址:https://www.cnblogs.com/columbus2/p/840329.html