Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reusable python scripts (now gdal_calc.py) #121

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions autotest/pyscripts/test_gdal_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,26 +68,35 @@ def test_gdal_calc_py_1():

test_py_scripts.run_py_script(script_path, 'gdal_calc', '-A tmp/test_gdal_calc_py.tif --calc=A --overwrite --outfile tmp/test_gdal_calc_py_1_1.tif')
test_py_scripts.run_py_script(script_path, 'gdal_calc', '-A tmp/test_gdal_calc_py.tif --A_band=2 --calc=A --overwrite --outfile tmp/test_gdal_calc_py_1_2.tif')
test_py_scripts.run_py_script(script_path, 'gdal_calc', '-Z tmp/test_gdal_calc_py.tif --Z_band=2 --calc=Z --overwrite --outfile tmp/test_gdal_calc_py_1_3.tif')

ds1 = gdal.Open('tmp/test_gdal_calc_py_1_1.tif')
ds2 = gdal.Open('tmp/test_gdal_calc_py_1_2.tif')
ds3 = gdal.Open('tmp/test_gdal_calc_py_1_3.tif')

if ds1 is None:
gdaltest.post_reason('ds1 not found')
return 'fail'
if ds2 is None:
gdaltest.post_reason('ds2 not found')
return 'fail'
if ds3 is None:
gdaltest.post_reason('ds3 not found')
return 'fail'

if ds1.GetRasterBand(1).Checksum() != 12603:
gdaltest.post_reason('ds1 wrong checksum')
return 'fail'
if ds2.GetRasterBand(1).Checksum() != 58561:
gdaltest.post_reason('ds2 wrong checksum')
return 'fail'
if ds3.GetRasterBand(1).Checksum() != 58561:
gdaltest.post_reason('ds3 wrong checksum')
return 'fail'

ds1 = None
ds2 = None
ds3 = None

return 'success'

Expand All @@ -105,26 +114,35 @@ def test_gdal_calc_py_2():

test_py_scripts.run_py_script(script_path, 'gdal_calc', '-A tmp/test_gdal_calc_py.tif --A_band 1 -B tmp/test_gdal_calc_py.tif --B_band 2 --calc=A+B --overwrite --outfile tmp/test_gdal_calc_py_2_1.tif')
test_py_scripts.run_py_script(script_path, 'gdal_calc', '-A tmp/test_gdal_calc_py.tif --A_band 1 -B tmp/test_gdal_calc_py.tif --B_band 2 --calc=A*B --overwrite --outfile tmp/test_gdal_calc_py_2_2.tif')
test_py_scripts.run_py_script(script_path, 'gdal_calc', '-A tmp/test_gdal_calc_py.tif --A_band 1 --calc="sqrt(A)" --type=Float32 --overwrite --outfile tmp/test_gdal_calc_py_2_3.tif')

ds1 = gdal.Open('tmp/test_gdal_calc_py_2_1.tif')
ds2 = gdal.Open('tmp/test_gdal_calc_py_2_2.tif')
ds3 = gdal.Open('tmp/test_gdal_calc_py_2_3.tif')

if ds1 is None:
gdaltest.post_reason('ds1 not found')
return 'fail'
if ds2 is None:
gdaltest.post_reason('ds2 not found')
return 'fail'
if ds3 is None:
gdaltest.post_reason('ds3 not found')
return 'fail'

if ds1.GetRasterBand(1).Checksum() != 12368:
gdaltest.post_reason('ds1 wrong checksum')
return 'fail'
if ds2.GetRasterBand(1).Checksum() != 62785:
gdaltest.post_reason('ds2 wrong checksum')
return 'fail'
if ds3.GetRasterBand(1).Checksum() != 47132:
gdaltest.post_reason('ds3 wrong checksum')
return 'fail'

ds1 = None
ds2 = None
ds3 = None

return 'success'

Expand Down Expand Up @@ -220,18 +238,74 @@ def test_gdal_calc_py_4():

return 'success'

###############################################################################
# test python interface, basic copy

def test_gdal_calc_py_5():

if gdalnumeric_not_available:
gdaltest.post_reason('gdalnumeric is not available, skipping all tests')
return 'skip'

script_path = test_py_scripts.get_py_script('gdal_calc')
if script_path is None:
return 'skip'

sys.path.insert(0, script_path)
import gdal_calc

shutil.copy('../gcore/data/stefan_full_rgba.tif', 'tmp/test_gdal_calc_py.tif')

