get the points of a polygon

3037
3
05-12-2010 09:42 AM
BillLanza
New Contributor
I have a silverlight app where a user can draw a polygon on a map. I want a way to take the polygon and get the vertices. I can see how to get the envelope, but  not the actual points. In arcobjects you can convert an Ipolygon to an IpointCollection, but that does not seem possible in the SL API. Any direction would be appreciated.
0 Kudos
3 Replies
DominiqueBroux
Esri Frequent Contributor
Get the rings of the polygon, each ring is a collection of point.

For example, to get the points of the first ring of your polygon, you can use :
ESRI.ArcGIS.Client.Geometry.PointCollection points = myPolygon.Rings[0];


/Dominique
0 Kudos
AvronPolakow
New Contributor III
// This is how I do it:

// Here's how I do it creating an XML type output (you can create whatever output you like)

////////////////////////////////////////////////////////////////////////////
//             ldblTolerance  ; Tolerance for the removal of proximal points
//             lbXML          ; XML output type:
//                                 true=short xml with attributes
//                                 false=long xml with tags
//             lbBrief         ; Points in single tag
////////////////////////////////////////////////////////////////////////////

StringBuilder lstrBuilder = new StringBuilder();
string        [] LPad        = ", ,  ,   ,    ,     ,      ,       ,        ,         ,          ".Split(',');

double ldblTolerance    = 0.000010; // For removal of proximal points. Usage: GetPoints()
bool    lbXML               = true;
bool    lbBrief               = true;

lstrBuilder.Append("");

if (lGraphic.Geometry is ESRI.ArcGIS.Client.Geometry.Polygon) 
{
ESRI.ArcGIS.Client.Geometry.Polygon lPolygon = (ESRI.ArcGIS.Client.Geometry.Polygon) lGraphic.Geometry;
foreach (ESRI.ArcGIS.Client.Geometry.PointCollection lPoints in lPolygon.Rings)
{
  lstrBuilder.Append(LPad[6] + "<ring>\n");
  lstrBuilder = GetPoints(lstrBuilder, lbXML, lbBrief, ldblTolerance, LPad, lPoints);
  lstrBuilder.Append(LPad[6] + "</ring>\n");
}

:
:
:

   return lstringBuilder.ToString();

//////////////////////////////////////////////////////////////////////
public static StringBuilder GetPoints(StringBuilder astrBuilder, bool abXML, bool abBrief, double adblTolerance, string[] LPad, ESRI.ArcGIS.Client.Geometry.PointCollection aPoints)
{
MapPoint lprevMapPoint = new MapPoint();
double   ldblX, ldblY;

lprevMapPoint.X = 0;
lprevMapPoint.Y = 0;

if (abBrief)
{
  astrBuilder.Append(LPad[7] + "<points>");
  if (abXML && !abBrief) {astrBuilder.Append("\n");}
}

foreach(MapPoint lMapPoint in aPoints)
{           
  ldblX = Math.Round(lMapPoint.X, 6);     // Math.Round(lMapPoint.X, 6);
  ldblY = Math.Round(lMapPoint.Y, 6);     // Math.Round(lMapPoint.Y, 6);

  if (Math.Abs(ldblX - lprevMapPoint.X) > adblTolerance && Math.Abs(ldblY - lprevMapPoint.Y) > adblTolerance)
  {
   lprevMapPoint.X = ldblX;
   lprevMapPoint.Y = ldblY;

   // Brief
   if (abBrief)
   {
    if (abXML) {astrBuilder.Append(ldblX.ToString()  + "," + ldblY.ToString() +",");}
    else      
    {
     astrBuilder.Append(ldblX.ToString() + ",");
     astrBuilder.Append(ldblY.ToString() + "\n");
    }
   }
   else
   {
    if (abXML) {astrBuilder.Append(LPad[7] + "<point x=\"" + ldblX.ToString() + "\" y=\"" + ldblY.ToString() + "\" />\n");}
    else      
    {
     astrBuilder.Append(LPad[7] + "<point>\n");
     astrBuilder.Append(LPad[8] + "<x>" + ldblX.ToString() + "</x>\n");
     astrBuilder.Append(LPad[8] + "<y>" + ldblY.ToString() + "</y>\n");
     astrBuilder.Append(LPad[7] + "</point>\n");
    }
   }
  }
}
if (abBrief)
{
  if (abXML && !abBrief) {astrBuilder.Append("\n" + LPad[7]);}
  astrBuilder.Append("</points>\n");          
}

return astrBuilder;
}
0 Kudos
BrianBehling
New Contributor III
                    ESRI.ArcGIS.Client.Geometry.Polygon polygon =   (ESRI.ArcGIS.Client.Geometry.Polygon)graphic.Geometry;

                    int ringCount = polygon.Rings.Count;

                    for (int i = 0; i < ringCount; i++)
                    {
                        ESRI.ArcGIS.Client.Geometry.PointCollection pc = polygon.Rings;

                        foreach (ESRI.ArcGIS.Client.Geometry.MapPoint mp in pc)
                        {
                            MapPoint coordinates = new MapPoint(mp.X, mp.Y);

                            
                        }
}

0 Kudos