这二天忙着在做 数据检查方面的小功能,希望自己快点入门吧。先根据全国二次土地调杳建立一个标准,然后用户将数据与标准图层进行比较,找出错误的字段,字段类型,字段长度。。。。提高了生产效率,节约时间 。如果让数据制作 人员手工检查,难度大不说,不出错可就怪了。
把关键代码贴出来,个人感觉 有点乱。
总结:变量取名得按标准来,当代码较时,应当根据功能新建函数 进行功能分割,否则太长了,自己也会晕滴。
在删除一个要素类中所有的要素时,采用Smart所说的用将Ifeatureclass转为ITabel,一次删除。效率高,
在数据导入时,之前是一个一个导入,太慢了,后改用IfeatureBuffer
IFeatureBuffer pFeaBuffer = m_AimFeaCls.CreateFeatureBuffer();
pFeaBuffer.Shape = pTemFea.ShapeCopy;
pFeaBuffer.set_Value(Convert.ToInt32(pAimIndexs[i]), pTemFea.get_Value(Convert.ToInt32(pSouceIndexs[i])));//pAimIndexs[i])
pFeaCurAim.InsertFeature(pFeaBuffer);
pFeaCurAim.Flush();
将标准添加到cboLayer:
Code
//打开personGeodatabase,并添加图层
IWorkspaceFactory pAccessWorkspaceFactory = new AccessWorkspaceFactoryClass();
//打开工作空间并遍历数据集
IWorkspace pWorkspace =pAccessWorkspaceFactory.OpenFromFile(strFullPath, 0);
pFeatureWorkspace = (IFeatureWorkspace)pWorkspace;
IEnumDataset pEnumDataset = pWorkspace.get_Datasets(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTAny);
pEnumDataset.Reset();
IDataset pDataset = pEnumDataset.Next();
//如果数据集是IFeatureDataset,则遍历它下面的子类
while (pDataset != null)
{
//如果是要素类 将它与目标图层比较
if (pDataset is IFeatureDataset)
{
LoadFeatureDataset(pDataset);
}
else
{
cboLayer.Items.Add(pDataset.Name);
}
pDataset = pEnumDataset.Next();
}
}
/// <summary>
/// 加载IFeatureDataset
/// </summary>
/// <param name="ds"></param>
private void LoadFeatureDataset(IDataset ds)
{
IEnumDataset ped = ds.Subsets;
IDataset pDataset = ped.Next();
while (pDataset != null)
{
cboLayer.Items.Add(pDataset.Name);
pDataset = ped.Next();
}
}
//批量打开shp文件 先判断图层是否已加过了
for (int i = 0; i < trlLayer.Nodes.Count; i++)
{
TreeListNode tnode = trlLayer.Nodes[i];
if (pFeaCls.Equals(tnode.Tag))
{
return;
}
}
private IFeatureClass GetShpSouceFeaCls(string sFileName)
{
try
{
this.m_WspFactory = new ShapefileWorkspaceFactoryClass();
//打开Shp文件
string pDirpath = System.IO.Path.GetDirectoryName (sFileName) + "\\";
string pFilename = System.IO.Path.GetFileNameWithoutExtension(sFileName);
this.m_Wsp = (IFeatureWorkspace)this.m_WspFactory.OpenFromFile(pDirpath, 0);
eturn this.m_Wsp.OpenFeatureClass(pFilename) as IFeatureClass;
}
catch (Exception ex)
{
return null;
}
}
检查数据
{
IFeatureClass srcFC;//源图层
IFeatureClass tarFC;//目标图层
srcFC = trlLayer.Nodes[i].Tag as IFeatureClass;
tarFC = (m_TarWorkspace as IFeatureWorkspace).OpenFeatureClass(tarLayerName);
CheckLayer(srcFC, tarFC);
}
for (int i = 0; i < tarFC.Fields.FieldCount; i++)
{
IField tarField = tarFC.Fields.get_Field(i);
#region 对系统字段不进行检查
if (tarField.Type == esriFieldType.esriFieldTypeGeometry ||
tarField.Type == esriFieldType.esriFieldTypeGlobalID ||
tarField.Type == esriFieldType.esriFieldTypeGUID ||
tarField.Type == esriFieldType.esriFieldTypeOID)
continue;
else if (!tarField.Editable || tarField.Name.Substring(tarField.Name.Length - 3).ToLower() == "oid")
continue;
#endregion
#region 检查非系统字段
else
{
int srcIdx = srcFC.Fields.FindField(tarField.Name);
if (srcIdx == -1)//字段不存在
{
AppendCheckInfo("缺少字段 [" + tarField.AliasName + "]", true, true);
}
else
{
IField srcField = srcFC.Fields.get_Field(srcIdx);
CheckFields(srcField, tarField);//检查二个字段
}
}
#endregion
}
private void CheckFields(IField srcField, IField tarField)
{
if (srcField.Type == tarField.Type)
{
if (srcField.Type == esriFieldType.esriFieldTypeString && srcField.Length != tarField.Length)
{
AppendCheckInfo("字段 [" + srcField.AliasName + "] 长度 " + srcField.Length + " 不对,正确的长度是 " + tarField.Length, true, true);
}
else if (srcField.Type == esriFieldType.esriFieldTypeDouble || srcField.Type == esriFieldType.esriFieldTypeSingle)
{
//if (srcField.Length != tarField.Length)
//{
// AppendCheckInfo("字段 [" + srcField.AliasName + "] 长度不对,正确的长度是 " + tarField.Length, true, true);
//}
//if (srcField.Scale != tarField.Scale)
//{
// AppendCheckInfo("字段 [" + srcField.AliasName + "] 小数位数不对,正确的位数是 " + tarField.Scale, true, true);
//}
}
}
else
{
AppendCheckInfo("字段 [" + srcField.AliasName + "]类型 " + TranFieldType(srcField.Type) + "不对,正确的类型是 " + TranFieldType(tarField.Type), true, true);
}
}
/// <summary>
/// 删除FeatureLayer上所有Feature
/// </summary>
/// <param name="pFeatureLayer"></param>
private void ClearFeatureClass(IFeatureClass pFeatureClass)
{
try
{
ITable pTable = pFeatureClass as ITable;
pTable.DeleteSearchedRows(null);
}
catch (Exception Err)
{
MessageBox.Show(Err.Message, "删除FeatureClass", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
for (int m = 0; m < m_SouceFeaCls.Fields.FieldCount; m++)
{
IFeatureCursor pFeaCur = this.m_SouceFeaCls.Search(null, false);
IFeature pTemFea = null;
IFeatureCursor pFeaCurAim = m_AimFeaCls.Insert(true);
IFeatureBuffer pFeaBuffer = m_AimFeaCls.CreateFeatureBuffer();
while ((pTemFea = pFeaCur.NextFeature()) != null)
{
int pErrIndex = -1;
string pErrName = "";
try
{
pFeaBuffer.Shape = pTemFea.ShapeCopy;
for (int i = 0; i < pSouceIndexs.Count; i++)
{
//记录最后一个入库字段
pErrIndex = i;
pErrName = pFeaBuffer.Fields.get_Field(i).Name;
pFeaBuffer.set_Value(Convert.ToInt32(pAimIndexs[i]), pTemFea.get_Value(Convert.ToInt32(pSouceIndexs[i])));//pAimIndexs[i])
}
pFeaCurAim.InsertFeature(pFeaBuffer);
nFinish++;
}
catch (Exception e)
{
//知道是 哪个字段写入错误
if (pErrIndex != pSouceIndexs.Count - 1)
{
strImport += "ID: " + pTemFea.OID.ToString() + "\r\n " + pErrName + " 字段导入失败!\r\n";
}
else
{
strImport += "ID: " + pTemFea.OID.ToString() + "\r\n";
}
strImport += "错误信息:" + e.Message + "\r\n";
}
this.m_Helper.Position++;
this.m_Helper.SetHint("正在导入 " + this.m_Helper.Position + "/" + pFeaCount, "");
}
pFeaCurAim.Flush();
}
//打开personGeodatabase,并添加图层
IWorkspaceFactory pAccessWorkspaceFactory = new AccessWorkspaceFactoryClass();
//打开工作空间并遍历数据集
IWorkspace pWorkspace =pAccessWorkspaceFactory.OpenFromFile(strFullPath, 0);
pFeatureWorkspace = (IFeatureWorkspace)pWorkspace;
IEnumDataset pEnumDataset = pWorkspace.get_Datasets(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTAny);
pEnumDataset.Reset();
IDataset pDataset = pEnumDataset.Next();
//如果数据集是IFeatureDataset,则遍历它下面的子类
while (pDataset != null)
{
//如果是要素类 将它与目标图层比较
if (pDataset is IFeatureDataset)
{
LoadFeatureDataset(pDataset);
}
else
{
cboLayer.Items.Add(pDataset.Name);
}
pDataset = pEnumDataset.Next();
}
}
/// <summary>
/// 加载IFeatureDataset
/// </summary>
/// <param name="ds"></param>
private void LoadFeatureDataset(IDataset ds)
{
IEnumDataset ped = ds.Subsets;
IDataset pDataset = ped.Next();
while (pDataset != null)
{
cboLayer.Items.Add(pDataset.Name);
pDataset = ped.Next();
}
}
//批量打开shp文件 先判断图层是否已加过了
for (int i = 0; i < trlLayer.Nodes.Count; i++)
{
TreeListNode tnode = trlLayer.Nodes[i];
if (pFeaCls.Equals(tnode.Tag))
{
return;
}
}
private IFeatureClass GetShpSouceFeaCls(string sFileName)
{
try
{
this.m_WspFactory = new ShapefileWorkspaceFactoryClass();
//打开Shp文件
string pDirpath = System.IO.Path.GetDirectoryName (sFileName) + "\\";
string pFilename = System.IO.Path.GetFileNameWithoutExtension(sFileName);
this.m_Wsp = (IFeatureWorkspace)this.m_WspFactory.OpenFromFile(pDirpath, 0);
eturn this.m_Wsp.OpenFeatureClass(pFilename) as IFeatureClass;
}
catch (Exception ex)
{
return null;
}
}
检查数据
{
IFeatureClass srcFC;//源图层
IFeatureClass tarFC;//目标图层
srcFC = trlLayer.Nodes[i].Tag as IFeatureClass;
tarFC = (m_TarWorkspace as IFeatureWorkspace).OpenFeatureClass(tarLayerName);
CheckLayer(srcFC, tarFC);
}
for (int i = 0; i < tarFC.Fields.FieldCount; i++)
{
IField tarField = tarFC.Fields.get_Field(i);
#region 对系统字段不进行检查
if (tarField.Type == esriFieldType.esriFieldTypeGeometry ||
tarField.Type == esriFieldType.esriFieldTypeGlobalID ||
tarField.Type == esriFieldType.esriFieldTypeGUID ||
tarField.Type == esriFieldType.esriFieldTypeOID)
continue;
else if (!tarField.Editable || tarField.Name.Substring(tarField.Name.Length - 3).ToLower() == "oid")
continue;
#endregion
#region 检查非系统字段
else
{
int srcIdx = srcFC.Fields.FindField(tarField.Name);
if (srcIdx == -1)//字段不存在
{
AppendCheckInfo("缺少字段 [" + tarField.AliasName + "]", true, true);
}
else
{
IField srcField = srcFC.Fields.get_Field(srcIdx);
CheckFields(srcField, tarField);//检查二个字段
}
}
#endregion
}
private void CheckFields(IField srcField, IField tarField)
{
if (srcField.Type == tarField.Type)
{
if (srcField.Type == esriFieldType.esriFieldTypeString && srcField.Length != tarField.Length)
{
AppendCheckInfo("字段 [" + srcField.AliasName + "] 长度 " + srcField.Length + " 不对,正确的长度是 " + tarField.Length, true, true);
}
else if (srcField.Type == esriFieldType.esriFieldTypeDouble || srcField.Type == esriFieldType.esriFieldTypeSingle)
{
//if (srcField.Length != tarField.Length)
//{
// AppendCheckInfo("字段 [" + srcField.AliasName + "] 长度不对,正确的长度是 " + tarField.Length, true, true);
//}
//if (srcField.Scale != tarField.Scale)
//{
// AppendCheckInfo("字段 [" + srcField.AliasName + "] 小数位数不对,正确的位数是 " + tarField.Scale, true, true);
//}
}
}
else
{
AppendCheckInfo("字段 [" + srcField.AliasName + "]类型 " + TranFieldType(srcField.Type) + "不对,正确的类型是 " + TranFieldType(tarField.Type), true, true);
}
}
/// <summary>
/// 删除FeatureLayer上所有Feature
/// </summary>
/// <param name="pFeatureLayer"></param>
private void ClearFeatureClass(IFeatureClass pFeatureClass)
{
try
{
ITable pTable = pFeatureClass as ITable;
pTable.DeleteSearchedRows(null);
}
catch (Exception Err)
{
MessageBox.Show(Err.Message, "删除FeatureClass", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
for (int m = 0; m < m_SouceFeaCls.Fields.FieldCount; m++)
{
IFeatureCursor pFeaCur = this.m_SouceFeaCls.Search(null, false);
IFeature pTemFea = null;
IFeatureCursor pFeaCurAim = m_AimFeaCls.Insert(true);
IFeatureBuffer pFeaBuffer = m_AimFeaCls.CreateFeatureBuffer();
while ((pTemFea = pFeaCur.NextFeature()) != null)
{
int pErrIndex = -1;
string pErrName = "";
try
{
pFeaBuffer.Shape = pTemFea.ShapeCopy;
for (int i = 0; i < pSouceIndexs.Count; i++)
{
//记录最后一个入库字段
pErrIndex = i;
pErrName = pFeaBuffer.Fields.get_Field(i).Name;
pFeaBuffer.set_Value(Convert.ToInt32(pAimIndexs[i]), pTemFea.get_Value(Convert.ToInt32(pSouceIndexs[i])));//pAimIndexs[i])
}
pFeaCurAim.InsertFeature(pFeaBuffer);
nFinish++;
}
catch (Exception e)
{
//知道是 哪个字段写入错误
if (pErrIndex != pSouceIndexs.Count - 1)
{
strImport += "ID: " + pTemFea.OID.ToString() + "\r\n " + pErrName + " 字段导入失败!\r\n";
}
else
{
strImport += "ID: " + pTemFea.OID.ToString() + "\r\n";
}
strImport += "错误信息:" + e.Message + "\r\n";
}
this.m_Helper.Position++;
this.m_Helper.SetHint("正在导入 " + this.m_Helper.Position + "/" + pFeaCount, "");
}
pFeaCurAim.Flush();
}
后面在导入属性数据时,发现ITable 中 的row 就相当于IFeatureClass 的Feature 。一个要素本来就对应于一行嘛
Code
int pRowCount = this.m_SoueceTable.RowCount(null);
this.m_Helper.Max = pRowCount;
this.m_Helper.Position = 0;
this.m_Helper.SetHint("正在导入", "");
strImport += "入库层: " + (m_SoueceTable as IDataset).BrowseName + ":\r\n";
int nTolFea = m_SoueceTable.RowCount(null);
int nFinish = 0;
ICursor pRowCur = this.m_SoueceTable.Search(null, false);
IRow pRow;
ICursor PRowCurAim = m_AimTable.Insert(true);
IRowBuffer pRowBuffer = m_AimTable.CreateRowBuffer();
#region 遍历每行
while ((pRow = pRowCur.NextRow()) != null)
{
int pErrIndex = -1;
string pErrName = "";
try
{
for (int i = 0; i < pSouceIndexs.Count; i++)
{
if (this.m_Canceled == true) return;
//记录最后一个入库字段
pErrIndex = i;
pErrName = pRowBuffer.Fields.get_Field(i).Name;
//pFeaBuffer.set_Value(Convert.ToInt32(pAimIndexs[i]), pTemFea.get_Value(Convert.ToInt32(pSouceIndexs[i])));//pAimIndexs[i])
pRowBuffer.set_Value(Convert.ToInt32(pAimIndexs[i]), pRow.get_Value(Convert.ToInt32(pSouceIndexs[i])));
}
PRowCurAim.InsertRow(pRowBuffer);
// pFeaCurAim.InsertFeature(pFeaBuffer);
nFinish++;
}
catch (Exception e)
{
//知道是 哪个字段写入错误
if (pErrIndex != pSouceIndexs.Count - 1)
{
strImport += "ID: " + pRow.OID.ToString() + "\r\n " + pErrName + " 字段导入失败!\r\n";
}
else
{
strImport += "ID: " + pRow.OID.ToString() + "\r\n";
}
strImport += "错误信息:" + e.Message + "\r\n";
}
this.m_Helper.Position++;
this.m_Helper.SetHint("正在导入 " + this.m_Helper.Position + "/" + pRowCount, "");
}
#endregion
PRowCurAim.Flush();
int pRowCount = this.m_SoueceTable.RowCount(null);
this.m_Helper.Max = pRowCount;
this.m_Helper.Position = 0;
this.m_Helper.SetHint("正在导入", "");
strImport += "入库层: " + (m_SoueceTable as IDataset).BrowseName + ":\r\n";
int nTolFea = m_SoueceTable.RowCount(null);
int nFinish = 0;
ICursor pRowCur = this.m_SoueceTable.Search(null, false);
IRow pRow;
ICursor PRowCurAim = m_AimTable.Insert(true);
IRowBuffer pRowBuffer = m_AimTable.CreateRowBuffer();
#region 遍历每行
while ((pRow = pRowCur.NextRow()) != null)
{
int pErrIndex = -1;
string pErrName = "";
try
{
for (int i = 0; i < pSouceIndexs.Count; i++)
{
if (this.m_Canceled == true) return;
//记录最后一个入库字段
pErrIndex = i;
pErrName = pRowBuffer.Fields.get_Field(i).Name;
//pFeaBuffer.set_Value(Convert.ToInt32(pAimIndexs[i]), pTemFea.get_Value(Convert.ToInt32(pSouceIndexs[i])));//pAimIndexs[i])
pRowBuffer.set_Value(Convert.ToInt32(pAimIndexs[i]), pRow.get_Value(Convert.ToInt32(pSouceIndexs[i])));
}
PRowCurAim.InsertRow(pRowBuffer);
// pFeaCurAim.InsertFeature(pFeaBuffer);
nFinish++;
}
catch (Exception e)
{
//知道是 哪个字段写入错误
if (pErrIndex != pSouceIndexs.Count - 1)
{
strImport += "ID: " + pRow.OID.ToString() + "\r\n " + pErrName + " 字段导入失败!\r\n";
}
else
{
strImport += "ID: " + pRow.OID.ToString() + "\r\n";
}
strImport += "错误信息:" + e.Message + "\r\n";
}
this.m_Helper.Position++;
this.m_Helper.SetHint("正在导入 " + this.m_Helper.Position + "/" + pRowCount, "");
}
#endregion
PRowCurAim.Flush();