# How to get the X,Y Coordinates vertices of selected polygon feature in C#

246
2
03-01-2021 06:01 AM
by
New Contributor

I need help with a sample code to get the X,Y Coordinates of the vertices of a selected polygon feature. I intend to pass this to text elements on the pagelayout control. Any help will be greatly appreciated.

Thank you

Worlanyo

1 Solution

Accepted Solutions
Occasional Contributor III

Here is a sample that gets you from an IFeature (from a cursor) to a List of all the points (type PointF)...

Hope it helps.

Brent Hoskisson

``````		private void GetVertices(IFeature f)
{
IPolygon4 parcel = f.Shape as IPolygon4;
if (parcel != null)
{
List<List<System.Drawing.PointF>> polycoll = new List<List<System.Drawing.PointF>>();
if (parcel.ExteriorRingCount > 0)
{
IGeometryBag exteriorRings = parcel.ExteriorRingBag;
IEnumGeometry exteriorRingsEnum = exteriorRings as IEnumGeometry;
exteriorRingsEnum.Reset();
int ering = 0;
IRing currentExteriorRing = exteriorRingsEnum.Next() as IRing;
int extrings = parcel.ExteriorRingCount;
while (extrings > 0 && currentExteriorRing != null)
{
ering++;
ISegmentCollection segments = currentExteriorRing as ISegmentCollection;

List<System.Drawing.PointF> spc = ProcessRing(segments);
if (spc != null)

IGeometryBag interiorRings = parcel.InteriorRingBag[currentExteriorRing];
IEnumGeometry interiorRingsEnum = exteriorRings as IEnumGeometry;
interiorRingsEnum.Reset();
int iring = 0;
IRing currentInteriorRing = exteriorRingsEnum.Next() as IRing;
int intrings = parcel.InteriorRingCount[currentExteriorRing];
while (intrings > 0 && currentInteriorRing != null)
{
iring++;
ISegmentCollection insegments = currentInteriorRing as ISegmentCollection;
List<System.Drawing.PointF> inspc = ProcessRing(insegments);
if (inspc != null)

currentInteriorRing = interiorRingsEnum.Next() as IRing;
if (iring >= intrings)
break;
}
currentExteriorRing = exteriorRingsEnum.Next() as IRing;
if (ering >= extrings)
break;
}
}
}
//do something with polycoll...
}

private List<System.Drawing.PointF> ProcessRing(ISegmentCollection segments)
{
int segcount = segments.SegmentCount;
IEnumSegment enumsegment = segments.EnumSegments as IEnumSegment;
if (enumsegment == null)
{
return null;
}
enumsegment.Reset();
int partIndex = 0;
int segIndex = 0;
ISegment querySeg;
enumsegment.Next(out querySeg, ref partIndex, ref segIndex);

List<System.Drawing.PointF> spc = new List<System.Drawing.PointF>();
while (querySeg != null && !querySeg.IsEmpty)
{
if (querySeg is Line)
{
}
else
{
//Circle, Ellipse, Bezier...
//get additional information to store about the line.
}
System.Drawing.PointF labpt = new System.Drawing.PointF((float)Math.Round(querySeg.FromPoint.X, 2, MidpointRounding.AwayFromZero),
(float)Math.Round(querySeg.FromPoint.Y, 2, MidpointRounding.AwayFromZero));
enumsegment.Next(out querySeg, ref partIndex, ref segIndex);
if (segIndex == 0)
break;
}
if (spc != null)
{
spc.Add(spc[0]);  //close the loop by adding the first point to the end...
return spc;
}
else
{
return null;
}
}
``````

2 Replies
Occasional Contributor III

Here is a sample that gets you from an IFeature (from a cursor) to a List of all the points (type PointF)...

Hope it helps.

Brent Hoskisson

``````		private void GetVertices(IFeature f)
{
IPolygon4 parcel = f.Shape as IPolygon4;
if (parcel != null)
{
List<List<System.Drawing.PointF>> polycoll = new List<List<System.Drawing.PointF>>();
if (parcel.ExteriorRingCount > 0)
{
IGeometryBag exteriorRings = parcel.ExteriorRingBag;
IEnumGeometry exteriorRingsEnum = exteriorRings as IEnumGeometry;
exteriorRingsEnum.Reset();
int ering = 0;
IRing currentExteriorRing = exteriorRingsEnum.Next() as IRing;
int extrings = parcel.ExteriorRingCount;
while (extrings > 0 && currentExteriorRing != null)
{
ering++;
ISegmentCollection segments = currentExteriorRing as ISegmentCollection;

List<System.Drawing.PointF> spc = ProcessRing(segments);
if (spc != null)

IGeometryBag interiorRings = parcel.InteriorRingBag[currentExteriorRing];
IEnumGeometry interiorRingsEnum = exteriorRings as IEnumGeometry;
interiorRingsEnum.Reset();
int iring = 0;
IRing currentInteriorRing = exteriorRingsEnum.Next() as IRing;
int intrings = parcel.InteriorRingCount[currentExteriorRing];
while (intrings > 0 && currentInteriorRing != null)
{
iring++;
ISegmentCollection insegments = currentInteriorRing as ISegmentCollection;
List<System.Drawing.PointF> inspc = ProcessRing(insegments);
if (inspc != null)

currentInteriorRing = interiorRingsEnum.Next() as IRing;
if (iring >= intrings)
break;
}
currentExteriorRing = exteriorRingsEnum.Next() as IRing;
if (ering >= extrings)
break;
}
}
}
//do something with polycoll...
}

private List<System.Drawing.PointF> ProcessRing(ISegmentCollection segments)
{
int segcount = segments.SegmentCount;
IEnumSegment enumsegment = segments.EnumSegments as IEnumSegment;
if (enumsegment == null)
{
return null;
}
enumsegment.Reset();
int partIndex = 0;
int segIndex = 0;
ISegment querySeg;
enumsegment.Next(out querySeg, ref partIndex, ref segIndex);

List<System.Drawing.PointF> spc = new List<System.Drawing.PointF>();
while (querySeg != null && !querySeg.IsEmpty)
{
if (querySeg is Line)
{
}
else
{
//Circle, Ellipse, Bezier...
//get additional information to store about the line.
}
System.Drawing.PointF labpt = new System.Drawing.PointF((float)Math.Round(querySeg.FromPoint.X, 2, MidpointRounding.AwayFromZero),
(float)Math.Round(querySeg.FromPoint.Y, 2, MidpointRounding.AwayFromZero));
enumsegment.Next(out querySeg, ref partIndex, ref segIndex);
if (segIndex == 0)
break;
}
if (spc != null)
{
spc.Add(spc[0]);  //close the loop by adding the first point to the end...
return spc;
}
else
{
return null;
}
}
``````

by
New Contributor

Thank you very much, this really helped