I would like to write some c# code to delete duplicate rows from a shapefile stored in a folder.
Can anyone provide me with an example or pointers on the best way to attempt this, please?
I would like to write some c# code to delete duplicate rows from a shapefile stored in a folder.
Can anyone provide me with an example or pointers on the best way to attempt this, please?
To remove duplicate geometry with different fid you may use an algorithm like:
List<IFeature> features = GetAllFeatures(featureClass)
foreach(var feature in features)
{
string query = "OIDFieldName = feature.OID";
List<IFeature> queryFeatures = GetFeaturesBasedOnQuery(featureClass, query);
foreach(var queryFeature in queryFeatures)
{
List<IFeature> sameLocationFeatures = GetFeaturesBySameLocation(featureClass, queryFeature.Shape);
for (int i = 1; i < sameLocationFeatures.Count; i++)
{
sameLocationFeatures[i].Delete();
}
}
}
To get features based on location you may use following code snippent:
public IFeatureCursor GetFeaturesBySameLocation(IFeatureClass featureClass, IGeometry queryGeometry)
{
string shapeFieldName = featureClass.ShapeFieldName;
ISpatialFilter spatialFilter = new SpatialFilter();
spatialFilter.Geometry = queryGeometry;
spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
spatialFilter.set_OutputSpatialReference(shapeFieldName, spatialReferance);
IFeatureCursor featureCursor = featureClass.Search(spatialFilter, false);
return featureCursor ;
}
Code snippet for getting all features of a layer:
public IFeatureCursor GetAllFeatureOfLayer(IFeatureClass featureClass)
{
IFeatureCursor featureCursor = featureClass.Search(null, false);
return featureCursor;
}
Code snippet for getting features based on query from a layer:
public IFeatureCursor GetFeaturesBasedOnQuery(IFeatureClass featureClass, string query)
{
IQueryFilter queryFilter = new QueryFilterClass();
queryFilter.WhereClause = query;
IFeatureCursor featureCursor = featureClass.Search(queryFilter, false);
return featureCursor;
}
I have a python code to delete duplicate geometry..
code snippet:
gp.SelectLayerByAttribute_management(featureverticeshp_Layer, "NEW_SELECTION", gbgquery)
gp.SelectLayerByLocation_management(featureverticeshp_Layer, "ARE_IDENTICAL_TO", featureverticeshp_Layer, "", "NEW_SELECTION")
gp.SelectLayerByAttribute_management(featureverticeshp_Layer, "REMOVE_FROM_SELECTION", gbgquery)
gp.DeleteFeatures_management(featureverticeshp_Layer)
If you want I can provide the entire code (author name: Gerry Gabrisch). (I forgot from where I download it, but works fine with shape file).
Now just convert this shape file tp featureclass and open this using Access and find out those duplicate records and delete those..