[GIS] Calculate buffer / multi-ring buffer of geometry with GDAL / gdalUtils

bashbuffergdalgdalutilsr

I wonder if there is any way to calculate a buffer on a spatial data set with basic command line GDAL or the R interface gdalUtils.

To specify the Q: I have a lot of points around the globe. For these points I have to calculate (multiring) buffers. Since this should take place in an automated framework implemented in a mix of R and bash, I wonder if there is any way to use the power of GDAL/OGR?

I found this geometry class during my research on this problem. However, I am not sure if this can be part of the solution.

For test purposes, let's assume the point is lon/lat (EPSG:4326): 0, 0.

Best Answer

A multi-ring buffer can be made using ogr2ogr with the SQLite dialect. The trick consists into making the difference between buffers and then uniting the inner buffer. Here's an example with two buffer respectively of 5 and 10 map units:

ogr2ogr -sql "select ST_Difference(ST_Buffer(geometry, 10), ST_Buffer(geometry, 5)) from my_shape UNION select ST_Buffer(geometry, 5) from my_shape" -dialect SQLite multibuffered.shp my_shape.shp

A dynamic R solution:

Since, OP was also about a usage in R, this approach could also be used in R's gdalUtils function in an automated way. This example creates a multiring buffer of rings from 1 km to 100 km (by 1 km).

bufferrings <- seq(1000, 100000, by = 1000)
sqlStatement <- paste(
                  paste("select ST_Difference(ST_Buffer(geometry,", 
                         bufferrings[2:length(bufferrings)], 
                         "), ST_Buffer(geometry,", bufferrings[1:(length(bufferrings) - 1)], 
                         ")) from tmp_rp UNION", collapse = " "), 
                  "select ST_Buffer(geometry,", bufferrings[1], ") from tmp_rp", 
                 collapse = " ")

ogr2ogr(src_datasource_name = "tmp/tmp_rp.shp", 
        dst_datasource_name = "tmp/multibuffer_100km.shp",
        sql = sqlStatement, 
        dialect = "SQLite"
        )
Related Question