Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm using ArcGIS 9.3 to find centroid of some polygon features. I'd like to do this in Python, but so far I can only find solutions with VBA. Can anyone provide some leads here?

share|improve this question
    
Do you need the centroid of each polygon, or the single centroid that would be from the combination of all the polygons? Also, what is your desired result? To have this as an attribute, or a variable to perform another operation, etc? There are a few ways to get the centroids, it just depends on why you need them as to what method to choose. –  Get Spatial Jun 16 '12 at 20:06
    
Thanks for your response. i need to create centroid for each polygon. basically, i need to add centroid lat/lon to the output table. –  Sweet n Sour Chicken Jun 18 '12 at 17:43
add comment

3 Answers

There are really two direct ways to pull the polygon centroids out using Python. The method you choose, depends solely on what your end usage is going to be.

If you simply want to use a python script to calculate fields that contain the polygon centroids, then you would use the following script. It is taken from this help document: Calculate Field (Data Management)

EXAMPLE 2:  PYTHON-based Calculation using geometry properties

# Calculate x and y centroid fields using the geometry property Centroid
import arcgisscripting, sys
gp = arcgisscripting.create()

inputFC = sys.argv[1]

gp.AddField_management(inputFC, "xCentroid", "DOUBLE", 18, 11)
gp.AddField_management(inputFC, "yCentroid", "DOUBLE", 18, 11)

# Centroid property returns a string with x and y separated by a space
xExpression = "float(!SHAPE.CENTROID!.split()[0])"
yExpression = "float(!SHAPE.CENTROID!.split()[1])"

gp.CalculateField_management(inputFC, "xCentroid", xExpression, "PYTHON")
gp.CalculateField_management(inputFC, "yCentroid", yExpression, "PYTHON")

On the other hand, if you want access the centroid information for use later in a script, or for other operations, you will need to access the geometry object of each feature.

This script is adapted from this help topic on The geometry object

import arcgisscripting, sys
gp = arcgisscripting.create()

inputFC = sys.argv[1]

# Create search cursor
#
features = gp.SearchCursor(inputFC)

# Add each centroid coordinate pair to a list
#
centroidlist = []
xcoord = 0
ycoord = 0

for feat in features:
    # Create the geometry object
    #
    geom = feat.shape
    # Centroid property returns a string with x and y separated by a space
    xcoord = geom.centroid.split()[0]
    ycoord = geom.centroid.split()[1]

    centroidlist.append((xcoord,ycoord))
share|improve this answer
    
Thanks a lot! this is extremely helpful! –  Sweet n Sour Chicken Jun 18 '12 at 18:46
    
@SweetnSourChicken - Glad that I could help. If this solution works for you, please click on the check box to accept the answer. How to ask questions? –  Get Spatial Jun 18 '12 at 18:54
    
Well put. I just saw that there is an example of calculating centroids on this ArcGIS help page: help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//… –  Timothy Michael May 16 '13 at 20:03
add comment

This should get you started hopefully:

http://forums.esri.com/Thread.asp?c=93&f=993&t=172860#509335

share|improve this answer
add comment

If you have an ArcInfo license the Feature To Point tool which has a CENTROID option would mean this might not need Python. It is a tool that I would like to see accessible with an ArcView license. After you have the point features created at the polygon centroids you can use the Add XY Coordinates tool to easily access their x,y locations as attributes.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.