Select to view content in your preferred language

Polygon Centroid

1778
14
Jump to solution
08-09-2013 05:03 AM
JamesSmith7
Emerging Contributor
I am attempting to create a script that zooms to a polygon centroid.  I want the polygon centroid to be based on a Subd attribute field.  Location is a feature class and I intend to use the SHAPE@XY to zoom to the polygon centroid.  I am receiving an RuntimeError: Invalid SQL statement.

centroid = "SHAPE@XY" spatialref = arcpy.Describe("Location").spatialReference cursor = arcpy.da.SearchCursor("Location", "Subd", centroid, spatialref)  row[0] = Subd del row del cursor
Tags (2)
0 Kudos
14 Replies
JamesCrandall
MVP Frequent Contributor
Yes, I am going with the centroid approach.  I took your advice on adding the above script into the add in.  But, when clicking on the button, I get the message stating the tool has no parameters.  I have been unable to get buttons to work without input parameters.


What are the parameters?

You could also try to simplify the script (untested):


      
        mxd = arcpy.mapping.MapDocument("CURRENT")
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        df.zoomToSelectedFeatures()       

0 Kudos
JamesSmith7
Emerging Contributor
String attribute fields.  If I select the polygon, enter the input parameters, the script selects and zooms to the polygon centroid.  But, without the input parameters, the button does not work.  I was thinking Python required input parameters, since scripts are similar to tools run from ArcToolbox.
0 Kudos
JamesSmith7
Emerging Contributor
try:
 for lyr in arcpy.mapping.ListLayers(mxd):
  tlyr = lyr   
  dsc = arcpy.Describe(tlyr)
  sel_set = dsc.FIDSet
  if dsc.shapeType == "Polygon":
   if len(sel_set) > 0:  
    with arcpy.da.SearchCursor(tlyr, ("SHAPE@")) as cursor:
     for row in cursor:
      shape = row[0]
      zoomextent = shape.extent
    arcpy.AddMessage(lyr.name)
    df.extent = zoomextent
    arcpy.RefreshActiveView()
except:
 print arcpy.GetMessages()
0 Kudos
JamesSmith7
Emerging Contributor
I have modified the suggestions provided to only zoom to one selected Feature Class.  But, I want to return the AddMessage line if the correct Feature Class is not selected.  The script does not get to the else statement for other Feature Classes.  When a Fields polygon is selected, the script zooms to the polygon extent.  But, when another polygon or polyline Feature Class is selected it zooms to full extent, as expected.  So, why does the script not use the else statement for Feature Classes other than Fields?

fds = arcpy.mapping.ListLayers(mxd, "Fields", df)[0]
for fds in arcpy.mapping.ListLayers(fds):
        desc = arcpy.Describe(fds)
type = desc.shapeType
if type == "Polygon":
  df.extent = fds.getSelectedExtent()
else:
  arcpy.AddMessage("Fields polygon not selected")
0 Kudos
JamesSmith7
Emerging Contributor
I have modified the suggestions provided to only zoom to one selected Feature Class. But, I want to return the AddMessage line if the correct Feature Class is not selected. The script does not get to the else statement for other Feature Classes. When a Fields polygon is selected, the script zooms to the polygon extent. But, when another polygon or polyline Feature Class is selected it zooms to full extent, as expected. So, why does the script not use the else statement for Feature Classes other than Fields?

fds = arcpy.mapping.ListLayers(mxd, "Fields", df)[0]
for fds in arcpy.mapping.ListLayers(fds):
    desc = arcpy.Describe(fds)
    type = desc.shapeType
    if type == "Polygon":
       df.extent = fds.getSelectedExtent()
    else:
       arcpy.AddMessage("Fields polygon not selected")
0 Kudos