Calculate polygon sides

2281
6
09-13-2011 10:14 AM
AlejandroMata
New Contributor
Hi all !

I have a polygon shp, and i want to calculate for this shp how many sides has each polygon, as the below image. Is it possible ? im almost sure that it is possible.


Thanks in advanced.
0 Kudos
6 Replies
DarrenWiens2
MVP Honored Contributor
This counts vertices, minus 1, in the Field Calculator which should be the number of sides.

Parser:
Python

Expression:
MySub(!shape!)


Codeblock:
def MySub(feat):    
    partnum = 0

    # Count the number of points in the current multipart feature
    partcount = feat.partCount
    pntcount = 0

    # Enter while loop for each part in the feature (if a singlepart feature
    # this will occur only once)
    #
    while partnum < partcount:
        part = feat.getPart(partnum)
        pnt = part.next()

        # Enter while loop for each vertex
        #
        while pnt:
            pntcount += 1   
            pnt = part.next()
   
            # If pnt is null, either the part is finished or there is an 
            # interior ring
            #
            if not pnt: 
                pnt = part.next()
        partnum += 1
    pntcount = pntcount - 1
    return pntcount
KaylaSnow
New Contributor

This worked perfectly! Thanks

0 Kudos
TimothyHales
Esri Notable Contributor
You can convert the polygon file to a polyline file using the Split Line at Vertices tool.  Then Spatially Join the Polygon to the Polyline.  There will be a count field in the output.
0 Kudos
TimothyHales
Esri Notable Contributor
Great code sample Darren.  Thanks for sharing!
0 Kudos
DanLee
by Esri Regular Contributor
Esri Regular Contributor
If your polygons are simple without holes or multiparts, you can do the following:
- Add a field of type = LONG.
- Use Calculate Field tool with this expression: !shape!.pointcount - 1; and specify PYTHON as the expression type.

The resulting values should be the polygon side counts.
0 Kudos
AlejandroMata
New Contributor
Hi folks !

Thanks a lot for your suggestions, i'm going to test your advices and i tell you how i was.

Regards.
0 Kudos