4

I have a shapefile with a single Polygon feature. This feature has a "ring not closed" geometry error (seen here in QGIS): enter image description here

I tried using buffering and the GRASS v.clean command as suggested in other answers, but no luck. I also can't find the gap by inspecting the feature visually (it looks perfect).

I need a valid geometry here. Ideally, I'd like to have a Python/command line solution. Any way of doing this manually in QGIS/ArcGIS would also be great.

UPDATE: this shapefile can be downloaded here.

5

One of the things I do when I receive a "ring x not closed" error for a polygon is to use the Polygons to Lines tool from the toolbar: Vector > Geometry Tools > Polygons to Lines...:

Polygons to Lines

Now you can easily see where the error is visually. Fortunately, there's a simple solution...use the Lines to Polygons tool from the same menu!

Lines to Polygons

Re-converting this should fill the ring as no errors were found using the Check geometry validity tool.

Hope this helps!


Edit:

You can use the following code in the Python Console to do the above operation:

import processing

input = qgis.utils.iface.activeLayer()  # Or change to path of shapefile (e.g. input = "C:/Test//example.shp")
result = "C:/Test//result.shp"

output_0 = processing.runalg("qgis:polygonstolines", input, None)
output_1 = processing.runalg("qgis:linestopolygons", output_0['OUTPUT'], result)
  • 1
    Perfect! I'll look for a way to translate this into Python. – kontextify Nov 20 '15 at 12:39
  • 1
    @kontextify - Awesome buddy, glad it worked! I've edited the post to include some python code which calls the same core functions used from QGIS, hope this is helpful in some way :) – Joseph Nov 20 '15 at 12:55
  • Wow, thanks man! A step in the right direction, but I'm getting GEOS_ERROR: IllegalArgumentException: Points of LinearRing do not form a closed linestring. Somehow the GUI does some extra ring-healing magic somewhere in there... – kontextify Nov 20 '15 at 15:24
  • @kontextify - Most welcome buddy! Hmm, you may need to do more advanced cleaning I would guess. The GRASS algorithm v.clean is pretty good, one of the cleaning tools is "snap" which snaps lines to its nearest vertex. This might be useful to you, it's something you have to experiment with as some layers probably require more 'ring-healing magic' than others :) – Joseph Nov 20 '15 at 15:33
3

In addition to Joseph's very good answer for QGIS, you can use the "repair geometry" tool in ArcGIS. Below is the list of errors that are fixed.

Null geometry—The feature will be deleted from the feature class. If the features with Null geometry are valid for how the data is being modeled Uncheck the "Delete Features with Null Geometry" option on the dialog, or specify the KEEP_NULL option at the command line or in scripting, then the features will not be deleted when encountered by this tool.

Short segment—The geometry's short segment will be deleted.

Incorrect ring ordering—The geometry will be updated to have correct ring ordering.

Incorrect segment orientation—The geometry will be updated to have correct segment orientation.

Self intersections—The geometry's segments that intersect will be split at their intersection.

**Unclosed rings** —The unclosed rings will be closed.

Empty parts—The parts that are null or empty will be deleted.

Note that if you look at the "vertex" level, the start AND the end of must be identical in order to close the loop.

  • 1
    +1, brilliant tip! I really should try and use ArcGIS at some point :) – Joseph Nov 20 '15 at 12:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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