Generating a centroid for a polygon

2380
31
10-13-2011 03:15 AM
YousafHassan
New Contributor II
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
JamesCrandall
MVP Frequent Contributor
Not sure if there is a forum specific to Python, but if you are going to write something similar to VBA, you'd probably want either create an Add-In or a COM implementation (I assume this is for extending ArcMap?)

In any event, here is some code that would get the centroid (and lat/lon values of that centroid) of a selected polygon feature for the first layer in the TOC. 

pDoc = m_pApp.Document
pMap = pDoc.FocusMap

''get the selection from the layer
Dim pFCursor As IFeatureCursor
Dim pSelectionSet As ISelectionSet
Dim pFSelection As IFeatureSelection
pFSelection = pMap.Layer(0)

Dim pPoly As IPolygon
Dim pArea As IArea
Dim pPoint As IPoint
Dim lat As Decimal 'not sure exactly which number format you'd want to use
Dim lon As Decimal

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


this may be a kind of round-about way to get at things, but it does seem to work.
0 Kudos
YousafHassan
New Contributor II
Thanks for this. But what do you mean by creating an Add In or COM implementation? Actually, don't answer that. I've found the link and am reading about them now.
0 Kudos
YousafHassan
New Contributor II
James, I am trying to create an Add-In following this link:

http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#//0001000001ms000000

But I cannot find ArcGIS node under project type. What do I need to do in order to get that in Visual Studio?

I apologise for these questions as I am a bit of a beginner in Arc.

I will be adding this to ArcMap and hopefully, once it is up and running, my colleagues will be using it too.
Thanks.
0 Kudos
JamesCrandall
MVP Frequent Contributor
James, I am trying to create an Add-In following this link:

http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#//0001000001ms000000

But I cannot find ArcGIS node under project type. What do I need to do in order to get that in Visual Studio?
I apologise for these questions as I am a bit of a beginner in Arc.

I will be adding this to ArcMap and hopefully, once it is up and running, my colleagues will be using it too.
Thanks.



I *think* this is because you do not have the ArcGIS .NET SDK ("Developer Kit") installed.  Can someone else confirm this?
0 Kudos
YousafHassan
New Contributor II
I *think* this is because you do not have the ArcGIS .NET SDK ("Developer Kit") installed.  Can someone else confirm this?


I think I need ArcGIS Visual Studio Integration Framework!

Is there any other way I can acheive this? i.e. build a custom tool based on a model?
0 Kudos
JamesCrandall
MVP Frequent Contributor
I think I need ArcGIS Visual Studio Integration Framework!

Is there any other way I can acheive this? i.e. build a custom tool based on a model?


Just fyi, from: http://edndoc.esri.com/arcobjects/9.2/NET/73516929-d390-456c-84bc-c8f8a58b645a.htm

"...The ArcGIS Visual Studio IDE Integration Framework is installed as part of the Software Developer Kit (SDK) for .NET for each ArcGIS product and is an optional feature during installation. This framework is available for Visual Studio 2005, Visual Basic 2005 Express, Visual C# 2005 Express, and Visual Web Developer 2005 Express editions."

So, I am not certain if the VS IDE Framework is what you need.  I do believe that the reason why no ArcGIS Project templates show up is because you simply do not have the ArcGIS .NET SDK installed.
0 Kudos
YousafHassan
New Contributor II

So, I am not certain if the VS IDE Framework is what you need.  I do believe that the reason why no ArcGIS Project templates show up is because you simply do not have the ArcGIS .NET SDK installed.


Yes, you are right. I have to get it installed in order to try your code.
0 Kudos
YousafHassan
New Contributor II
Hi

I have made a slight amendment to your code:

Sub CreateCentroid()

pDoc = m_pApp.Document
pMap = pDoc.FocusMap

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

Dim pPoly As IPolygon
Dim pArea As IArea
Dim pPoint As IPoint
Dim lat As Long
Dim lon As Long

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

End Sub


And I am trying to run this code as a VBA macro but it is failing at this line:

pFSelection = pMap.Layer(0)


Compile Error: Method or data member not found.

Any ideas as to why is failing here?
Thanks
0 Kudos
JamesCrandall
MVP Frequent Contributor
First, are you declaring pDoc and pMap somewhere else?

Dim pDoc As IMxDocument
Dim pMap As IMap


If not, you will need to put these at the top of your CreateCentroid Sub or make them Private variables in the entire class.

It has been a long time since working with VBA, but I don't think this will work:


pDoc = m_pApp.Document



Try to replace it with:


Dim pDoc As IMxDocument
pDoc = Application.Document 

0 Kudos