{
  "id": 28489,
  "title": "Is there a better way to make polygons in R",
  "url": "/competitions/dstl-satellite-imagery-feature-detection/discussion/28489",
  "author_name": "",
  "post_date": "2017-02-05T17:23:06.567389500Z",
  "votes": 1,
  "comment_count": 4,
  "views": 0,
  "content": "<p>Do you have suggestions on simplifying the WKT polygon complexity in R?</p>\n\n<p>I am using raster::rasterToPolygons to create merged polygons. However this package takes a long time to build the polygons, and they are overly complex. I'm thinking of using a package like EBimage to erode and dilate the mask before creating the polygons. I wonder if there is a better method like the <a href=\"https://www.kaggle.com/lopuhin/dstl-satellite-imagery-feature-detection/full-pipeline-demo-poly-pixels-ml-poly\">contour method used by lopuhin</a>.</p>\n\n<p>This is the code I am currently using to create WKT submissions.</p>\n\n<pre><code>library(raster) # Read and plot TIFF files, work with raster &amp; polygons\nlibrary(rgeos) # Read and convert WKT to spatial polygons\n\n# make raster mask of class\nclass_mask &lt;- raster(ifelse(class_pred_matrix == im_class, 1, 0))\nextent(class_mask) &lt;- img_extent\n\n# make polygons from mask\nclass_poly &lt;- rasterToPolygons(class_mask, dissolve = TRUE)\n\n# add to WKT results\nclass_wkt &lt;- c(class_wkt, writeWKT(class_poly))\n</code></pre>",
  "messages": [
    {
      "id": "160035",
      "postDate": "02/05/2017 17:23:06",
      "content": "<p>Do you have suggestions on simplifying the WKT polygon complexity in R?</p>\n\n<p>I am using raster::rasterToPolygons to create merged polygons. However this package takes a long time to build the polygons, and they are overly complex. I'm thinking of using a package like EBimage to erode and dilate the mask before creating the polygons. I wonder if there is a better method like the <a href=\"https://www.kaggle.com/lopuhin/dstl-satellite-imagery-feature-detection/full-pipeline-demo-poly-pixels-ml-poly\">contour method used by lopuhin</a>.</p>\n\n<p>This is the code I am currently using to create WKT submissions.</p>\n\n<pre><code>library(raster) # Read and plot TIFF files, work with raster &amp; polygons\nlibrary(rgeos) # Read and convert WKT to spatial polygons\n\n# make raster mask of class\nclass_mask &lt;- raster(ifelse(class_pred_matrix == im_class, 1, 0))\nextent(class_mask) &lt;- img_extent\n\n# make polygons from mask\nclass_poly &lt;- rasterToPolygons(class_mask, dissolve = TRUE)\n\n# add to WKT results\nclass_wkt &lt;- c(class_wkt, writeWKT(class_poly))\n</code></pre>",
      "rawMarkdown": "Do you have suggestions on simplifying the WKT polygon complexity in R?\n\nI am using raster::rasterToPolygons to create merged polygons. However this package takes a long time to build the polygons, and they are overly complex. I'm thinking of using a package like EBimage to erode and dilate the mask before creating the polygons. I wonder if there is a better method like the [contour method used by lopuhin][1].\n\nThis is the code I am currently using to create WKT submissions.\n\n\n    library(raster) # Read and plot TIFF files, work with raster & polygons\n    library(rgeos) # Read and convert WKT to spatial polygons\n    \n    # make raster mask of class\n    class_mask <- raster(ifelse(class_pred_matrix == im_class, 1, 0))\n    extent(class_mask) <- img_extent\n    \n    # make polygons from mask\n    class_poly <- rasterToPolygons(class_mask, dissolve = TRUE)\n    \n    # add to WKT results\n    class_wkt <- c(class_wkt, writeWKT(class_poly))\n\n\n  [1]: https://www.kaggle.com/lopuhin/dstl-satellite-imagery-feature-detection/full-pipeline-demo-poly-pixels-ml-poly",
      "votes": null
    },
    {
      "id": "160086",
      "postDate": "02/06/2017 00:07:44",
      "content": "<p>I'm using a very similar approach and also finding rasterToPolygons too slow. I guess you have read the search engine responses which say use R to make an external python call to rgdal...  </p>\n\n<p><a href=\"https://johnbaumgartner.wordpress.com/2012/07/26/getting-rasters-into-shape-from-r/\">https://johnbaumgartner.wordpress.com/2012/07/26/getting-rasters-into-shape-from-r/</a></p>\n\n<p>I'm leaving that as a last resort. Instead I've taken to sieving the raster to remove small areas prior to converting to applying rasterToPolygons. </p>\n\n<p><a href=\"http://gis.stackexchange.com/questions/130993/remove-clumps-of-pixels-in-r\">http://gis.stackexchange.com/questions/130993/remove-clumps-of-pixels-in-r</a></p>\n\n<p>Can't say that it makes it much better, but it's early days for me. </p>\n\n<p>Thinking about the contour approach the rasterToContour function may be if use, it's certainly much quicker and outputs a spatialLinesDataFrame. Should then be able to convert to spatialpolygons somehow...</p>\n\n<p><a href=\"http://stackoverflow.com/questions/14379828/how-does-one-turn-contour-lines-into-filled-contours\">http://stackoverflow.com/questions/14379828/how-does-one-turn-contour-lines-into-filled-contours</a></p>\n\n<p>If you get something that works it will certainly get my Kernel vote!</p>",
      "rawMarkdown": "I'm using a very similar approach and also finding rasterToPolygons too slow. I guess you have read the search engine responses which say use R to make an external python call to rgdal...  \n\n[https://johnbaumgartner.wordpress.com/2012/07/26/getting-rasters-into-shape-from-r/][1]\n\nI'm leaving that as a last resort. Instead I've taken to sieving the raster to remove small areas prior to converting to applying rasterToPolygons. \n\n[http://gis.stackexchange.com/questions/130993/remove-clumps-of-pixels-in-r][2]\n\nCan't say that it makes it much better, but it's early days for me. \n\nThinking about the contour approach the rasterToContour function may be if use, it's certainly much quicker and outputs a spatialLinesDataFrame. Should then be able to convert to spatialpolygons somehow...\n\n[http://stackoverflow.com/questions/14379828/how-does-one-turn-contour-lines-into-filled-contours][3]\n\nIf you get something that works it will certainly get my Kernel vote!\n\n\n  [1]: https://johnbaumgartner.wordpress.com/2012/07/26/getting-rasters-into-shape-from-r/\n  [2]: http://gis.stackexchange.com/questions/130993/remove-clumps-of-pixels-in-r\n  [3]: http://stackoverflow.com/questions/14379828/how-does-one-turn-contour-lines-into-filled-contours",
      "votes": null
    },
    {
      "id": "160273",
      "postDate": "02/06/2017 21:31:52",
      "content": "<p>Alternative ways for converting raster to polygons are using <a href=\"https://github.com/jannes-m/RQGIS\">RQGIS</a> or the <a href=\"https://cran.r-project.org/package=rgrass7\">rgrass7</a>/<a href=\"https://cran.r-project.org/package=spgrass6\">spgrass6</a> packages. </p>\n\n<p>With RQGIS, which is an interface to QGIS, one could use \"gdalogr:polygonize\".</p>\n\n<p>With either rgrass7 (interface to GRASS GIS 7) or spgrass6 (for GRASS GIS 6), one could use the <code>execGRASS</code> command to execute <code>r.to.vect</code> to produce an output of type \"area\". This is the approach I'm following and I've found it quite fast.</p>\n\n<p>Hope this helps</p>",
      "rawMarkdown": "Alternative ways for converting raster to polygons are using [RQGIS][1] or the [rgrass7][2]/[spgrass6][3] packages. \n\nWith RQGIS, which is an interface to QGIS, one could use \"gdalogr:polygonize\".\n\nWith either rgrass7 (interface to GRASS GIS 7) or spgrass6 (for GRASS GIS 6), one could use the `execGRASS` command to execute `r.to.vect` to produce an output of type \"area\". This is the approach I'm following and I've found it quite fast.\n\nHope this helps\n\n\n  [1]: https://github.com/jannes-m/RQGIS\n  [2]: https://cran.r-project.org/package=rgrass7\n  [3]: https://cran.r-project.org/package=spgrass6",
      "votes": null
    },
    {
      "id": "160294",
      "postDate": "02/06/2017 23:28:06",
      "content": "<p>Thanks amsantac. Every day brings a suprise... </p>\n\n<p>I've used GRASS and QGIS for many years when needing to perform heavier GIS lifting than R is suited for but I never knew that there are now packages to interface to them directly from R. I'll be giving them a go as contours to polygons looks messy.</p>",
      "rawMarkdown": "Thanks amsantac. Every day brings a suprise... \n\nI've used GRASS and QGIS for many years when needing to perform heavier GIS lifting than R is suited for but I never knew that there are now packages to interface to them directly from R. I'll be giving them a go as contours to polygons looks messy.",
      "votes": null
    },
    {
      "id": "160322",
      "postDate": "02/07/2017 03:04:24",
      "content": "<p>Thanks for the recommendations! </p>\n\n<p>@Chippy, yes the raster countour feature won't work the way we need. I was thinking of using a KNN model to define regions. I still think that is viable, but I'm going to try rgrass7. I have a feeling that it will be very useful for future projects.</p>",
      "rawMarkdown": "Thanks for the recommendations! \n\n@Chippy, yes the raster countour feature won't work the way we need. I was thinking of using a KNN model to define regions. I still think that is viable, but I'm going to try rgrass7. I have a feeling that it will be very useful for future projects.",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 160086,
      "author_name": "nigelcarpenter",
      "author_url": "",
      "post_date": "02/06/2017 00:07:44",
      "content": "<p>I'm using a very similar approach and also finding rasterToPolygons too slow. I guess you have read the search engine responses which say use R to make an external python call to rgdal...  </p>\n\n<p><a href=\"https://johnbaumgartner.wordpress.com/2012/07/26/getting-rasters-into-shape-from-r/\">https://johnbaumgartner.wordpress.com/2012/07/26/getting-rasters-into-shape-from-r/</a></p>\n\n<p>I'm leaving that as a last resort. Instead I've taken to sieving the raster to remove small areas prior to converting to applying rasterToPolygons. </p>\n\n<p><a href=\"http://gis.stackexchange.com/questions/130993/remove-clumps-of-pixels-in-r\">http://gis.stackexchange.com/questions/130993/remove-clumps-of-pixels-in-r</a></p>\n\n<p>Can't say that it makes it much better, but it's early days for me. </p>\n\n<p>Thinking about the contour approach the rasterToContour function may be if use, it's certainly much quicker and outputs a spatialLinesDataFrame. Should then be able to convert to spatialpolygons somehow...</p>\n\n<p><a href=\"http://stackoverflow.com/questions/14379828/how-does-one-turn-contour-lines-into-filled-contours\">http://stackoverflow.com/questions/14379828/how-does-one-turn-contour-lines-into-filled-contours</a></p>\n\n<p>If you get something that works it will certainly get my Kernel vote!</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 160273,
      "author_name": "amsantac",
      "author_url": "",
      "post_date": "02/06/2017 21:31:52",
      "content": "<p>Alternative ways for converting raster to polygons are using <a href=\"https://github.com/jannes-m/RQGIS\">RQGIS</a> or the <a href=\"https://cran.r-project.org/package=rgrass7\">rgrass7</a>/<a href=\"https://cran.r-project.org/package=spgrass6\">spgrass6</a> packages. </p>\n\n<p>With RQGIS, which is an interface to QGIS, one could use \"gdalogr:polygonize\".</p>\n\n<p>With either rgrass7 (interface to GRASS GIS 7) or spgrass6 (for GRASS GIS 6), one could use the <code>execGRASS</code> command to execute <code>r.to.vect</code> to produce an output of type \"area\". This is the approach I'm following and I've found it quite fast.</p>\n\n<p>Hope this helps</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 160294,
      "author_name": "nigelcarpenter",
      "author_url": "",
      "post_date": "02/06/2017 23:28:06",
      "content": "<p>Thanks amsantac. Every day brings a suprise... </p>\n\n<p>I've used GRASS and QGIS for many years when needing to perform heavier GIS lifting than R is suited for but I never knew that there are now packages to interface to them directly from R. I'll be giving them a go as contours to polygons looks messy.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 160322,
      "author_name": "jeffhebert",
      "author_url": "",
      "post_date": "02/07/2017 03:04:24",
      "content": "<p>Thanks for the recommendations! </p>\n\n<p>@Chippy, yes the raster countour feature won't work the way we need. I was thinking of using a KNN model to define regions. I still think that is viable, but I'm going to try rgrass7. I have a feeling that it will be very useful for future projects.</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "160035": "Do you have suggestions on simplifying the WKT polygon complexity in R?\n\nI am using raster::rasterToPolygons to create merged polygons. However this package takes a long time to build the polygons, and they are overly complex. I'm thinking of using a package like EBimage to erode and dilate the mask before creating the polygons. I wonder if there is a better method like the [contour method used by lopuhin][1].\n\nThis is the code I am currently using to create WKT submissions.\n\n\n    library(raster) # Read and plot TIFF files, work with raster & polygons\n    library(rgeos) # Read and convert WKT to spatial polygons\n    \n    # make raster mask of class\n    class_mask <- raster(ifelse(class_pred_matrix == im_class, 1, 0))\n    extent(class_mask) <- img_extent\n    \n    # make polygons from mask\n    class_poly <- rasterToPolygons(class_mask, dissolve = TRUE)\n    \n    # add to WKT results\n    class_wkt <- c(class_wkt, writeWKT(class_poly))\n\n\n  [1]: https://www.kaggle.com/lopuhin/dstl-satellite-imagery-feature-detection/full-pipeline-demo-poly-pixels-ml-poly",
    "160086": "I'm using a very similar approach and also finding rasterToPolygons too slow. I guess you have read the search engine responses which say use R to make an external python call to rgdal...  \n\n[https://johnbaumgartner.wordpress.com/2012/07/26/getting-rasters-into-shape-from-r/][1]\n\nI'm leaving that as a last resort. Instead I've taken to sieving the raster to remove small areas prior to converting to applying rasterToPolygons. \n\n[http://gis.stackexchange.com/questions/130993/remove-clumps-of-pixels-in-r][2]\n\nCan't say that it makes it much better, but it's early days for me. \n\nThinking about the contour approach the rasterToContour function may be if use, it's certainly much quicker and outputs a spatialLinesDataFrame. Should then be able to convert to spatialpolygons somehow...\n\n[http://stackoverflow.com/questions/14379828/how-does-one-turn-contour-lines-into-filled-contours][3]\n\nIf you get something that works it will certainly get my Kernel vote!\n\n\n  [1]: https://johnbaumgartner.wordpress.com/2012/07/26/getting-rasters-into-shape-from-r/\n  [2]: http://gis.stackexchange.com/questions/130993/remove-clumps-of-pixels-in-r\n  [3]: http://stackoverflow.com/questions/14379828/how-does-one-turn-contour-lines-into-filled-contours",
    "160273": "Alternative ways for converting raster to polygons are using [RQGIS][1] or the [rgrass7][2]/[spgrass6][3] packages. \n\nWith RQGIS, which is an interface to QGIS, one could use \"gdalogr:polygonize\".\n\nWith either rgrass7 (interface to GRASS GIS 7) or spgrass6 (for GRASS GIS 6), one could use the `execGRASS` command to execute `r.to.vect` to produce an output of type \"area\". This is the approach I'm following and I've found it quite fast.\n\nHope this helps\n\n\n  [1]: https://github.com/jannes-m/RQGIS\n  [2]: https://cran.r-project.org/package=rgrass7\n  [3]: https://cran.r-project.org/package=spgrass6",
    "160294": "Thanks amsantac. Every day brings a suprise... \n\nI've used GRASS and QGIS for many years when needing to perform heavier GIS lifting than R is suited for but I never knew that there are now packages to interface to them directly from R. I'll be giving them a go as contours to polygons looks messy.",
    "160322": "Thanks for the recommendations! \n\n@Chippy, yes the raster countour feature won't work the way we need. I was thinking of using a KNN model to define regions. I still think that is viable, but I'm going to try rgrass7. I have a feeling that it will be very useful for future projects."
  },
  "source": "meta"
}