2

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?

7
  • 4
    how do you define duplicate?
    – Ian Turton
    Commented Dec 7, 2012 at 8:24
  • 3
    duplicate geometry or duplicate attribute?
    – vinayan
    Commented Dec 7, 2012 at 8:35
  • Actually i used clip method from IBasiGeoProcessor interface to clip a shapefile with a given boundary. The output shapefile shows some duplicate rows(i.e., two fid denoting same feature).
    – Navish
    Commented Dec 7, 2012 at 8:47
  • 1
    Perhaps you can provide some screenshots of the geometry being affected. Most likely they are legitimate features that were split during the clip operation.
    – blah238
    Commented Dec 7, 2012 at 10:54
  • @Navish, a simple google search (e.g. arcobjects find dupoicate featuers) should give you a number of code snippets of how to do this.
    – artwork21
    Commented Dec 7, 2012 at 12:21

3 Answers 3

2

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;
    }
2
  • This algorithm will delete (all-1) features in a same location.
    – Emi
    Commented Jan 7, 2013 at 4:49
  • @Navish: Doesn't this what you are looking for?
    – Emi
    Commented Jan 8, 2013 at 7:12
1

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).

-5

Now just convert this shape file tp featureclass and open this using Access and find out those duplicate records and delete those..

2
  • It sounds like a programmatic solution is being asked for. Besides, messing with a personal geodatabase feature class's attribute table in Access is likely to corrupt it.
    – blah238
    Commented Dec 7, 2012 at 10:51
  • Actually same feature is being denoted by two or in some cases more than two fid.
    – Navish
    Commented Dec 7, 2012 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.