Find polygon centroid in C# or vb.net

7020
2
09-07-2012 10:36 AM
JoseSanchez
Occasional Contributor III
Hi all,

I am looking for asamples that shwo how to find a polygon centroid in C# or vb.net

thanks
0 Kudos
2 Replies
RichardFairhurst
MVP Honored Contributor
Hi all,

I am looking for asamples that shwo how to find a polygon centroid in C# or vb.net

thanks


You need to access the Centroid through the IArea interface.  There is an example that extracts the Centroid's X and Y coordinates and that creates a Centroid point object from the shape input's area interface here.
0 Kudos
KevinOrcutt
New Contributor
This might be overkill, but here is a quick snapshot with slight modifications of some code that I use to get a centroid of a polygon feature...

    Private Function GetPolygonFeatureCentroid(ByVal PolygonFeatureLayer As IFeatureLayer) as IPoint
        Dim PolygonFeatureClass As IFeatureClass
        PolygonFeatureClass = PolygonFeatureLayer.FeatureClass

        Dim PolygonFeatureSelection As IFeatureSelection = PolygonFeatureLayer
        Dim PolygonFeatureSelectionSet As ISelectionSet = PolygonFeatureSelection.SelectionSet

        Dim PolygonFeatureCursor As IFeatureCursor

        Dim newPolygonFeature As IFeature
        Dim newPolygonArea As IArea
        Dim newPolygonFeatureCentroid As IPoint

        Dim queryFilter As IQueryFilter = New QueryFilterClass()
        'ToDo: Setup the where clause here to select the feature that you want...  or skip altogether if you already have that feature! 

        PolygonFeatureFSelectionSet = PolygonFeatureClass.Select(queryFilter, esriSelectionType.esriSelectionTypeSnapshot, esriSelectionOption.esriSelectionOptionNormal, Nothing)
        PolygonFeatureCursor = PolygonFeatureClass.Search(queryFilter, False)

        If PolygonFeatureFSelectionSet.Count > 0 Then
            newPolygonFeature = PolygonFeatureFCursor.NextFeature
            newPolygonArea = newPolygonFeature.ShapeCopy
            newPolygonFeatureCentroid = TryCast(newPolygonArea.Centroid, IPoint)
        End If

        Return newPolygonFeatureCentroid

    End Function


I left out the specific where clause for the query filter used, I figured that you would have that already, or already have the particular feature that you want the centroid of...
Hope this helps.


Kevin Orcutt
GIS Developer/Consultant
City of Cincinnati - Cincinnati Area GIS (CAGIS)
(513) 850-1335 (cell)
Kevin.Orcutt@cincinnati-oh.gov
www.cagis.org
0 Kudos