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());
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Aug 19, 2017 at 4:45
1
  • 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? Commented Aug 19, 2017 at 15:40

1 Answer 1

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.

answered Aug 20, 2017 at 21:37
4
  • 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. Commented Aug 21, 2017 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. Commented Aug 21, 2017 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. Commented Aug 21, 2017 at 21:00
  • Yes, that's right. I used non-recycling cursor and the code worked great. Thank you very much for your guidance. Commented Aug 22, 2017 at 5:33

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.