2

I would like to compare the geometry between two feature, Already I selected the two feature. How do I start the comparison?

depend on the image below I need to write code to compare this two features and get the difference (blue colour is different) and save the difference in a new feature class,

ITopologicalOperator2 pTopOpOld = oldFeature.ShapeCopy as ITopologicalOperator2;
ITopologicalOperator2 pTopOpNew = newFeature.ShapeCopy as ITopologicalOperator2;

IGeometry differenceGeometry = pTopOpOld.Difference(newFeature.Shape);

pTopOpOld = differenceGeometry as ITopologicalOperator2;
pTopOpOld.IsKnownSimple_2 = false;
pTopOpOld.Simplify();
newFeature.Shape = pTopOpOld as IGeometry;
newFeature.Store();

enter image description here

4
  • Welcome to GIS SE! As a new user be sure to take the Tour. For questions that involve code we ask that you show us where you are stuck with your own code by including a code snippet in your question. There is an edit button beneath your question which will enable you to do that and a {} button that enables you to format any highlighted code nicely.
    – PolyGeo
    Commented Feb 19, 2018 at 7:54
  • The code you have added does not look like an attempt to solve the problem, and is missing too much to be of use as the basis for an answer. I've seen lots of ArcObjects questions here of late, but no answers, which makes me think this question would be more usefully asked in GeoNet (once it's structured as a function with enough declarations to make sense)
    – Vince
    Commented Feb 19, 2018 at 12:01
  • Does the code you posted work or does it error? If it errors whats the error message?
    – Dowlers
    Commented Feb 20, 2018 at 16:38
  • It was working but with logical error, I added the correct code below please see it, If you have any comment tell me, Thanks :)
    – Moe
    Commented Feb 25, 2018 at 7:25

2 Answers 2

0

You are using difference but what you really want is Symmetric Difference (the bits of both input features that aren't common). Look at the pictures in both links, they will help you understand what each method does.

ITopologicalOperator2 pTopOpOld = oldFeature.ShapeCopy as ITopologicalOperator2;
// ITopologicalOperator2 pTopOpNew = newFeature.ShapeCopy as ITopologicalOperator2; // Not used.

IGeometry differenceGeometry = pTopOpOld.Difference(newFeature.ShapeCopy );// always use shapecopy or bad things can happen

pTopOpOld = differenceGeometry as ITopologicalOperator2;
pTopOpOld.IsKnownSimple_2 = false;
pTopOpOld.Simplify();
newFeature.Shape = pTopOpOld as IGeometry;
newFeature.Store();

This of course has some assumptions:

  1. Both geometries are polygon.
  2. newFeature is a new IFeature created with its IFeatureClass.CreateFeature and not an IFeatureBuffer created by IFeatureClass.CreateFeatureBuffer, which needs to be inserted on an insert cursor, not stored, if you try to store an IFeatureBuffer object you will get a method not found error.
1
  • I solved my issue in below code,If you have any comment please advise
    – Moe
    Commented Feb 25, 2018 at 7:19
0
        IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesGDB.FileGDBWorkspaceFactory();
        IFeatureWorkspace featureWorkspace1 = (IFeatureWorkspace)workspaceFactory.OpenFromFile(shapeFileName, 0);
        IFeatureClass featureClass1 = featureWorkspace1.OpenFeatureClass("geometryChanges");

        IFeatureBuffer feature = featureClass1.CreateFeatureBuffer();
        #region Catch New and compare with old

        ITopologicalOperator2 pTopOpNew = newFeature.ShapeCopy as ITopologicalOperator2;
        IGeometry differenceGeometry = pTopOpNew.Difference(oldFeature.Shape);

        feature.Shape = differenceGeometry;

        UpdateFieldValuesToChanges(featureClass1, feature, uniqueId, "GAINED");
        #endregion

        #region Catch Old and compare with new

        IFeatureBuffer featureOld = featureClass1.CreateFeatureBuffer();
        ITopologicalOperator pTopOpOld = oldFeature.ShapeCopy as ITopologicalOperator;
        IGeometry differenceGeometryO = pTopOpOld.Difference(newFeature.Shape);
        featureOld.Shape = differenceGeometryO;
        UpdateFieldValuesToChanges(featureClass1, featureOld, uniqueId, "REDUCED");
        
        #endregion

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.