Is there any way to detect corner of each shapefile in python !? (not BBox) Or somehow to detect the edges?

enter image description here

I want to detect each shapefile vertices to calculate the differences in latitude for my program.

I've got some idea for that:

  1. Loop over the points going round the polygon and count the number of times the sign of the difference in latitude between point i and i+1 changes.
  2. Calculate consecutive three points angle, then pick four sharpest angles
  • 1
    What would you consider the "corner" of a circle? The best you can get is the largest angle subtended by three consecutive vertices.
    – Vince
    Commented Jan 30, 2016 at 13:42
  • 4
    Well you have to define "corner" if you want to identify it. By most definitions, every vertex at which the angle changes at all is a corner.
    – Tom
    Commented Jan 30, 2016 at 14:23
  • 2
    Should each of your polygons have exactly four "sides"? If so, then you could identify the four sharpest angles.
    – Tom
    Commented Jan 30, 2016 at 14:31
  • 3
    @MK83, as soon as we get an answer on whether all the features will have the same number of "sides" by your definition, then we can begin to address your question. If you are not interpreting them all as having the same number of sides (and since you don't want an angle threshold to define a "corner"), then it boils down to a variation on the Justice Stewart conundrum: if you can't define it, we can't code it.
    – Tom
    Commented Jan 30, 2016 at 15:50
  • 4
    All above comments are legitimate, but I think after that many instructions MK83 deserved some code to start with, since his intent seems to be quiet obvious for the human brain.
    – Detlev
    Commented Jan 30, 2016 at 17:20

1 Answer 1


As many users stated this question has no distinct answer. The results of an analysis may incomplete or fetch the wrong points, dependent on the definition of what should be considered as a corner.

The simplest possibility would be the comparison of angles between adjacent points. As this question is familiar with this I will pick up this code fragment.

import shapefile
from math import atan2

# this is the most critical value, and it might be NOT possible to
# find a distinct value for all shapes; in radians
threshold = 0.5

# the input polygons
sf = shapefile.Reader(r'banana.shp')

# for identified corner points
pf = shapefile.Writer(shapefile.POINT)

shapes = sf.shapeRecords()
for shp in shapes:
    x0, y0 = shp.shape.points[0]
    x1, y1 = shp.shape.points[1]
    az1 = atan2(y1 - y0, x1 - x0)

    for i in range(2, len(shp.shape.points)):
        xi, yi = shp.shape.points[i]
        azi = atan2(yi - y1, xi - x1)

        if abs(azi - az1) > threshold:
            pf.point(x1, y1)

        az1 = azi
        x1, y1 = xi, yi

    xi, yi = shp.shape.points[1]
    azi = atan2(yi - y1, xi - x1)
    if abs(azi - az1) > threshold:
        pf.point(x1, y1)


Simple test case with threshold 0.5 radians (-1 < x, y < 1, EPSG: 4326):

test case

  • 2
    Interesting, because you stated above that "his intent seems to be quiet [sic] obvious for the human brain", but you and I have very different interpretations of what he meant. Your answer ignores the OP's response to the question: Is there some arbitrary angle threshold you would like to set to define a "corner"? He replied, "no there is not". This answer does not address the OP's clarified intent, as reflected in the comments and in his edited post.
    – Tom
    Commented Feb 1, 2016 at 16:57

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