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.

  • can we make square buffer using ogr2ogr -f "ESRI Shapefile" -dialect SQLite -sql "select ST_Buffer(geometry, 10) from my_shape" buffered.shp my_shape.shp – Neeraj Singh Jul 24 '18 at 8:37

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,", 
                         "), 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"
  • 1
    Thanks so much. Solved it! I added a small R example how use it in R. Since OP was also asking about R, future users will be interested into that, too. – loki Nov 17 '17 at 8:38
  • 1
    +1 Very useful the sqlStatement concatenation with paste, thank you. – Antonio Falciano Nov 17 '17 at 10:09

Use ogr2ogr http://www.gdal.org/ogr2ogr.html with the SQLite SQL dialect http://www.gdal.org/ogr_sql_sqlite.html. Then you can use all the SpatiaLite functions http://www.gaia-gis.it/gaia-sins/spatialite-sql-latest.html including ST_Buffer.

Usage example:

ogr2ogr -f "ESRI Shapefile" -dialect SQLite -sql "select ST_Buffer(geometry, 10) from my_shape" buffered.shp my_shape.shp

If your data are in PostGIS the native PostGIS functions can be used directly and you do not need to select the SQLite dialect. You can find many examples about using ogr2ogr with SQL from old questions.

  • Do you have any ideas how to calculate a multi-ring-buffer? Perhaps this specific Q could be extended to "Is it possible to use (postgis) functions in the approach you showed here?". – loki Nov 16 '17 at 17:49

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.