You are here: > ESRI Forums > arcgis desktop discussion forums > Thread Replies

ArcGIS Desktop Discussion Forums

ArcGIS Desktop - Geoprocessing Scripting (Python, JavaScript, VB) forum

Python-how to return the geometry of the ce...   Russel Chu Jan 04, 2010
Re: Python-how to return the geometry of th...   Srinivas Suryanarayanaiah Jan 04, 2010
Re: Python-how to return the geometry of th...   Russel Chu Jan 05, 2010
Re: Python-how to return the geometry of th...   Srinivas Suryanarayanaiah Jan 05, 2010
Re: Python-how to return the geometry of th...   Russel Chu Jan 05, 2010
Re: Python-how to return the geometry of th...   Srinivas Suryanarayanaiah Jan 05, 2010
Re: Python-how to return the geometry of th...   Russel Chu Jan 05, 2010
Re: Python-how to return the geometry of th...   Srinivas Suryanarayanaiah Jan 05, 2010
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Python-how to return the geometry of the centroid of each polygon 
Author Russel Chu 
Date Jan 04, 2010 
Message Dear All,

I want to use python to get the geometry of the centroid of each polygon when looping through a feature class. So I can use the geometry of the centroid to intersect with other layers. How to do this?

Russel

while row:
rowShape = row.GetValue("Shape")
rowCentroid = rowShape.Centroid

gp.SelectLayerByLocation(USACountiesLayer, "INTERSECT", rowCentroid, "", "NEW_SELECTION")
....


 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Python-how to return the geometry of the centroid of each polygon 
Author Srinivas Suryanarayanaiah 
Date Jan 04, 2010 
Message Try this..

while row:
rowShape = row.Shape
rowCentroid = rowShape.Centroid
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Python-how to return the geometry of the centroid of each polygon 
Author Russel Chu 
Date Jan 05, 2010 
Message it didn't work either. 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Python-how to return the geometry of the centroid of each polygon 
Author Srinivas Suryanarayanaiah 
Date Jan 05, 2010 
Message I just tried that attached code and worked fine. 
 
# centrd.py
# ---------
# Python script to list Centroid value of features in a polygon feature class
# Arguments: polygon feature class

# Import System Modules
import arcgisscripting, sys

# Create a variable to access Geoprecessor
gp = arcgisscripting.create()

# Set arguments to variables
fcName = sys.argv[1]

# Process: Make Table View ...
gp.MakeTableView_management(fcName, "fcName_View")
count = gp.GetCount_management("fcName_View")
gp.AddMessage ("Number of records = " + str(count))

rows = gp.UpdateCursor(fcName)
row = rows.Next()
while row:
    rowShape = row.Shape
    rowCentroid = rowShape.Centroid
    gp.AddMessage (str(rowCentroid))
    row = rows.Next()
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Python-how to return the geometry of the centroid of each polygon 
Author Russel Chu 
Date Jan 05, 2010 
Message Thanks for your reply.

The above script (rowShape.Centroid) will return the values of Centroid. But what I am interested in is to get the geometry of the centroid and then use it to intersect with another polygon layer.

any idea? Thanks,

Russel 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Python-how to return the geometry of the centroid of each polygon 
Author Srinivas Suryanarayanaiah 
Date Jan 05, 2010 
Message Ok. How about converting the centroids to points using GP Tool Feature To Point and using the points for Select By Location? 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Python-how to return the geometry of the centroid of each polygon 
Author Russel Chu 
Date Jan 05, 2010 
Message OK, i got it using the CreateObject method.

rowCentroid = rowShape.Centroid
centroidX = rowCentroid.x
centroidY = rowCentroid.y
print centroidX, centroidY

pnt = gp.CreateObject("Point")
pnt.x = centroidX
pnt.y = centroidY

pntGeom = gp.CreateObject("geometry", "point", pnt)

 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Python-how to return the geometry of the centroid of each polygon 
Author Srinivas Suryanarayanaiah 
Date Jan 05, 2010 
Message How cool..