[gdal-dev] how to create just the msk file from a rgba vrt file

Even Rouault even.rouault at mines-paris.org
Sun Feb 9 01:55:49 PST 2014


Le vendredi 07 février 2014 10:55:06, Duarte Carreira a écrit :
> Thanks Brian.
> 
> But this way you rewrite the whole image to disk. It uses lots of disk
> space and takes forever.
> 
> I want to avoid that and just get the mask out to a msk file as fast as
> possible.
> 
> I don't want to convert my rgba vrt mosaic.
> 
> The final objective is to get a msk file I can use with the simple rgb vrt
> mosaic. Then I'll be able to build overviews with jpeg/ycbcr compression
> which I can't do with rgba vrt because of the 4 bands.
> 
> For now I have tried 3 ways:
> 
> 1) use gdal_rasterize to create a mask directly from the mask polygon
> shapefile. Then just edit the rgb vrt mosaic and add a  maskband to it.
> The problem here is gdaladdo does not honor the maskband. This is the
> fastest way I know, pitty it doesn't work in the end.

I'd be curious that you provide ways of reproducing this. The overview 
computation code has explicit code to deal with mask bands.

> 
> 2) use gdal_translate like you suggested but use -scale to write all 0s in
> all 3 bands, and compress with deflate. You get a valid mask and a very,
> very small useless mosaic. This works but takes a while still.
> 
> 3) use gdal_translate like you suggested but exaggerate the jpeg
> compression so it errors out (jpeg_quality=15). You get an invalid 1kb
> mosaic and an apparently good msk. But it's corrupted in some way. So
> doesn't work.
> 
> I think #1 has potential. If there was a way to somehow turn the tif
> created by gdal_rasterize into a "true" mask file and have it honored by
> gdaladdo we would have a winner.
> 
> Maybe there's a way to directly export the alpha band from the rgba vrt
> mosaic to a mks file without writing anything else? That I guess would be
> the fastest way of all.

You can generate a valid .msk file with the following gdal_translate command :

gdal_translate -b 4 rgba.tif out.tif.msk \
-mo "INTERNAL_MASK_FLAGS_1=2" -mo "INTERNAL_MASK_FLAGS_2=2" \
-mo "INTERNAL_MASK_FLAGS_3=2" -co COMPRESS=DEFLATE -co INTERLEAVE=BAND \
-co NBITS=1

You need to provide as many -mo "INTERNAL_MASK_FLAGS_X=2" option as there are 
bands (so 3 for a RGB dataset as in the above example). This is the important 
option that will make a .msk file being recognized as a mask band.

Even

-- 
Geospatial professional services
http://even.rouault.free.fr/services.html


More information about the gdal-dev mailing list