|View all articles regarding Polygon|
Polygon is a Python package that handles polygonal shapes in 2D. It contains Python bindings for gpc, the excellent General Polygon Clipping Library by Alan Murta and some extensions written in C and pure Python. With Polygon you may handle complex polygonal shapes in Python in a very intuitive way. Polygons are simple Python objects, clipping operations are bound to standard operators like +, -, |, & and ^. TriStrips can be constructed from Polygons with a single statement. Functions to compute the area, center point, convex hull, point containment and much more are included.
Points can be organized in tuples or lists. NumPy arrays are also supported but Polygon can be compiled without support for it.
This package was already used to process shapes with more than one million points!
- License: LGPL (not for gpc itself, see below!)
- Status: Should be almost stable now, but there may be memory leaks. Malformed files and illegal contours may crash the library and your running Python interpreter! Use Polygon at your own risk or don't use it at all!
- gpc-Homepage: http://www.cs.man.ac.uk/~toby/alan/software/
gpc is included in Polygon, you don't need to download it separately. I made two small changes to gpc:
- fixed warnings regarding a printf format string and
- made GPC_EPSILON adjustable, this may slow down the clipping a little bit.
The author of gpc (Alan Murta) is not responsible for this distribution! The wrapping and extension code is free software, but the core gpc library is free for non-commercial usage only. The author says:
GPC is free for non-commercial use only. We invite non-commercial users to make a voluntary donation towards the upkeep of GPC. If you wish to use GPC in support of a commercial product, you must obtain an official GPC Commercial Use Licence from The University of Manchester.
Please respect this statement and contact the author (see gpc homepage) if you wish to use this software in commercial projects!
Download, Documentation & Examples
Documentation and examples are included in the source distribution in the folder doc. The development of the packages is hosted at bitbucket. There are two different projects, one for Python 2.x and one for Python 3.1 and newer.
I tried to keep both versions synchronized as long as possible. But now a lot of projects are moving to Python 3 and so will I. In the future Polygon2 will only receive bug fixes while new features are added to Polygon3.
Download Polygon2 for Python 2
Download Polygon3 for Python 3





