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 | |
---|
31 | try: |
---|
32 | from osgeo import gdal, ogr |
---|
33 | except ImportError: |
---|
34 | import gdal |
---|
35 | import ogr |
---|
36 | |
---|
37 | import sys |
---|
38 | import os.path |
---|
39 | |
---|
40 | def 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 | |
---|
46 | def Usage(): |
---|
47 | print(""" |
---|
48 | gdal_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 | |
---|
58 | max_distance = 100 |
---|
59 | smoothing_iterations = 0 |
---|
60 | options = [] |
---|
61 | quiet_flag = 0 |
---|
62 | src_filename = None |
---|
63 | src_band = 1 |
---|
64 | |
---|
65 | dst_filename = None |
---|
66 | format = 'GTiff' |
---|
67 | |
---|
68 | mask = 'default' |
---|
69 | |
---|
70 | gdal.AllRegister() |
---|
71 | argv = gdal.GeneralCmdLineProcessor( sys.argv ) |
---|
72 | if argv is None: |
---|
73 | sys.exit( 0 ) |
---|
74 | |
---|
75 | # Parse command line arguments. |
---|
76 | i = 1 |
---|
77 | while 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 | |
---|
124 | if src_filename is None: |
---|
125 | Usage() |
---|
126 | |
---|
127 | # ============================================================================= |
---|
128 | # Verify we have next gen bindings with the sievefilter method. |
---|
129 | # ============================================================================= |
---|
130 | try: |
---|
131 | gdal.FillNodata |
---|
132 | except: |
---|
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 | |
---|
143 | if dst_filename is None: |
---|
144 | src_ds = gdal.Open( src_filename, gdal.GA_Update ) |
---|
145 | else: |
---|
146 | src_ds = gdal.Open( src_filename, gdal.GA_ReadOnly ) |
---|
147 | |
---|
148 | if src_ds is None: |
---|
149 | print('Unable to open ', src_filename) |
---|
150 | sys.exit(1) |
---|
151 | |
---|
152 | srcband = src_ds.GetRasterBand(src_band) |
---|
153 | |
---|
154 | if mask is 'default': |
---|
155 | maskband = srcband.GetMaskBand() |
---|
156 | elif mask is 'none': |
---|
157 | maskband = None |
---|
158 | else: |
---|
159 | mask_ds = gdal.Open( mask ) |
---|
160 | maskband = mask_ds.GetRasterBand(1) |
---|
161 | |
---|
162 | # ============================================================================= |
---|
163 | # Create output file if one is specified. |
---|
164 | # ============================================================================= |
---|
165 | |
---|
166 | if 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 | |
---|
179 | else: |
---|
180 | dstband = srcband |
---|
181 | |
---|
182 | # ============================================================================= |
---|
183 | # Invoke algorithm. |
---|
184 | # ============================================================================= |
---|
185 | |
---|
186 | if quiet_flag: |
---|
187 | prog_func = None |
---|
188 | else: |
---|
189 | prog_func = gdal.TermProgress |
---|
190 | |
---|
191 | result = gdal.FillNodata( dstband, maskband, |
---|
192 | max_distance, smoothing_iterations, options, |
---|
193 | callback = prog_func ) |
---|
194 | |
---|
195 | |
---|
196 | |
---|
197 | |
---|
198 | |
---|
199 | |
---|