1#!/usr/bin/env python3
2# ******************************************************************************
3#  $Id: gdal_proximity.py 05a98f65afa7e5048cae16a58c2268510cd89103 2021-01-15 16:16:00 +0200 Idan Miara $
4#
5#  Name:     gdalproximity
6#  Project:  GDAL Python Interface
7#  Purpose:  Application for computing raster proximity maps.
8#  Author:   Frank Warmerdam, warmerdam@pobox.com
9#
10# ******************************************************************************
11#  Copyright (c) 2008, Frank Warmerdam
12#  Copyright (c) 2009-2011, Even Rouault <even dot rouault at spatialys.com>
13#
14#  Permission is hereby granted, free of charge, to any person obtaining a
15#  copy of this software and associated documentation files (the "Software"),
16#  to deal in the Software without restriction, including without limitation
17#  the rights to use, copy, modify, merge, publish, distribute, sublicense,
18#  and/or sell copies of the Software, and to permit persons to whom the
19#  Software is furnished to do so, subject to the following conditions:
20#
21#  The above copyright notice and this permission notice shall be included
22#  in all copies or substantial portions of the Software.
23#
24#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25#  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27#  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29#  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30#  DEALINGS IN THE SOFTWARE.
31# ******************************************************************************
32
33import sys
34
35from osgeo import gdal
36from osgeo_utils.auxiliary.util import GetOutputDriverFor
37
38
39def Usage():
40    print("""
41gdal_proximity.py srcfile dstfile [-srcband n] [-dstband n]
42                  [-of format] [-co name=value]*
43                  [-ot Byte/UInt16/UInt32/Float32/etc]
44                  [-values n,n,n] [-distunits PIXEL/GEO]
45                  [-maxdist n] [-nodata n] [-use_input_nodata YES/NO]
46                  [-fixed-buf-val n] [-q] """)
47    return 1
48
49
50def main(argv):
51    frmt = None
52    creation_options = []
53    options = []
54    src_filename = None
55    src_band_n = 1
56    dst_filename = None
57    dst_band_n = 1
58    creation_type = 'Float32'
59    quiet_flag = 0
60
61    argv = gdal.GeneralCmdLineProcessor(argv)
62    if argv is None:
63        return 0
64
65    # Parse command line arguments.
66    i = 1
67    while i < len(argv):
68        arg = argv[i]
69
70        if arg == '-of' or arg == '-f':
71            i = i + 1
72            frmt = argv[i]
73
74        elif arg == '-co':
75            i = i + 1
76            creation_options.append(argv[i])
77
78        elif arg == '-ot':
79            i = i + 1
80            creation_type = argv[i]
81
82        elif arg == '-maxdist':
83            i = i + 1
84            options.append('MAXDIST=' + argv[i])
85
86        elif arg == '-values':
87            i = i + 1
88            options.append('VALUES=' + argv[i])
89
90        elif arg == '-distunits':
91            i = i + 1
92            options.append('DISTUNITS=' + argv[i])
93
94        elif arg == '-nodata':
95            i = i + 1
96            options.append('NODATA=' + argv[i])
97
98        elif arg == '-use_input_nodata':
99            i = i + 1
100            options.append('USE_INPUT_NODATA=' + argv[i])
101
102        elif arg == '-fixed-buf-val':
103            i = i + 1
104            options.append('FIXED_BUF_VAL=' + argv[i])
105
106        elif arg == '-srcband':
107            i = i + 1
108            src_band_n = int(argv[i])
109
110        elif arg == '-dstband':
111            i = i + 1
112            dst_band_n = int(argv[i])
113
114        elif arg == '-q' or arg == '-quiet':
115            quiet_flag = 1
116
117        elif src_filename is None:
118            src_filename = argv[i]
119
120        elif dst_filename is None:
121            dst_filename = argv[i]
122
123        else:
124            return Usage()
125
126        i = i + 1
127
128    if src_filename is None or dst_filename is None:
129        return Usage()
130
131    # =============================================================================
132    #    Open source file
133    # =============================================================================
134
135    src_ds = gdal.Open(src_filename)
136
137    if src_ds is None:
138        print('Unable to open %s' % src_filename)
139        return 1
140
141    srcband = src_ds.GetRasterBand(src_band_n)
142
143    # =============================================================================
144    #       Try opening the destination file as an existing file.
145    # =============================================================================
146
147    try:
148        driver = gdal.IdentifyDriver(dst_filename)
149        if driver is not None:
150            dst_ds = gdal.Open(dst_filename, gdal.GA_Update)
151            dstband = dst_ds.GetRasterBand(dst_band_n)
152        else:
153            dst_ds = None
154    except:
155        dst_ds = None
156
157    # =============================================================================
158    #     Create output file.
159    # =============================================================================
160    if dst_ds is None:
161        if frmt is None:
162            frmt = GetOutputDriverFor(dst_filename)
163
164        drv = gdal.GetDriverByName(frmt)
165        dst_ds = drv.Create(dst_filename,
166                            src_ds.RasterXSize, src_ds.RasterYSize, 1,
167                            gdal.GetDataTypeByName(creation_type), creation_options)
168
169        dst_ds.SetGeoTransform(src_ds.GetGeoTransform())
170        dst_ds.SetProjection(src_ds.GetProjectionRef())
171
172        dstband = dst_ds.GetRasterBand(1)
173
174    # =============================================================================
175    #    Invoke algorithm.
176    # =============================================================================
177
178    if quiet_flag:
179        prog_func = None
180    else:
181        prog_func = gdal.TermProgress_nocb
182
183    gdal.ComputeProximity(srcband, dstband, options,
184                          callback=prog_func)
185
186    srcband = None
187    dstband = None
188    src_ds = None
189    dst_ds = None
190
191
192if __name__ == '__main__':
193    sys.exit(main(sys.argv))
194