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

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