1

I wanted to clip an OSM vector layer with the boundaries of a raster image--a GeoTIFF file. I was able to polygonize the raster and obtain its boundary in a vector format. But I am getting a error about the vector layer having invalid geometry when I try to clip the OSM layer with the polygonize vector layer. So let me walk you through what I did.

I took a GeoTIFF image and polygonized it using this code from a previous post.

gdal_translate -b mask -of vrt -a_nodata 0 test.tif test.vrt
# Note the  -a_nodata 0 doesn't seem to work when the mask is input, so do another pass
gdal_translate -b 1 -of vrt -a_nodata 0 test.vrt test2.vrt
gdal_polygonize.py -q  -8 test2.vrt -b 1 -f "ESRI Shapefile" testdata.shp

I get the corresponding image when I import the data to QGIS

enter image description here

However, when I go to clip the OSM layer with the new boundary layer, I get this error about an invalid geometry.

enter image description here

I tried to fix this inside of QGIS, but not sure how to proceed. I tried to use the "Validate Geometry" tool, but that did not fix the invalid geometry issue. Any suggestions about how to either correct the original GDAL code to make valid geometries, or how to fix the subsequently generated polygons?

Vince
  • 20,017
  • 15
  • 45
  • 64
krishnab
  • 909
  • 1
  • 12
  • 23
  • 2
    It looks like the geometry has a spike in it, at around 2-o'clock. – Vince May 29 '19 at 01:22
  • @Vince oh I can see that now. How do I fix something like that. Can I use the simplify function or something? – krishnab May 29 '19 at 01:28
  • This post might help https://gis.stackexchange.com/questions/112687/fixing-geometry-validity-errors-in-qgis\ – Michael Stimson May 29 '19 at 03:35
  • Did you try 'fix geomery'? – Mat May 29 '19 at 05:13
  • @Mat I tried wrapping a very narrow buffer around the polygon and am trying to see if that works. I was just trying some different buffer widths. – krishnab May 29 '19 at 05:22
  • There are probably self-intersections in resulting polygon. In QGIS 3 there is Fix Geometries tool. It usually helps running layer through it when there are invalid geometries reported. – Mat May 29 '19 at 06:45
  • @Mat oh yes, that worked great. I never knew there was a fix geometries tool in QGIS. Do you know what algorithm it uses to fix the geometries? I was looking around online but could not find any information on the code behind the QGIS tool. Would you happen to know how to fix geometries using code instead of QGIS? – krishnab May 29 '19 at 08:19
  • Unfortunately not. I imagine its similar to v.clean. You could run same tool (Fix Geometries) in python script, for more on this try HERE – Mat May 29 '19 at 09:17

1 Answers1

1

There are probably self-intersections in resulting polygon. In QGIS 3 there is Fix Geometries tool. It usually helps running layer through it when there are invalid geometries reported.

Mat
  • 1,588
  • 1
  • 8
  • 18