root/trunk/gdal/swig/python/samples/val_repl.py

Revision 13106, 4.5 kB (checked in by hobu, 7 months ago)

attempt new-style imports. This script has not been ported over to use numpy yet, however

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1 #!/usr/bin/env python
2 ###############################################################################
3 # $Id$
4 #
5 # Project:  GDAL Python samples
6 # Purpose:  Script to replace specified values from the input raster file
7 #           with the new ones. May be useful in cases when you don't like
8 #           value, used for NoData indication and want replace it with other
9 #           value. Input file remains unchanged, results stored in other file.
10 # Author:   Andrey Kiselev, dron@remotesensing.org
11 #
12 ###############################################################################
13 # Copyright (c) 2003, Andrey Kiselev <dron@remotesensing.org>
14 #
15 # Permission is hereby granted, free of charge, to any person obtaining a
16 # copy of this software and associated documentation files (the "Software"),
17 # to deal in the Software without restriction, including without limitation
18 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
19 # and/or sell copies of the Software, and to permit persons to whom the
20 # Software is furnished to do so, subject to the following conditions:
21 #
22 # The above copyright notice and this permission notice shall be included
23 # in all copies or substantial portions of the Software.
24 #
25 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
26 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
31 # DEALINGS IN THE SOFTWARE.
32 ###############################################################################
33
34
35 try:
36     from osgeo import gdal
37     from osgeo.gdalconst import *
38     gdal.TermProgress = gdal.TermProgress_nocb
39 except ImportError:
40     import gdal
41     from gdalconst import *
42
43 try:
44     import numpy as Numeric
45     Numeric.arrayrange = Numeric.arange
46 except ImportError:
47     import Numeric
48
49 try:
50     from osgeo import gdal_array as gdalnumeric
51 except ImportError:
52     import gdalnumeric
53
54 import sys
55
56 # =============================================================================
57 def Usage():
58     print 'Usage: val_repl.py -innd in_nodata_value -outnd out_nodata_value'
59     print '                   [-of out_format] [-ot out_type] infile outfile'
60     print
61     sys.exit( 1 )
62
63 # =============================================================================
64
65 # =============================================================================
66 def ParseType(type):
67     if type == 'Byte':
68         return GDT_Byte
69     elif type == 'Int16':
70         return GDT_Int16
71     elif type == 'UInt16':
72         return GDT_UInt16
73     elif type == 'Int32':
74         return GDT_Int32
75     elif type == 'UInt32':
76         return GDT_UInt32
77     elif type == 'Float32':
78         return GDT_Float32
79     elif type == 'Float64':
80         return GDT_Float64
81     elif type == 'CInt16':
82         return GDT_CInt16
83     elif type == 'CInt32':
84         return GDT_CInt32
85     elif type == 'CFloat32':
86         return GDT_CFloat32
87     elif type == 'CFloat64':
88         return GDT_CFloat64
89     else:
90         return GDT_Byte
91 # =============================================================================
92
93 inNoData = None
94 outNoData = None
95 infile = None
96 outfile = None
97 format = 'GTiff'
98 type = GDT_Byte
99
100 # Parse command line arguments.
101 i = 1
102 while i < len(sys.argv):
103     arg = sys.argv[i]
104
105     if arg == '-innd':
106         i = i + 1
107         inNoData = float(sys.argv[i])
108
109     elif arg == '-outnd':
110         i = i + 1
111         outNoData = float(sys.argv[i])
112
113     elif arg == '-of':
114         i = i + 1
115         format = sys.argv[i]
116
117     elif arg == '-ot':
118         i = i + 1
119         type = ParseType(sys.argv[i])
120
121     elif infile is None:
122         infile = arg
123
124     elif outfile is None:
125         outfile = arg
126
127     else:
128         Usage()
129
130     i = i + 1
131
132 if infile is None:
133     Usage()
134 if  outfile is None:
135     Usage()
136 if inNoData is None:
137     Usage()
138 if outNoData is None:
139     Usage()
140
141 indataset = gdal.Open( infile, GA_ReadOnly )
142
143 out_driver = gdal.GetDriverByName(format)
144 outdataset = out_driver.Create(outfile, indataset.RasterXSize, indataset.RasterYSize, indataset.RasterCount, type)
145
146 for iBand in range(1, indataset.RasterCount + 1):
147     inband = indataset.GetRasterBand(iBand)
148     outband = outdataset.GetRasterBand(iBand)
149
150     for i in range(inband.YSize - 1, -1, -1):
151         scanline = inband.ReadAsArray(0, i, inband.XSize, 1, inband.XSize, 1)
152         scanline = gdalnumeric.choose( gdalnumeric.equal( scanline, inNoData),
153                                        (scanline, outNoData) )
154         outband.WriteArray(scanline, 0, i)
155
Note: See TracBrowser for help on using the browser.