bhavanisankarnimmala

Get Corner Points of Polygon using Arcobjects

Blog Post created by bhavanisankarnimmala on May 14, 2018

// Getting the corner points of Polygon is write as follows.

// Raw Lines of Code

IEnvelope polygonExtent = unitFeat.ShapeCopy.Envelope;

IPoint lowerLeft_point = polygonExtent.LowerLeft;

IPoint lowerRight_point = polygonExtent.LowerRight;

IPoint upperLeft_point = polygonExtent.UpperLeft;

IPoint upperRight_point = polygonExtent.UpperRight;

IPoint sw_Point = GetCornerPoint(unitFeat, lowerLeft_point);

IPoint se_Point = GetCornerPoint(unitFeat, lowerRight_point);

IPoint ne_Point = GetCornerPoint(unitFeat, upperRight_point);

IPoint nw_Point = GetCornerPoint(unitFeat, upperLeft_point);

 

//Get Corner Point

private static IPoint GetCornerPoint(IFeature feat, IPoint lowerLeft_point)

{

double min = 0;

IPoint nearestPT = null;

try

{

IPolygon polygon = (feat.ShapeCopy as IGeometry) as IPolygon;

IPointCollection ptcoll = polygon as IPointCollection;

for (int i = 0; i < ptcoll.PointCount; i++)

{

IPoint pt = ptcoll.get_Point(i);

double distance = GetDistanceOfCoordinates(lowerLeft_point, pt);

if (i == 0)

{

min = distance;

nearestPT = pt;

}

else if (min > distance)

{

min = distance;

nearestPT = pt;

}

}

}

catch (Exception ex)

{

Globals.logging(ex.Message, false, "FATAL");

Globals.logging(ex.StackTrace, false, "FATAL");

Globals.logging("ERROR in " + System.Reflection.MethodBase.GetCurrentMethod().Name + " Method.", false, "FATAL");

nearestPT = null;

}

finally

{

GC.Collect();

}

return nearestPT;

}

// Get Distance of Coordinates

 

public static double GetDistanceOfCoordinates(IPoint p1, IPoint p2)

{

double distnace = 0;

try

{

distnace = Math.Sqrt(((p2.X - p1.X) * (p2.X - p1.X) + ((p2.Y - p1.Y) * (p2.Y - p1.Y))));

}

catch (Exception ex)

{

Globals.logging(ex.Message, false, "FATAL");

Globals.logging(ex.StackTrace, false, "FATAL");

Globals.logging("ERROR in " + System.Reflection.MethodBase.GetCurrentMethod().Name + " Method.", false, "FATAL");

distnace = 0;

}

finally

{

GC.Collect();

}

return distnace;

}

 

Outcomes