gdal_calc.Calc('A', A='tmp/test_gdal_calc_py.tif', overwrite=True, quiet=True, outfile='tmp/test_gdal_calc_py_1_1.tif')
gdal_calc.Calc('A', A='tmp/test_gdal_calc_py.tif', A_band=2, overwrite=True, quiet=True, outfile='tmp/test_gdal_calc_py_1_2.tif')
gdal_calc.Calc('Z', Z='tmp/test_gdal_calc_py.tif', Z_band=2, overwrite=True, quiet=True, outfile='tmp/test_gdal_calc_py_1_3.tif')

ds1 = gdal.Open('tmp/test_gdal_calc_py_1_1.tif')
ds2 = gdal.Open('tmp/test_gdal_calc_py_1_2.tif')
ds3 = gdal.Open('tmp/test_gdal_calc_py_1_3.tif')

if ds1 is None:
gdaltest.post_reason('ds1 not found')
return 'fail'
if ds2 is None:
gdaltest.post_reason('ds2 not found')
return 'fail'
if ds3 is None:
gdaltest.post_reason('ds3 not found')
return 'fail'

if ds1.GetRasterBand(1).Checksum() != 12603:
gdaltest.post_reason('ds1 wrong checksum')
return 'fail'
if ds2.GetRasterBand(1).Checksum() != 58561:
gdaltest.post_reason('ds2 wrong checksum')
return 'fail'
if ds3.GetRasterBand(1).Checksum() != 58561:
gdaltest.post_reason('ds3 wrong checksum')
return 'fail'

ds1 = None
ds2 = None
ds3 = None

return 'success'

def test_gdal_calc_py_cleanup():

lst = [ 'tmp/test_gdal_calc_py.tif',
'tmp/test_gdal_calc_py_1_1.tif',
'tmp/test_gdal_calc_py_1_2.tif',
'tmp/test_gdal_calc_py_1_3.tif',
'tmp/test_gdal_calc_py_2_1.tif',
'tmp/test_gdal_calc_py_2_2.tif',
'tmp/test_gdal_calc_py_2_3.tif',
'tmp/test_gdal_calc_py_3.tif',
'tmp/test_gdal_calc_py_4_1.tif',
'tmp/test_gdal_calc_py_4_2.tif',
'tmp/test_gdal_calc_py_4_3.tif',
'tmp/test_gdal_calc_py_5_1.tif',
'tmp/test_gdal_calc_py_5_2.tif',
'tmp/test_gdal_calc_py_5_3.tif',
]
for filename in lst:
try:
Expand All @@ -246,6 +320,7 @@ def test_gdal_calc_py_cleanup():
test_gdal_calc_py_2,
test_gdal_calc_py_3,
test_gdal_calc_py_4,
test_gdal_calc_py_5,
test_gdal_calc_py_cleanup
]

Expand Down
27 changes: 13 additions & 14 deletions gdal/swig/python/scripts/gdal_calc.dox
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,29 @@ Command line raster calculator with numpy syntax
\section gdal_calc_synopsis SYNOPSIS

\verbatim
gdal_calc.py [-A <filename>] [--A_band] [-B...-Z filename] [other_options]
gdal_calc.py --calc=expression --outfile=out_filename [-A filename]
[--A_band=n] [-B...-Z filename] [other_options]

Options:
-h, --help show this help message and exit
--calc=CALC calculation in gdalnumeric syntax using +-/* or any
numpy array functions (i.e. logical_and())
-A A input gdal raster file, note you can use any letter
A-Z
--A_band=A_BAND number of raster band for file A (default 1)
--outfile=OUTF output file to generate or fill
--NoDataValue=NODATAVALUE
set output nodata value (Defaults to datatype specific
value)
--type=TYPE output datatype, must be one of ['Int32', 'Int16',
--calc=expression calculation in gdalnumeric syntax using +-/* or any
numpy array functions (i.e. log10())
-A filename input gdal raster file, you can use any letter (A-Z)
--A_band=n number of raster band for file A (default 1)
--outfile=filename output file to generate or fill
--NoDataValue=value output nodata value (default datatype specific value)
--type=datatype output datatype, must be one of ['Int32', 'Int16',
'Float64', 'UInt16', 'Byte', 'UInt32', 'Float32']
--format=FORMAT GDAL format for output file (default 'GTiff')
--creation-option=CREATION_OPTIONS, --co=CREATION_OPTIONS
--format=gdal_format GDAL format for output file (default 'GTiff')
--creation-option=option, --co=option
Passes a creation option to the output format driver.
Multiple options may be listed. See format specific
documentation for legal creation options for each
format.
--allBands=ALLBANDS process all bands of given raster (A-Z)
--allBands=[A-Z] process all bands of given raster (A-Z)
--overwrite overwrite output file if it already exists
--debug print debugging information
--quiet suppress progress messages
\endverbatim

\section gdal_edit_description DESCRIPTION
Expand Down
Loading