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

Any code snippet is highly welcome:)


EDIT: 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();
        }
share|improve this question
    
I assume you have tried to debug. Does it reach the for loop? Does it move the expected number of times though the loop? –  mhoran_psprep Jun 21 '12 at 11:55
    
I was not using an updated pointcollection earlier to update the geometry..fixed now.. –  vinayan Jun 21 '12 at 12:33
2  
Swapping the coordinates is an affine transformation. Use a=c=e=f=0 and b=d=1. –  whuber Jun 21 '12 at 15:02
    
@whuber - so simple!! never even thought of it.. –  vinayan Jun 22 '12 at 6:52
    
@vinayan, @whuber - but which IAffineTransformation2D method should I use? I haven't found one with such set of parameters. –  Marcin Jun 22 '12 at 8:15

2 Answers 2

up vote 2 down vote accepted

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();

Edit:

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

share|improve this answer
    
Thanks for this code. Unfortunately I'm stuck in foreach loop. I get error: foreach statement cannot operate on variables of type 'ESRI.ArcGIS.Geometry.IPointCollection'. Do you know how to fix it? –  Marcin Jun 21 '12 at 11:15
    
@Marcin -fixed now..didn't check using IDE ;-) –  vinayan Jun 21 '12 at 11:22
    
I've posted adapted code as Edit - it compiles but does nothing. –  Marcin Jun 21 '12 at 11:45
    
@Marcin - should work now...just modified –  vinayan Jun 21 '12 at 12:28
    
Thanks again. Now it works for simple polygons (multipart and with void polygons are not recreated correctly) and is good starting point for futher development. –  Marcin Jun 22 '12 at 6:43

Yes. Look at IPointCollection and the IPoint interface.

share|improve this answer

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.