~ubuntu-branches/debian/sid/gdal/sid

« back to all changes in this revision

Viewing changes to swig/python/scripts/gdal_merge.py

  • Committer: Package Import Robot
  • Author(s): Francesco Paolo Lovergine
  • Date: 2012-05-07 15:04:42 UTC
  • mfrom: (5.5.16 experimental)
  • Revision ID: package-import@ubuntu.com-20120507150442-2eks97loeh6rq005
Tags: 1.9.0-1
* Ready for sid, starting transition.
* All symfiles updated to latest builds.
* Added dh_numpy call in debian/rules to depend on numpy ABI.
* Policy bumped to 3.9.3, no changes required.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/env python
2
2
###############################################################################
3
 
# $Id: gdal_merge.py 18953 2010-02-28 12:00:54Z rouault $
 
3
# $Id: gdal_merge.py 22325 2011-05-07 19:50:58Z rouault $
4
4
#
5
5
# Project:  InSAR Peppers
6
6
# Purpose:  Module to extract data from many rasters into one output.
24
24
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25
25
# Boston, MA 02111-1307, USA.
26
26
###############################################################################
 
27
# changes 29Apr2011
 
28
# if the input image is a multi-band one, use all 
 
29
# the channels in building the stack
 
30
# anssi.pekkarinen@fao.org
 
31
 
27
32
 
28
33
try:
29
34
    from osgeo import gdal
30
 
    gdal.TermProgress = gdal.TermProgress_nocb
31
35
except ImportError:
32
36
    import gdal
33
37
 
 
38
try:
 
39
    progress = gdal.TermProgress_nocb
 
40
except:
 
41
    progress = gdal.TermProgress
 
42
 
 
43
 
34
44
import sys
35
45
import glob
 
46
import math
36
47
 
37
48
__version__ = '$id$'[5:-1]
38
49
verbose = 0
59
70
    t_band = t_fh.GetRasterBand( t_band_n )
60
71
 
61
72
    data = s_band.ReadRaster( s_xoff, s_yoff, s_xsize, s_ysize,
62
 
                              t_xsize, t_ysize, t_band.DataType )
 
73
                             t_xsize, t_ysize, t_band.DataType )
63
74
    t_band.WriteRaster( t_xoff, t_yoff, t_xsize, t_ysize,
64
75
                        data, t_xsize, t_ysize, t_band.DataType )
65
76
        
89
100
 
90
101
    nodata_test = Numeric.equal(data_src,nodata)
91
102
    to_write = Numeric.choose( nodata_test, (data_src, data_dst) )
92
 
                               
 
103
 
93
104
    t_band.WriteArray( to_write, t_xoff, t_yoff )
94
105
 
95
106
    return 0
234
245
# =============================================================================
235
246
def Usage():
236
247
    print('Usage: gdal_merge.py [-o out_filename] [-of out_format] [-co NAME=VALUE]*')
237
 
    print('                     [-ps pixelsize_x pixelsize_y] [-separate] [-q] [-v] [-pct]')
238
 
    print('                     [-ul_lr ulx uly lrx lry] [-n nodata_value] [-init "value [value...]"]')
 
248
    print('                     [-ps pixelsize_x pixelsize_y] [-tap] [-separate] [-q] [-v] [-pct]')
 
249
    print('                     [-ul_lr ulx uly lrx lry] [-init "value [value...]"]')
 
250
    print('                     [-n nodata_value] [-a_nodata output_nodata_value]')
239
251
    print('                     [-ot datatype] [-createonly] input_files')
240
252
    print('                     [--help-general]')
241
253
    print('')
259
271
    separate = 0
260
272
    copy_pct = 0
261
273
    nodata = None
 
274
    a_nodata = None
262
275
    create_options = []
263
276
    pre_init = []
264
277
    band_type = None
265
278
    createonly = 0
 
279
    bTargetAlignedPixels = False
266
280
    
267
281
    gdal.AllRegister()
268
282
    if argv is None:
315
329
            i = i + 1
316
330
            nodata = float(argv[i])
