-1

I want to create a Square Buffer of 10 km around a .vrt (Raster) file in QGIS using Python programming in QGIS Python Console.

Please help me to write this python program for creating buffer around a .vrt file in QGIS.

What I have tried is that I first convert the input .vrt file (raster) into a .shp file (vector).

processing.runalg("gdalogr:polygonize","E:/Sreeraj/Task Global Data CONVERSION/Split/XYZ to UTM/contour1.vrt","DN","E:/Sreeraj/Task Global Data CONVERSION/Split/XYZ to UTM/shapefile.shp") 

And then created a buffer around the .shp file.

processing.runalg("gdalogr:buffervectors","E:/Sreeraj/Task Global Data CONVERSION/Split/XYZ to UTM/shapefile.shp","geometry",10,False,None,False,"","E:/Sreeraj/Task Global Data CONVERSION/Split/XYZ to UTM/buffer.shp")

Then I converted the buffered output .shp file (vector) back to a .vrt file (raster).

processing.runalg("gdalogr:rasterize","E:/Sreeraj/Task Global Data CONVERSION/Split/XYZ to UTM/buffer.shp","ID",0,100,100,"-116.669027778,-116.280972222,34.02625,34.3456944447",False,5,"",4,75,6,1,False,0,"","E:/Sreeraj/Task Global Data CONVERSION/Split/XYZ to UTM/contour2.vrt") 

So, I got a buffer (contour2.vrt) output .vrt file around the input (contour1.vrt) .vrt file.

But, I know that this is a really crazy idea. What I am checking is for a python code which will directly create a buffer around a raster in a single step (as we create a buffer around a vector).

  • What have you tried so far? What was the outcome and how did it differ from what you expected? – Kersten Jan 18 '18 at 14:30
  • This related post may be helpful: gis.stackexchange.com/questions/61512/… – Aaron Jan 24 '18 at 17:30
  • 1
    @Aaron I will check this post. Thank You for your suggestion. – Sreeraj Jan 29 '18 at 9:03
1
from PyQt4.QtCore import *
from qgis.core import *

# Loads Raster.
vrt_path = 'path/to/vrt/file.vrt'
vrt_info = QFileInfo(vrt_path)
vrt_name = vrt_info.baseName()
vrt_layer = QgsRasterLayer(vrt_path, vrt_name)
if not vrt_layer.isValid(): print 'Not valid.'

# Gets bounding box and buffers it with whatever value you pass in offset.
bbox = vrt_layer.extent()
buff_bbox = bbox.buffer(offset)

# Prints coordinates in console.
print buff_bbox.asWktPolygon()

For more information, you can consult the PyQGIS Cookbook, as well as the PyQGIS API.

  • 1
    When I print buff_bbox.asWktPolygon() , I got the output : POLYGON((-126.50624999999999432 24.16125000000000256, -106.44374999999999432 24.16125000000000256, -106.44374999999999432 44.21124999999999972, -126.50624999999999432 44.21124999999999972, -126.50624999999999432 24.16125000000000256)) . But, how can I proceed to generate a buffer around the input .vrt file using this buff_bbox.asWktPolygon() Polygon output coordinates ? – Sreeraj Jan 24 '18 at 12:11
  • @Sreeraj this is the buffer already. A buffer is always a polygon, and these are the polygon's coordinates. – Roberto Ribeiro Jan 24 '18 at 12:17
  • 1
    Ok. I am able to extract the coordinates of the buffer using print buff_bbox.xMinimum(), print buff_bbox.yMinimum(), print buff_bbox.xMaximum(), and print buff_bbox.yMaximum(). Thank You so much. – Sreeraj Jan 24 '18 at 12:25
  • I have a small clarification to be made from you. If I have to create a 10 km buffer, then which value should I give as 'offset' in the above code? When I given offset = 10, I got buff_bbox.xMinimum = -126.50625 (but for the input .vrt file, xMinimum = -116.50625). So, in order to get a 10km buffer, which value should I give as offset ? – Sreeraj Jan 24 '18 at 12:32
  • Finally, I got the required solution. If I give offset = 10, then I am getting a buffer of +- 10 degrees. So, I wrote a mathematical formula relating the buffer km and buffer degrees. Now, I am able to generate the required output. Thank You for your suggestions. – Sreeraj Jan 24 '18 at 13:17

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.