Menu
  • About us
    • Company
    • Team
    • Career
    • References
    • Partners
    • Contact
  • Products
    • Earth Observation metadata enhancer
    • Temperaturdaten
    • LST-Data Client
    • Trainings
  • Services
    • actinia – geoprocessing in the cloud
    • Geospatial and EO data analysis
    • CORONA spy satellite data
    • maps.mundialis
    • Web Map Services
    • GIS Development
    • Open Source GIS
  • Markets
    • Remote Sensing for Agriculture
    • Satellite Images for Forest and Land Cover Restoration
    • Glass fibre route planning – FTTH
    • Copernicus and Sentinel
  • News & Blog
    • News
    • Blog
    • Satellite image of the month
  • English English
  • Deutsch Deutsch
  • About us
    • Company
    • Team
    • Career
    • References
    • Partners
    • Contact
  • Products
    • Earth Observation metadata enhancer
    • Temperaturdaten
    • LST-Data Client
    • Trainings
  • Services
    • actinia – geoprocessing in the cloud
    • Geospatial and EO data analysis
    • CORONA spy satellite data
    • maps.mundialis
    • Web Map Services
    • GIS Development
    • Open Source GIS
  • Markets
    • Remote Sensing for Agriculture
    • Satellite Images for Forest and Land Cover Restoration
    • Glass fibre route planning – FTTH
    • Copernicus and Sentinel
  • News & Blog
    • News
    • Blog
    • Satellite image of the month
  • English English
  • Deutsch Deutsch

Filter raster files by bounding box using GDAL


29. June 2017 | Category blog

Often it is not necessary to process all available raster maps, but just a subset defined by a geographical region. Most of the time the regional extend is defined by a rectangular bounding box with a X- and Y-min/-max value. So how to filter all the raster files in a convenient and efficient way?

finding raster files

As a first step, we need to get an overview which files are available. Bash has the build-in function find to easily obtain a list of all the, e.g. TIFF, files in your directory.

Shell
1
find /path/to/file -type f -name "*.tif" > list_of_raster.txt

filter by bounding box using gdalbuiltvrt

Now that we got the list, next we want to filter it. GDAL offers the program gdalbuiltvrt, which “builds a VRT (Virtual Dataset) that is a mosaic of the list of input GDAL datasets“. This function is fast and moreover the VRT-file requires nearly no space on your hard drive. The option -te sets the extent of the VRT as the minimum bounding box specified by the X- and Y-coordinates. A command-line with the bounding box of North Rhine Westphalia would look like this:

Shell
1
gdalbuildvrt -te 5.8887,50.3332,9.4702,52.5212 output.vrt -input_file_list list_of_raster.txt

GDAL Python functions

In the next step, we need to use the GDAL functions modified for Python. The actual list of raster files within the bounding box is saved in the VRT and the Virtual Dataset needs to be read out with the Python command GetFileList(). To avoid also printing out “output.vrt“ as part of the list, we skip the first entry by added [1:] after filelist.

Python
1
2
3
4
5
6
7
8
dataset = gdal.Open("output.vrt", GA_ReadOnly)
filelist = dataset.GetFileList()
 
#writes it to a new file
with open(dir + "/vrt_list.txt", 'w+') as f:
    for line in filelist[1:]:
        f.write(str(line) +"\n")
f.close()

The text-file created in this way can be easily used, for example by executing the while-loop in Bash:

Shell
1
2
3
while read line; do
    #do something here
done < vrt_list.txt

The script can be found on GitHub and is available for general usage.

Tags: bounding box, GDAL, python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Follow Us

Contact

mundialis GmbH & Co. KG
Kölnstrasse 99
53111 Bonn

Phone: +49 (0)228 / 387 580 80
Fax: +49 (0)228 / 962 899 57

E-Mail: info [at] mundialis [dot] de

Latest News

  • ++ Eine neue Heimat für Actinia ++

    Tuesday January 24th, 2023
  • Positive towards the future – Management changes at mundialis

    Tuesday October 4th, 2022
  • Recap of FOSS4G 2022 in Florence

    Friday September 16th, 2022
  • FOSS4G 2022 in “La Bella” – Greetings from Florence!

    Thursday July 14th, 2022
  • VALE project successfully completed

    Tuesday June 21st, 2022
View all News

Blog

  • Satellite image of the month – January – Yalu River (People’s Republic of China and Democratic People’s Republic of Korea) 2. January 2023
    Yalu River – People’s Republic of China and Democratic People’s ...
  • Satellite image of the month – December – Super Pit gold mine (Australia) 1. December 2022
    Super Pit gold mine – Australia, recorded by the Sentinel-2A ...
  • Satellite image of the month – November – Kufra-Oases (Libya) 1. November 2022
    Kufra-Oases – Libya, recorded by the Sentinel-2A satellite on March ...
View all Blog Posts

Copyright © 2015-2021 mundialis GmbH & Co. KG

Imprint Privacy

Theme created by PWT. Powered by WordPress.org