source: branches/0.5/win/PostNAS-0.5/bin/gdal_fillnodata.py @ 23

Revision 23, 5.6 KB checked in by astrid.emde, 14 years ago (diff)
Line 
1#!/usr/bin/env python
2#******************************************************************************
3#  $Id: gdal_sieve.py 15700 2008-11-10 15:29:13Z warmerdam $
4#
5#  Project:  GDAL Python Interface
6#  Purpose:  Application for filling nodata areas in a raster by interpolation
7#  Author:   Frank Warmerdam, warmerdam@pobox.com
8#
9#******************************************************************************
10#  Copyright (c) 2008, Frank Warmerdam
11#
12#  Permission is hereby granted, free of charge, to any person obtaining a
13#  copy of this software and associated documentation files (the "Software"),
14#  to deal in the Software without restriction, including without limitation
15#  the rights to use, copy, modify, merge, publish, distribute, sublicense,
16#  and/or sell copies of the Software, and to permit persons to whom the
17#  Software is furnished to do so, subject to the following conditions:
18#
19#  The above copyright notice and this permission notice shall be included
20#  in all copies or substantial portions of the Software.
21#
22#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23#  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25#  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27#  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28#  DEALINGS IN THE SOFTWARE.
29#******************************************************************************
30
31try:
32    from osgeo import gdal, ogr
33except ImportError:
34    import gdal
35    import ogr
36
37import sys
38import os.path
39
40def CopyBand( srcband, dstband ):
41    for line in range(srcband.YSize):
42        line_data = srcband.ReadRaster( 0, line, srcband.XSize, 1 )
43        dstband.WriteRaster( 0, line, srcband.XSize, 1, line_data,
44                             buf_type = srcband.DataType )
45       
46def Usage():
47    print("""
48gdal_nodatafill [-q] [-md max_distance] [-si smooth_iterations]
49                [-o name=value] [-b band]
50                srcfile [-nomask] [-mask filename] [-of format] [dstfile]
51""")
52    sys.exit(1)
53   
54# =============================================================================
55#       Mainline
56# =============================================================================
57
58max_distance = 100
59smoothing_iterations = 0
60options = []
61quiet_flag = 0
62src_filename = None
63src_band = 1
64
65dst_filename = None
66format = 'GTiff'
67
68mask = 'default'
69
70gdal.AllRegister()
71argv = gdal.GeneralCmdLineProcessor( sys.argv )
72if argv is None:
73    sys.exit( 0 )
74
75# Parse command line arguments.
76i = 1
77while i < len(argv):
78    arg = argv[i]
79
80    if arg == '-of':
81        i = i + 1
82        format = argv[i]
83
84    elif arg == '-q' or arg == '-quiet':
85        quiet_flag = 1
86       
87    elif arg == '-si':
88        i = i + 1
89        smoothing_iterations = int(argv[i])
90       
91    elif arg == '-b':
92        i = i + 1
93        src_band = int(argv[i])
94       
95    elif arg == '-md':
96        i = i + 1
97        max_distance = float(argv[i])
98       
99    elif arg == '-nomask':
100        mask = 'none'
101       
102    elif arg == '-mask':
103        i = i + 1
104        mask = argv[i]
105       
106    elif arg == '-mask':
107        i = i + 1
108        mask = argv[i]
109       
110    elif arg[:2] == '-h':
111        Usage()
112       
113    elif src_filename is None:
114        src_filename = argv[i]
115
116    elif dst_filename is None:
117        dst_filename = argv[i]
118
119    else:
120        Usage()
121
122    i = i + 1
123
124if src_filename is None:
125    Usage()
126   
127# =============================================================================
128#       Verify we have next gen bindings with the sievefilter method.
129# =============================================================================
130try:
131    gdal.FillNodata
132except:
133    print('')
134    print('gdal.FillNodata() not available.  You are likely using "old gen"')
135    print('bindings or an older version of the next gen bindings.')
136    print('')
137    sys.exit(1)
138
139# =============================================================================
140#       Open source file
141# =============================================================================
142
143if dst_filename is None:
144    src_ds = gdal.Open( src_filename, gdal.GA_Update )
145else:
146    src_ds = gdal.Open( src_filename, gdal.GA_ReadOnly )
147   
148if src_ds is None:
149    print('Unable to open ', src_filename)
150    sys.exit(1)
151
152srcband = src_ds.GetRasterBand(src_band)
153
154if mask is 'default':
155    maskband = srcband.GetMaskBand()
156elif mask is 'none':
157    maskband = None
158else:
159    mask_ds = gdal.Open( mask )
160    maskband = mask_ds.GetRasterBand(1)
161
162# =============================================================================
163#       Create output file if one is specified.
164# =============================================================================
165
166if dst_filename is not None:
167
168    drv = gdal.GetDriverByName(format)
169    dst_ds = drv.Create( dst_filename,src_ds.RasterXSize, src_ds.RasterYSize,1,
170                         srcband.DataType )
171    wkt = src_ds.GetProjection()
172    if wkt != '':
173        dst_ds.SetProjection( wkt )
174    dst_ds.SetGeoTransform( src_ds.GetGeoTransform() )
175   
176    dstband = dst_ds.GetRasterBand(1)
177    CopyBand( srcband, dstband )
178   
179else:
180    dstband = srcband
181
182# =============================================================================
183#       Invoke algorithm.
184# =============================================================================
185
186if quiet_flag:
187    prog_func = None
188else:
189    prog_func = gdal.TermProgress
190   
191result = gdal.FillNodata( dstband, maskband,
192                          max_distance, smoothing_iterations, options,
193                          callback = prog_func )
194
195
196
197
198
199
Note: See TracBrowser for help on using the repository browser.