Hi Jörg,
Thanks for the effort! However, I have a question and I am not sure if it is GPC related or in any case if there is something that can be done about it.
My problem is when I want to add multiple polygons that leave a hole in the center. For example,
import Polygon.Shapes as ps
from math import pi
polys = [ps.Polygon([[1,1], [4,1],[4,2],[1,2]], hole = True) for i in range(4)]
polys[1].rotate(pi/2,1,1)
polys[1].shift(1,0)
polys[2].shift(0,2)
polys[3].rotate(pi/2,1,1)
polys[3].shift(3,0)
pp = reduce(lambda x,y: x+y, polys)
In principle these 4 rectangles should leave an empty square in the center. However, the result is a square with filled in center.
I want to have polygons with multiple holes when I make an union.
Thanks!
Best regards,
Jorge
Hi Jorge,
I cannot reproduce any problems with this example. In my tests it gives a rectangular contour with a rectangular hole in the center. Please send me a complete script with some example output and information on your platform and versions of python and Polygon.
Jörg
Hi Jörg,
I’m a little new to working with C libraries and I have a problem subclassing Polygon. Maybe you could help?
I have my code:
import Polygon as poly
class Shape(poly.Polygon):
def __init__(self, points):
super().__init__(points)
def __contains__(self, shape):
return shape.overlaps(self)
a = Shape(list of points)
b = Shape(some more points)
a in b
Here I will run into trouble for using the wrong type of arguments.
I changed the code to look like:
_shape = poly.Polygon(shape[0])
_self = poly.Polygon(self[0])
return _shape.overlaps(_self)
But it seems like a rather awkward solution. Any ideas?
Silly comment removing indentation.
I don’t _need_ to subclass Polygon, but it is nice to add the methods I need to it.
Hi Zim,
I changed a check function in the C library. Now instances of subclasses should be accepted by most methods. The changes are checked into the git repository at
https://bitbucket.org/jraedler/polygon2 ,
https://bitbucket.org/jraedler/polygon3 .
There are no packages, you have to download or checkout the master branch and compile it yourself.
Greetings,
Jörg
Works like a charm! Thank you very much!
Also thanks for the fantastic library, it has been very useful, especially since there aren’t many libraries like it in Python.
Hi Jörg,
I wonder If you could help me please, since although I’ve already installed Polygon2, when importing the library I get the next message :
Traceback (most recent call last):
File “”, line 1, in
File “Polygon/__init__.py”, line 5, in
from Polygon.cPolygon import *
ImportError: No module named cPolygon
Hope you can help me please. I’d be really grateful to you.
Cheers.
Wronzio
Hi Wronzio,
I know about two different sources for this error message:
1. The package is not correctly installed.
2. You are working in the base folder of the source of the package. This doesn’t work.
Good luck, Joerg
Hi Jörg,
Thanks for the binding! I’ve run into a bug (a segfault, to be precise), however, and I’m wondering if there’s something specific to my installation that’s causing it. Here’s what I’m doing, and the corresponding segfault:
from Polygon import Polygon, Utils
a = array([[0.,0.], [0.,1.], [1.,1.], [1.,0.]]) # a simple square
p = Polygon(a)
Utils.pointList(p)
Segmentation fault (core dumped)
I’m running python-2.7.3, numpy-1.6.2, Polygon-2.0.4. Could you help?
Thanks,
Sourish
Tried that first – just putting a print “hello” before the if statement.
Doesn’t work. I wonder if it’s because I’m using Python 2.5…
Thanks for your help,
Simon
Hi Jörg,
Thanks very much for the tip. I was indeed working int the install library which was somewhat lazy of me. It works fine elsewhere!
I now have another issue – can you help me? I am looking to extend the code to provide grow and shrink operations for the polygons. I have already figured out the algorithm when I was using gpc with MATLAB – but now I want to use it with Python.
I would like to be able to overload the ‘+’ and ‘-’ operators for this, so the following:
x=y+2
will make x a polygon that’s the y polygon grown by 2.
There appears to be strong type checking in the cPolygon.c and this causes an error when I try to implement the above. At first I suspected that the Py_TPFLAGS_CHECKTYPES might be set – but it isn’t.
I can’t find where the type checking is enabled – and so can’t disable it.
Any ideas?
Simon
The + and or operators are both mapped with the table Polygon_NumberMethods to the function Polygon_opUnion (everything in the file cPolygon.c). This function checks if both operands are Polygon objects. To change the behaviour of the +-operator you nedd to change the mapping and/or the function.
Joerg
Hi Simon,
I know about two different sources for this error message:
1. The package is not correctly installed.
2. You are working in the base folder of the source of the package. This doesn’t work.
Good luck, Joerg
Hi Jörg,
Thanks for those great bindings.
Any idea what the following is all about?
Python 2.5.1 (r251:54863, Aug 12 2010, 11:27:22)
[GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-59)] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import Polygon
Traceback (most recent call last):
File “”, line 1, in
File “Polygon/__init__.py”, line 6, in
from cPolygon import *
ImportError: No module named cPolygon
Hi Jörg,
Thanks for those great bindings.
Could you please change “”.join(l) into b”".join(l) in py3k version of encodeBinary() ?
The change is already in the SVN and will be part of the next release. Thaks for the hint!
thanks for the code. This is great
Hi Diederik,
thank you for the hint! I fixed this bug in the SVN now. You can wait for the next release or fix it yourself: in Io.py search for the string
','.joinand replace the comma with a space. There should be only one occurrence near line 240.Jörg
Hi Jörg,
Thanks for providing the Python bindings for the polygon library, that’s really much appreciated! There’s however a small glitch I ran into while exporting svg files: these files cannot be viewed in all SVG capable programs, e.g. Inkscape or Firefox. This issue has been reported here: https://bugs.launchpad.net/inkscape/+bug/683066. I’m not sure if this export functionality has been created by you or by Alan Murta, but I figured that reporting it here wouldn’t hurt.
Thanks once again.
Diederik van Lierop