“Bad, bad polygon”… fixing invalid geometries with Quantum GIS

A few days ago a Quantum GIS user sent me a polygon shapefile that was giving her a big headache. When she needed to digitize new polygons using the “avoid intersection” feature (that allows to draw perfectly adjacent polygons, without worrying too much with the snapping), the new geometry didn’t stick in the canvas. I immediately checked if the “avoid intersection” feature was enabled in another layer, but that was not the case, so the other option was to check for some kind of problem in the geometry.

Toggle the editing mode, pick the QGIS “node tool” and select the offending geometry, in the bottom left corner you will see a message

Validation started

after a few seconds (depends on how many nodes the geometry has) you will se another message, something like

Validation finished (2 error(s) found)

in this case QGIS tells you that there are two errors and it marks them with two green crosses. Just zoom in the proper spot and fix them.

Screenshot from 2012-05-26 09:00:08

You can also get the errors as a list, in this case use the “check geometry validity” tool in the “vector” menu. After running the tool just select one of the errors and QGIS will zoom exactly where the problem occur.Screenshot from 2012-05-26 09:27:45

If your vector layer is in a very bad shape, with hundreds or thousands of geometry/topology errors (like self intersections), overlapped areas, empty spaces between polygons that are supposed to be adjacent, etc. then we need to use GRASS.

Long story short: GRASS is a fully topological GIS, this means that when importing a vector layer in its own data model it does not tolerate topological errors (that the simple features models allows, ex: overlaps). At the same time it considers holes as possible errors. Along the importing process you may also want to set a minimum size for areas to be imported and a snapping distance, under which nearby boundaries are snapped together. This way you will be able to fix in a matter of minutes (and a few tries) vectors in a very bad shape. Fire up the QGIS/GRASS plugin (or the GRASS native GUI if you prefer) and open/create a mapset and open the GRASS toolbox. Then just import your simple features vector (shapefile, postgis, etc.) using the v.in.ogr GRASS module without forgetting to have a look in its advanced options:

Screenshot from 2012-05-26 10:05:27

Once you are done you can export the GRASS vector in another format (ex: shapefile) by using the “save as…” QGIS function or the v.out.ogr GRASS module.

About these ads