1

I want to reduce the size of my selected polygons. In other words, I want to implement the "scale" program which is in the editor section of ArcGIS. For that purpose I have searched a lot and just found the example for point features as following. But my problem is that when I copy this code to visual studio, it doesn't work and also I don't know how to use it for polygons.

        IPoint startingPoint = new PointClass() as IPoint;
        startingPoint.PutCoords(10, 10);
        //Create a point to act as origin
        IPoint origin = new PointClass() as IPoint;
        origin.PutCoords(15, 15);
        //Cast the startingPoint into the correct Interface
        ITransform2D transformScalePoint = startingPoint as ITransform2D;
        //Perform Scale 
        transformScalePoint.Scale(origin, .5, .5);
        double x;
        double y;
        startingPoint.QueryCoords(out x, out y);
        Debug.Print(x.ToString());
        Debug.Print(y.ToString());
  • Please edit the question to provide a great more detail than "it doesn't work". What does it do instead? Any error messages? What diagnostic output did you add? Did you consider just writing the transform code yourself? – Vince Aug 19 '17 at 15:40
0

I can see where you got that code from, it is a bit confusing; I don't think the use of a point geometry to scale is particularly helpful in this case and gets confused with the scale centre point.

To modify the code to scale polygons there's only a few changes that need be made. I am hoping you are familiar with Update Cursors in C#.

To scale you must know the point you need to scale about (look at the pictures on the page you got the code from), I am assuming you want to scale in place around the centroid of each features' polygon and that you have ensured that only polygon geometries can get to this section of code.

// assuming you've got an update cursor pFCur (non recycling)
// which has returned feature pFeat on NextFeature

IGeometry pFeatureGeom = pFeat.ShapeCopy;
// Create a point to act as origin at the
// centre of *this* polygon or collection 
// of polygons using IArea interface
IArea pFeatureArea = (IArea)pFeatureGeom;
IPoint origin = pFeatureArea.Centroid; 
//Cast the feature geometry into the correct Interface
ITransform2D transformScalePoint = pFeatureGeom as ITransform2D;
//Perform Scale 
transformScalePoint.Scale(origin, .5, .5);
// now the object stored in pFeatureGeom is reduced to 
// 50% of size scaled around the centre of the feature
pFeat.Shape = pFeatureGeom; // update the features' geometry
pFCur.UpdateFeature(pFeat); // and update the feature

To scale around the centre of each feature I use the Centroid property of IArea, I know it's a bit counter-intuitive to cast to an IArea to get a centroid but it works..

As ITransform2D is implemented by IPolygon you can cast directly from your features' geometry, which should be a polygon, to the transform 2d interface then scale, and I assume update the features' geometry and store the feature with the updated geometry if that is what you wish to do with the scaled geometry.

  • Thank you very much for your help. I wrote the above code but I received this error when it tries to update the data: "Exception from HRESULT: 0x80041051". I don't know how to solve it. – parastoo Aug 21 '17 at 7:05
  • 1
    I solved this problem by changing the search cursor to update cursor. At first I didn't pay attention to your first statement about update cursor. Thank you again for you help. The code worked great. – parastoo Aug 21 '17 at 8:53
  • Ah yes, even though the method exists you can't update with a search cursor, nor insert; No type of cursor will let you update and insert. Be sure you use a non-recycling cursor for update cursor or it will lead to different errors. – Michael Stimson Aug 21 '17 at 21:00
  • Yes, that's right. I used non-recycling cursor and the code worked great. Thank you very much for your guidance. – parastoo Aug 22 '17 at 5:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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