# Find the irregular polygons in QGIS using Python

I have a vector shapefile and I want to find the irregular polygons. What are the parameters to be considered to check the irregularity of the polygon features?

Calculating the perimeter and extent will be useful for rectangle so how can I find the irregular polygons in the polygon shapefile?

``````for f in layer.getFeatures():
perimeter = feat.geometry().length()
bbox = geom.boundingBox()
width = bbox.xMaximum() - bbox.xMinimum()
height = bbox.yMaximum() - bbox.xMinimum()
``````
• What do you mean by irregular polygons? Do you mean invalid (self-crossing etc) polygons? Aug 26 '15 at 8:04
• @webrian yes polygons having irregular shape. Aug 26 '15 at 8:53
• A regular polygon is a specific thing, mathematically speaking: en.wikipedia.org/wiki/Regular_polygon - you probably mean "invalid". Or something else. What do you mean by "irregular"? Aug 26 '15 at 13:50

Easy way

You don't need to write code to this

• Vector > Geometry Tools > Check Geometry Validity

will tell you if there are any problems such as

• self intersection
• winding rule violations

.. although it's only a report, it won't fix your geometry.

To fix these, you can use v.clean in Grass, via the Processing Toolkit.

In code

If you're wanting to do this in code (e.g. as part of a plugin)

• QGSFeature has a geometry() method to get the QGSGeometry
• QGSGeometry has a isGeosValid() method returning boolean to test for validity

So something like (not tested)

``````for f in layer.getFeatures():
geom = feat.geometry()
if geom.isGeosValid():
# is valid
else:
# not valid
``````
• Our geometry is valid but i need to find the shape irregularity. Aug 26 '15 at 13:19
• Ah, ok - I thought you meant Valid geometry.. By irregular, do you mean this? Might be worth adding an illustration or clarifying your question for those not familiar with the term (like me!) ;-) Aug 26 '15 at 14:20
• @user99 also, do you want a way to quantify how irregular the shape is (e.g. how 'spiky' or 'skewed'), or do you only wish to detect irregular versus regular? Aug 26 '15 at 14:40
• Yes correct even i want to quantify how iregular the shape is. Aug 26 '15 at 16:35

Hausdorff Distance

To actually quantify how regular a polygon is, POSTGIS has an implementation of Hausdorff Distance, ST_HausdorffDistance(geom1,geom2).

You could compare the polygon geometry with something like

• the bounding box of your polygon,
• the minumum bounding circle
• a simplified version of your polygon

for example.

any of these would give an objective measure of how irregular your polygon is.

I once used this on a map of the US counties, to identify and cluster states according to shape similarity to other counties. It worked quite well, although it only recognised shapes as similar when the sizes were close. But if you follow my suggestion of comparing with bounding box, that shouldn't be a problem.