public static bool DeleteDuplicatedRasterDataset(ref IRasterCatalog targetRasterCatalog)
{
try
{
IFeatureClass pFeatureClass = targetRasterCatalog as IFeatureClass;
int featurecount = pFeatureClass.FeatureCount(null);
int oidindex = pFeatureClass.FindField(pFeatureClass.OIDFieldName);
int shpindex = pFeatureClass.FindField(pFeatureClass.ShapeFieldName);
IList<int> oids = new List<int>();
IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, false);
IFeature pFeature = pFeatureCursor.NextFeature();
while (pFeature != null)
{
oids.Add((int)pFeature.get_Value(oidindex));
pFeature = pFeatureCursor.NextFeature();
}
IFeature pfirstFeature;
IFeature potherFeature;
IRelationalOperator pRelationalOperator;
ITopologicalOperator pTopologicalOperator;
IGeometryBag pGeometryBag;
IGeometryCollection pGeometryCollection;
IPolygon4 pOutPolygon;
object before = Type.Missing;
object after = Type.Missing;
foreach (int o in oids)
{
if (IsFeatureExisted(pFeatureClass, o))
{
pfirstFeature = pFeatureClass.GetFeature(o);
pRelationalOperator = (IRelationalOperator)pfirstFeature.Shape.Envelope;
pGeometryBag = new GeometryBagClass();
pGeometryCollection = pGeometryBag as IGeometryCollection;
foreach (int i in oids) //效率太低,可以优化成先查询获取相交的feature.shape
{
if (IsFeatureExisted(pFeatureClass, i))
{
potherFeature = pFeatureClass.GetFeature(i);
if (pRelationalOperator.Overlaps(potherFeature.Shape.Envelope))
{
pGeometryCollection.AddGeometry(potherFeature.Shape.Envelope as IGeometry, ref before, ref after);
}
}
}
if (pGeometryCollection.GeometryCount > 0)
{
pTopologicalOperator = new PolygonClass();
pTopologicalOperator.ConstructUnion(pGeometryBag as IEnumGeometry);
pOutPolygon = pTopologicalOperator as IPolygon4;
if ((pTopologicalOperator as IRelationalOperator).Contains(pfirstFeature.Shape))
{
pfirstFeature.Delete();
UpdataFootprint(targetRasterCatalog);
}
}
}
}
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return false;
}
}
//改进后
public bool DeleteDuplicatedRasterDataset(ref IRasterCatalog targetRasterCatalog,string shppath,string shpname)
{
try
{
IFeatureClass pFeatureClass = targetRasterCatalog as IFeatureClass;
int featurecount = pFeatureClass.FeatureCount(null);
int oidindex = pFeatureClass.FindField(pFeatureClass.OIDFieldName);
int shpindex = pFeatureClass.FindField(pFeatureClass.ShapeFieldName);
IList<int> oids = new List<int>();
IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, false);
IFeature pFeature = pFeatureCursor.NextFeature();
while (pFeature != null)
{
oids.Add((int)pFeature.get_Value(oidindex));
pFeature = pFeatureCursor.NextFeature();
}
ITopologicalOperator pTopologicalOperator;
IGeometryBag pGeometryBag;
IGeometryCollection pGeometryCollection;
object before = Type.Missing;
object after = Type.Missing;
IFeature pnFeature;
ISpatialFilter pnspatialFilter;
IFeatureCursor pnFeatureCursor;
IFeatureClass pHelperFeatureClass;
IFeature pHelperFeature;
int count = oids.Count;
int i = 0;
this.prgBarAll.Maximum = count;
foreach (int o in oids)
{
this.Refresh();
lblPrgInfo.Text = "共计" + count.ToString()+"条数据," +"开始扫描第" + (i+1).ToString() + "个feature";
if (AEHelper.IsFeatureExisted(pFeatureClass, o))
{
pFeature = pFeatureClass.GetFeature(o);
pGeometryBag = new GeometryBagClass();
pGeometryCollection = (IGeometryCollection)pGeometryBag;
pnspatialFilter = new SpatialFilterClass();
pnspatialFilter.Geometry = pFeature.Shape;
pnspatialFilter.GeometryField = pFeatureClass.ShapeFieldName;
pnspatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
pnFeatureCursor = pFeatureClass.Search(pnspatialFilter, false);
pnFeature = pnFeatureCursor.NextFeature();
while (pnFeature != null)
{
if (pnFeature.OID != o)
{
pGeometryCollection.AddGeometry(pnFeature.Shape, ref before, ref after);
}
pnFeature = pnFeatureCursor.NextFeature();
}
if (pGeometryCollection.GeometryCount > 0)
{
pTopologicalOperator = new PolygonClass();
pTopologicalOperator.ConstructUnion((IEnumGeometry)pGeometryBag);
//赋值给一个feature,然后再获取feature.shape
pHelperFeatureClass = AEHelper.GetFeatureClassFromShapefileOnDisk(shppath, shpname);
pHelperFeature = pHelperFeatureClass.CreateFeature();
pHelperFeature.Shape = (IGeometry)pTopologicalOperator;
pHelperFeature.Store();
pHelperFeature = pHelperFeatureClass.CreateFeature();
pHelperFeature.Shape = pFeature.Shape;
pHelperFeature.Store();
if (((IRelationalOperator)pTopologicalOperator).Contains(pFeature.Shape))
{
lblPrgInfo.Text = "共计"+ count.ToString()+"条数据,"+"删除第" + (i + 1).ToString() + "个feature";
pFeature.Delete();
}
(pHelperFeatureClass as ITable).DeleteSearchedRows(null); //删除所有的feature
}
}
this.prgBarAll.Value = ++i;
}
MessageBox.Show("删除完成!");
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return false;
}
}
}