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?

  • 2
    It looks like the geometry has a spike in it, at around 2-o'clock. – Vince May 29 at 1: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 at 1:28
  • This post might help gis.stackexchange.com/questions/112687/…\ – Michael Stimson May 29 at 3:35
  • Did you try 'fix geomery'? – Mat May 29 at 5: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 at 5:22
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.

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.