[GIS] How to update geometry in ArcObjects

arcobjectseditinggeometry

I'm just starting with ArcObjects and I want to update coordinates of geometry vertices. Specifically, I want to swap X, Y coordinates for each vertex of polygons (which can be multipart and have inner rings).

I've found web help topic Updating geometry of existing features where ITransform2D interface is suggested. But it's methods does not seem solve my problem directly. I think I can use Move method, but it requires calculating dx and dy.

Is it possible to do coordinate replacement in ArcObjects like in Python vertex.X = vertex.Y but without recreating geometry? (this is quite tricky in Python for polygons with inner rings)

I adapted @vinayan code, but it does nothing. What do I miss?

protected override void OnClick()
{
    IMxDocument mxDocument = ((IMxDocument)(ArcMap.Application.Document)); // Explicit Cast   
    IContentsView currentContentsView = mxDocument.CurrentContentsView;
    IFeatureLayer featureLayer = (IFeatureLayer)currentContentsView.SelectedItem; // Explicit Cast
    IFeatureClass featureClass = featureLayer.FeatureClass;

    //Get the Feature or FeatureCursor as you like..
    IFeatureClass pFC = featureClass;
    IFeature polygonFeature = pFC.GetFeature(1);

    IPointCollection pPtsColl = (IPointCollection)polygonFeature.ShapeCopy;

    //swap X and Y
    double tempValue;
    IPoint pPoint;

    for (int i = 0; i < pPtsColl.PointCount; i++)
    {
        pPoint = pPtsColl.get_Point(i);
        tempValue = pPoint.X;
        pPoint.X = pPoint.Y;
        pPoint.Y = tempValue;
    }

    //Update Feature
    polygonFeature.Shape = (IGeometry)pPtsColl;
    polygonFeature.Store();
}

Best Answer

The below Snippet could help..

//Get the Feature or FeatureCursor as you like..
IMxDocument pMxdoc = (IMxDocument)m_application.Document;
IMap pMap = pMxdoc.FocusMap;

IFeatureLayer pFtrLyr = (IFeatureLayer)pMap.get_Layer(0);
IFeatureClass pFC = pFtrLyr.FeatureClass;

IFeature polygonFeature = pFC.GetFeature(yourobjectid);

IPointCollection pPtsColl = (IPointCollection)polygonFeature.ShapeCopy;

double tempValue;
IPoint pPoint;

IPointCollection updColl = new PolygonClass();

for (int i = 0; i < pPtsColl.PointCount; i++)
{
    pPoint = pPtsColl.get_Point(i);
    tempValue = pPoint.X;
    pPoint.X = pPoint.Y;
    pPoint.Y = tempValue;

    object missing = Type.Missing;

    updColl.AddPoint(pPoint, ref missing, ref missing);
}

polygonFeature.Shape = (IGeometry)updColl;
polygonFeature.Store();

The best solution would be to follow @WHuber's Comment to do an affine transformation..something which can be done using IAffineTransformation2D..So there will be no need to worry about Multipart polygons and polygons with hole..