基于ArcGIS10.0和Oracle10g的空间数据管理平台九(C#开发)-空间数据导入RDBMS上-Shape格式
先打一个广告:我的独立博客网址是:http://it_blog.jd-app.com/。
我的新浪微博:http://weibo.com/freshairbrucewoo。
欢迎大家相互交流,共同提高技术。
这一篇博文终于要真正接触操作空间数据了,今天要完成讲解的功能就是导入Shape格式和MDB的空间数据格式到基于ArcSDE空间数据库插件的Oracle10g数据库中。这里面涉及到的功能和操作非常的多,我准备用两篇文章来介绍,这一篇介绍导入前的准备工作和Shape格式的导入。对于空间数据和ArcGIS没有基础知识的可以先了解和学习一下这方面的知识,在我这个项目系列博文中也有一些这方面的基础知识介绍,可以看看!下面开始具体介绍这个过程。
1.选择导入的格式
当然这里只支持两种格式(Shape和MDB),当然可以支持更多的空间数据格式,我在一篇博文专门介绍了八种数据格式的空间数据。为什么需要各种空间数据格式的导入呢?因为空间数据的来源多种多样,具体来源可以到google搜索。为了统一管理各种格式或各种来源的空间数据格式,也为了从集中的空间数据中发现更大的商业信息,所以必须找一种统一的格式来管理,我这个项目当然就是采用的基于空间数据库插件的Oracle10g,采用这种方式主要是想借用RDBMS的强大功能。
实现选择格式的思路相当的简单,就是用一个界面采用单选按钮来选择,具体实现选择的功能如下:
/// <summary> /// 下一步,选择导入空间数据的格式,支持Shape和MDB格式 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void nextBtn_Click(object sender, EventArgs e) { this.Close(); if (radioButton1.Checked) { //导入为Shape空间数据格式 FrmImportShapeFile faif = new FrmImportShapeFile(); faif.ShowDialog(); } else { //导入为MDB空间数据格式 FrmImportMDBFile fim = new FrmImportMDBFile(); fim.ShowDialog(); } }
从代码可以看出我们可以通过选择会进入下一个具体导入的界面,下一个界面的功能就是很复杂了。主要复杂的功能是对于需要导入的空间数据格式的检查,包括完整性检查、与数据库中已有表结构的数据结构是否一一对应或者能够兼容----如字段个数、字段类型、字段长度等。
2.Shape空间数据格式的导入
2.1 变量定义与初始化(在构造函数中初始化,也可以在对话框的Load函数中),见如下代码:
private IFeatureWorkspace pFW;//工作空间 public int sc_id = 4326;//空间参考系的ID private bool bSelectField = false; private Dictionary<string, IFields> fieldDic;//用于保存需要检查各个字段 public FrmImportShapeFile() { InitializeComponent(); if (pFW == null) { pFW = MapOperation.GetFeatrueWorkspace(); } fieldDic = new Dictionary<string, IFields>(); }
2.2 回到上一步:选择导入格式界面
/// <summary> /// 回到上一步:就是选择导入空间数据选择界面 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void preBtn_Click(object sender, EventArgs e) { FrmSelectFileFormat fsff = new FrmSelectFileFormat(); fsff.index = 0; fsff.Show(); this.Close(); }
2.3 使能相应按钮功能的复选按钮功能
/// <summary> /// 根据复选按钮使能相应的其他功能按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void checkBox1_CheckedChanged(object sender, EventArgs e) { if (checkBox1.Checked)//使能选择字段 { bSelectField = true; selectUploadFieldBtn.Enabled = true; checkUploadFieldBtn.Enabled = true; } else { bSelectField = false; selectUploadFieldBtn.Enabled = false; checkUploadFieldBtn.Enabled = false; } }
2.4 转换导入Shape文件数据到SDE数据库中
/// <summary> /// 转换导入Shape文件数据到SDE数据库中 /// </summary> /// <param name="strPath">Shape文件路径</param> /// <param name="strFile">Shape文件名称,到后缀名的</param> /// <param name="strTableName">导入到SDE中的表名称</param> private void ConvertShpToSDE(string strPath, string strFile, string strTableName) { //创建一个输出shp文件的工作空间 IWorkspaceFactory pShpWorkspaceFactory = new ShapefileWorkspaceFactoryClass(); IWorkspace pSourceWS = pShpWorkspaceFactory.OpenFromFile(strPath, 0); /* //1.遍历数据集中的要素类并导入 IEnumDataset enumDs = pSourceWS.get_Datasets(esriDatasetType.esriDTFeatureDataset); IFeatureDataset featureDs = enumDs.Next() as IFeatureDataset; while (featureDs != null) { IFeatureClass featClass; IFeatureClassContainer fcContainer = featureDs as IFeatureClassContainer; for (int i = 0; i < fcContainer.ClassCount; i++) { featClass = fcContainer.get_Class(i); if (MapOperation.FindClassByName(workspace as IWorkspace, null, featClass.AliasName) != null) { MapOperation.AddFeatureClassToSDE(pSourceWS as IWorkspace, workspace as IWorkspace, featClass.AliasName, featClass.AliasName); } else { MapOperation.ConvertFeatureClass(pSourceWS as IWorkspace, workspace as IWorkspace, featClass.AliasName, featClass.AliasName); } } featureDs = enumDs.Next() as IFeatureDataset; } //2.遍历游离的要素类并导入 enumDs = pSourceWS.get_Datasets(esriDatasetType.esriDTFeatureClass); IFeatureClass featCl = enumDs.Next() as IFeatureClass; while (featCl != null) { if (MapOperation.FindClassByName(workspace as IWorkspace, null, featCl.AliasName) != null) { MapOperation.AddFeatureClassToSDE(pSourceWS as IWorkspace, workspace as IWorkspace, featCl.AliasName, featCl.AliasName); } else { MapOperation.ConvertFeatureClass(pSourceWS as IWorkspace, workspace as IWorkspace, featCl.AliasName, featCl.AliasName); } featCl = enumDs.Next() as IFeatureClass; }*/ IFeatureWorkspace pSourceFWS = (IFeatureWorkspace)pSourceWS; SqlHelper sh = new SqlHelper(); IFeatureClass pFC = pSourceFWS.OpenFeatureClass(strFile); IWorkspace2 pW2 = pFW as IWorkspace2; string sql = string.Empty; //判断是否在SDE中已经存在要素类,存在就追加,否则新建 if (pW2.get_NameExists(esriDatasetType.esriDTFeatureClass, strTableName)) { //是否选择了上载字段 if (fieldDic.ContainsKey(strFile)) { //选择了上载字段,就将对应字段插入到要素类中 AddFeatureClassToSDE(pSourceFWS, pFW, strFile, strTableName); } else { //没有就将全部字段插入要素类中 MapOperation.AddFeatureClassToSDE(pSourceWS, pFW as IWorkspace, pFC.AliasName, strTableName); } } else { //新建导入,在数据字典中插入一条游离图层的记录 MapOperation.ConvertFeatureClass(pSourceWS as IWorkspace, pFW as IWorkspace, pFC.AliasName, strTableName, sc_id); //MapOperation.CreateFeatureClass(pFW as IWorkspace2, null, strTableName, pFC.Fields, null, null, ""); //MapOperation.AddFeatureClassToSDE(pSourceWS, pFW as IWorkspace, pFC.AliasName, strTableName); sql = "select * from layer where table_name='" + strTableName + "'"; if (sh.GetRecordCount(sql) > 0) { return; } sql = "select max(ID) from layer"; OracleDataReader odr = sh.ReturnDataReader(sql); int result = 0; if (odr.Read()) { result = int.Parse(odr[0].ToString()); result += 1; } Hashtable ht = new Hashtable(); ht.Add("ID", result); sql = "select id from element where name='游离图层'"; odr = sh.ReturnDataReader(sql); if (odr.Read()) { ht.Add("PID", int.Parse(odr[0].ToString())); } else { ht.Add("PID", 2025); } ht.Add("TABLE_NAME", strTableName); ht.Add("TABLE_MAPNAME", strTableName); ht.Add("DESCRIPTION", "无辅助信息!"); if (pFC.ShapeType == esriGeometryType.esriGeometryPoint || pFC.ShapeType == esriGeometryType.esriGeometryMultipoint) { ht.Add("TYPE", "PT"); } else if (pFC.ShapeType == esriGeometryType.esriGeometryLine || pFC.ShapeType == esriGeometryType.esriGeometryPolyline) { ht.Add("TYPE", "PL"); } else if (pFC.ShapeType == esriGeometryType.esriGeometryPolygon) { ht.Add("TYPE", "PY"); } else { ht.Add("TYPE", "PY"); } sh.Insert("layer", ht); } }
上面的代码比较复杂,其中涉及到一个功能就是是否选择一些字段追加到以后的数据库表中,如果不是就全部导入,否则就是执行部分导入功能,全部导入功能是在前面介绍的空间数据操作类((1)MapOperation.AddFeatureClassToSDE(pSourceWS, pFW as IWorkspace, pFC.AliasName, strTableName);追加功能。(2)MapOperation.ConvertFeatureClass(pSourceWS as IWorkspace, pFW as IWorkspace, pFC.AliasName, strTableName, sc_id);新建一个表导入)介绍实现的,就不在具体介绍了,下面介绍部分追求字段功能,如下:
1 ///<summary>
2 /// 将Shape文件中要素类选择的字段转换追加到sde数据库已有的要素类中
3 ///</summary>
4 ///<param name="pSourceFW">源要素工作空间</param>
5 ///<param name="pTargetFW">目标要素工作空间</param>
6 ///<param name="nameOfSourceFeatureClass">源要素类名称</param>
7 ///<param name="nameOfTargetFeatureClass">目标要素类名称</param>
8 public void AddFeatureClassToSDE(IFeatureWorkspace pSourceFW, IFeatureWorkspace pTargetFW,
9 string nameOfSourceFeatureClass, string nameOfTargetFeatureClass)
10 {
11 //打开源要素类
12 IFeatureClass pSourceFC = pSourceFW.OpenFeatureClass(nameOfSourceFeatureClass);
13 //打开目标要素类
14 IFeatureClass pTargetFC = pTargetFW.OpenFeatureClass(nameOfTargetFeatureClass);
15 //遍历源要素类的要素并依次插入目标要素类
16 IFeatureCursor pFeaCursor = pSourceFC.Search(null, false);
17 IFeature pFeature = pFeaCursor.NextFeature();
18
19 IField pField = new FieldClass();
20 int iIndex = 0;
21
22 while (pFeature != null)
23 {
24 IFeature tempFeature = pTargetFC.CreateFeature();
25 tempFeature.Shape = pFeature.Shape;
26
27 try
28 {
29 //添加字段值
30 IFields pFs = fieldDic[nameOfSourceFeatureClass];
31 for (int j = 0; j < pFs.FieldCount; j++)
32 {
33 pField = pFs.get_Field(j);
34 iIndex = tempFeature.Fields.FindField(pField.Name);
35 if (pField.Editable && iIndex != -1)
36 {
37 tempFeature.set_Value(iIndex, pFeature.get_Value(j));
38 }
39 }
40 tempFeature.Store();
41 }
42 catch (System.Exception ex)
43 {
44 MessageBox.Show(ex.Message);
45 return;
46 }
47 pFeature = pFeaCursor.NextFeature();
48 }
49 }
2.5 导入要素类到某一个具体的工作空间
1 ///<summary>
2 /// 导入要素类到工作空间
3 ///</summary>
4 ///<param name="apFD"></param>
5 ///<param name="pathName"></param>
6 ///<param name="fileName"></param>
7 private void ImportFeatureClassToWorkSpace(IFeatureDataset apFD, string pathName, string fileName)
8 {
9 IWorkspaceName pInWorkspaceName;
10 IFeatureDatasetName pOutFeatureDSName;
11 IFeatureClassName pInFeatureClassName;
12 IDatasetName pInDatasetName;
13 IFeatureClassName pOutFeatureClassName;
14 IDatasetName pOutDatasetName;
15 long iCounter;
16 IFields pOutFields, pInFields;
17 IFieldChecker pFieldChecker;
18 IField pGeoField;
19 IGeometryDef pOutGeometryDef;
20 IGeometryDefEdit pOutGeometryDefEdit;
21 IName pName;
22 IFeatureClass pInFeatureClass;
23 IFeatureDataConverter pShpToClsConverter;
24 IEnumFieldError pEnumFieldError = null;
25
26 //得到一个输入SHP文件的工作空间,
27 pInWorkspaceName = new WorkspaceNameClass();
28 pInWorkspaceName.PathName = pathName;
29 pInWorkspaceName.WorkspaceFactoryProgID = "esriCore.ShapefileWorkspaceFactory.1";
30 //创建一个新的要素类名称,目的是为了以来PNAME接口的OPEN方法打开SHP文件
31 pInFeatureClassName = new FeatureClassNameClass();
32 pInDatasetName = (IDatasetName)pInFeatureClassName;
33 pInDatasetName.Name = fileName;
34 pInDatasetName.WorkspaceName = pInWorkspaceName;
35 //打开一个SHP文件,将要读取它的字段集合
36 pName = (IName)pInFeatureClassName;
37 pInFeatureClass = (IFeatureClass)pName.Open();
38 //通过FIELDCHECKER检查字段的合法性,为输入要素类获得字段集合
39 pInFields = pInFeatureClass.Fields;
40 pFieldChecker = new FieldChecker();
41 pFieldChecker.Validate(pInFields, out pEnumFieldError, out pOutFields);
42 //通过循环查找几何字段
43 pGeoField = null;
44 for (iCounter = 0; iCounter < pOutFields.FieldCount; iCounter++)
45 {
46 if (pOutFields.get_Field((int)iCounter).Type == esriFieldType.esriFieldTypeGeometry)
47 {
48 pGeoField = pOutFields.get_Field((int)iCounter);
49 break;
50 }
51 }
52 //得到几何字段的几何定义
53 pOutGeometryDef = pGeoField.GeometryDef;
54 //设置几何字段的空间参考和网格
55 pOutGeometryDefEdit = (IGeometryDefEdit)pOutGeometryDef;
56 pOutGeometryDefEdit.GridCount_2 = 1;
57 pOutGeometryDefEdit.set_GridSize(0, 1500000);
58
59 //创建一个新的要素类名称作为可用的参数
60 pOutFeatureClassName = new FeatureClassNameClass();
61 pOutDatasetName = (IDatasetName)pOutFeatureClassName;
62 pOutDatasetName.Name = pInDatasetName.Name;
63
64 //创建一个新的数据集名称作为可用的参数
65 pOutFeatureDSName = (IFeatureDatasetName)new FeatureDatasetName();
66 //因为ConvertFeatureClass需要传入一个IFeatureDatasetName的参数,
67 //通过它确定导入生成的要素类的工作空间和要素集合
68 //情况一
69 //如果本函数的参数(IFeatureDataset)是一个确切的值,
70 //那么将它转换成IFeatureDatasetName接口就可以了。因为ConvertFeatureClass根据该接口就
71 //可以确定工作空间和要素集合,IFeatureClassName就可以不考虑上述问题
72 //情况二
73 //如果本函数的参数(IFeatureDataset)是一个NULL值,表示要创建独立要素类,
74 //那么ConvertFeatureClass函数无法根据IFeatureDatasetName参数确定工作空间和要素集合
75 //这个时候需要IFeatureClassName参数确定工作空间和要素集合
76
77 //如果参数的值是NULL,说明要创建独立要素类
78 if (apFD == null)
79 {
80 //创建一个不存在的要素集合pFDN,通过它将IFeatureClassName和工作空间连接起来,
81 //而ConvertFeatureClass函数并不使用该变量作为参数,
82 IFeatureDatasetName pFDN = new FeatureDatasetNameClass();
83 IDatasetName pDN = (IDatasetName)pFDN;
84 IDataset pDS = (IDataset)pFW;
85 pDN.WorkspaceName = (IWorkspaceName)pDS.FullName;
86 pDN.Name = pDS.Name;
87 pOutFeatureClassName.FeatureDatasetName = (IDatasetName)pFDN;
88 //将pOutFeatureDSName设置为Null,将它做为参数给ConvertFeatureClass函数,
89 //因为IFeatureClassName本身已经和工作空间关联了,生成的
90 //要素类在工作空间的根目录下,即独立要素类
91 pOutFeatureDSName = null;
92
93 }
94 else//创建的要素类是在给定的参数(要素集合)下
95 {
96 pOutFeatureDSName = (IFeatureDatasetName)apFD.FullName;
97 }
98
99 //开始导入
100 pShpToClsConverter = new FeatureDataConverterClass();
101 pShpToClsConverter.ConvertFeatureClass(pInFeatureClassName, null, pOutFeatureDSName,
102 pOutFeatureClassName, pOutGeometryDef, pOutFields, "", 1000, 0);
103 }
2.6 添加一个Shape文件到需要导入的空间列表中(包括具体的信息,以供查看和选择):
1 ///<summary>
2 /// 添加一个Shape文件到DataGridViewX1中
3 ///</summary>
4 ///<param name="sender"></param>
5 ///<param name="e"></param>
6 private void addFileBtn_Click(object sender, EventArgs e)
7 {
8 OpenFileDialog ofd = new OpenFileDialog();
9
10 //打开SHP文件
11 string StrFilter = "SHP文件(.shp) | *.shp";
12 ofd.Filter = StrFilter;
13 string ImportShapeFileName;
14 if (ofd.ShowDialog() == DialogResult.OK)
15 {
16 ImportShapeFileName = ofd.FileName;
17 }
18 else
19 {
20 return;
21 }
22
23 string ImportFileName = System.IO.Path.GetFileName(ImportShapeFileName);
24 string ImportFileShortName = System.IO.Path.GetFileNameWithoutExtension(ImportShapeFileName);
25 string ImportFilePath = System.IO.Path.GetDirectoryName(ImportShapeFileName);
26 //导入Shape文件的相关信息到空间中
27 object[] obj = new object[3];
28 obj[0] = ImportFileName;
29 obj[1] = ImportFileShortName;
30 obj[2] = ImportFilePath;
31 dataGridViewX1.Rows.Add(obj);
32 }
2.7 和上一个功能类型,不过这个是添加一个目录,即这目录下的所有Shape文件都会被添加到控件列表中。
1 ///<summary>
2 /// 添加一个目录下的所有Shape文件到DataGridViewX1中
3 ///</summary>
4 ///<param name="sender"></param>
5 ///<param name="e"></param>
6 private void addFolderBtn_Click(object sender, EventArgs e)
7 {
8 FolderBrowserDialog folder = new FolderBrowserDialog();
9 if (folder.ShowDialog() == DialogResult.OK)
10 {
11 string strPath = folder.SelectedPath;
12 if (strPath == "")
13 {
14 return ;
15 }
16
17 string []fileNames = Directory.GetFiles(strPath);
18 string strTemp;
19 foreach (string str in fileNames)
20 {
21 strTemp = str.Remove(0, str.LastIndexOf('\\')+1);
22 if (strTemp.Substring(strTemp.IndexOf('.')) == ".shp")
23 {
24 object[] obj = new object[3];
25
26 obj[0] = strTemp;
27 strTemp = strTemp.Remove(strTemp.IndexOf('.'));
28 obj[1] = strTemp;
29 obj[2] = strPath;
30 dataGridViewX1.Rows.Add(obj);
31 }
32 }
33 }
34 }
2.8从显示Shape文件信息的控件列表中删除一个Shape文件的信息,就不需要导入的Shape文件可以先删除,免得影响视线。
1 ///<summary>
2 /// 从DataGridViewX1删除一个文件
3 ///</summary>
4 ///<param name="sender"></param>
5 ///<param name="e"></param>
6 private void delFileBtn_Click(object sender, EventArgs e)
7 {
8
9 if (dataGridViewX1.CurrentRow.Index >= 0)
10 {
11 dataGridViewX1.Rows.Remove(dataGridViewX1.CurrentRow);
12 }
13 else
14 {
15 MessageBox.Show("请选择一个要删除的行!");
16 return ;
17 }
18 }
2.9完成导入Shape文件的按钮功能
1 ///<summary>
2 /// 完成导入的功能,当所有准备和检查功能已经做完并且通过就可以通过完成按钮完成导入了
3 ///</summary>
4 ///<param name="sender"></param>
5 ///<param name="e"></param>
6 private void finishBtn_Click(object sender, EventArgs e)
7 {
8 int fileCount = dataGridViewX1.Rows.Count;
9 //多个Shape文件内容依次导入
10 for (int i = 0; i < fileCount; ++i)
11 {
12 // string str = dataGridViewX1.Rows[i].Cells[1].Value.ToString();
13 // ImportFeatureClassToWorkSpace(null,dataGridViewX1.Rows[i].Cells[2].Value.ToString(), str);
14 ConvertShpToSDE(dataGridViewX1.Rows[i].Cells[2].Value.ToString(),
15 dataGridViewX1.Rows[i].Cells[0].Value.ToString(),
16 dataGridViewX1.Rows[i].Cells[1].Value.ToString());
17 }
18 LogHelp.writeLog(FrmMain.username, "空间数据管理", "空间数据Shape格式转换导入成功");
19 MessageBox.Show("导入数据完成!");
20 Close();
21 }
真正的导入函数是前面已经介绍过的ConvertShpToSDE函数。
2.10 把控件中的Shape文件信息保存起来(配置文件)以便下一次直接导入这些文件。
1 ///<summary>
2 /// 保存DataGridViewX1的文件列表到配置文件中
3 ///</summary>
4 ///<param name="sender"></param>
5 ///<param name="e"></param>
6 private void saveSetBtn_Click(object sender, EventArgs e)
7 {
8 System.IO.FileStream fs = new System.IO.FileStream(Directory.GetCurrentDirectory()+"\\set.ini", FileMode.Create);
9 StreamWriter sw = new StreamWriter(fs);
10
11 int fileCount = dataGridViewX1.Rows.Count - 1;
12 //开始写入
13 for (int i = 0; i < fileCount; ++i)
14 {
15 string str = dataGridViewX1.Rows[i].Cells[0].Value.ToString();
16 sw.WriteLine(str);
17 str = dataGridViewX1.Rows[i].Cells[1].Value.ToString();
18 sw.WriteLine(str);
19 str = dataGridViewX1.Rows[i].Cells[2].Value.ToString();
20 sw.WriteLine(str);
21 }
22 //清空缓冲区
23 sw.Flush();
24 //关闭流
25 sw.Close();
26 fs.Close();
27 }
2.11读取配置文件的文件列表加载到控件中
1 ///<summary>
2 /// 读取配置文件的文件列表加载到DataGridViewX1中
3 ///</summary>
4 ///<param name="sender"></param>
5 ///<param name="e"></param>
6 private void readSetBtn_Click(object sender, EventArgs e)
7 {
8 dataGridViewX1.Rows.Clear();
9
10 StreamReader objReader = new StreamReader(Directory.GetCurrentDirectory() + "\\set.ini");
11 string sLine = "";
12 while ((sLine = objReader.ReadLine()) != null)
13 {
14 object[] obj = new object[3];
15 obj[0] = sLine;
16 obj[1] = objReader.ReadLine();
17 obj[2] = objReader.ReadLine();
18
19 dataGridViewX1.Rows.Add(obj);
20 }
21 objReader.Close();
22 }
2.12 选择空间参考系(空间数据都有的)
1 ///<summary>
2 /// 选择空间参考系
3 ///</summary>
4 ///<param name="sender"></param>
5 ///<param name="e"></param>
6 private void selectBtn_Click(object sender, EventArgs e)
7 {
8 FrmSelectSpatialReference fssr = new FrmSelectSpatialReference();
9 fssr.ShowDialog();
10 if (fssr.bOk)
11 {
12 spatialReTxt.Text = fssr.sc_id.ToString();
13 sc_id = fssr.sc_id;
14 }
15 }
2.13选择上载字段(没有选择的将不会导入到数据库中)
1 ///<summary>
2 /// 选择上载字段
3 ///</summary>
4 ///<param name="sender"></param>
5 ///<param name="e"></param>
6 private void selectUploadFieldBtn_Click(object sender, EventArgs e)
7 {
8 if (dataGridViewX1.CurrentRow == null)
9 {
10 MessageBox.Show("选择一行!");
11 return;
12 }
13 string strPath = dataGridViewX1.CurrentRow.Cells[2].Value.ToString();
14 string strName = dataGridViewX1.CurrentRow.Cells[0].Value.ToString();
15
16 FrmSelectField fsf = new FrmSelectField();
17 fsf.InitDataGridView();
18 //创建一个输出shp文件的工作空间
19 IWorkspaceFactory pShpWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
20 IWorkspace pSourceWS = pShpWorkspaceFactory.OpenFromFile(strPath, 0);
21 IFeatureWorkspace pSourceFWS = (IFeatureWorkspace)pSourceWS;
22 IFeatureClass pFC = pSourceFWS.OpenFeatureClass(strName);
23 object[] obj = new object[4];
24 for (int i = 0; i < pFC.Fields.FieldCount; i++)
25 {
26 IField field = pFC.Fields.get_Field(i);
27 obj[0] = false;
28 obj[1] = field.Name;
29 obj[2] = field.Type;
30 obj[3] = field.Length;
31 fsf.AddRowToDataGridView(obj);
32 }
33 fsf.ShowDialog();
34 //确实确定了选择,就把选择的结果和文件名对应保存起来。
35 if (fsf.bSelect)
36 {
37 IFields pFds = new FieldsClass();
38 IFieldsEdit pFdsEdit = pFds as IFieldsEdit;
39 for (int i = 0; i < pFC.Fields.FieldCount; i++)
40 {
41 if (fsf.RowsIsSelect(i))
42 {
43 IField field = pFC.Fields.get_Field(i);
44 pFdsEdit.AddField(field);
45 }
46 }
47 if (fieldDic.ContainsKey(strName))
48 {
49 fieldDic.Remove(strName);
50 }
51 fieldDic.Add(strName, pFds);
52 }
53 }
2.14检查导入字段是否满足要求,不满足就提示哪些不满足,满足以后才能执行导入功能。
1 ///<summary>
2 /// 检查字段按钮功能,检查需要导入字段是否满足要求
3 ///</summary>
4 ///<param name="sender"></param>
5 ///<param name="e"></param>
6 private void checkUploadFieldBtn_Click(object sender, EventArgs e)
7 {
8 if (dataGridViewX1.CurrentRow == null)
9 {
10 MessageBox.Show("选择一行!");
11 return;
12 }
13
14 string strPath = dataGridViewX1.CurrentRow.Cells[2].Value.ToString();
15 string strTableName = dataGridViewX1.CurrentRow.Cells[1].Value.ToString();
16 string strName = dataGridViewX1.CurrentRow.Cells[0].Value.ToString();
17 /*
18 IWorkspace2 pW = pFW as IWorkspace2;
19 if (!pW.get_NameExists(esriDatasetType.esriDTFeatureClass, strTableName))
20 {
21 MessageBox.Show("数据库中不存在此表结构,不能检查!");
22 return;
23 }
24
25 //1.遍历数据集中的要素类找到并打开目标要素类
26
27 IEnumDataset enumDs = (pFW as IWorkspace).get_Datasets(esriDatasetType.esriDTFeatureDataset);
28 IFeatureDataset featureDs = enumDs.Next() as IFeatureDataset;
29 IFeatureClass pSDEFC = null;
30 while (featureDs != null)
31 {
32 IFeatureClassContainer fcContainer = featureDs as IFeatureClassContainer;
33 for (int i = 0; i < fcContainer.ClassCount; i++)
34 {
35 IFeatureClass pTempFC = fcContainer.get_Class(i);
36 string strTemp = pTempFC.AliasName;
37 if (pTempFC.AliasName.IndexOf('.') >= 0)
38 {
39 strTemp = strTemp.Substring(strTemp.IndexOf('.')+1);
40 }
41
42 if (strTemp.ToUpper() == strTableName.ToUpper())
43 {
44 pSDEFC = fcContainer.get_Class(i);
45 break;
46 }
47 }
48 if (pSDEFC != null)
49 {
50 break;
51 }
52 featureDs = enumDs.Next() as IFeatureDataset;
53 }
54
55 //2.遍历游离的要素类
56 if (pSDEFC == null)
57 {
58 enumDs = (pFW as IWorkspace).get_Datasets(esriDatasetType.esriDTFeatureClass);
59 IFeatureClass pTempFC = enumDs.Next() as IFeatureClass;
60 while (pTempFC != null)
61 {
62 string strTemp = pTempFC.AliasName;
63 if (pTempFC.AliasName.IndexOf('.') >= 0)
64 {
65 strTemp = strTemp.Substring(strTemp.IndexOf('.')+1);
66 }
67
68 if (strTemp.ToUpper() == strTableName.ToUpper())
69 {
70 pSDEFC = pTempFC;
71 break;
72 }
73 pTempFC = enumDs.Next() as IFeatureClass;
74 }
75 }
76 */
77 SqlHelper sh = new SqlHelper();
78 string sql = "select * from jcsjk_fielddefine where table_name='" + strTableName.ToLower() + "'";
79 if (sh.GetRecordCount(sql) <= 0)
80 {
81 MessageBox.Show("没有定义该数据标准,不能检查!");
82 return;
83 }
84
85 if (!fieldDic.ContainsKey(strName))
86 {
87 if (MessageBox.Show("此文件还没有选择上载字段,默认全部上载!是否继续检查?", "提示信息",
88 MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
89 {
90 IWorkspaceFactory pShpWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
91 IWorkspace pSourceWS = pShpWorkspaceFactory.OpenFromFile(strPath, 0);
92 IFeatureWorkspace pSourceFWS = (IFeatureWorkspace)pSourceWS;
93 IFeatureClass pFC = pSourceFWS.OpenFeatureClass(strName);
94
95 if (CheckFields(pFC.Fields, strTableName))
96 {
97 MessageBox.Show("字段匹配");
98 }
99 else
100 {
101 MessageBox.Show("字段不匹配");
102 }
103 }
104 }
105 else
106 {
107 if (CheckFields(fieldDic[strName], strTableName))
108 {
109 MessageBox.Show("字段匹配");
110 }
111 else
112 {
113 MessageBox.Show("字段不匹配");
114 }
115 }
116 }
117
118 ///<summary>
119 /// 检查字段是否符合数据标准定义
120 ///</summary>
121 ///<param name="pSourceFileds">字段集</param>
122 ///<param name="strTableName">对应的标准名</param>
123 ///<returns></returns>
124 private bool CheckFields(IFields pSourceFileds, string strTableName)
125 {
126 SqlHelper sh = new SqlHelper();
127 string sql = "select * from jcsjk_fielddefine where table_name ='"
128 + strTableName.ToUpper() + "' or table_name='" + strTableName.ToLower() + "'";
129
130 if (pSourceFileds.FieldCount != sh.GetRecordCount(sql))
131 {
132 MessageBox.Show("字段数量不匹配!");
133 return false;
134 }
135 bool result = true;
136 FrmFieldCheckResult ffcr = new FrmFieldCheckResult();
137 ffcr.InitDataGridView1();
138 ffcr.InitDataGridView2();
139 OracleDataReader odr = sh.ReturnDataReader(sql);
140 object[] obj1 = new object[5];
141 object[] obj2 = new object[4];
142 for (int i = 0; i < pSourceFileds.FieldCount; i++)
143 {
144 odr.Read();
145 bool bMatch = true;
146 if (odr["TYPE"].ToString() == "RowID")
147 {
148 IField pSourceField = pSourceFileds.get_Field(pSourceFileds.FindField("FID"));
149
150 obj1[0] = pSourceField.Name;
151 obj1[1] = pSourceField.Type;
152 obj1[2] = pSourceField.Length;
153 obj1[3] = pSourceField.IsNullable;
154
155 obj2[0] = odr["NAME"].ToString();
156 obj2[1] = esriFieldType.esriFieldTypeOID;
157 obj2[2] = odr["LENGTH"].ToString();
158 if (odr["ISNULL"].ToString() == "是")
159 {
160 obj2[3] = true;
161 }
162 else
163 {
164 obj2[3] = false;
165 }
166
167 if (obj1[1].ToString() != obj2[1].ToString()
168 || obj1[2].ToString() != obj2[2].ToString()
169 || obj1[3].ToString() != obj2[3].ToString())
170 {
171 bMatch = false;
172 result = false;
173 }
174 obj1[4] = bMatch;
175 ffcr.AddRowToDataGridView1(obj1);
176 ffcr.AddRowToDataGridView2(obj2);
177 }
178 else if (pSourceFileds.FindField(odr["NAME"].ToString()) >= 0)
179 {
180 IField pSourceField = pSourceFileds.get_Field(pSourceFileds.FindField(odr["NAME"].ToString()));
181
182 obj1[0] = pSourceField.Name;
183 obj1[1] = pSourceField.Type;
184 obj1[2] = pSourceField.Length;
185 obj1[3] = pSourceField.IsNullable;
186
187 obj2[0] = odr["NAME"].ToString();
188 if (odr["TYPE"].ToString() == "RowID")
189 {
190 obj2[1] = esriFieldType.esriFieldTypeOID;
191 }
192 else if (odr["type"].ToString() == "整数型")
193 {
194 obj2[1] = esriFieldType.esriFieldTypeInteger;
195 }
196 else if (odr["type"].ToString() == "浮点型")
197 {
198 obj2[1] = esriFieldType.esriFieldTypeDouble;
199 }
200 else if (odr["type"].ToString() == "字符型")
201 {
202 obj2[1] = esriFieldType.esriFieldTypeString;
203 }
204 else if (odr["type"].ToString() == "图元")
205 {
206 obj2[1] = esriFieldType.esriFieldTypeGeometry;
207 }
208 obj2[2] = odr["LENGTH"].ToString();
209 if (odr["ISNULL"].ToString() == "是")
210 {
211 obj2[3] = true;
212 }
213 else
214 {
215 obj2[3] = false;
216 }
217
218 if (obj1[1].ToString() != obj2[1].ToString()
219 || obj1[2].ToString() != obj2[2].ToString()
220 || obj1[3].ToString() != obj2[3].ToString())
221 {
222 bMatch = false;
223 result = false;
224 }
225 obj1[4] = bMatch;
226 ffcr.AddRowToDataGridView1(obj1);
227 ffcr.AddRowToDataGridView2(obj2);
228 }
229 else
230 {
231 MessageBox.Show("字段名称不匹配!");
232 result = false;
233 }
234 }
235 ffcr.ShowDialog();
236 return result;
237 }
2.15根据已有的Shape文件,打开检查字段界面,选中一个具体的Shape文件就检查一个Shape文件,通过的就在控件中标示出来。
1 ///<summary>
2 /// 检查文件字段,字段类型和长度
3 ///</summary>
4 ///<param name="sender"></param>
5 ///<param name="e"></param>
6 private void checkFileFieldBtn_Click(object sender, EventArgs e)
7 {
8 if (dataGridViewX1.CurrentRow == null)
9 {
10 MessageBox.Show("选择一行!");
11 return;
12 }
13 string strPath = dataGridViewX1.CurrentRow.Cells[2].Value.ToString();
14 string strName = dataGridViewX1.CurrentRow.Cells[0].Value.ToString();
15
16 FrmSelectField fsf = new FrmSelectField();
17 fsf.SetBtnVisible(false);
18 fsf.Text = ((ButtonX)sender).Text;
19 fsf.InitDataGridView();
20 //创建一个输出shp文件的工作空间
21 IWorkspaceFactory pShpWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
22 IWorkspace pSourceWS = pShpWorkspaceFactory.OpenFromFile(strPath, 0);
23 IFeatureWorkspace pSourceFWS = (IFeatureWorkspace)pSourceWS;
24 IFeatureClass pFC = pSourceFWS.OpenFeatureClass(strName);
25 object[] obj = new object[4];
26 for (int i = 0; i < pFC.Fields.FieldCount; i++)
27 {
28 IField field = pFC.Fields.get_Field(i);
29 obj[0] = false;
30 obj[1] = field.Name;
31 obj[2] = field.Type;
32 obj[3] = field.Length;
33 fsf.AddRowToDataGridView(obj);
34 }
35 fsf.SetDataGridViewColumn(0);
36 fsf.ShowDialog();
37 }
3.总结
空间数据的转换是比较复杂和繁琐的功能,需要考虑的方面非常多,只有细心的一点一滴的做好每一步才能出色的完成这些功能,尤其是数据格式的检查是最复杂的,必须一个一个字段去对比。
今天到此为止!