[23] | 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 | |
---|
| 32 | try: |
---|
| 33 | from osgeo import gdal |
---|
| 34 | except ImportError: |
---|
| 35 | import gdal |
---|
| 36 | |
---|
| 37 | import sys |
---|
| 38 | import os.path |
---|
| 39 | |
---|
| 40 | def Usage(): |
---|
| 41 | print(""" |
---|
| 42 | gdal_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 | |
---|
| 53 | format = 'GTiff' |
---|
| 54 | creation_options = [] |
---|
| 55 | options = [] |
---|
| 56 | src_filename = None |
---|
| 57 | src_band_n = 1 |
---|
| 58 | dst_filename = None |
---|
| 59 | dst_band_n = 1 |
---|
| 60 | creation_type = 'Float32' |
---|
| 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 == '-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 | |
---|
| 126 | if src_filename is None or dst_filename is None: |
---|
| 127 | Usage() |
---|
| 128 | |
---|
| 129 | # ============================================================================= |
---|
| 130 | # Open source file |
---|
| 131 | # ============================================================================= |
---|
| 132 | |
---|
| 133 | src_ds = gdal.Open( src_filename ) |
---|
| 134 | |
---|
| 135 | if src_ds is None: |
---|
| 136 | print('Unable to open ', src_filename) |
---|
| 137 | sys.exit(1) |
---|
| 138 | |
---|
| 139 | srcband = src_ds.GetRasterBand(src_band_n) |
---|
| 140 | |
---|
| 141 | # ============================================================================= |
---|
| 142 | # Try opening the destination file as an existing file. |
---|
| 143 | # ============================================================================= |
---|
| 144 | |
---|
| 145 | try: |
---|
| 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 |
---|
| 152 | except: |
---|
| 153 | dst_ts = None |
---|
| 154 | |
---|
| 155 | # ============================================================================= |
---|
| 156 | # Create output file. |
---|
| 157 | # ============================================================================= |
---|
| 158 | if 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 | |
---|
| 173 | if quiet_flag: |
---|
| 174 | prog_func = None |
---|
| 175 | else: |
---|
| 176 | prog_func = gdal.TermProgress |
---|
| 177 | |
---|
| 178 | gdal.ComputeProximity( srcband, dstband, options, |
---|
| 179 | callback = prog_func ) |
---|
| 180 | |
---|
| 181 | srcband = None |
---|
| 182 | dstband = None |
---|
| 183 | src_ds = None |
---|
| 184 | |
---|
| 185 | |
---|
| 186 | |
---|
| 187 | |
---|
| 188 | |
---|
| 189 | |
---|