Get Center of Polygon Graphic

6570
10
05-09-2011 08:14 AM
deleted-user-ATjHIWsdQYmT
Occasional Contributor III
Stupid question (I think..)

I have a Graphic that is a polygon.  How do I find it's center?  It's easy enough to do with the Geometry's extent using the ".GetCenter" method, but there doesn't seem to be one that exists for polygons. 

What I want to do is find the mapPoint for the center of a specific graphic, which happens to be a polygon.
0 Kudos
10 Replies
nakulmanocha
Esri Regular Contributor
Since Polygon is derived from Geometry class. You can always do Polygon.Extent.GetCenter().
0 Kudos
deleted-user-ATjHIWsdQYmT
Occasional Contributor III
Right, but that is not the center(Centroid) of the polygon itself. Instead it is the center of the polygon's extent, or Envelope.
0 Kudos
nakulmanocha
Esri Regular Contributor
If you want the true centroid of the polygon. I would try using a GP service. I haven't tried but I believe Feature To Point GP tool could do the job.
0 Kudos
dotMorten_esri
Esri Notable Contributor
The GeometryService is probably a simpler option than GP. Here's an example that does just this:
http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#LabelPoints
0 Kudos
LanceCrumbliss
Occasional Contributor II
there are probably half a dozen reasons not to do it this way that Morten and Co. can point out, but doing it client-side using the extension method below works for me.  this gets the centroid based on simple 2D euclidean coordinates.  nothing fancy going on at all...

    <Extension()> _
    Public Function GetTruePolygonCenter(ByVal pgnPolygon As Polygon) As MapPoint
        Try
            Dim iNumberOfMapPoints As Integer = 0
            Dim centroidX As Double = 0
            Dim centroidY As Double = 0
            For Each pc As PointCollection In pgnPolygon.Rings
                For Each mp As MapPoint In pc
                    centroidX += mp.X
                    centroidY += mp.Y
                    iNumberOfMapPoints += 1
                Next
            Next
            Return New MapPoint(centroidX / iNumberOfMapPoints, centroidY / iNumberOfMapPoints, pgnPolygon.SpatialReference)
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
0 Kudos
DominiqueBroux
Esri Frequent Contributor

there are probably half a dozen reasons not to do it this way that Morten and Co. can point out, but doing it client-side using the extension method below works for me.

It's depending on what you call 'centroid' and what you are trying to do with it.

If the goal is to place a label on a polygon, the geometry service is the best option since it guarantees that the returned point will be inside the polygon. Nevertheless there is a performance drawback that can be avoided by writing your own code at the client side.
0 Kudos
LanceCrumbliss
Occasional Contributor II
indeed I do use this to calculate a label position at the center and inside of the polygon. it has yet to fail at that! 

even after using it thousands of times, I suppose I should still stress "yet" though.
0 Kudos
dotMorten_esri
Esri Notable Contributor
As far as I can tell, the aboce code calculates the weighted center. There is no guarantee that this is inside the polygon. For instance try it on a polygon shaped like a "C", or a ring with a hole in the center.
0 Kudos
LanceCrumbliss
Occasional Contributor II
As far as I can tell, the aboce code calculates the weighted center. There is no guarantee that this is inside the polygon. For instance try it on a polygon shaped like a "C", or a ring with a hole in the center.


true.  i'm sure there's a way to code that, too.  i suppose it works better than the GetCenter method though since the latter gets the polygon's extent envelope's centoid.
0 Kudos