source: trunk/gdal/swig/python/samples/val_repl.py

Last change on this file was 31957, checked in by goatbar, 7 months ago

Clean swig/python/samples.

  • Remove extranous whitespace on blank lines
  • Spelling of truely, rgb
  • import {gdal,ogr,osr} -> only from osgeo import {gdal,ogr,osr}
  • Remove C style comment delimiters from python ( /* */ )
  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 4.2 KB
Line 
1#!/usr/bin/env python
2###############################################################################
3# $Id: val_repl.py 31957 2015-12-02 12:44:54Z rouault $
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# Copyright (c) 2009-2010, Even Rouault <even dot rouault at mines-paris dot org>
15#
16# Permission is hereby granted, free of charge, to any person obtaining a
17# copy of this software and associated documentation files (the "Software"),
18# to deal in the Software without restriction, including without limitation
19# the rights to use, copy, modify, merge, publish, distribute, sublicense,
20# and/or sell copies of the Software, and to permit persons to whom the
21# Software is furnished to do so, subject to the following conditions:
22#
23# The above copyright notice and this permission notice shall be included
24# in all copies or substantial portions of the Software.
25#
26# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
27# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
29# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32# DEALINGS IN THE SOFTWARE.
33###############################################################################
34
35
36from osgeo import gdal
37gdal.TermProgress = gdal.TermProgress_nocb
38
39try:
40    import numpy
41except ImportError:
42    import Numeric as numpy
43
44
45import sys
46
47# =============================================================================
48def Usage():
49    print('Usage: val_repl.py -innd in_nodata_value -outnd out_nodata_value')
50    print('                   [-of out_format] [-ot out_type] infile outfile')
51    print('')
52    sys.exit( 1 )
53
54# =============================================================================
55
56# =============================================================================
57def ParseType(type):
58    gdal_dt = gdal.GetDataTypeByName(type)
59    if gdal_dt is gdal.GDT_Unknown:
60        gdal_dt = gdal.GDT_Byte
61    return gdal_dt
62
63# =============================================================================
64
65inNoData = None
66outNoData = None
67infile = None
68outfile = None
69format = 'GTiff'
70type = gdal.GDT_Byte
71
72# Parse command line arguments.
73i = 1
74while i < len(sys.argv):
75    arg = sys.argv[i]
76
77    if arg == '-innd':
78        i = i + 1
79        inNoData = float(sys.argv[i])
80
81    elif arg == '-outnd':
82        i = i + 1
83        outNoData = float(sys.argv[i])
84
85    elif arg == '-of':
86        i = i + 1
87        format = sys.argv[i]
88
89    elif arg == '-ot':
90        i = i + 1
91        type = ParseType(sys.argv[i])
92
93    elif infile is None:
94        infile = arg
95
96    elif outfile is None:
97        outfile = arg
98
99    else:
100        Usage()
101
102    i = i + 1
103
104if infile is None:
105    Usage()
106if  outfile is None:
107    Usage()
108if inNoData is None:
109    Usage()
110if outNoData is None:
111    Usage()
112
113indataset = gdal.Open( infile, gdal.GA_ReadOnly )
114
115out_driver = gdal.GetDriverByName(format)
116outdataset = out_driver.Create(outfile, indataset.RasterXSize, indataset.RasterYSize, indataset.RasterCount, type)
117
118gt = indataset.GetGeoTransform()
119if gt is not None and gt != (0.0, 1.0, 0.0, 0.0, 0.0, 1.0):
120    outdataset.SetGeoTransform(gt)
121
122prj = indataset.GetProjectionRef()
123if prj is not None and len(prj) > 0:
124    outdataset.SetProjection(prj)
125
126for iBand in range(1, indataset.RasterCount + 1):
127    inband = indataset.GetRasterBand(iBand)
128    outband = outdataset.GetRasterBand(iBand)
129
130    for i in range(inband.YSize - 1, -1, -1):
131        scanline = inband.ReadAsArray(0, i, inband.XSize, 1, inband.XSize, 1)
132        scanline = numpy.choose( numpy.equal( scanline, inNoData),
133                                       (scanline, outNoData) )
134        outband.WriteArray(scanline, 0, i)
135
Note: See TracBrowser for help on using the repository browser.