2

I am doing research on the protected areas of the Amazon rainforest and I need to create a single polygon layer that has all the forested non-protected areas in one attribute row. I have tried using different polygonise functions in QGIS with different dissolve functions and fixing geometries, but I always end up with the same final layer which has invalid geometry.

After polygonising the raster and saving the forested areas, I have a multipolygon layer (with thousands of rows in the attribute table) which looks like what I wish to achieve: Multipolygon layer

However, when I try to dissolve the layer into a single polygon layer (with SAGA Polygon Dissolve (all polygons), but the same happens with GDAL Dissolve), I get these polygons within polygons:

Singlepolygon layer

(This is just one area, not the whole layer, showing the areas that appear after trying to dissolve the multipolygon layer)

I've tried to use "Fix geometries", but that only removed some of the unwanted areas (the fixed image is included here).

I have tried to fix this problem for more than a week now and am running out of options to try.

Edit: I am running 3.2.0-Bonn on Windows 10. My GDAL/OGR version is 2.2.4 and I use WGS84 as my CRS.

TeemuK
  • 29
  • 1
  • 5
  • Any chance you could crop your raster down to something small that illustrates the problem which you could share with us? – Spacedman Aug 17 '18 at 07:34
  • 1
    Welcome to GIS SE. As a new user, please take the [Tour], which emphasizes the importance of asking one question per Question. Unfortunately, "Using A or B" questions are considered two questions, since the solutions are very different. Given that your description is about QGIS, I suggest yor remove R, and include exact details about the version(s) of QGIS (and GDAL) in use, and the exact spatial reference and command parameters involved. – Vince Aug 17 '18 at 07:42
  • Not sure if it will fix it, but I also had the problem that after I vectorized a rasterimage the created polygons had tons of geometry errors (intersections) and I was not able to process them. The "fix geometry" tools did not work either. What I did to fix it in the end is, that I drew a really small buffer around the polygons. The buffered polygons had no geometry errors. Perhaps it does work for you. – gHupf Aug 17 '18 at 07:52
  • Can you select manually the little polygons that fill the holes ? – Christophe P. Aug 17 '18 at 12:39
  • @Spacedman I extracted a small area of the raster to share. The problem remained with the small area. Here is what I did with this smaller area in more detail:
    1. I clipped the raster using “Clip raster by mask layer” tool.
    2. I used “Polygonize (raster to vector)” tool (GDAL) to get a polygon
    3. I selected and saved the forested areas (“DN”=1)
    4. I used “Polygon dissolve (all polygons)” tool (SAGA) to dissolve the polygon layer of the forested areas.

    You can get the files from each step here: https://www.dropbox.com/sh/ymspybohfwfvmdj/AACVMGnPRF9nUHfixCWgV_vza?dl=0

    – TeemuK Aug 20 '18 at 07:46
  • I edited the question to only ask one question, thank you @Vince for the tour link and guidance! – TeemuK Aug 20 '18 at 07:49
  • @Volta I cannot manually select the little polygons because they appear after I have used the dissolve function. However, if I use the “Multipart to singleparts” tool, I can select at least most of the little erroneous polygons. – TeemuK Aug 20 '18 at 07:49
  • @gHupf I tried the “Fixed distance buffer“ tool (SAGA) with a buffer distance of 0.000001 (smallest), 1 buffer zone, 5.0 Arc Vertex Distance (I do not know what this does, I couldn’t find an explanation online), Dissolve buffers ticked. When I looked at this layer closely. I noticed that although the error polygons were gone, some small holes had for some reason dissolved into the forested polygon area (noticeable if one flips between 4 and 5). Also, I don’t know if the polygon approach is optimal for me, as I do not want any non-forested area to be under the forested polygon. – TeemuK Aug 20 '18 at 07:50
  • I need to create a single polygon layer that has all the forested non-protected areas in one attribute row. > what is your final objective with that ? If you need to erase something from another raster, a raster treatment approach may be more effective. – gisnside Aug 20 '18 at 08:10
  • 1
    @gisnside No, I need to take a random sample from the non-protected forested area and I use the polygon layer as the area from which the sample is taken. I use the spsample function in R for sampling. My code requires the polygon layer to have one polygon with one attribute row in order to work... I think I could use the multipolygon layer if I used x = "Polygons", but because these issues have come up I fear the geometry is invalid and there would be errors in the sampling. – TeemuK Aug 20 '18 at 10:33
  • I see. Maybe this could help, it's very similar to what you want to do, except it uses a grid of square meshes : https://wpqr4.adb.org/servlet/QuickrSupportUtil?type=quickrdownload&key=/LotusQuickr/agstat_ap/Main.nsf/0/FE1A5E7F3280508548257DF2001ED469/$file/06_Sampling%2520Point.pdf – gisnside Aug 20 '18 at 12:11
  • I added a new part to my answer. – gisnside Aug 20 '18 at 12:18
  • 1
    It looks like the rings in your polygon are being turned into separate polygons, which can happen sometimes when non-core tools (ie SAGA and GDAL). Have you tried dissolving using the built-in QGIS > Vector Geometry > Dissolve tool? You could also try the Merge tool (advanced digitizing toolbar). – csk Aug 21 '18 at 20:14
  • @csk Interesting. I have tried both without success. – TeemuK Oct 09 '18 at 08:08

