I have a set of polygons in PostGIS DB.

I also have a set of GeoTIFF files.

I would like to know what is the best way (using GDAL) to get one polygon from DB (based on SQL Query) and then take all TIFF files that intersect with that polygon, clip those that need to be cut and merge result into one TIFF file.

I found method here: Merging GDAL tiles and crop via bounding box?

So I have that VRT file already but I don't know how to get that KML file or maybe I can simply replace is somehow with SQL Query to PostGIS to take that clipper?

1 Answer 1


One solution involving PostGIS and GDAL would be to:

1) Load the rasters into PostGIS using raster2pgsql

2) Clip them with ST_Clip()

3) Merge clipped rasters with ST_Union()

4) Export the result to the filesystem as a new raster with gdal_translate

You can do 1) and 2) as one single ST_Union(ST_Clip()) query.

  • Thanks, I'm using command from here: postgis.refractions.net/docs/… raster2pgsql -s 25832 -I -C -M *.tif -F public.rasters > rasters.sql I have 12 tif files each one ~3MB and the result SQL is 1,5 GB big :) So I assume that I'm doing something wrong. Do you have any suggestions?
    – Witos
    Feb 25, 2019 at 12:09
  • You have to pipe the result directly to psql like it is explained a bit further. The file is big because it is an uncompressed text file. If you pipe it you won't notice. Feb 25, 2019 at 14:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.