Generating a centroid for a polygon

1587
10
10-13-2011 08:28 AM
YousafHassan
New Contributor II
Hi
I posted this question in a generic thread earlier, but was wondering if it can be done in python.

I am trying to help my colleague who wants to select a polygon and then click a button (or a custom tool), 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 all 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.

I am looking at the CalculateField example: Calculate centroids on this link:

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00170000004m000000

I was wondering how I can adapt this python script in to doing the above, i.e. the input is the selected polygon and the output is a pop-up displaying X and Y.

Any help would be extremely appreciated.
Thanks.
Tags (2)
10 Replies
JoelCalhoun
New Contributor III
It seems like the simplest solution is to add X and Y fields to the polygon feature class and calculate the X and & values as you mention.

9.3 code would look something like:

# Process: Add Field
gp.AddField_management(parcels_shp, "X_COORD", "DOUBLE", "", "", "", "", "NON_NULLABLE", "NON_REQUIRED", "")
# Process: Add Field
gp.AddField_management(parcels_shp, "Y_COORD", "DOUBLE", "", "", "", "", "NON_NULLABLE", "NON_REQUIRED", "")

# Process: Calculate Field
gp.CalculateField_management(parcels_shp, "X_COORD", "!shape.centroid.x!", "PYTHON_9.3", "")
# Process: Calculate Field
gp.CalculateField_management(parcels_shp, "Y_COORD", "!shape.centroid.y!", "PYTHON_9.3", "")



Once you have those added to the polys as attributes, why not simply use the identify tool?
You can copy and paste the X and Y values from the tool.

If you don't want to see all of the other attributes when you use the identify tool you can "hide" the other attributes from the identify tool by simply unchecking the other attributes in the Field tab of the layer properties.
0 Kudos
DarrenWiens2
MVP Honored Contributor
ArcGIS already has a tool that does this: Feature to Point (requires ArcInfo). Like most tools, it will honor your selection (ie. make points only for those selected polygons). It doesn't automatically pop up the coordinates, however, so you'd have to code that.
0 Kudos
MarcNakleh
New Contributor III
ETGeowizard's unregistered version can do this as well, through arcpy, ArcCatalog or ArcMap, and it's free.
The function is called Polygon To Point, i think.

Hope this helps!
0 Kudos
YousafHassan
New Contributor II
It seems like the simplest solution is to add X and Y fields to the polygon feature class and calculate the X and & values as you mention.

9.3 code would look something like:

# Process: Add Field
gp.AddField_management(parcels_shp, "X_COORD", "DOUBLE", "", "", "", "", "NON_NULLABLE", "NON_REQUIRED", "")
# Process: Add Field
gp.AddField_management(parcels_shp, "Y_COORD", "DOUBLE", "", "", "", "", "NON_NULLABLE", "NON_REQUIRED", "")

# Process: Calculate Field
gp.CalculateField_management(parcels_shp, "X_COORD", "!shape.centroid.x!", "PYTHON_9.3", "")
# Process: Calculate Field
gp.CalculateField_management(parcels_shp, "Y_COORD", "!shape.centroid.y!", "PYTHON_9.3", "")



Once you have those added to the polys as attributes, why not simply use the identify tool?
You can copy and paste the X and Y values from the tool.

If you don't want to see all of the other attributes when you use the identify tool you can "hide" the other attributes from the identify tool by simply unchecking the other attributes in the Field tab of the layer properties.


Thanks for this. However, the guy I am trying to help is not very good with ArcMap and I come from a programming background with two years experience of basic GIS work in ArcMap. So it's a case of blind leading the blind here.

The first thing is where I run this code? I was thinking of creating a custom tool box and creating a tool for the rest of the team. I also know a bit about Models. So how can a model take a selected feature as an input parameter? Also, the displaying X and Y would be very useful so any link to a code for the pop-up would be very helpful.
0 Kudos
YousafHassan
New Contributor II
ArcGIS already has a tool that does this: Feature to Point (requires ArcInfo). Like most tools, it will honor your selection (ie. make points only for those selected polygons). It doesn't automatically pop up the coordinates, however, so you'd have to code that.


Thanks for this. However, we don't have access to ArcInfo. I am on a lowly ArcView licence. But the code examples are really good on the Feature to Point link. I wonder if it is possible to use the code behind Feature to Point and create a custom tool!
0 Kudos
YousafHassan
New Contributor II
ETGeowizard's unregistered version can do this as well, through arcpy, ArcCatalog or ArcMap, and it's free.
The function is called Polygon To Point, i think.

Hope this helps!


Great! I'll download this and try it now.
0 Kudos
YousafHassan
New Contributor II
ETGeowizard's unregistered version can do this as well, through arcpy, ArcCatalog or ArcMap, and it's free.
The function is called Polygon To Point, i think.

Hope this helps!


Thanks, I tried this but it takes all the records in a layer. I just want to select one feature polygon directly on SDE and generate a centroid. Ideally, get the X and Y in a pop-up.
0 Kudos
MarcNakleh
New Contributor III
Hello Yousaf,

My apologies: I had misread the thread!
ET Geowizards tools does in fact operate on all items of the shape or feature class.

If you want to do it point by point, you could do one of two things:
- As described above, do an AddField followed by a CalculateField, but only on the active layer.
- Create your fields, select the points you're interested in and do the following: Attribute Table - Right Click on X or Y Field - Calculate Geometry... - X / Y Coordinate of Point. The overhead of manually clicking on each point, then activating the tool you had in mind seems comparable in my books to using this built-in function of ArcMap.

I hope this helps! Write back if anything's unclear.
0 Kudos
YousafHassan
New Contributor II
Hello Yousaf,

My apologies: I had misread the thread!
ET Geowizards tools does in fact operate on all items of the shape or feature class.

If you want to do it point by point, you could do one of two things:
- As described above, do an AddField followed by a CalculateField, but only on the active layer.
- Create your fields, select the points you're interested in and do the following: Attribute Table - Right Click on X or Y Field - Calculate Geometry... - X / Y Coordinate of Point. The overhead of manually clicking on each point, then activating the tool you had in mind seems comparable in my books to using this built-in function of ArcMap.

I hope this helps! Write back if anything's unclear.


Thanks for this. I'll try to create a custom tool using model builder. I am reading about model building in ArcGIS Resource Centre at the moment. I want to automate this as much as possible as my colleages are not willing to do any of it manually.
0 Kudos