[gdal-dev] Is there an easy way to clip an image to the realdata?

lars.schylberg at blixtmail.se lars.schylberg at blixtmail.se
Thu Jun 27 07:30:57 PDT 2019

Hi Steve,

Sorry for the late reply!

I have some scripts that I developed this winter that contains a function
to clip an image or images to the parts that only contains data.

My aim was to create tighter tile indexes for Mapserver.  
I achieved good results for my use case.  

This will be one of the topics that I will present in my talk at 
FOSS4G in Bucharest in August.  I haven't yet prepared the details how to 
explain what I did.  But here we go:

1) First I retile the data to suitable size,
2) then check for empty tiles that I throw away.

The function "trim_tile" is actually what You are asking for, I think.

What I do in the function trim_tile is to use:

1) gdal_calc to produce a binary image where I have data
2) polygonize where data exists
3) compare extents
4) clip data with gdal_translate with projwin argument, if polygon extent is smaller

All is done in a bash script, since it is my preferred language to do things fast.
I use gnu parallel to speed things up. In my case I had 16 cpus in the server.
Some versions of parallel requires the argument "--will-cite", others don't. 

I made my own bash version of retiling that keeps masks.

Further notes is that the script is working on paletted tif 8 bit image.
I have a version for rbg images also.  The main difference is that the
empty image test looks like this in that case:

STDDEV=$(gdalinfo $tile -stats | \
	sed 's/STATISTICS_STDDEV=//g' | \
	awk '{$1=$1;printf "%d", $1}')
	if [ "$STDDEV" = "000" ]; then
		/bin/rm $tile
If You have tagged nodata values, those have to be removed with gdal_edit.py 
before You run statistics and then reaplied afterwards with gdal_edit again.	

This might hopefully give some inspiration for you how to tackle your 
use case.

The full script can be found here:  


Have fun

Lars Schylberg

20 juni 2019 kl. 22:06, "Stephen Woodbridge" <stephenwoodbridge37 at gmail.com> skrev:

> Hi,
> I'm working with VIIRS L3U images of L2P (level 2 patches). The L3U data 
> is gridded to the +-180 x +-90 but the patch only fills a small 
> percentage of that. I'm compositing a the patch for a day using a vrt.
> Performance is an issue because all the patches cover the whole globe 
> instead of the extents of the patch.
> Is there a way to automatically clip an image to the extents of the 
> patch, where all the rest of the image it filled with nodata? IE. remove 
> all rows and columns from the exterior edges that are all nodata and 
> stop when you hit real data.
> -Steve
> ---
> This email has been checked for viruses by Avast antivirus software.
> https://www.avast.com/antivirus
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> https://lists.osgeo.org/mailman/listinfo/gdal-dev

More information about the gdal-dev mailing list