317
331
 
 
332
        elif arg == '-a_nodata':
 
333
            i = i + 1
 
334
            a_nodata = float(argv[i])
 
335
 
318
336
        elif arg == '-f':
319
337
            # for backward compatibility.
320
338
            i = i + 1
333
351
            psize_y = -1 * abs(float(argv[i+2]))
334
352
            i = i + 2
335
353
 
 
354
        elif arg == '-tap':
 
355
            bTargetAlignedPixels = True
 
356
 
336
357
        elif arg == '-ul_lr':
337
358
            ulx = float(argv[i+1])
338
359
            uly = float(argv[i+2])
397
418
    
398
419
    # Create output file if it does not already exist.
399
420
    if t_fh is None:
 
421
    
 
422
        if bTargetAlignedPixels:
 
423
            ulx = math.floor(ulx / psize_x) * psize_x
 
424
            lrx = math.ceil(lrx / psize_x) * psize_x
 
425
            lry = math.floor(lry / -psize_y) * -psize_y
 
426
            uly = math.ceil(uly / -psize_y) * -psize_y
 
427
    
400
428
        geotransform = [ulx, psize_x, 0, uly, 0, psize_y]
401
429
 
402
430
        xsize = int((lrx - ulx) / geotransform[1] + 0.5)
403
431
        ysize = int((lry - uly) / geotransform[5] + 0.5)
404
432
 
 
433
 
405
434
        if separate != 0:
406
 
            bands = len(file_infos)
 
435
            bands=0
 
436
 
 
437
            for fi in file_infos:
 
438
                bands=bands + fi.bands
407
439
        else:
408
440
            bands = file_infos[0].bands
409
441
 
 
442
 
410
443
        t_fh = Driver.Create( out_file, xsize, ysize, bands,
411
444
                              band_type, create_options )
412
445
        if t_fh is None:
420
453
            t_fh.GetRasterBand(1).SetRasterColorTable(file_infos[0].ct)
421
454
    else:
422
455
        if separate != 0:
423
 
            bands = len(file_infos)
 
456
            bands=0
 
457
            for fi in file_infos:
 
458
                bands=bands + fi.bands            
424
459
            if t_fh.RasterCount < bands :
425
 
                print('Existing output file has less bands than the number of input files. You should delete it before. Terminating gdal_merge.')
 
460
                print('Existing output file has less bands than the input files. You should delete it before. Terminating gdal_merge.')
426
461
                sys.exit( 1 )
427
462
        else:
428
463
            bands = min(file_infos[0].bands,t_fh.RasterCount)
429
464
 
 
465
    # Do we need to set nodata value ?
 
466
    if a_nodata is not None:
 
467
        for i in range(t_fh.RasterCount):
 
468
            t_fh.GetRasterBand(i+1).SetNoDataValue(a_nodata)
 
469
 
430
470
    # Do we need to pre-initialize the whole mosaic file to some value?
431
471
    if pre_init is not None:
432
472
        if t_fh.RasterCount <= len(pre_init):
440
480
    t_band = 1
441
481
 
442
482
    if quiet == 0 and verbose == 0:
443
 
        gdal.TermProgress( 0.0 )
 
483
        progress( 0.0 )
444
484
    fi_processed = 0
445
485
    
446
486
    for fi in file_infos:
458
498
            for band in range(1, bands+1):
459
499
                fi.copy_into( t_fh, band, band, nodata )
460
500
        else:
461
 
            fi.copy_into( t_fh, 1, t_band, nodata )
462
 
            t_band = t_band+1
 
501
            for band in range(1, fi.bands+1):
 
502
                fi.copy_into( t_fh, band, t_band, nodata )
 
503
                t_band = t_band+1
463
504
            
464
505
        fi_processed = fi_processed+1
465
506
        if quiet == 0 and verbose == 0:
466
 
            gdal.TermProgress( fi_processed / float(len(file_infos))  )
 
507
            progress( fi_processed / float(len(file_infos))  )
467
508
    
468
509
    # Force file to be closed.
469
510
    t_fh = None