1 | #!/usr/bin/env python |
---|
2 | #****************************************************************************** |
---|
3 | # $Id: gdal_sieve.py 18306 2009-12-15 18:57:11Z rouault $ |
---|
4 | # |
---|
5 | # Project: GDAL Python Interface |
---|
6 | # Purpose: Application for applying sieve filter to raster data. |
---|
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 Usage(): |
---|
41 | print(""" |
---|
42 | gdal_sieve [-q] [-st threshold] [-4] [-8] [-o name=value] |
---|
43 | srcfile [-nomask] [-mask filename] [-of format] [dstfile] |
---|
44 | """) |
---|
45 | sys.exit(1) |
---|
46 | |
---|
47 | # ============================================================================= |
---|
48 | # Mainline |
---|
49 | # ============================================================================= |
---|
50 | |
---|
51 | threshold = 2 |
---|
52 | connectedness = 4 |
---|
53 | options = [] |
---|
54 | quiet_flag = 0 |
---|
55 | src_filename = None |
---|
56 | |
---|
57 | dst_filename = None |
---|
58 | format = 'GTiff' |
---|
59 | |
---|
60 | mask = 'default' |
---|
61 | |
---|
62 | gdal.AllRegister() |
---|
63 | argv = gdal.GeneralCmdLineProcessor( sys.argv ) |
---|
64 | if argv is None: |
---|
65 | sys.exit( 0 ) |
---|
66 | |
---|
67 | # Parse command line arguments. |
---|
68 | i = 1 |
---|
69 | while i < len(argv): |
---|
70 | arg = argv[i] |
---|
71 | |
---|
72 | if arg == '-of': |
---|
73 | i = i + 1 |
---|
74 | format = argv[i] |
---|
75 | |
---|
76 | elif arg == '-4': |
---|
77 | connectedness = 4 |
---|
78 | |
---|
79 | elif arg == '-8': |
---|
80 | connectedness = 8 |
---|
81 | |
---|
82 | elif arg == '-q' or arg == '-quiet': |
---|
83 | quiet_flag = 1 |
---|
84 | |
---|
85 | elif arg == '-st': |
---|
86 | i = i + 1 |
---|
87 | threshold = int(argv[i]) |
---|
88 | |
---|
89 | elif arg == '-nomask': |
---|
90 | mask = 'none' |
---|
91 | |
---|
92 | elif arg == '-mask': |
---|
93 | i = i + 1 |
---|
94 | mask = argv[i] |
---|
95 | |
---|
96 | elif arg == '-mask': |
---|
97 | i = i + 1 |
---|
98 | mask = argv[i] |
---|
99 | |
---|
100 | elif arg[:2] == '-h': |
---|
101 | Usage() |
---|
102 | |
---|
103 | elif src_filename is None: |
---|
104 | src_filename = argv[i] |
---|
105 | |
---|
106 | elif dst_filename is None: |
---|
107 | dst_filename = argv[i] |
---|
108 | |
---|
109 | else: |
---|
110 | Usage() |
---|
111 | |
---|
112 | i = i + 1 |
---|
113 | |
---|
114 | if src_filename is None: |
---|
115 | Usage() |
---|
116 | |
---|
117 | # ============================================================================= |
---|
118 | # Verify we have next gen bindings with the sievefilter method. |
---|
119 | # ============================================================================= |
---|
120 | try: |
---|
121 | gdal.SieveFilter |
---|
122 | except: |
---|
123 | print('') |
---|
124 | print('gdal.SieveFilter() not available. You are likely using "old gen"') |
---|
125 | print('bindings or an older version of the next gen bindings.') |
---|
126 | print('') |
---|
127 | sys.exit(1) |
---|
128 | |
---|
129 | # ============================================================================= |
---|
130 | # Open source file |
---|
131 | # ============================================================================= |
---|
132 | |
---|
133 | if dst_filename is None: |
---|
134 | src_ds = gdal.Open( src_filename, gdal.GA_Update ) |
---|
135 | else: |
---|
136 | src_ds = gdal.Open( src_filename, gdal.GA_ReadOnly ) |
---|
137 | |
---|
138 | if src_ds is None: |
---|
139 | print('Unable to open ', src_filename) |
---|
140 | sys.exit(1) |
---|
141 | |
---|
142 | srcband = src_ds.GetRasterBand(1) |
---|
143 | |
---|
144 | if mask is 'default': |
---|
145 | maskband = srcband.GetMaskBand() |
---|
146 | elif mask is 'none': |
---|
147 | maskband = None |
---|
148 | else: |
---|
149 | mask_ds = gdal.Open( mask ) |
---|
150 | maskband = mask_ds.GetRasterBand(1) |
---|
151 | |
---|
152 | # ============================================================================= |
---|
153 | # Create output file if one is specified. |
---|
154 | # ============================================================================= |
---|
155 | |
---|
156 | if dst_filename is not None: |
---|
157 | |
---|
158 | drv = gdal.GetDriverByName(format) |
---|
159 | dst_ds = drv.Create( dst_filename,src_ds.RasterXSize, src_ds.RasterYSize,1, |
---|
160 | srcband.DataType ) |
---|
161 | wkt = src_ds.GetProjection() |
---|
162 | if wkt != '': |
---|
163 | dst_ds.SetProjection( wkt ) |
---|
164 | dst_ds.SetGeoTransform( src_ds.GetGeoTransform() ) |
---|
165 | |
---|
166 | dstband = dst_ds.GetRasterBand(1) |
---|
167 | else: |
---|
168 | dstband = srcband |
---|
169 | |
---|
170 | # ============================================================================= |
---|
171 | # Invoke algorithm. |
---|
172 | # ============================================================================= |
---|
173 | |
---|
174 | if quiet_flag: |
---|
175 | prog_func = None |
---|
176 | else: |
---|
177 | prog_func = gdal.TermProgress |
---|
178 | |
---|
179 | result = gdal.SieveFilter( srcband, maskband, dstband, |
---|
180 | threshold, connectedness, |
---|
181 | callback = prog_func ) |
---|
182 | |
---|
183 | |
---|
184 | |
---|
185 | |
---|
186 | |
---|
187 | |
---|
188 | |
---|
189 | |
---|
190 | |
---|
191 | |
---|