Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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?

share|improve this question
4  
What have you tried so far? –  R.K. Dec 7 '12 at 8:23
4  
how do you define duplicate? –  iant Dec 7 '12 at 8:24
3  
duplicate geometry or duplicate attribute? –  vinayan Dec 7 '12 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 Dec 7 '12 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 Dec 7 '12 at 10:54
show 3 more comments

3 Answers

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

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

share|improve this answer
add comment

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

share|improve this answer
    
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 Dec 7 '12 at 10:51
    
Actually same feature is being denoted by two or in some cases more than two fid. –  Navish Dec 7 '12 at 11:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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