Select to view content in your preferred language

Generating a centroid for a polygon

4121
31
10-13-2011 03:15 AM
YousafHassan
Emerging Contributor
Hi
I am trying to help my colleague who wants to select a polygon and then click a button, which would generate a centroid for that polygon. Ideally, he wants a pop-up to appear which would display a copy&pasteable X and Y. He wants this automation as he wants X and Y to be generated for polygons using the same algorithm.

I was thinking of building a custom tool for him using a model. Could anyone please point me to the tools I need in order to achieve this? If you have already written a tool, would you like share it with me? I am quite comfortable in python and VBA but am not so familiar with Arc tools. I would actually prefer to do this in python as the support for VBA won't be there in future.

Any help would be extremely appreciated.
Thanks.
0 Kudos
31 Replies
YousafHassan
Emerging Contributor
Thanks a lot guys. The code is running without errors and is giving me the X and Y:

Option Explicit

Sub CreateCentroid()

Dim pDoc As IMxDocument
Dim pMap As IMap

Set pDoc = Application.Document
Set pMap = pDoc.FocusMap

Dim pFCursor As IFeatureCursor
Dim pSelectionSet As ISelectionSet
Dim pFSelection As IFeatureSelection
Set pFSelection = pMap.Layer(0)

Set pSelectionSet = pFSelection.SelectionSet
''set the pfeaturecursor
Set pFCursor = Nothing
''Get a cursor from the selected features
pSelectionSet.Search Nothing, False, pFCursor
Dim pPoly As IPolygon
Dim pArea As IArea
Dim pPoint As IPoint
Dim lat As Long
Dim lon As Long

Dim pFeat As IFeature
Set pFeat = pFCursor.NextFeature
Do Until pFeat Is Nothing
    Set pPoly = pFeat.ShapeCopy
    Set pArea = pPoly
    Set pPoint = pArea.Centroid
    lat = pPoint.X
    lon = pPoint.Y
    Set pFeat = pFCursor.NextFeature
Loop

MsgBox "The Eastings:" & lat & " The Northings:" & lon

End Sub


Is there a way of getting the lat and lon in a copy and pastable text field instead?

Also, am I getting eastings and northings or latitude and longitude here?

Thanks again for your generous help.
0 Kudos
JamesCrandall
MVP Alum


Also, am I getting eastings and northings or latitude and longitude here?

Thanks again for your generous help.


You are probably not going to like hearing this, but its not quite that simple.  You are getting the x/y of the point's spatial reference (I have no way of knowing what that is, it is your point layer).  To make sure you are getting the Lat/Lon, you will have to make sure the point has a geographic coordinate system assigned and in order to do this I think you will have to project it from something.

...In essence, you will need to know where you are coming from in order to get it to project to something.

Luckily the old forums are full of examples.  You might start here:

http://forums.esri.com/Thread.asp?c=93&f=993&t=227713&mc=1#msgid691091

Found with these search terms: IPoint.X
0 Kudos
AlexanderGray
Honored Contributor
You might also want to look at the IConversionNotation interface on the point.  Depends on the coordinate system you start out in.  James' link is a more generic way.
0 Kudos
YousafHassan
Emerging Contributor
Thanks for your help again. One final thing is that at the moment, some points are being created outside the selected polygons. Is there a way to keep them inside polygons?
0 Kudos
YousafHassan
Emerging Contributor
Hi
Still stuck at this final bit. I was reading about the IArea.Centroid Property and in the remarks at the end of the page, it says:

"The Centroid does not always occur inside the Area of the geometry.  The Centroid is not the same as the center of the geometry or the Envelope binding the geometry (but it may be if and only if that is also the center of the weighted area)."

Is there anyway I can keep the centroid inside the selected polygon?