2 Answers2

2

For your specific question :

  • Reclassify your raster (to avoid further polygon dissolving) to make sure to have only 0/1 values, so that "1" values are the ones your need to convert into polygons (See Performing Raster Reclassification in QGIS? if necessary)

  • Convert this raster into a polygon with the r.to.vect GRASS tool, you should get all the raster value converted, cut in polygones with "0" and "1" values, on the raster footprint.

  • In the attribute table, delete "0" values, you should only have left "1" values polygons

  • Use the "Single to Multipart" tool to restore one polygone multipolygon

For your final objective :

Notice : For this part, you don't need one multipolygon.

  • To complete your final objective and inspired by this document , create a mesh or a regular point or polygon grid on your polygon layer boundary. You can use the vector Menu/Research Tools/Create grid tool or with QGIS geoprocessing tools/ Vectero creation tool/regular points

  • Select all points or polygons that are within your selected polygon area, invert selection, then delete external features.

  • With the vector Menu/Research Tools/Random selection tool, select a number of random interest points.

gisnside
  • 7,818
  • 2
  • 29
  • 73
  • I am not sure what you meant by “raster footprint”, this is a new term for me, but I used the default options of the r.to.vect tool to create a polygon layer from the raster. I also assume you meant Multipart to singleparts tool, which I used after deleting the 0 values from the attribute table. However, this did not create one multipolygon layer, I still had separate rows of data. When I dissolved this, the result had the same errors. – TeemuK Oct 09 '18 at 15:06
  • The random sampling you suggested does not work for my purposes, unfortunately. – TeemuK Oct 09 '18 at 15:07
  • Interestingly, as I used Fixed geometries to remove the errors, they were all gone and I had the layer I want, but I am unable to save it, because when I do, the errors reappear in the saved file. Does anyone know why that is? – TeemuK Oct 09 '18 at 15:08
0

I decided to check if this was a QGIS specific problem and did the same process in ArcGIS on a different computer. I dissolved the multipolygon layer, and ArcGIS did not visualize the errors – even though I was using a copy of the same file. However, when I looked at this dissolved "error free" file in QGIS, the errors reappeared.

I decided to try the sampling to see if the sample points would appear on the error areas or not. I created a new singlepolygon layer in the latest version of QGIS (3.2.3. Bonn) and used Fix geometries tool. I used the fixed layer in the sampling and no samples appeared on the "error" areas (see picture). Besides a visual check, I also reaffirmed this by quantitative means in R.

My best guess is that this was a visual bug in QGIS since ArcGIS did not show these errors in the same file.

Sample points visualized in QGIS: No samples appeared on the "erroroneous" coloured polygon within polygon areas.

TeemuK
  • 29
  • 1
  • 5