|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
The package can be downloaded from the Python Package Index PyPI:
The development files as well as documentation and examples are hosted at GitHub. There are two different projects:
46 Responses to Polygon
Leave a Reply Cancel reply
You must be logged in to post a comment.
Tags
Other
Hi, I wanted to know how does the Polygon library finds the centroid of an irregular polygon, what algorithm is being used for this task?
Thanks for your help.
The algorithm is desribed here: http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon
A sample implementation can be found here: http://stackoverflow.com/questions/2792443/finding-the-centroid-of-a-polygon
Hi Jörg,
Could it be that when Polygon_addContour() is called in Polygon_init(), any errors raised in Polygon__addContour() don’t bubble up? I’m not very familiar with this Python/C interface, but shouldn’t Polygon_init() return -1 in case of an error? I could be wrong though…
Kind regards,
Diederik
Hi Diederik,
you are right, the errors were not propageted correctly. Please try the newest version from git, it should be fixed.
Greetings, Jörg
Fix confirmed, thanks!
Hello Jörg, the Windows installer for Python 3.3 (Polygon3-3.0.6.win32-py3.3_withNumPy) seems to be broken: after clicking on Next when it reads “ready to install” the application crashes.
Any thoughts on what could be going on?
Sorry, I can not reproduce the error. On my system the installation works as expected. Can you give more information (Windows System, Python version etc.)?
Jörg
Hi! I can’t seem to import the packages properly.
The error message was:
Tracback :
File “”, lin 1, in
File “C\Python27\lib\site-packages\Polygon\__init__.py”, line 8, in
__version__ = version
NameError: name ‘version’ is not defined
Please send a reply, I’m going the library for a Machine Problem in school.
Hi Louie,
please send a correct and complete error message! Which version of Polygon do you use? How did you install it? What code was used to trigger the error? Jörg
Hi Jörg,
Is there an easy way to delete a contour from a polygon, other than creating a new polygon and copying over all contours that are _not_ to be deleted?
Thanks,
Diederik
Hi Diederik,
no, I don’t think there’s an easier way, sorry!
Jörg
Hi Jörg,
I’m sorry but I don’t quite understand from the documentation how I should input the fill color for when I save a svg:
For example if I want the width and height to be 1000 and the rgb to be 128,128,230 how do I do it ?
Ex of what I’ve been trying:
writeSVG(“my.svg”,polyCircle,1000,1000,(128,128,230);
writeSVG() expects a sequence (list, tuple, …) of polygons and sequences of the attributes, even if you have just one polygon object. A list with one element can be written
[e]
. A tuple with one element is written(e,)
. A working example would be:import Polygon, Polygon.Shapes, Polygon.IO
p = Polygon.Shapes.Star()
Polygon.IO.writeSVG('out.svg', (p,), fill_color=((0, 255, 0),))
Jörg
Hi Jörg,
Is it possible to set the GPC_EPSILON without recompiling the GPC library? I’ve found out that I need a bigger value, but haven’t found a way to specify it directly in the Python code itself.
Thanks,
Diederik
The Polygon module has two functions: setTolerance(tol) and getTolerance(), that should change or return the value of GPC_EPSILON.
Jörg
I searched for epsilon or setEpsilon, didn’t think of just reading the manual more carefully 😉
Thanks for your help!
Hi Jörg,
The library looks great very intuitive, If I may ask is there a method to export the shapes to some form of bitmap image format (for ex jpg) ?
Or would I have to use another library after I generate the images with yours ?
There’s no bitmap export. You can export to SVG and use an external tool to convert or write your own export function. Jörg
vishal@vishal-Reserved:~/acads/python/Polygon2-2.0.6$ sudo python setup.py install
NumPy extension not found – disabling support for it!
running install
running build
running build_py
running build_ext
running install_lib
running install_egg_info
Removing /usr/local/lib/python2.7/dist-packages/Polygon2-2.0.6.egg-info
Writing /usr/local/lib/python2.7/dist-packages/Polygon2-2.0.6.egg-info
vishal@vishal-Reserved:~/acads/python/Polygon2-2.0.6$ python
Python 2.7.3 (default, Apr 10 2013, 06:20:15)
[GCC 4.6.3] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import Polygon, Polygon.IO
Traceback (most recent call last):
File “”, line 1, in
File “Polygon/__init__.py”, line 5, in
from cPolygon import *
ImportError: No module named cPolygon
Please help me with this
Please do not try this in the source folder of Polygon. This will not work! Jörg
I am trying to install the package but I am not able to install it. I can understand the reason. This is what appears:
C:\Program Files (x86)\python\Polygon3-3.0.6>python setup.py install
NumPy extension not found – disabling support for it!
running install
running build
running build_py
running build_ext
building ‘Polygon.cPolygon’ extension
Traceback (most recent call last):
File “setup.py”, line 100, in
setup(**args)
File “C:\Program Files (x86)\python\lib\distutils\core.py”, line 148, in setup
dist.run_commands()
Vishal
*I cant understand the reason
Hi Jörg,
When using writeSVG with a tuple of RGB values, the svg contains e.g. “fill:(255, 0, 0)”. This doesn’t work and should be “fill:rgb(255, 0, 0)” instead. See http://www.w3.org/TR/SVG/types.html#DataTypeColor
Kind regards,
Diederik
Thank you! This is fixed now in the repositores on bitbucket and will be part of the next release. Jörg
Hi Jörg,
Suppose I have a single solid polygon with multiple holes. Now when I do a boolean on it, I might end up with a single polygon consisting of multiple solids, with each solid having multiple holes. Is there an easy way to tell which holes belongs to which solid? I can iterate over all contours in the polygon and find out which are the solids and which are the holes, but that’s about it….
Of course I could look at all permutations and use the overlap() and covers() methods to find this out, but isn’t there an easier way?
Regards,
Diederik
Hi Diederik,
Sorry, I don’t think there’s an easier way. The list of solids and holes is returned by gpc, which does the clipping operations. What I get is only a flat list without any information on the structure.
Greetings from Berlin, Jörg
Thanks a lot for the quick reply, I will figure something out myself then.
Diederik
BTW, I visited Berlin last Friday…
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
','.join
and 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