Thanks
0 Kudos
AlexanderGray
Honored Contributor
the area labelpoint is guaranteed to be inside the polygon.  It may not be in the center (whatever that means to you) though.
0 Kudos
JamesCrandall
MVP Alum
I am not sure why you are seeing centroids outside of any selected polygons --- it really isn't the centroid of the polygon then, right?  Not sure what you have going on there.  I did some limited testing of the code below and it places graphic points at the centroid locations of each of the selected polygons, or at least what appears to be the centroid.  None are placed outside of any selected polygon.




            Dim pGraphics As IGraphicsContainer
            pGraphics = pMap
            Dim pMarkerElem As IMarkerElement
            pMarkerElem = New MarkerElement
            Dim pElement As IElement
            pElement = pMarkerElem


            Dim pFCursor As IFeatureCursor
            Dim pSelectionSet As ISelectionSet
            Dim pFSelection As IFeatureSelection
            pFSelection = pMap.Layer(0)

            pSelectionSet = pFSelection.SelectionSet
            ''set the pfeaturecursor 
            pFCursor = Nothing
            ''Get a cursor from the selected features
            pSelectionSet.Search(Nothing, False, pFCursor)

            Dim pPoly As IPolygon
            Dim pArea As IArea
            Dim pPoint As IPoint

            Dim pFeat As IFeature
            pFeat = pFCursor.NextFeature
            Do Until pFeat Is Nothing
                pPoly = pFeat.ShapeCopy
                pArea = pPoly
                pPoint = pArea.Centroid

                pElement = New MarkerElement
                pElement.Geometry = pPoint
                pGraphics.AddElement(pElement, 0)

                pFeat = pFCursor.NextFeature
            Loop

            pDoc.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, Nothing, Nothing)
            pDoc.ActiveView.Refresh()
0 Kudos
YousafHassan
Emerging Contributor
Well, your do loop contains a few lines which are different from the code I have in VBA:

Option Explicit

Sub CreateCentroid()
''provides access to members that control the MXD
Dim pDoc As IMxDocument
''Provides access to members that control the map.
Dim pMap As IMap

''Set the MXD to this mxd file
Set pDoc = Application.Document
''Set the focus to the current map
Set pMap = pDoc.FocusMap

''Provides access to members that hand out enumerated features,
''field collections and allows for the updating, deleting and inserting of features.
Dim pFCursor As IFeatureCursor
''Provides access to members that manage a set of selected table rows or features.
Dim pSelectionSet As ISelectionSet2
''Provides access to members that control feature selection.
Dim pFSelection As IFeatureSelection
''Set the first layer as the active layer
Set pFSelection = pMap.Layer(0)
''Set the selected features
Set pSelectionSet = pFSelection.SelectionSet
''set the pfeaturecursor
Set pFCursor = Nothing
''Get a cursor from the selected features
pSelectionSet.Search Nothing, False, pFCursor
''Provides access to members that identify a polygon and permit controlled access to its inner and outer rings.
Dim pPoly As IPolygon4
''Provides access to members that return properties common to rings and polygons.
Dim pArea As IArea
''Provides access to members that define two dimensional points.
Dim pPoint As IPoint
''Variable to hold the X coordinate
Dim lat As Long
''Variable to hold the Y coordinate
Dim lon As Long
''Provides access to members that return and set properties of a feature.
Dim pFeat As IFeature
Set pFeat = pFCursor.NextFeature
Do Until pFeat Is Nothing
    Set pPoly = pFeat.ShapeCopy
    Set pArea = pPoly
    Set pPoint = pArea.Centroid
    lat = pPoint.X
    lon = pPoint.Y
    Set pFeat = pFCursor.NextFeature
Loop

MsgBox "The Eastings:" & lat & " The Northings:" & lon

End Sub


Could this be the reason?

See the attached screenshot for an example of a polygon that I am trying to work with. With square or rectangle polygons, it is fine but with any other strange shape it is not appearing inside the polygon.
0 Kudos
AlexanderGray
Honored Contributor
The reason they are called centroids and not centre is that there are many definitions of centre.  Centre of the polygon's envelope, centre of gravity, etc.  With regular shapes such as your example, all the centroid definitions are pretty much the same and inside the polygon.  There is a whole debate in geography about what constitutes the centre.  I think geographers are still debating about what constitutes the centre of Scotland, many definitions place it in the sea.

Please refer to esri documentation on centroid for examples on how it can end up outside of the polygon
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#/Centroid_Property/002m00...
0 Kudos
YousafHassan
Emerging Contributor
Sorry, forgot to show you where the point is appearing. See attached.
0 Kudos