5

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

2 Answers 2

3

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

5
  • 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
    Commented Jun 21, 2012 at 11:15
  • @Marcin -fixed now..didn't check using IDE ;-)
    – vinayan
    Commented Jun 21, 2012 at 11:22
  • I've posted adapted code as Edit - it compiles but does nothing.
    – Marcin
    Commented Jun 21, 2012 at 11:45
  • @Marcin - should work now...just modified
    – vinayan
    Commented Jun 21, 2012 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
    Commented Jun 22, 2012 at 6:43
1

Yes. Look at IPointCollection and the IPoint interface.

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.