From cceed0b2a72aaa577979528d30e9f3cae8215c44 Mon Sep 17 00:00:00 2001 From: Antonio Valentino Date: Sun, 13 Apr 2014 11:47:01 +0200 Subject: [PATCH 01/69] Initial commit --- .gitignore | 5 + LICENSE.txt | 17 + Makefile | 57 + README.txt | 83 + autotest/gcore/data/cfloat64.tif | Bin 0 -> 6736 bytes autotest/gcore/data/cint_sar.tif | Bin 0 -> 1587 bytes autotest/gcore/data/float32.tif | Bin 0 -> 1936 bytes autotest/gcore/data/int32.tif | Bin 0 -> 1936 bytes autotest/gcore/data/pixfun_cmul_c.vrt | 18 + autotest/gcore/data/pixfun_cmul_r.vrt | 18 + autotest/gcore/data/pixfun_conj_c.vrt | 13 + autotest/gcore/data/pixfun_conj_r.vrt | 13 + autotest/gcore/data/pixfun_dB2amp.vrt | 13 + autotest/gcore/data/pixfun_dB2pow.vrt | 13 + autotest/gcore/data/pixfun_diff_c.vrt | 18 + autotest/gcore/data/pixfun_diff_r.vrt | 18 + autotest/gcore/data/pixfun_imag_c.vrt | 13 + autotest/gcore/data/pixfun_imag_r.vrt | 13 + autotest/gcore/data/pixfun_intensity_c.vrt | 13 + autotest/gcore/data/pixfun_intensity_r.vrt | 13 + autotest/gcore/data/pixfun_inv_c.vrt | 13 + autotest/gcore/data/pixfun_inv_r.vrt | 13 + autotest/gcore/data/pixfun_log10.vrt | 13 + autotest/gcore/data/pixfun_mod_c.vrt | 13 + autotest/gcore/data/pixfun_mod_r.vrt | 13 + autotest/gcore/data/pixfun_mul_c.vrt | 18 + autotest/gcore/data/pixfun_mul_r.vrt | 24 + autotest/gcore/data/pixfun_phase_c.vrt | 13 + autotest/gcore/data/pixfun_phase_r.vrt | 13 + autotest/gcore/data/pixfun_real_c.vrt | 13 + autotest/gcore/data/pixfun_real_r.vrt | 13 + autotest/gcore/data/pixfun_sqrt.vrt | 13 + autotest/gcore/data/pixfun_sum_c.vrt | 24 + autotest/gcore/data/pixfun_sum_r.vrt | 24 + autotest/gcore/data/uint16.tif | Bin 0 -> 1136 bytes autotest/gcore/pixfun.py | 762 ++++++++++ autotest/gcore/testnonboundtoswig.py | 383 +++++ autotest/pymod/gdaltest.py | 1578 ++++++++++++++++++++ autotest/pymod/gdaltest_python2.py | 189 +++ autotest/pymod/gdaltest_python3.py | 177 +++ autotest/pymod/ogrtest.py | 194 +++ autotest/pymod/test_cli_utilities.py | 189 +++ pixelfunctions.c | 818 ++++++++++ pixfunplugin.c | 56 + 44 files changed, 4904 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE.txt create mode 100644 Makefile create mode 100644 README.txt create mode 100644 autotest/gcore/data/cfloat64.tif create mode 100644 autotest/gcore/data/cint_sar.tif create mode 100644 autotest/gcore/data/float32.tif create mode 100644 autotest/gcore/data/int32.tif create mode 100644 autotest/gcore/data/pixfun_cmul_c.vrt create mode 100644 autotest/gcore/data/pixfun_cmul_r.vrt create mode 100644 autotest/gcore/data/pixfun_conj_c.vrt create mode 100644 autotest/gcore/data/pixfun_conj_r.vrt create mode 100644 autotest/gcore/data/pixfun_dB2amp.vrt create mode 100644 autotest/gcore/data/pixfun_dB2pow.vrt create mode 100644 autotest/gcore/data/pixfun_diff_c.vrt create mode 100644 autotest/gcore/data/pixfun_diff_r.vrt create mode 100644 autotest/gcore/data/pixfun_imag_c.vrt create mode 100644 autotest/gcore/data/pixfun_imag_r.vrt create mode 100644 autotest/gcore/data/pixfun_intensity_c.vrt create mode 100644 autotest/gcore/data/pixfun_intensity_r.vrt create mode 100644 autotest/gcore/data/pixfun_inv_c.vrt create mode 100644 autotest/gcore/data/pixfun_inv_r.vrt create mode 100644 autotest/gcore/data/pixfun_log10.vrt create mode 100644 autotest/gcore/data/pixfun_mod_c.vrt create mode 100644 autotest/gcore/data/pixfun_mod_r.vrt create mode 100644 autotest/gcore/data/pixfun_mul_c.vrt create mode 100644 autotest/gcore/data/pixfun_mul_r.vrt create mode 100644 autotest/gcore/data/pixfun_phase_c.vrt create mode 100644 autotest/gcore/data/pixfun_phase_r.vrt create mode 100644 autotest/gcore/data/pixfun_real_c.vrt create mode 100644 autotest/gcore/data/pixfun_real_r.vrt create mode 100644 autotest/gcore/data/pixfun_sqrt.vrt create mode 100644 autotest/gcore/data/pixfun_sum_c.vrt create mode 100644 autotest/gcore/data/pixfun_sum_r.vrt create mode 100644 autotest/gcore/data/uint16.tif create mode 100644 autotest/gcore/pixfun.py create mode 100644 autotest/gcore/testnonboundtoswig.py create mode 100644 autotest/pymod/gdaltest.py create mode 100644 autotest/pymod/gdaltest_python2.py create mode 100644 autotest/pymod/gdaltest_python3.py create mode 100644 autotest/pymod/ogrtest.py create mode 100644 autotest/pymod/test_cli_utilities.py create mode 100644 pixelfunctions.c create mode 100644 pixfunplugin.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000000..e433efa61b1b --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +*.c +*.pyc +*.o +*.so +*~ diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 000000000000..c1326585c5b6 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,17 @@ + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + . + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + . + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 000000000000..70c143bb40e6 --- /dev/null +++ b/Makefile @@ -0,0 +1,57 @@ +############################################################################## +# +# Project: GDAL +# Purpose: Implementation of a set of GDALDerivedPixelFunc(s) to be used +# with source raster band of virtual GDAL datasets. +# Author: Antonio Valentino +# +############################################################################## +# Copyright (c) 2008-2014 Antonio Valentino +# +# Permission is hereby granted, free of charge, to any person obtaining a +# copy of this software and associated documentation files (the "Software"), +# to deal in the Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, distribute, sublicense, +# and/or sell copies of the Software, and to permit persons to whom the +# Software is furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included +# in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +# DEALINGS IN THE SOFTWARE. +############################################################################## + +.PHONY: all clean dist + +OBJS = pixfunplugin.o pixelfunctions.o +CFLAGS := -fPIC -Wall -Wno-long-long $(shell gdal-config --cflags) $(CFLAGS) +CFLAGS := -pedantic $(CFLAGS) + +CFLAGS := -g $(CFLAGS) +#CFLAGS := -O3 $(CFLAGS) + +TARGET = gdal_PIXFUN.so + +all: $(TARGET) + +clean: + $(RM) $(TARGET) *.o *~ + $(RM) autotest/pymod/*.pyc autotest/gcore/*.pyc + +$(TARGET): $(OBJS) + $(CC) -shared -o $@ $(OBJS) $(shell gdal-config --libs) + + +PYTHON=python + +check: + cd autotest/gcore && \ + env GDAL_DRIVER_PATH=$(PWD):$(GDAL_DRIVER_PATH) \ + PYTHONPATH=$(PWD)/autotest/pymod:$(PYTHONPATH) \ + $(PYTHON) pixfun.py diff --git a/README.txt b/README.txt new file mode 100644 index 000000000000..ab2a6587022f --- /dev/null +++ b/README.txt @@ -0,0 +1,83 @@ +gdal-pixfun-plugin +================== + +A small gdal plugin that provides a small set of "pixel functions" (see +http://www.gdal.org/gdal_vrttut.html) that can be used to create derived +raster bands. + + +List of pixel functions +----------------------- + +:"real": + extract real part from a single raster band (just a copy if the + input is non-complex) +:"imag": + extract imaginary part from a single raster band (0 for + non-complex) +:"mod": + extract module from a single raster band (real or complex) +:"phase": + extract phase from a single raster band (0 for non-complex) +:"conj": + computes the complex conjugate of a single raster band (just a + copy if the input is non-complex) +:"sum": + sum 2 or more raster bands +:"diff": + computes the difference between 2 raster bands (b1 - b2) +:"mul": + multilpy 2 or more raster bands +:"cmul": + multiply the first band for the complex conjugate of the second +:"inv": + inverse (1./x). Note: no check is performed on zero division +:"intensity": + computes the intensity Re(x*conj(x)) of a single raster band + (real or complex) +:"sqrt": + perform the square root of a single raster band (real only) +:"log10": + compute the logarithm (base 10) of the abs of a single raster + band (real or complex): log10( abs( x ) ) +:"dB2amp": + perform scale conversion from logarithmic to linear + (amplitude) (i.e. 10 ^ ( x / 20 ) ) of a single raster band (real only) +:"dB2pow": + perform scale conversion from logarithmic to linear + (power) (i.e. 10 ^ ( x / 10 ) ) of a single raster band (real only) + + +How to get it +------------- + +The project home page is at https://github.com/avalentino/gdal-pixfun-plugin. +A copy of the latest version of the sources can be obtained ising git_:: + + $ git clone https://github.com/avalentino/gdal-pixfun-plugin.git + +.. _git: http://git-scm.com + + +How to build, test and install +----------------------------- + +The gdal-pixfun-plugin can be built using the following command:: + + $ make + +in the root folder of the source distribution. +It assumes that GDAL is correctly installed on your system. + +To run the unit test suite:: + + $ make check + +To install the plugin just copy the generated shared object (gdal_PIXFUN.so) +into the GDAL plugin directory (/usr/lib/gdalplugins/1.XX/ on unix). + + +License +------- + +See the LICENSE.txt file. diff --git a/autotest/gcore/data/cfloat64.tif b/autotest/gcore/data/cfloat64.tif new file mode 100644 index 0000000000000000000000000000000000000000..0e75cb9919aba8c88ce28ce98b34f7251500eb80 GIT binary patch literal 6736 zcmb{1&uWuV5C!m?CYB;a5wvP?QE;UjsT;vn3vLS9h5r!|TdmejyB8Nhg}#O44Ww@n zT>AvBy7D>vl17^Ecf?E3Ldl(P=FXgRX6{YWZfsmH3zy6B&+ysWWHqs0wC(=Owmp2_ zw)Ka-HDkQ>z-NB(>(@4Z2;boM3jEt`JK`(&t=F&Q8@#U6L*FVt`95vu550)jU$cki zN6(smefU{VJtH6c)yMp?-+rP;k& z?Wg?u%xCzSr|{E%`BZKC6a4YV{eWM5p&$AiuO9Yy@V0$_40ir*%ST&%!Vf?Bh(Get zpWw%RCEsd3@(KXOv@8Au3;E#IrV`lv9x1Y6q5l=q(M!fvgS3mh9pZm4F zFPRU|qy4M-)l2^NFYtpe^40t`eejD%yyE*;+x(%g=dFS_?g!_^{*%vsR_`zMCtvW> z|DX4Zzgk~Cu=|g>f5031;E#E#`RSMQ6@0)ep6Wc<4{h=42d{iH=7k==mx8HxwfdRJ6Vt7=BL)@I=%7Uh35H%spm_R z^(g1XNla<}+j3$fK6))>@ogM{Lo3~bOtUP*nZ{_{|-pgI>?3ra^3wg3PC literal 0 HcmV?d00001 diff --git a/autotest/gcore/data/cint_sar.tif b/autotest/gcore/data/cint_sar.tif new file mode 100644 index 0000000000000000000000000000000000000000..45e7c29907d246482e74e03065df4e1b7951c30c GIT binary patch literal 1587 zcma)5OKa3n82x5i$5M-}(rQ~Pj8madX`0Lz#>NIgk$i9jlwf_6+W>$7>$Q6=s<2KK%@mCSiv5=B6S=?9IfLx z;+8u0c(DLJ5NY+bZFt@jI6qX!UYx^q&K1N10gheAUfz{!9ng#WNlb8WwZ+Ffh&NZG z;Kd$}0xWMt0YgC&adW*`d*tEe*aLs}@Q~1Ud*IeW;l{_M@ys+|@XnfRcBSCqAb)y` zc0|_~3f*_L*NfGE@SoiGb)NR}!}b2rwS|J8XXiJ5qn-dCi`b$!7YZAf#@e61`N@wo zT*sawQ0vV_lGTWPW!_uTQr~(Ijf97Ex>x zQlD;WR8@5Mys1}EMvI9&mM4g!gvpY!BudE;H70NsL(|dMWSYw+(;-~joNy);tCj;> zN{-mNIwEpHh$b*Cibf38pB2UbP+_XZchXHmqfVuW<&B6LE+vHJ(y2lIa6QGz{)mOm zm}6B)a3nHZCY#MwGcr{Q6q=fQyfrxvzNX6PKNHZ|Xe6-%iU%Pe07ceA@hP@jL{*jDzz|!FmL& zZa&Nh_B?rR{As77)hDZ`7ngS;H9a_f@t5;ldU!kQp~oeh@!ER>;hx(l4X+>IJeowlFtCd|t{!r5CN-hP?IqaWFQd2pk(!Ke-DcVxX{ z(8I$HPm3RQm~O3yZw|(*-fP8g^!UvV^Ssq*_UhMayQg{6;ZbR>|Cls+c(t|fKPSx( z+g9N}^HIB9_+{ba`LJykJ}P|vH`_bI$>){J(B8?`KK|eDSE~#UR!?xHF6#`}x+`ut oKAvw7Oq}0;9Ix`dJG1>~kDg2)PT#$KJw5vT>BDqqXZE%I2U*=wm;e9( literal 0 HcmV?d00001 diff --git a/autotest/gcore/data/int32.tif b/autotest/gcore/data/int32.tif new file mode 100644 index 0000000000000000000000000000000000000000..dab53d96e0e7ecf59bdc55ed5da3d9d91779c745 GIT binary patch literal 1936 zcmZwIJ!_Ov5C!17A4ZEs5loa|v0$Z*#Kv|57AEL6DuS&Df}n+6w9>#fmX=~?DU!m{ z(oXyt{sl{&XY(F%5d*`VJ9EyNxi`DX-rlu#d!w~yt+iJbhZQe}48QBJGhUyAY3V*x zydQAiOYPIpPR`<^p> z{Ikcud(q>VnLXs3v!ndALe`6~Irz@OvQSH%4ljP;W>)T~4~I{09)5O8j(0dYXMVCe zKj(10LqEgJyjSjr8-8%kc{gWw5RWQo`N)0r^KtXLRgZV(s=@h8!I=T;n-BAW-6!`| z=ZPn1W&LQ?@|j4FMvY(m<=l&gw=+$DcfH($NBraM`=jnVhsP&@YYr|f3w>vM%jRZg z-X^=|?-XwKi>CL3^A7xBVK1L=e)$}P$H(0L3Ydp8ZvKXtpI!8M{I2*@;XBRCJRDkY zp+=Xt(NojM?X%s4d3i@Td(-9JGmA$b*hO5L^QunLaq z!Uu&5oXR0pHA2L>Xqg0 Zts6Jz*XIxJ-<==4c>ZL*v$K5N{sJpD+a&-1 literal 0 HcmV?d00001 diff --git a/autotest/gcore/data/pixfun_cmul_c.vrt b/autotest/gcore/data/pixfun_cmul_c.vrt new file mode 100644 index 000000000000..ea7a30b5451d --- /dev/null +++ b/autotest/gcore/data/pixfun_cmul_c.vrt @@ -0,0 +1,18 @@ + + + Product with complex conjugate + cmul + + cint_sar.tif + 1 + + + + + cint_sar.tif + 1 + + + + + diff --git a/autotest/gcore/data/pixfun_cmul_r.vrt b/autotest/gcore/data/pixfun_cmul_r.vrt new file mode 100644 index 000000000000..be1ffe198c7a --- /dev/null +++ b/autotest/gcore/data/pixfun_cmul_r.vrt @@ -0,0 +1,18 @@ + + + Product with complex conjugate + cmul + + uint16.tif + 1 + + + + + int32.tif + 1 + + + + + diff --git a/autotest/gcore/data/pixfun_conj_c.vrt b/autotest/gcore/data/pixfun_conj_c.vrt new file mode 100644 index 000000000000..41e6ac20d769 --- /dev/null +++ b/autotest/gcore/data/pixfun_conj_c.vrt @@ -0,0 +1,13 @@ + + + conjugate + conj + CInt16 + + cint_sar.tif + 1 + + + + + diff --git a/autotest/gcore/data/pixfun_conj_r.vrt b/autotest/gcore/data/pixfun_conj_r.vrt new file mode 100644 index 000000000000..a91219bf5aee --- /dev/null +++ b/autotest/gcore/data/pixfun_conj_r.vrt @@ -0,0 +1,13 @@ + + + conjugate + conj + Int32 + + int32.tif + 1 + + + + + diff --git a/autotest/gcore/data/pixfun_dB2amp.vrt b/autotest/gcore/data/pixfun_dB2amp.vrt new file mode 100644 index 000000000000..0db48ef93863 --- /dev/null +++ b/autotest/gcore/data/pixfun_dB2amp.vrt @@ -0,0 +1,13 @@ + + + db to amplitude + dB2amp + Float64 + + float32.tif + 1 + + + + + diff --git a/autotest/gcore/data/pixfun_dB2pow.vrt b/autotest/gcore/data/pixfun_dB2pow.vrt new file mode 100644 index 000000000000..ca295795adbc --- /dev/null +++ b/autotest/gcore/data/pixfun_dB2pow.vrt @@ -0,0 +1,13 @@ + + + dB to power + dB2pow + Float64 + + float32.tif + 1 + + + + + diff --git a/autotest/gcore/data/pixfun_diff_c.vrt b/autotest/gcore/data/pixfun_diff_c.vrt new file mode 100644 index 000000000000..c2f6cf0d1bb7 --- /dev/null +++ b/autotest/gcore/data/pixfun_diff_c.vrt @@ -0,0 +1,18 @@ + + + Difference + diff + + cint_sar.tif + 1 + + + + + cfloat64.tif + 1 + + + + + diff --git a/autotest/gcore/data/pixfun_diff_r.vrt b/autotest/gcore/data/pixfun_diff_r.vrt new file mode 100644 index 000000000000..3937e1de7d2e --- /dev/null +++ b/autotest/gcore/data/pixfun_diff_r.vrt @@ -0,0 +1,18 @@ + + + Difference + diff + + int32.tif + 1 + + + + + float32.tif + 1 + + + + + diff --git a/autotest/gcore/data/pixfun_imag_c.vrt b/autotest/gcore/data/pixfun_imag_c.vrt new file mode 100644 index 000000000000..41c3bd452e61 --- /dev/null +++ b/autotest/gcore/data/pixfun_imag_c.vrt @@ -0,0 +1,13 @@ + + + Imaginary part + imag + CFloat64 + + cint_sar.tif + 1 + + + + + diff --git a/autotest/gcore/data/pixfun_imag_r.vrt b/autotest/gcore/data/pixfun_imag_r.vrt new file mode 100644 index 000000000000..f0c147ed37bd --- /dev/null +++ b/autotest/gcore/data/pixfun_imag_r.vrt @@ -0,0 +1,13 @@ + + + Imaginary part + imag + Float32 + + float32.tif + 1 + + + + + diff --git a/autotest/gcore/data/pixfun_intensity_c.vrt b/autotest/gcore/data/pixfun_intensity_c.vrt new file mode 100644 index 000000000000..559d1f84e5ab --- /dev/null +++ b/autotest/gcore/data/pixfun_intensity_c.vrt @@ -0,0 +1,13 @@ + + + Intensity + intensity + CFloat64 + + cint_sar.tif + 1 + + + + + diff --git a/autotest/gcore/data/pixfun_intensity_r.vrt b/autotest/gcore/data/pixfun_intensity_r.vrt new file mode 100644 index 000000000000..bdaafde8b363 --- /dev/null +++ b/autotest/gcore/data/pixfun_intensity_r.vrt @@ -0,0 +1,13 @@ + + + Intensity + intensity + Float32 + + float32.tif + 1 + + + + + diff --git a/autotest/gcore/data/pixfun_inv_c.vrt b/autotest/gcore/data/pixfun_inv_c.vrt new file mode 100644 index 000000000000..50d88b1766d6 --- /dev/null +++ b/autotest/gcore/data/pixfun_inv_c.vrt @@ -0,0 +1,13 @@ + + + Inverse + inv + CFloat64 + + cint_sar.tif + 1 + + + + + diff --git a/autotest/gcore/data/pixfun_inv_r.vrt b/autotest/gcore/data/pixfun_inv_r.vrt new file mode 100644 index 000000000000..7726ebd45925 --- /dev/null +++ b/autotest/gcore/data/pixfun_inv_r.vrt @@ -0,0 +1,13 @@ + + + Inverse + inv + Float64 + + float32.tif + 1 + + + + + diff --git a/autotest/gcore/data/pixfun_log10.vrt b/autotest/gcore/data/pixfun_log10.vrt new file mode 100644 index 000000000000..fc9cbbc919be --- /dev/null +++ b/autotest/gcore/data/pixfun_log10.vrt @@ -0,0 +1,13 @@ + + + Log10 + log10 + Float32 + + float32.tif + 1 + + + + + diff --git a/autotest/gcore/data/pixfun_mod_c.vrt b/autotest/gcore/data/pixfun_mod_c.vrt new file mode 100644 index 000000000000..a97668465a6b --- /dev/null +++ b/autotest/gcore/data/pixfun_mod_c.vrt @@ -0,0 +1,13 @@ + + + Magnitude + mod + CFloat64 + + cint_sar.tif + 1 + + + + + diff --git a/autotest/gcore/data/pixfun_mod_r.vrt b/autotest/gcore/data/pixfun_mod_r.vrt new file mode 100644 index 000000000000..844cc14f1c68 --- /dev/null +++ b/autotest/gcore/data/pixfun_mod_r.vrt @@ -0,0 +1,13 @@ + + + Magnitude + mod + Int32 + + int32.tif + 1 + + + + + diff --git a/autotest/gcore/data/pixfun_mul_c.vrt b/autotest/gcore/data/pixfun_mul_c.vrt new file mode 100644 index 000000000000..5c05266f9e20 --- /dev/null +++ b/autotest/gcore/data/pixfun_mul_c.vrt @@ -0,0 +1,18 @@ + + + Product + mul + + cint_sar.tif + 1 + + + + + cint_sar.tif + 1 + + + + + diff --git a/autotest/gcore/data/pixfun_mul_r.vrt b/autotest/gcore/data/pixfun_mul_r.vrt new file mode 100644 index 000000000000..0a3c246674b2 --- /dev/null +++ b/autotest/gcore/data/pixfun_mul_r.vrt @@ -0,0 +1,24 @@ + + + Product + mul + + uint16.tif + 1 + + + + + int32.tif + 1 + + + + + float32.tif + 1 + + + + + diff --git a/autotest/gcore/data/pixfun_phase_c.vrt b/autotest/gcore/data/pixfun_phase_c.vrt new file mode 100644 index 000000000000..f332b118f22c --- /dev/null +++ b/autotest/gcore/data/pixfun_phase_c.vrt @@ -0,0 +1,13 @@ + + + Phase + phase + CFloat64 + + cint_sar.tif + 1 + + + + + diff --git a/autotest/gcore/data/pixfun_phase_r.vrt b/autotest/gcore/data/pixfun_phase_r.vrt new file mode 100644 index 000000000000..2f5c12bf84e2 --- /dev/null +++ b/autotest/gcore/data/pixfun_phase_r.vrt @@ -0,0 +1,13 @@ + + + Phase + phase + Float64 + + pixfun_imag_c.vrt + 1 + + + + + diff --git a/autotest/gcore/data/pixfun_real_c.vrt b/autotest/gcore/data/pixfun_real_c.vrt new file mode 100644 index 000000000000..471a7d2ff037 --- /dev/null +++ b/autotest/gcore/data/pixfun_real_c.vrt @@ -0,0 +1,13 @@ + + + Real part + real + CFloat64 + + cint_sar.tif + 1 + + + + + diff --git a/autotest/gcore/data/pixfun_real_r.vrt b/autotest/gcore/data/pixfun_real_r.vrt new file mode 100644 index 000000000000..41dbd5439ac8 --- /dev/null +++ b/autotest/gcore/data/pixfun_real_r.vrt @@ -0,0 +1,13 @@ + + + Real part + real + CFloat64 + + int32.tif + 1 + + + + + diff --git a/autotest/gcore/data/pixfun_sqrt.vrt b/autotest/gcore/data/pixfun_sqrt.vrt new file mode 100644 index 000000000000..c22fa3e8237e --- /dev/null +++ b/autotest/gcore/data/pixfun_sqrt.vrt @@ -0,0 +1,13 @@ + + + Square root + sqrt + Float32 + + float32.tif + 1 + + + + + diff --git a/autotest/gcore/data/pixfun_sum_c.vrt b/autotest/gcore/data/pixfun_sum_c.vrt new file mode 100644 index 000000000000..6c976dd32a67 --- /dev/null +++ b/autotest/gcore/data/pixfun_sum_c.vrt @@ -0,0 +1,24 @@ + + + Sum + sum + + uint16.tif + 1 + + + + + cint_sar.tif + 1 + + + + + cfloat64.tif + 1 + + + + + diff --git a/autotest/gcore/data/pixfun_sum_r.vrt b/autotest/gcore/data/pixfun_sum_r.vrt new file mode 100644 index 000000000000..24e253cc9d26 --- /dev/null +++ b/autotest/gcore/data/pixfun_sum_r.vrt @@ -0,0 +1,24 @@ + + + Sum + sum + + uint16.tif + 1 + + + + + int32.tif + 1 + + + + + float32.tif + 1 + + + + + diff --git a/autotest/gcore/data/uint16.tif b/autotest/gcore/data/uint16.tif new file mode 100644 index 0000000000000000000000000000000000000000..73fced68e554cf743b7921bfda3174a9e85377e0 GIT binary patch literal 1136 zcmZvbF-sg#6ov2XZY0HGBB%+HV!$Ta1k>0m*cg&+q6pg#f*>Ttc224|u+5ZHE_Pck zVzFh)bmEV&$X^K9nDgDckDVeu?!2@2oqO(icgEx6U3=d(yR_0O8!0NQ{aGm&!L!ua z#2RPt3p7>KepXr`E()wdOq9@}4o>Zv(lzNb5+WMsz@m~pg44!1QCUedIinh0qnIl2 zhu4YlFPmvKTGU5Bv?p?vx;%*e+gY`H4Da|1_i>Y%^U+9JwdrPGb3&<9CB9&>mOI|K-^hl_&J?lI-IxhJq6WuIiYe0_ zM^x2f(Hq^uJ5tYSOf%FP{f>7^FY1TlM0Y%v`=<_pD&4QEbzwup*^R6_RQ|a9orNh3NQZ#2Z7VRJoz6w__K9L{{H5(@8HaD v@WM>GVA=J()4LmQK +# +############################################################################### +# Copyright (c) 2010-2014, Antonio Valentino +# +# Permission is hereby granted, free of charge, to any person obtaining a +# copy of this software and associated documentation files (the "Software"), +# to deal in the Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, distribute, sublicense, +# and/or sell copies of the Software, and to permit persons to whom the +# Software is furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included +# in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +# DEALINGS IN THE SOFTWARE. +############################################################################### + +import sys +import numpy +from osgeo import gdal + +sys.path.append('../pymod') + +import gdaltest + +# @TODO: remove in non-plugin configuration +#gdal.SetConfigOption('CPL_DEBUG', 'PIXFUN') +gdal.SetConfigOption('GDAL_DRIVER_PATH', '../..') +gdal.AllRegister() + + +############################################################################### +# Verify real part extraction from a complex dataset. + +def pixfun_real_c(): + + filename = 'data/pixfun_real_c.vrt' + ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) + if ds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % filename) + return 'fail' + data = ds.GetRasterBand(1).ReadAsArray() + + reffilename = 'data/cint_sar.tif' + refds = gdal.Open(reffilename) + if refds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) + return 'fail' + refdata = refds.GetRasterBand(1).ReadAsArray() + + if not numpy.alltrue(data == refdata.real): + return 'fail' + + return 'success' + + +############################################################################### +# Verify real part extraction from a complex dataset. + +def pixfun_real_r(): + + filename = 'data/pixfun_real_r.vrt' + ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) + if ds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % filename) + return 'fail' + data = ds.GetRasterBand(1).ReadAsArray() + + reffilename = 'data/int32.tif' + refds = gdal.Open(reffilename) + if refds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) + return 'fail' + refdata = refds.GetRasterBand(1).ReadAsArray() + + if not numpy.alltrue(data == refdata.real): + return 'fail' + + return 'success' + + +############################################################################### +# Verify imaginary part extraction from a complex dataset. + +def pixfun_imag_c(): + + filename = 'data/pixfun_imag_c.vrt' + ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) + if ds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % filename) + return 'fail' + data = ds.GetRasterBand(1).ReadAsArray() + + reffilename = 'data/cint_sar.tif' + refds = gdal.Open(reffilename) + if refds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) + return 'fail' + refdata = refds.GetRasterBand(1).ReadAsArray() + + if not numpy.alltrue(data == refdata.imag): + return 'fail' + + return 'success' + + +############################################################################### +# Verify imaginary part extraction from a real dataset. + +def pixfun_imag_r(): + + filename = 'data/pixfun_imag_r.vrt' + ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) + if ds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % filename) + return 'fail' + data = ds.GetRasterBand(1).ReadAsArray() + + if not numpy.alltrue(data == 0): + return 'fail' + + return 'success' + + +############################################################################### +# Verify modulus extraction from a complex (float) dataset. + +def pixfun_mod_c(): + + filename = 'data/pixfun_mod_c.vrt' + ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) + if ds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % filename) + return 'fail' + data = ds.GetRasterBand(1).ReadAsArray() + + reffilename = 'data/cint_sar.tif' + refds = gdal.Open(reffilename) + if refds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) + return 'fail' + refdata = refds.GetRasterBand(1).ReadAsArray() + + if not numpy.alltrue(data == numpy.abs(refdata)): + return 'fail' + + return 'success' + + +############################################################################### +# Verify modulus extraction from a real (integer type) dataset. + +def pixfun_mod_r(): + + filename = 'data/pixfun_mod_r.vrt' + ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) + if ds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % filename) + return 'fail' + data = ds.GetRasterBand(1).ReadAsArray() + + reffilename = 'data/int32.tif' + refds = gdal.Open(reffilename) + if refds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) + return 'fail' + refdata = refds.GetRasterBand(1).ReadAsArray() + + if not numpy.alltrue(data == numpy.abs(refdata)): + return 'fail' + + return 'success' + + +############################################################################### +# Verify phase extraction from a complex dataset. + +def pixfun_phase_c(): + + filename = 'data/pixfun_phase_c.vrt' + ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) + if ds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % filename) + return 'fail' + data = ds.GetRasterBand(1).ReadAsArray() + + reffilename = 'data/cint_sar.tif' + refds = gdal.Open(reffilename) + if refds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) + return 'fail' + refdata = refds.GetRasterBand(1).ReadAsArray() + refdata = refdata.astype('complex128') + + #if not numpy.allclose(data, numpy.arctan2(refdata.imag, refdata.real)): + if not numpy.alltrue(data == numpy.arctan2(refdata.imag, refdata.real)): + return 'fail' + + return 'success' + + +############################################################################### +# Verify phase extraction from a real dataset. + +def pixfun_phase_r(): + + filename = 'data/pixfun_phase_r.vrt' + ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) + if ds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % filename) + return 'fail' + data = ds.GetRasterBand(1).ReadAsArray() + + reffilename = 'data/pixfun_imag_c.vrt' + refds = gdal.Open(reffilename) + if refds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) + return 'fail' + refdata = refds.GetRasterBand(1).ReadAsArray() + + if not numpy.alltrue(data == numpy.arctan2(0, refdata)): + return 'fail' + + return 'success' + + +############################################################################### +# Verify cmplex conjugare computation on a complex dataset. + +def pixfun_conj_c(): + + filename = 'data/pixfun_conj_c.vrt' + ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) + if ds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % filename) + return 'fail' + data = ds.GetRasterBand(1).ReadAsArray() + + reffilename = 'data/cint_sar.tif' + refds = gdal.Open(reffilename) + if refds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) + return 'fail' + refdata = refds.GetRasterBand(1).ReadAsArray() + + if not numpy.alltrue(data == numpy.conj(refdata)): + return 'fail' + + return 'success' + + +############################################################################### +# Verify cmplex conjugare computation on a real dataset. + +def pixfun_conj_r(): + + filename = 'data/pixfun_conj_r.vrt' + ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) + if ds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % filename) + return 'fail' + data = ds.GetRasterBand(1).ReadAsArray() + + reffilename = 'data/int32.tif' + refds = gdal.Open(reffilename) + if refds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) + return 'fail' + refdata = refds.GetRasterBand(1).ReadAsArray() + + if not numpy.alltrue(data == numpy.conj(refdata)): + return 'fail' + + return 'success' + + +############################################################################### +# Verify the sum of 3 (real) datasets. + +def pixfun_sum_r(): + + filename = 'data/pixfun_sum_r.vrt' + ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) + if ds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % filename) + return 'fail' + data = ds.GetRasterBand(1).ReadAsArray() + + refdata = numpy.zeros(data.shape, 'float') + for reffilename in ('data/uint16.tif', 'data/int32.tif', + 'data/float32.tif'): + refds = gdal.Open(reffilename) + if refds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) + return 'fail' + refdata += refds.GetRasterBand(1).ReadAsArray() + + if not numpy.alltrue(data == refdata): + return 'fail' + + return 'success' + + +############################################################################### +# Verify the sum of 3 (two complex and one real) datasets. + +def pixfun_sum_c(): + + filename = 'data/pixfun_sum_c.vrt' + ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) + if ds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % filename) + return 'fail' + data = ds.GetRasterBand(1).ReadAsArray() + + refdata = numpy.zeros(data.shape, 'complex') + for reffilename in ('data/uint16.tif', 'data/cint_sar.tif', + 'data/cfloat64.tif'): + refds = gdal.Open(reffilename) + if refds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) + return 'fail' + refdata += refds.GetRasterBand(1).ReadAsArray(0, 0, 5, 6) + + if not numpy.alltrue(data == refdata): + return 'fail' + + return 'success' + + +############################################################################### +# Verify the difference of 2 (real) datasets. + +def pixfun_diff_r(): + + filename = 'data/pixfun_diff_r.vrt' + ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) + if ds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % filename) + return 'fail' + data = ds.GetRasterBand(1).ReadAsArray() + + reffilename = 'data/int32.tif' + refds = gdal.Open(reffilename) + if refds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) + return 'fail' + refdata1 = refds.GetRasterBand(1).ReadAsArray(0, 0, 5, 6) + + reffilename = 'data/float32.tif' + refds = gdal.Open(reffilename) + if refds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) + return 'fail' + refdata2 = refds.GetRasterBand(1).ReadAsArray(10, 10, 5, 6) + + if not numpy.alltrue(data == refdata1-refdata2): + return 'fail' + + return 'success' + + +############################################################################### +# Verify the difference of 2 (complex) datasets. + +def pixfun_diff_c(): + + filename = 'data/pixfun_diff_c.vrt' + ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) + if ds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % filename) + return 'fail' + data = ds.GetRasterBand(1).ReadAsArray() + + reffilename = 'data/cint_sar.tif' + refds = gdal.Open(reffilename) + if refds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) + return 'fail' + refdata1 = refds.GetRasterBand(1).ReadAsArray() + + reffilename = 'data/cfloat64.tif' + refds = gdal.Open(reffilename) + if refds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) + return 'fail' + refdata2 = refds.GetRasterBand(1).ReadAsArray(0, 0, 5, 6) + + if not numpy.alltrue(data == refdata1-refdata2): + return 'fail' + + return 'success' + + +############################################################################### +# Verify the product of 3 (real) datasets. + +def pixfun_mul_r(): + + filename = 'data/pixfun_mul_r.vrt' + ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) + if ds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % filename) + return 'fail' + data = ds.GetRasterBand(1).ReadAsArray() + + refdata = numpy.ones(data.shape, 'float') + for reffilename in ('data/uint16.tif', 'data/int32.tif', + 'data/float32.tif'): + refds = gdal.Open(reffilename) + if refds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) + return 'fail' + refdata *= refds.GetRasterBand(1).ReadAsArray() + + if not numpy.alltrue(data == refdata): + return 'fail' + + return 'success' + + +############################################################################### +# Verify the product of 2 (complex) datasets. + +def pixfun_mul_c(): + + filename = 'data/pixfun_mul_c.vrt' + ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) + if ds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % filename) + return 'fail' + data = ds.GetRasterBand(1).ReadAsArray() + + reffilename = 'data/cint_sar.tif' + refds = gdal.Open(reffilename) + if refds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) + return 'fail' + refdata = refds.GetRasterBand(1).ReadAsArray() + + if not numpy.alltrue(data == refdata*refdata): + return 'fail' + + return 'success' + + +############################################################################### +# Verify the product with complex conjugate of a complex datasets. + +def pixfun_cmul_c(): + + filename = 'data/pixfun_cmul_c.vrt' + ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) + if ds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % filename) + return 'fail' + data = ds.GetRasterBand(1).ReadAsArray() + + reffilename = 'data/cint_sar.tif' + refds = gdal.Open(reffilename) + if refds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) + return 'fail' + refdata = refds.GetRasterBand(1).ReadAsArray() + + if not numpy.alltrue(data == refdata*refdata.conj()): + return 'fail' + + return 'success' + + +############################################################################### +# Verify the product with complex conjugate of two real datasets. + +def pixfun_cmul_r(): + + filename = 'data/pixfun_cmul_r.vrt' + ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) + if ds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % filename) + return 'fail' + data = ds.GetRasterBand(1).ReadAsArray() + + reffilename = 'data/uint16.tif' + refds = gdal.Open(reffilename) + if refds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) + return 'fail' + refdata1 = refds.GetRasterBand(1).ReadAsArray() + refdata1 = refdata1.astype('float64') + + reffilename = 'data/int32.tif' + refds = gdal.Open(reffilename) + if refds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) + return 'fail' + refdata2 = refds.GetRasterBand(1).ReadAsArray() + refdata2 = refdata2.astype('float64') + + if not numpy.alltrue(data == refdata1 * refdata2.conj()): + return 'fail' + + return 'success' + + +############################################################################### +# Verify computation of the inverse of a real datasets. + +def pixfun_inv_r(): + + filename = 'data/pixfun_inv_r.vrt' + ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) + if ds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % filename) + return 'fail' + data = ds.GetRasterBand(1).ReadAsArray() + + reffilename = 'data/uint16.tif' + refds = gdal.Open(reffilename) + if refds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) + return 'fail' + refdata = refds.GetRasterBand(1).ReadAsArray() + refdata = refdata.astype('float64') + + if not numpy.alltrue(data == 1./refdata): + return 'fail' + + return 'success' + + +############################################################################### +# Verify computation of the inverse of a complex datasets. + +def pixfun_inv_c(): + + filename = 'data/pixfun_inv_c.vrt' + ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) + if ds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % filename) + return 'fail' + data = ds.GetRasterBand(1).ReadAsArray() + + reffilename = 'data/cint_sar.tif' + refds = gdal.Open(reffilename) + if refds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) + return 'fail' + refdata = refds.GetRasterBand(1).ReadAsArray() + refdata = refdata.astype('complex') + delta = data - 1./refdata + + if not numpy.alltrue(abs(delta.real) < 1e-13): + return 'fail' + if not numpy.alltrue(abs(delta.imag) < 1e-13): + return 'fail' + + return 'success' + + +############################################################################### +# Verify intensity computation of a complex dataset. + +def pixfun_intensity_c(): + + filename = 'data/pixfun_intensity_c.vrt' + ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) + if ds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % filename) + return 'fail' + data = ds.GetRasterBand(1).ReadAsArray() + + reffilename = 'data/cint_sar.tif' + refds = gdal.Open(reffilename) + if refds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) + return 'fail' + refdata = refds.GetRasterBand(1).ReadAsArray() + + if not numpy.alltrue(data == (refdata*refdata.conj()).real): + return 'fail' + + return 'success' + + +############################################################################### +# Verify intensity computation of real dataset. + +def pixfun_intensity_r(): + + filename = 'data/pixfun_intensity_r.vrt' + ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) + if ds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % filename) + return 'fail' + data = ds.GetRasterBand(1).ReadAsArray() + + reffilename = 'data/float32.tif' + refds = gdal.Open(reffilename) + if refds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) + return 'fail' + refdata = refds.GetRasterBand(1).ReadAsArray() + + if not numpy.alltrue(data == (refdata*refdata.conj()).real): + return 'fail' + + return 'success' + + +############################################################################### +# Verify square root computation. + +def pixfun_sqrt(): + + filename = 'data/pixfun_sqrt.vrt' + ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) + if ds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % filename) + return 'fail' + data = ds.GetRasterBand(1).ReadAsArray() + + reffilename = 'data/float32.tif' + refds = gdal.Open(reffilename) + if refds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) + return 'fail' + refdata = refds.GetRasterBand(1).ReadAsArray() + + if not numpy.alltrue(data == numpy.sqrt(refdata)): + return 'fail' + + return 'success' + + +############################################################################### +# Verify logarithm computation. + +def pixfun_log10(): + + filename = 'data/pixfun_log10.vrt' + ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) + if ds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % filename) + return 'fail' + data = ds.GetRasterBand(1).ReadAsArray() + + reffilename = 'data/float32.tif' + refds = gdal.Open(reffilename) + if refds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) + return 'fail' + refdata = refds.GetRasterBand(1).ReadAsArray() + + if not numpy.alltrue(data == numpy.log10(refdata)): + return 'fail' + + return 'success' + + +############################################################################### +# Verify conversion from dB to amplitude. + +def pixfun_dB2amp(): + + filename = 'data/pixfun_dB2amp.vrt' + ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) + if ds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % filename) + return 'fail' + data = ds.GetRasterBand(1).ReadAsArray() + + reffilename = 'data/float32.tif' + refds = gdal.Open(reffilename) + if refds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) + return 'fail' + refdata = refds.GetRasterBand(1).ReadAsArray() + + #if not numpy.alltrue(data == 10.**(refdata/20.)): + if not numpy.allclose(data, 10.**(refdata/20.)): + return 'fail' + + return 'success' + + +############################################################################### +# Verify conversion from dB to power. + +def pixfun_dB2pow(): + + filename = 'data/pixfun_dB2pow.vrt' + ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) + if ds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % filename) + return 'fail' + data = ds.GetRasterBand(1).ReadAsArray() + + reffilename = 'data/float32.tif' + refds = gdal.Open(reffilename) + if refds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) + return 'fail' + refdata = refds.GetRasterBand(1).ReadAsArray() + refdata = refdata.astype('float64') + + #if not numpy.allclose(data, 10.**(refdata/10.)): + if not numpy.alltrue(data == 10.**(refdata/10.)): + return 'fail' + + return 'success' + + +############################################################################### + +gdaltest_list = [ + pixfun_real_c, + pixfun_real_r, + pixfun_imag_c, + pixfun_imag_r, + pixfun_mod_c, + pixfun_mod_r, + pixfun_phase_c, + pixfun_phase_r, + pixfun_conj_c, + pixfun_conj_r, + pixfun_sum_r, + pixfun_sum_c, + pixfun_diff_r, + pixfun_diff_c, + pixfun_mul_r, + pixfun_mul_c, + pixfun_cmul_c, + pixfun_cmul_r, + pixfun_inv_r, + pixfun_inv_c, + pixfun_intensity_c, + pixfun_intensity_r, + pixfun_sqrt, + pixfun_log10, + pixfun_dB2amp, + pixfun_dB2pow, +] + + +if __name__ == '__main__': + gdaltest.setup_run('pixfun') + gdaltest.run_tests(gdaltest_list) + gdaltest.summarize() diff --git a/autotest/gcore/testnonboundtoswig.py b/autotest/gcore/testnonboundtoswig.py new file mode 100644 index 000000000000..f6d8fe90b74b --- /dev/null +++ b/autotest/gcore/testnonboundtoswig.py @@ -0,0 +1,383 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +############################################################################### +# $Id$ +# +# Project: GDAL/OGR Test Suite +# Purpose: Test GDAL functions not bound SWIG with ctypes +# Author: Even Rouault, +# +############################################################################### +# Copyright (c) 2011-2012, Even Rouault +# +# Permission is hereby granted, free of charge, to any person obtaining a +# copy of this software and associated documentation files (the "Software"), +# to deal in the Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, distribute, sublicense, +# and/or sell copies of the Software, and to permit persons to whom the +# Software is furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included +# in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +# DEALINGS IN THE SOFTWARE. +############################################################################### + +from osgeo import gdal +import sys +import os +from sys import version_info + +sys.path.append( '../pymod' ) + +try: + import ctypes +except: + pass + +import gdaltest + +gdal_handle_init = False +gdal_handle = None +gdal_handle_stdcall = None + +############################################################################### +# find_libgdal() + +def find_libgdal(): + return gdaltest.find_lib('gdal') + +############################################################################### +# Init + +def testnonboundtoswig_init(): + + global gdal_handle_init, gdal_handle, gdal_handle_stdcall + + if gdal_handle_init: + if gdal_handle is None: + return 'skip' + return 'success' + + gdal_handle_init = True + + try: + ctypes.cdll + except: + print('cannot find ctypes') + return 'skip' + + name = find_libgdal() + if name is None: + return 'skip' + + print('Found libgdal we are running against : %s' % name) + + static_version = gdal.VersionInfo(None) + short_static_version = static_version[0:2] + + try: + gdal_handle = ctypes.cdll.LoadLibrary(name) + try: + gdal_handle_stdcall = ctypes.windll.LoadLibrary(name) + except: + gdal_handle_stdcall = gdal_handle + + gdal_handle_stdcall.GDALVersionInfo.argtypes = [ ctypes.c_char_p ] + gdal_handle_stdcall.GDALVersionInfo.restype = ctypes.c_char_p + + dynamic_version = gdal_handle_stdcall.GDALVersionInfo(None) + if version_info >= (3,0,0): + dynamic_version = str(dynamic_version, 'utf-8') + + if dynamic_version != static_version: + gdaltest.post_reason('dynamic version(%s) does not match static version (%s)' % (dynamic_version, static_version)) + gdal_handle = None + gdal_handle_stdcall = None + return 'skip' + + return 'success' + except: + print('cannot find gdal shared object') + return 'skip' + +############################################################################### +# Call GDALDestroyDriverManager() + +def GDALDestroyDriverManager(): + + if gdal_handle is None: + testnonboundtoswig_init() + + if gdal_handle is None: + return 'skip' + + gdal_handle_stdcall.GDALDestroyDriverManager.argtypes = [ ] + gdal_handle_stdcall.GDALDestroyDriverManager.restype = None + + gdal_handle_stdcall.GDALDestroyDriverManager() + + return 'success' + +############################################################################### +# Call OGRCleanupAll() + +def OGRCleanupAll(): + + if gdal_handle is None: + testnonboundtoswig_init() + + if gdal_handle is None: + return 'skip' + + gdal_handle_stdcall.OGRCleanupAll.argtypes = [ ] + gdal_handle_stdcall.OGRCleanupAll.restype = None + + gdal_handle_stdcall.OGRCleanupAll() + + return 'success' + +############################################################################### +# Call OSRCleanup() + +def OSRCleanup(): + + if gdal_handle is None: + testnonboundtoswig_init() + + if gdal_handle is None: + return 'skip' + + gdal_handle.OSRCleanup.argtypes = [ ] + gdal_handle.OSRCleanup.restype = None + + gdal_handle.OSRCleanup() + + return 'success' + +############################################################################### +# Test GDALSimpleImageWarp + +def testnonboundtoswig_GDALSimpleImageWarp(): + + if gdal_handle is None: + return 'skip' + + src_ds = gdal.Open('data/byte.tif') + gt = src_ds.GetGeoTransform() + wkt = src_ds.GetProjectionRef() + src_ds = None + + gdal_handle_stdcall.GDALOpen.argtypes = [ ctypes.c_char_p, ctypes.c_int] + gdal_handle_stdcall.GDALOpen.restype = ctypes.c_void_p + + gdal_handle_stdcall.GDALClose.argtypes = [ ctypes.c_void_p ] + gdal_handle_stdcall.GDALClose.restype = None + + gdal_handle.GDALCreateGenImgProjTransformer2.restype = ctypes.c_void_p + gdal_handle.GDALCreateGenImgProjTransformer2.argtypes = [ ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p] + + gdal_handle_stdcall.GDALSimpleImageWarp.argtypes = [ ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p] + gdal_handle_stdcall.GDALSimpleImageWarp.restype = ctypes.c_int + + gdal_handle.GDALDestroyGenImgProjTransformer.argtypes = [ ctypes.c_void_p ] + gdal_handle.GDALDestroyGenImgProjTransformer.restype = None + + out_ds = gdal.GetDriverByName('GTiff').Create('/vsimem/out.tif', 20, 20, 1) + out_ds.SetGeoTransform(gt) + out_ds.SetProjection(wkt) + out_ds = None + + filename = 'data/byte.tif' + if version_info >= (3,0,0): + filename = bytes(filename, 'utf-8') + + native_in_ds = gdal_handle_stdcall.GDALOpen(filename, gdal.GA_ReadOnly) + if native_in_ds is None: + gdaltest.post_reason('fail') + return 'fail' + + filename = '/vsimem/out.tif' + if version_info >= (3,0,0): + filename = bytes(filename, 'utf-8') + + native_out_ds = gdal_handle_stdcall.GDALOpen(filename, gdal.GA_Update) + if native_out_ds is None: + gdaltest.post_reason('fail') + return 'fail' + + pTransformerArg = gdal_handle.GDALCreateGenImgProjTransformer2( native_in_ds, native_out_ds, None ) + if pTransformerArg is None: + gdaltest.post_reason('fail') + return 'fail' + + ret = gdal_handle_stdcall.GDALSimpleImageWarp(native_in_ds, native_out_ds, 0, None, gdal_handle_stdcall.GDALGenImgProjTransform, pTransformerArg, None, None, None) + if ret != 1: + gdaltest.post_reason('fail') + print(ret) + return 'fail' + + gdal_handle.GDALDestroyGenImgProjTransformer(pTransformerArg) + + gdal_handle_stdcall.GDALClose(native_in_ds) + gdal_handle_stdcall.GDALClose(native_out_ds) + + ds = gdal.Open('/vsimem/out.tif') + cs = ds.GetRasterBand(1).Checksum() + ds = None + + gdal.Unlink('/vsimem/out.tif') + + if cs != 4672: + gdaltest.post_reason('fail') + print(cs) + return 'fail' + + return 'success' + +############################################################################### +# Test VRT derived bands with callback functions implemented in Python! + +def GDALTypeToCTypes(gdaltype): + + if gdaltype == gdal.GDT_Byte: + return ctypes.c_ubyte + elif gdaltype == gdal.GDT_Int16: + return ctypes.c_short + elif gdaltype == gdal.GDT_UInt16: + return ctypes.c_ushort + elif gdaltype == gdal.GDT_Int32: + return ctypes.c_int + elif gdaltype == gdal.GDT_UInt32: + return ctypes.c_uint + elif gdaltype == gdal.GDT_Float32: + return ctypes.c_float + elif gdaltype == gdal.GDT_Float64: + return ctypes.c_double + else: + return None + +def my_pyDerivedPixelFunc(papoSources, nSources, pData, nBufXSize, nBufYSize, eSrcType, eBufType, nPixelSpace, nLineSpace): + if nSources != 1: + print(nSources) + gdaltest.post_reason('did not get expected nSources') + return 1 + + srcctype = GDALTypeToCTypes(eSrcType) + if srcctype is None: + print(eSrcType) + gdaltest.post_reason('did not get expected eSrcType') + return 1 + + dstctype = GDALTypeToCTypes(eBufType) + if dstctype is None: + print(eBufType) + gdaltest.post_reason('did not get expected eBufType') + return 1 + + if nPixelSpace != gdal.GetDataTypeSize(eBufType) / 8: + print(nPixelSpace) + gdaltest.post_reason('did not get expected nPixelSpace') + return 1 + + if (nLineSpace % nPixelSpace) != 0: + print(nLineSpace) + gdaltest.post_reason('did not get expected nLineSpace') + return 1 + + nLineStride = (int)(nLineSpace/nPixelSpace) + + srcValues = ctypes.cast(papoSources[0], ctypes.POINTER(srcctype)) + dstValues = ctypes.cast(pData, ctypes.POINTER(dstctype)) + for j in range(nBufYSize): + for i in range(nBufXSize): + dstValues[j * nLineStride + i] = srcValues[j * nBufXSize + i] + + return 0 + +def testnonboundtoswig_VRTDerivedBands(): + + if gdal_handle is None: + return 'skip' + + DerivedPixelFuncType = ctypes.CFUNCTYPE(ctypes.c_int, # ret CPLErr + ctypes.POINTER(ctypes.c_void_p), # void **papoSources + ctypes.c_int, # int nSources + ctypes.c_void_p, #void *pData + ctypes.c_int, #int nBufXSize + ctypes.c_int, #int nBufYSize + ctypes.c_int, # GDALDataType eSrcType + ctypes.c_int, # GDALDataType eBufType + ctypes.c_int, #int nPixelSpace + ctypes.c_int ) #int nLineSpace + + my_cDerivedPixelFunc = DerivedPixelFuncType(my_pyDerivedPixelFunc) + + #CPLErr CPL_DLL CPL_STDCALL GDALAddDerivedBandPixelFunc( const char *pszName, + # GDALDerivedPixelFunc pfnPixelFunc ); + + gdal_handle_stdcall.GDALAddDerivedBandPixelFunc.argtypes = [ ctypes.c_char_p, DerivedPixelFuncType] + gdal_handle_stdcall.GDALAddDerivedBandPixelFunc.restype = ctypes.c_int + + funcName = "pyDerivedPixelFunc" + if version_info >= (3,0,0): + funcName = bytes(funcName, 'utf-8') + ret = gdal_handle_stdcall.GDALAddDerivedBandPixelFunc(funcName, my_cDerivedPixelFunc) + if ret != 0: + gdaltest.post_reason('fail') + return 'fail' + + vrt_xml = """ + + pyDerivedPixelFunc + Byte + + data/byte.tif + 1 + + + + +""" + + src_ds = gdal.Open('data/byte.tif') + ref_cs = src_ds.GetRasterBand(1).Checksum() + ref_data = src_ds.GetRasterBand(1).ReadRaster(0,0,20,20) + src_ds = None + + ds = gdal.Open(vrt_xml) + got_cs = ds.GetRasterBand(1).Checksum() + got_data = ds.GetRasterBand(1).ReadRaster(0,0,20,20) + ds = None + + if ref_cs != got_cs: + gdaltest.post_reason('wrong checksum') + print(got_cs) + return 'fail' + + if ref_data != got_data: + gdaltest.post_reason('wrong data') + print(ref_data) + print(got_data) + return 'fail' + + return 'success' + +gdaltest_list = [ testnonboundtoswig_init, + testnonboundtoswig_GDALSimpleImageWarp, + testnonboundtoswig_VRTDerivedBands ] + +if __name__ == '__main__': + + gdaltest.setup_run( 'testnonboundtoswig' ) + + gdaltest.run_tests( gdaltest_list ) + + gdaltest.summarize() + diff --git a/autotest/pymod/gdaltest.py b/autotest/pymod/gdaltest.py new file mode 100644 index 000000000000..4802818fc606 --- /dev/null +++ b/autotest/pymod/gdaltest.py @@ -0,0 +1,1578 @@ +# -*- coding: utf-8 -*- +############################################################################### +# $Id$ +# +# Project: GDAL/OGR Test Suite +# Purpose: Python Library supporting GDAL/OGR Test Suite +# Author: Frank Warmerdam +# +############################################################################### +# Copyright (c) 2003, Frank Warmerdam +# Copyright (c) 2008-2013, Even Rouault +# +# Permission is hereby granted, free of charge, to any person obtaining a +# copy of this software and associated documentation files (the "Software"), +# to deal in the Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, distribute, sublicense, +# and/or sell copies of the Software, and to permit persons to whom the +# Software is furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included +# in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +# DEALINGS IN THE SOFTWARE. +############################################################################### + +import sys +import string +import os +import time + +from osgeo import gdal +from osgeo import osr + +cur_name = 'default' + +success_counter = 0 +failure_counter = 0 +expected_failure_counter = 0 +blow_counter = 0 +skip_counter = 0 +failure_summary = [] + +reason = None +count_skipped_tests_download = 0 +count_skipped_tests_slow = 0 +start_time = None +end_time = None + +jp2kak_drv = None +jpeg2000_drv = None +jp2ecw_drv = None +jp2mrsid_drv = None +jp2kak_drv_unregistered = False +jpeg2000_drv_unregistered = False +jp2ecw_drv_unregistered = False +jp2mrsid_drv_unregistered = False + +from sys import version_info +if version_info >= (3,0,0): + from gdaltest_python3 import * +else: + from gdaltest_python2 import * + +# Process commandline arguments for stuff like --debug, --locale, --config + +argv = gdal.GeneralCmdLineProcessor( sys.argv ) + +############################################################################### + +def setup_run( name ): + + if 'APPLY_LOCALE' in os.environ: + import locale + locale.setlocale(locale.LC_ALL, '') + + global cur_name + cur_name = name + +############################################################################### + +def run_tests( test_list ): + global success_counter, failure_counter, expected_failure_counter, blow_counter, skip_counter + global reason, failure_summary, cur_name + global start_time, end_time + + set_time = start_time is None + if set_time: + start_time = time.time() + had_errors_this_script = 0 + + for test_item in test_list: + if test_item is None: + continue + + try: + (func, name) = test_item + if func.__name__[:4] == 'test': + outline = ' TEST: ' + func.__name__[4:] + ': ' + name + ' ... ' + else: + outline = ' TEST: ' + func.__name__ + ': ' + name + ' ... ' + except: + func = test_item + name = func.__name__ + outline = ' TEST: ' + name + ' ... ' + + sys.stdout.write( outline ) + sys.stdout.flush() + + reason = None + result = run_func(func) + + if result[:4] == 'fail': + if had_errors_this_script == 0: + failure_summary.append( 'Script: ' + cur_name ) + had_errors_this_script = 1 + failure_summary.append( outline + result ) + if reason is not None: + failure_summary.append( ' ' + reason ) + + if reason is not None: + print((' ' + reason)) + + if result == 'success': + success_counter = success_counter + 1 + elif result == 'expected_fail': + expected_failure_counter = expected_failure_counter + 1 + elif result == 'fail': + failure_counter = failure_counter + 1 + elif result == 'skip': + skip_counter = skip_counter + 1 + else: + blow_counter = blow_counter + 1 + + if set_time: + end_time = time.time() + +############################################################################### + +def get_lineno_2framesback( frames ): + try: + import inspect + frame = inspect.currentframe() + while frames > 0: + frame = frame.f_back + frames = frames-1 + + return frame.f_lineno + except: + return -1 + +############################################################################### + +def post_reason( msg, frames=2 ): + lineno = get_lineno_2framesback( frames ) + global reason + + if lineno >= 0: + reason = 'line %d: %s' % (lineno, msg) + else: + reason = msg + +############################################################################### + +def summarize(): + global count_skipped_tests_download, count_skipped_tests_slow + global success_counter, failure_counter, blow_counter, skip_counter + global cur_name + global start_time, end_time + + print('') + if cur_name is not None: + print('Test Script: %s' % cur_name) + print('Succeeded: %d' % success_counter) + print('Failed: %d (%d blew exceptions)' \ + % (failure_counter+blow_counter, blow_counter)) + print('Skipped: %d' % skip_counter) + print('Expected fail:%d' % expected_failure_counter) + if start_time is not None: + duration = end_time - start_time + if duration >= 60: + print('Duration: %02dm%02.1fs' % (duration / 60., duration % 60.)) + else: + print('Duration: %02.2fs' % duration) + if count_skipped_tests_download != 0: + print('As GDAL_DOWNLOAD_TEST_DATA environment variable is not defined, %d tests relying on data to downloaded from the Web have been skipped' % count_skipped_tests_download) + if count_skipped_tests_slow != 0: + print('As GDAL_RUN_SLOW_TESTS environment variable is not defined, %d "slow" tests have been skipped' % count_skipped_tests_slow) + print('') + + sys.path.append( 'gcore' ) + sys.path.append( '../gcore' ) + import testnonboundtoswig + # Do it twice to ensure that cleanup routines properly do their jobs + for i in range(2): + testnonboundtoswig.OSRCleanup() + testnonboundtoswig.GDALDestroyDriverManager() + testnonboundtoswig.OGRCleanupAll() + + return failure_counter + blow_counter + +############################################################################### + +def run_all( dirlist, option_list ): + + global start_time, end_time + global cur_name + + start_time = time.time() + + for dir_name in dirlist: + files = os.listdir(dir_name) + + old_path = sys.path + sys.path.append('.') + + for file in files: + if not file[-3:] == '.py': + continue + + module = file[:-3] + try: + wd = os.getcwd() + os.chdir( dir_name ) + + exec("import " + module) + try: + print('Running tests from %s/%s' % (dir_name,file)) + setup_run( '%s/%s' % (dir_name,file) ) + exec("run_tests( " + module + ".gdaltest_list)") + except: + pass + + os.chdir( wd ) + + except: + os.chdir( wd ) + print('... failed to load %s ... skipping.' % file) + + import traceback + traceback.print_exc() + + # We only add the tool directory to the python path long enough + # to load the tool files. + sys.path = old_path + + end_time = time.time() + cur_name = None + + if len(failure_summary) > 0: + print('') + print(' ------------ Failures ------------') + for item in failure_summary: + print(item) + print(' ----------------------------------') + +############################################################################### + +def clean_tmp(): + all_files = os.listdir('tmp') + for file in all_files: + if file == 'CVS' or file == 'do-not-remove': + continue + + try: + os.remove( 'tmp/' + file ) + except: + pass + return 'success' + +############################################################################### +def testCreateCopyInterruptCallback(pct, message, user_data): + if pct > 0.5: + return 0 # to stop + else: + return 1 # to continue + +############################################################################### + +class GDALTest: + def __init__(self, drivername, filename, band, chksum, + xoff = 0, yoff = 0, xsize = 0, ysize = 0, options = [], + filename_absolute = 0 ): + self.driver = None + self.drivername = drivername + self.filename = filename + self.filename_absolute = filename_absolute + self.band = band + self.chksum = chksum + self.xoff = xoff + self.yoff = yoff + self.xsize = xsize + self.ysize = ysize + self.options = options + + def testDriver(self): + if self.driver is None: + self.driver = gdal.GetDriverByName( self.drivername ) + if self.driver is None: + post_reason( self.drivername + ' driver not found!' ) + return 'fail' + + return 'success' + + def testOpen(self, check_prj = None, check_gt = None, gt_epsilon = None, \ + check_stat = None, check_approx_stat = None, \ + stat_epsilon = None, skip_checksum = None): + """check_prj - projection reference, check_gt - geotransformation + matrix (tuple), gt_epsilon - geotransformation tolerance, + check_stat - band statistics (tuple), stat_epsilon - statistics + tolerance.""" + if self.testDriver() == 'fail': + return 'skip' + + if self.filename_absolute: + wrk_filename = self.filename + else: + wrk_filename = 'data/' + self.filename + + ds = gdal.Open( wrk_filename, gdal.GA_ReadOnly ) + + if ds is None: + post_reason( 'Failed to open dataset: ' + wrk_filename ) + return 'fail' + + if ds.GetDriver().ShortName != gdal.GetDriverByName( self.drivername ).ShortName: + post_reason( 'The driver of the returned dataset is %s instead of %s.' % ( ds.GetDriver().ShortName, self.drivername ) ) + return 'fail' + + if self.xsize == 0 and self.ysize == 0: + self.xsize = ds.RasterXSize + self.ysize = ds.RasterYSize + + # Do we need to check projection? + if check_prj is not None: + new_prj = ds.GetProjection() + + src_osr = osr.SpatialReference() + src_osr.SetFromUserInput( check_prj ) + + new_osr = osr.SpatialReference( wkt=new_prj ) + + if not src_osr.IsSame(new_osr): + print('') + print('old = %s' % src_osr.ExportToPrettyWkt()) + print('new = %s' % new_osr.ExportToPrettyWkt()) + post_reason( 'Projections differ' ) + return 'fail' + + # Do we need to check geotransform? + if check_gt: + # Default to 100th of pixel as our test value. + if gt_epsilon is None: + gt_epsilon = (abs(check_gt[1])+abs(check_gt[2])) / 100.0 + + new_gt = ds.GetGeoTransform() + for i in range(6): + if abs(new_gt[i]-check_gt[i]) > gt_epsilon: + print('') + print('old = ', check_gt) + print('new = ', new_gt) + post_reason( 'Geotransform differs.' ) + return 'fail' + + oBand = ds.GetRasterBand(self.band) + if skip_checksum is None: + chksum = oBand.Checksum(self.xoff, self.yoff, self.xsize, self.ysize) + + # Do we need to check approximate statistics? + if check_approx_stat: + # Default to 1000th of pixel value range as our test value. + if stat_epsilon is None: + stat_epsilon = \ + abs(check_approx_stat[1] - check_approx_stat[0]) / 1000.0 + + new_stat = oBand.GetStatistics(1, 1) + for i in range(4): + + # NOTE - mloskot: Poor man Nan/Inf value check. It's poor + # because we need to support old and buggy Python 2.3. + # Tested on Linux, Mac OS X and Windows, with Python 2.3/2.4/2.5. + sv = str(new_stat[i]).lower() + if sv.find('n') >= 0 or sv.find('i') >= 0 or sv.find('#') >= 0: + post_reason( 'NaN or Invinite value encountered '%'.' % sv ) + return 'fail' + + if abs(new_stat[i]-check_approx_stat[i]) > stat_epsilon: + print('') + print('old = ', check_approx_stat) + print('new = ', new_stat) + post_reason( 'Approximate statistics differs.' ) + return 'fail' + + # Do we need to check statistics? + if check_stat: + # Default to 1000th of pixel value range as our test value. + if stat_epsilon is None: + stat_epsilon = abs(check_stat[1] - check_stat[0]) / 1000.0 + + # FIXME: how to test approximate statistic results? + new_stat = oBand.GetStatistics(1, 1) + + new_stat = oBand.GetStatistics(0, 1) + for i in range(4): + + sv = str(new_stat[i]).lower() + if sv.find('n') >= 0 or sv.find('i') >= 0 or sv.find('#') >= 0: + post_reason( 'NaN or Invinite value encountered '%'.' % sv ) + return 'fail' + + if abs(new_stat[i]-check_stat[i]) > stat_epsilon: + print('') + print('old = ', check_stat) + print('new = ', new_stat) + post_reason( 'Statistics differs.' ) + return 'fail' + + ds = None + + if is_file_open(wrk_filename): + post_reason('file still open after dataset closing') + return 'fail' + + if skip_checksum is not None: + return 'success' + elif self.chksum is None or chksum == self.chksum: + return 'success' + else: + post_reason('Checksum for band %d in "%s" is %d, but expected %d.' \ + % (self.band, self.filename, chksum, self.chksum) ) + return 'fail' + + def testCreateCopy(self, check_minmax = 1, check_gt = 0, check_srs = None, + vsimem = 0, new_filename = None, strict_in = 0, + skip_preclose_test = 0, delete_copy = 1, gt_epsilon = None, + check_checksum_not_null = None, interrupt_during_copy = False): + + if self.testDriver() == 'fail': + return 'skip' + + if self.filename_absolute: + wrk_filename = self.filename + else: + wrk_filename = 'data/' + self.filename + + src_ds = gdal.Open( wrk_filename ) + if self.band > 0: + minmax = src_ds.GetRasterBand(self.band).ComputeRasterMinMax() + + src_prj = src_ds.GetProjection() + src_gt = src_ds.GetGeoTransform() + + if new_filename is None: + if vsimem: + new_filename = '/vsimem/' + self.filename + '.tst' + else: + new_filename = 'tmp/' + self.filename + '.tst' + + gdal.PushErrorHandler( 'CPLQuietErrorHandler' ) + if interrupt_during_copy: + new_ds = self.driver.CreateCopy( new_filename, src_ds, + strict = strict_in, + options = self.options, + callback = testCreateCopyInterruptCallback) + else: + new_ds = self.driver.CreateCopy( new_filename, src_ds, + strict = strict_in, + options = self.options ) + gdal.PopErrorHandler() + + if interrupt_during_copy: + if new_ds is None: + gdal.PushErrorHandler( 'CPLQuietErrorHandler' ) + self.driver.Delete( new_filename ) + gdal.PopErrorHandler() + return 'success' + else: + post_reason( 'CreateCopy() should have failed due to interruption') + new_ds = None + self.driver.Delete( new_filename ) + return 'fail' + + if new_ds is None: + post_reason( 'Failed to create test file using CreateCopy method.'\ + + '\n' + gdal.GetLastErrorMsg() ) + return 'fail' + + if new_ds.GetDriver().ShortName != gdal.GetDriverByName( self.drivername ).ShortName: + post_reason( 'The driver of the returned dataset is %s instead of %s.' % ( new_ds.GetDriver().ShortName, self.drivername ) ) + return 'fail' + + if self.band > 0 and skip_preclose_test == 0: + bnd = new_ds.GetRasterBand(self.band) + if check_checksum_not_null is True: + if bnd.Checksum() == 0: + post_reason('Got null checksum on still-open file.') + return 'fail' + elif self.chksum is not None and bnd.Checksum() != self.chksum: + post_reason( + 'Did not get expected checksum on still-open file.\n' \ + ' Got %d instead of %d.' % (bnd.Checksum(),self.chksum)) + return 'fail' + if check_minmax: + got_minmax = bnd.ComputeRasterMinMax() + if got_minmax != minmax: + post_reason( \ + 'Did not get expected min/max values on still-open file.\n' \ + ' Got %g,%g instead of %g,%g.' \ + % ( got_minmax[0], got_minmax[1], minmax[0], minmax[1] ) ) + return 'fail' + + bnd = None + new_ds = None + + # hopefully it's closed now! + + new_ds = gdal.Open( new_filename ) + if new_ds is None: + post_reason( 'Failed to open dataset: ' + new_filename ) + return 'fail' + + if self.band > 0: + bnd = new_ds.GetRasterBand(self.band) + if check_checksum_not_null is True: + if bnd.Checksum() == 0: + post_reason('Got null checksum on reopened file.') + return 'fail' + elif self.chksum is not None and bnd.Checksum() != self.chksum: + post_reason( 'Did not get expected checksum on reopened file.\n' + ' Got %d instead of %d.' \ + % (bnd.Checksum(), self.chksum) ) + return 'fail' + + if check_minmax: + got_minmax = bnd.ComputeRasterMinMax() + if got_minmax != minmax: + post_reason( \ + 'Did not get expected min/max values on reopened file.\n' \ + ' Got %g,%g instead of %g,%g.' \ + % ( got_minmax[0], got_minmax[1], minmax[0], minmax[1] ) ) + return 'fail' + + # Do we need to check the geotransform? + if check_gt: + if gt_epsilon is None: + eps = 0.00000001 + else: + eps = gt_epsilon + new_gt = new_ds.GetGeoTransform() + if abs(new_gt[0] - src_gt[0]) > eps \ + or abs(new_gt[1] - src_gt[1]) > eps \ + or abs(new_gt[2] - src_gt[2]) > eps \ + or abs(new_gt[3] - src_gt[3]) > eps \ + or abs(new_gt[4] - src_gt[4]) > eps \ + or abs(new_gt[5] - src_gt[5]) > eps: + print('') + print('old = ', src_gt) + print('new = ', new_gt) + post_reason( 'Geotransform differs.' ) + return 'fail' + + # Do we need to check the geotransform? + if check_srs is not None: + new_prj = new_ds.GetProjection() + + src_osr = osr.SpatialReference( wkt=src_prj ) + new_osr = osr.SpatialReference( wkt=new_prj ) + + if not src_osr.IsSame(new_osr): + print('') + print('old = %s' % src_osr.ExportToPrettyWkt()) + print('new = %s' % new_osr.ExportToPrettyWkt()) + post_reason( 'Projections differ' ) + return 'fail' + + bnd = None + new_ds = None + src_ds = None + + if gdal.GetConfigOption( 'CPL_DEBUG', 'OFF' ) != 'ON' and delete_copy == 1 : + self.driver.Delete( new_filename ) + + return 'success' + + def testCreate(self, vsimem = 0, new_filename = None, out_bands = 1, + check_minmax = 1 ): + if self.testDriver() == 'fail': + return 'skip' + + if self.filename_absolute: + wrk_filename = self.filename + else: + wrk_filename = 'data/' + self.filename + + src_ds = gdal.Open( wrk_filename ) + xsize = src_ds.RasterXSize + ysize = src_ds.RasterYSize + src_img = src_ds.GetRasterBand(self.band).ReadRaster(0,0,xsize,ysize) + minmax = src_ds.GetRasterBand(self.band).ComputeRasterMinMax() + + if new_filename is None: + if vsimem: + new_filename = '/vsimem/' + self.filename + '.tst' + else: + new_filename = 'tmp/' + self.filename + '.tst' + + new_ds = self.driver.Create( new_filename, xsize, ysize, out_bands, + src_ds.GetRasterBand(self.band).DataType, + options = self.options ) + if new_ds is None: + post_reason( 'Failed to create test file using Create method.' ) + return 'fail' + + src_ds = None + + try: + for band in range(1,out_bands+1): + new_ds.GetRasterBand(band).WriteRaster( 0, 0, xsize, ysize, src_img ) + except: + post_reason( 'Failed to write raster bands to test file.' ) + return 'fail' + + for band in range(1,out_bands+1): + if self.chksum is not None \ + and new_ds.GetRasterBand(band).Checksum() != self.chksum: + post_reason( + 'Did not get expected checksum on still-open file.\n' \ + ' Got %d instead of %d.' \ + % (new_ds.GetRasterBand(band).Checksum(),self.chksum)) + + return 'fail' + + computed_minmax = new_ds.GetRasterBand(band).ComputeRasterMinMax() + if computed_minmax != minmax and check_minmax: + post_reason( 'Did not get expected min/max values on still-open file.' ) + print('expect: ', minmax) + print('got: ', computed_minmax) + return 'fail' + + new_ds = None + + new_ds = gdal.Open( new_filename ) + if new_ds is None: + post_reason( 'Failed to open dataset: ' + new_filename ) + return 'fail' + + for band in range(1,out_bands+1): + if self.chksum is not None \ + and new_ds.GetRasterBand(band).Checksum() != self.chksum: + post_reason( 'Did not get expected checksum on reopened file.' \ + ' Got %d instead of %d.' \ + % (new_ds.GetRasterBand(band).Checksum(),self.chksum)) + return 'fail' + + if new_ds.GetRasterBand(band).ComputeRasterMinMax() != minmax and check_minmax: + post_reason( 'Did not get expected min/max values on reopened file.' ) + return 'fail' + + new_ds = None + + if gdal.GetConfigOption( 'CPL_DEBUG', 'OFF' ) != 'ON': + self.driver.Delete( new_filename ) + + return 'success' + + def testSetGeoTransform(self): + if self.testDriver() == 'fail': + return 'skip' + + src_ds = gdal.Open( 'data/' + self.filename ) + xsize = src_ds.RasterXSize + ysize = src_ds.RasterYSize + + new_filename = 'tmp/' + self.filename + '.tst' + new_ds = self.driver.Create( new_filename, xsize, ysize, 1, + src_ds.GetRasterBand(self.band).DataType, + options = self.options ) + if new_ds is None: + post_reason( 'Failed to create test file using Create method.' ) + return 'fail' + + gt = (123.0, 1.18, 0.0, 456.0, 0.0, -1.18 ) + if new_ds.SetGeoTransform( gt ) is not gdal.CE_None: + post_reason( 'Failed to set geographic transformation.' ) + return 'fail' + + src_ds = None + new_ds = None + + new_ds = gdal.Open( new_filename ) + if new_ds is None: + post_reason( 'Failed to open dataset: ' + new_filename ) + return 'fail' + + eps = 0.00000001 + new_gt = new_ds.GetGeoTransform() + if abs(new_gt[0] - gt[0]) > eps \ + or abs(new_gt[1] - gt[1]) > eps \ + or abs(new_gt[2] - gt[2]) > eps \ + or abs(new_gt[3] - gt[3]) > eps \ + or abs(new_gt[4] - gt[4]) > eps \ + or abs(new_gt[5] - gt[5]) > eps: + print('') + print('old = ', gt) + print('new = ', new_gt) + post_reason( 'Did not get expected geotransform.' ) + return 'fail' + + new_ds = None + + if gdal.GetConfigOption( 'CPL_DEBUG', 'OFF' ) != 'ON': + self.driver.Delete( new_filename ) + + return 'success' + + def testSetProjection(self, prj = None, expected_prj = None ): + if self.testDriver() == 'fail': + return 'skip' + + src_ds = gdal.Open( 'data/' + self.filename ) + xsize = src_ds.RasterXSize + ysize = src_ds.RasterYSize + + new_filename = 'tmp/' + self.filename + '.tst' + new_ds = self.driver.Create( new_filename, xsize, ysize, 1, + src_ds.GetRasterBand(self.band).DataType, + options = self.options ) + if new_ds is None: + post_reason( 'Failed to create test file using Create method.' ) + return 'fail' + + gt = (123.0, 1.18, 0.0, 456.0, 0.0, -1.18 ) + if prj is None: + # This is a challenging SRS since it has non-meter linear units. + prj='PROJCS["NAD83 / Ohio South",GEOGCS["NAD83",DATUM["North_American_Datum_1983",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],AUTHORITY["EPSG","6269"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4269"]],PROJECTION["Lambert_Conformal_Conic_2SP"],PARAMETER["standard_parallel_1",40.03333333333333],PARAMETER["standard_parallel_2",38.73333333333333],PARAMETER["latitude_of_origin",38],PARAMETER["central_meridian",-82.5],PARAMETER["false_easting",1968500],PARAMETER["false_northing",0],UNIT["feet",0.3048006096012192]]' + + src_osr = osr.SpatialReference() + src_osr.ImportFromWkt(prj) + + new_ds.SetGeoTransform( gt ) + if new_ds.SetProjection( prj ) is not gdal.CE_None: + post_reason( 'Failed to set geographic projection string.' ) + return 'fail' + + src_ds = None + new_ds = None + + new_ds = gdal.Open( new_filename ) + if new_ds is None: + post_reason( 'Failed to open dataset: ' + new_filename ) + return 'fail' + + expected_osr = osr.SpatialReference() + if expected_prj is None: + expected_osr = src_osr + else: + expected_osr.ImportFromWkt( expected_prj ) + + new_osr = osr.SpatialReference() + new_osr.ImportFromWkt(new_ds.GetProjection()) + if not new_osr.IsSame(expected_osr): + post_reason( 'Did not get expected projection reference.' ) + print('Got: ') + print(new_osr.ExportToPrettyWkt()) + print('Expected:') + print(expected_osr.ExportToPrettyWkt()) + return 'fail' + + new_ds = None + + if gdal.GetConfigOption( 'CPL_DEBUG', 'OFF' ) != 'ON': + self.driver.Delete( new_filename ) + + return 'success' + + def testSetMetadata(self): + if self.testDriver() == 'fail': + return 'skip' + + src_ds = gdal.Open( 'data/' + self.filename ) + xsize = src_ds.RasterXSize + ysize = src_ds.RasterYSize + + new_filename = 'tmp/' + self.filename + '.tst' + new_ds = self.driver.Create( new_filename, xsize, ysize, 1, + src_ds.GetRasterBand(self.band).DataType, + options = self.options ) + if new_ds is None: + post_reason( 'Failed to create test file using Create method.' ) + return 'fail' + + dict = {} + dict['TEST_KEY'] = 'TestValue' + new_ds.SetMetadata( dict ) +# FIXME + #if new_ds.SetMetadata( dict ) is not gdal.CE_None: + #print new_ds.SetMetadata( dict ) + #post_reason( 'Failed to set metadata item.' ) + #return 'fail' + + src_ds = None + new_ds = None + + new_ds = gdal.Open( new_filename ) + if new_ds is None: + post_reason( 'Failed to open dataset: ' + new_filename ) + return 'fail' + + md_dict = new_ds.GetMetadata() + + if (not 'TEST_KEY' in md_dict): + post_reason( 'Metadata item TEST_KEY does not exist.') + return 'fail' + + if md_dict['TEST_KEY'] != 'TestValue': + post_reason( 'Did not get expected metadata item.' ) + return 'fail' + + new_ds = None + + if gdal.GetConfigOption( 'CPL_DEBUG', 'OFF' ) != 'ON': + self.driver.Delete( new_filename ) + + return 'success' + + def testSetNoDataValue(self): + if self.testDriver() == 'fail': + return 'skip' + + src_ds = gdal.Open( 'data/' + self.filename ) + xsize = src_ds.RasterXSize + ysize = src_ds.RasterYSize + + new_filename = 'tmp/' + self.filename + '.tst' + new_ds = self.driver.Create( new_filename, xsize, ysize, 1, + src_ds.GetRasterBand(self.band).DataType, + options = self.options ) + if new_ds is None: + post_reason( 'Failed to create test file using Create method.' ) + return 'fail' + + nodata = 11 + if new_ds.GetRasterBand(1).SetNoDataValue(nodata) is not gdal.CE_None: + post_reason( 'Failed to set NoData value.' ) + return 'fail' + + src_ds = None + new_ds = None + + new_ds = gdal.Open( new_filename ) + if new_ds is None: + post_reason( 'Failed to open dataset: ' + new_filename ) + return 'fail' + + if nodata != new_ds.GetRasterBand(1).GetNoDataValue(): + post_reason( 'Did not get expected NoData value.' ) + return 'fail' + + new_ds = None + + if gdal.GetConfigOption( 'CPL_DEBUG', 'OFF' ) != 'ON': + self.driver.Delete( new_filename ) + + return 'success' + + def testSetDescription(self): + if self.testDriver() == 'fail': + return 'skip' + + src_ds = gdal.Open( 'data/' + self.filename ) + xsize = src_ds.RasterXSize + ysize = src_ds.RasterYSize + + new_filename = 'tmp/' + self.filename + '.tst' + new_ds = self.driver.Create( new_filename, xsize, ysize, 1, + src_ds.GetRasterBand(self.band).DataType, + options = self.options ) + if new_ds is None: + post_reason( 'Failed to create test file using Create method.' ) + return 'fail' + + description = "Description test string" + new_ds.GetRasterBand(1).SetDescription(description) + + src_ds = None + new_ds = None + + new_ds = gdal.Open( new_filename ) + if new_ds is None: + post_reason( 'Failed to open dataset: ' + new_filename ) + return 'fail' + + if description != new_ds.GetRasterBand(1).GetDescription(): + post_reason( 'Did not get expected description string.' ) + return 'fail' + + new_ds = None + + if gdal.GetConfigOption( 'CPL_DEBUG', 'OFF' ) != 'ON': + self.driver.Delete( new_filename ) + + return 'success' + + +def approx_equal( a, b ): + a = float(a) + b = float(b) + if a == 0 and b != 0: + return 0 + + if abs(b/a - 1.0) > .00000000001: + return 0 + else: + return 1 + + +def user_srs_to_wkt( user_text ): + srs = osr.SpatialReference() + srs.SetFromUserInput( user_text ) + return srs.ExportToWkt() + +def equal_srs_from_wkt( expected_wkt, got_wkt ): + expected_srs = osr.SpatialReference() + expected_srs.ImportFromWkt( expected_wkt ) + + got_srs = osr.SpatialReference() + got_srs.ImportFromWkt( got_wkt ) + + if got_srs.IsSame( expected_srs ): + return 1 + else: + print('Expected:\n%s' % expected_wkt) + print('Got: \n%s' % got_wkt) + + post_reason( 'SRS differs from expected.' ) + return 0 + + +############################################################################### +# Compare two sets of RPC metadata, and establish if they are essentially +# equivelent or not. + +def rpcs_equal( md1, md2 ): + + simple_fields = [ 'LINE_OFF', 'SAMP_OFF', 'LAT_OFF', 'LONG_OFF', + 'HEIGHT_OFF', 'LINE_SCALE', 'SAMP_SCALE', 'LAT_SCALE', + 'LONG_SCALE', 'HEIGHT_SCALE' ] + coef_fields = [ 'LINE_NUM_COEFF', 'LINE_DEN_COEFF', + 'SAMP_NUM_COEFF', 'SAMP_DEN_COEFF' ] + + for sf in simple_fields: + + try: + if not approx_equal(float(md1[sf]),float(md2[sf])): + post_reason( '%s values differ.' % sf ) + print(md1[sf]) + print(md2[sf]) + return 0 + except: + post_reason( '%s value missing or corrupt.' % sf ) + print(md1) + print(md2) + return 0 + + for cf in coef_fields: + + try: + list1 = md1[cf].split() + list2 = md2[cf].split() + + except: + post_reason( '%s value missing or corrupt.' % cf ) + print(md1[cf]) + print(md2[cf]) + return 0 + + if len(list1) != 20: + post_reason( '%s value list length wrong(1)' % cf ) + print(list1) + return 0 + + if len(list2) != 20: + post_reason( '%s value list length wrong(2)' % cf ) + print(list2) + return 0 + + for i in range(20): + if not approx_equal(float(list1[i]),float(list2[i])): + post_reason( '%s[%d] values differ.' % (cf,i) ) + print(list1[i], list2[i]) + return 0 + + return 1 + +############################################################################### +# Test if geotransforms are equal with an epsilon tolerance +# + +def geotransform_equals(gt1, gt2, gt_epsilon): + for i in range(6): + if abs(gt1[i]-gt2[i]) > gt_epsilon: + print('') + print('gt1 = ', gt1) + print('gt2 = ', gt2) + post_reason( 'Geotransform differs.' ) + return False + return True + + +############################################################################### +# Download file at url 'url' and put it as 'filename' in 'tmp/cache/' +# +# If 'filename' already exits in 'tmp/cache/', it is not downloaded +# If GDAL_DOWNLOAD_TEST_DATA is not defined, the function fails +# If GDAL_DOWNLOAD_TEST_DATA is defined, 'url' is downloaded as 'filename' in 'tmp/cache/' + +def download_file(url, filename, download_size = -1): + global count_skipped_tests_download + try: + os.stat( 'tmp/cache/' + filename ) + return True + except: + if 'GDAL_DOWNLOAD_TEST_DATA' in os.environ: + val = None + try: + handle = gdalurlopen(url) + if download_size == -1: + try: + handle_info = handle.info() + content_length = handle_info['content-length'] + print('Downloading %s (length = %s bytes)...' % (url, content_length)) + except: + print('Downloading %s...' % (url)) + val = handle.read() + else: + print('Downloading %d bytes from %s...' % (download_size, url)) + val = handle.read(download_size) + except: + return False + + try: + os.stat( 'tmp/cache' ) + except: + os.mkdir('tmp/cache') + + try: + open( 'tmp/cache/' + filename, 'wb').write(val) + return True + except: + print('Cannot write %s' % (filename)) + return False + else: + if count_skipped_tests_download == 0: + print('As GDAL_DOWNLOAD_TEST_DATA environment variable is not defined, some tests relying on data to downloaded from the Web will be skipped') + count_skipped_tests_download = count_skipped_tests_download + 1 + return False + + +############################################################################### +# GDAL data type to python struct format +def gdal_data_type_to_python_struct_format(datatype): + type_char = 'B' + if datatype == gdal.GDT_Int16: + type_char = 'h' + elif datatype == gdal.GDT_UInt16: + type_char = 'H' + elif datatype == gdal.GDT_Int32: + type_char = 'i' + elif datatype == gdal.GDT_UInt32: + type_char = 'I' + elif datatype == gdal.GDT_Float32: + type_char = 'f' + elif datatype == gdal.GDT_Float64: + type_char = 'd' + return type_char + +############################################################################### +# Compare the values of the pixels + +def compare_ds(ds1, ds2, xoff = 0, yoff = 0, width = 0, height = 0, verbose=1): + import struct + + if width == 0: + width = ds1.RasterXSize + if height == 0: + height = ds1.RasterYSize + data1 = ds1.GetRasterBand(1).ReadRaster(xoff, yoff, width, height) + type_char = gdal_data_type_to_python_struct_format(ds1.GetRasterBand(1).DataType) + val_array1 = struct.unpack(type_char * width * height, data1) + + data2 = ds2.GetRasterBand(1).ReadRaster(xoff, yoff, width, height) + type_char = gdal_data_type_to_python_struct_format(ds2.GetRasterBand(1).DataType) + val_array2 = struct.unpack(type_char * width * height, data2) + + maxdiff = 0.0 + ndiffs = 0 + for i in range(width*height): + diff = val_array1[i] - val_array2[i] + if diff != 0: + #print(val_array1[i]) + #print(val_array2[i]) + ndiffs = ndiffs + 1 + if abs(diff) > maxdiff: + maxdiff = abs(diff) + if verbose: + print("Diff at pixel (%d, %d) : %f" % (i % width, i / width, float(diff))) + elif ndiffs < 10: + if verbose: + print("Diff at pixel (%d, %d) : %f" % (i % width, i / width, float(diff))) + if maxdiff != 0 and verbose: + print("Max diff : %d" % (maxdiff)) + print("Number of diffs : %d" % (ndiffs)) + + return maxdiff + + + +############################################################################### +# Deregister all JPEG2000 drivers, except the one passed as an argument + +def deregister_all_jpeg2000_drivers_but(name_of_driver_to_keep): + global jp2kak_drv, jpeg2000_drv, jp2ecw_drv, jp2mrsid_drv, jp2openjpeg_drv + global jp2kak_drv_unregistered,jpeg2000_drv_unregistered,jp2ecw_drv_unregistered,jp2mrsid_drv_unregistered,jp2openjpeg_drv_unregistered + + # Deregister other potential conflicting JPEG2000 drivers that will + # be re-registered in the cleanup + try: + jp2kak_drv = gdal.GetDriverByName('JP2KAK') + if name_of_driver_to_keep != 'JP2KAK' and jp2kak_drv: + gdal.Debug('gdaltest','Deregistering JP2KAK') + jp2kak_drv.Deregister() + jp2kak_drv_unregistered = True + except: + pass + + try: + jpeg2000_drv = gdal.GetDriverByName('JPEG2000') + if name_of_driver_to_keep != 'JPEG2000' and jpeg2000_drv: + gdal.Debug('gdaltest','Deregistering JPEG2000') + jpeg2000_drv.Deregister() + jpeg2000_drv_unregistered = True + except: + pass + + try: + jp2ecw_drv = gdal.GetDriverByName('JP2ECW') + if name_of_driver_to_keep != 'JP2ECW' and jp2ecw_drv: + gdal.Debug('gdaltest.','Deregistering JP2ECW') + jp2ecw_drv.Deregister() + jp2ecw_drv_unregistered = True + except: + pass + + try: + jp2mrsid_drv = gdal.GetDriverByName('JP2MrSID') + if name_of_driver_to_keep != 'JP2MrSID' and jp2mrsid_drv: + gdal.Debug('gdaltest.','Deregistering JP2MrSID') + jp2mrsid_drv.Deregister() + jp2mrsid_drv_unregistered = True + except: + pass + + try: + jp2openjpeg_drv = gdal.GetDriverByName('JP2OpenJPEG') + if name_of_driver_to_keep != 'JP2OpenJPEG' and jp2openjpeg_drv: + gdal.Debug('gdaltest.','Deregistering JP2OpenJPEG') + jp2openjpeg_drv.Deregister() + jp2openjpeg_drv_unregistered = True + except: + pass + + return True + +############################################################################### +# Re-register all JPEG2000 drivers previously disabled by +# deregister_all_jpeg2000_drivers_but + +def reregister_all_jpeg2000_drivers(): + global jp2kak_drv, jpeg2000_drv, jp2ecw_drv, jp2mrsid_drv, jp2openjpeg_drv + global jp2kak_drv_unregistered,jpeg2000_drv_unregistered,jp2ecw_drv_unregistered,jp2mrsid_drv_unregistered, jp2openjpeg_drv_unregistered + + try: + if jp2kak_drv_unregistered: + jp2kak_drv.Register() + jp2kak_drv_unregistered = False + gdal.Debug('gdaltest','Registering JP2KAK') + except: + pass + + try: + if jpeg2000_drv_unregistered: + jpeg2000_drv.Register() + jpeg2000_drv_unregistered = False + gdal.Debug('gdaltest','Registering JPEG2000') + except: + pass + + try: + if jp2ecw_drv_unregistered: + jp2ecw_drv.Register() + jp2ecw_drv_unregistered = False + gdal.Debug('gdaltest','Registering JP2ECW') + except: + pass + + try: + if jp2mrsid_drv_unregistered: + jp2mrsid_drv.Register() + jp2mrsid_drv_unregistered = False + gdal.Debug('gdaltest','Registering JP2MrSID') + except: + pass + + try: + if jp2openjpeg_drv_unregistered: + jp2openjpeg_drv.Register() + jp2openjpeg_drv = False + gdal.Debug('gdaltest','Registering JP2OpenJPEG') + except: + pass + + return True + +############################################################################### +# Determine if the filesystem supports sparse files. +# Currently, this will only work on Linux (or any *NIX that has the stat +# command line utility) + +def filesystem_supports_sparse_files(path): + + if skip_on_travis(): + return False + + try: + (ret, err) = runexternal_out_and_err('stat -f -c "%T" ' + path) + except: + return False + + if err != '': + post_reason('Cannot determine if filesystem supports sparse files') + return False + + if ret.find('fat32') != -1: + post_reason('File system does not support sparse files') + return False + + # Add here any missing filesystem supporting sparse files + # See http://en.wikipedia.org/wiki/Comparison_of_file_systems + if ret.find('ext3') == -1 and \ + ret.find('ext4') == -1 and \ + ret.find('reiser') == -1 and \ + ret.find('xfs') == -1 and \ + ret.find('jfs') == -1 and \ + ret.find('zfs') == -1 and \ + ret.find('ntfs') == -1 : + post_reason('Filesystem %s is not believed to support sparse files' % ret) + return False + + return True + +############################################################################### +# Unzip a file + +def unzip(target_dir, zipfilename, verbose = False): + + try: + import zipfile + zf = zipfile.ZipFile(zipfilename) + except: + os.system('unzip -d ' + target_dir + ' ' + zipfilename) + return + + for filename in zf.namelist(): + if verbose: + print(filename) + outfilename = os.path.join(target_dir, filename) + if filename.endswith('/'): + if not os.path.exists(outfilename): + os.makedirs(outfilename) + else: + outdirname = os.path.dirname(outfilename) + if not os.path.exists(outdirname): + os.makedirs(outdirname) + + outfile = open(outfilename,'wb') + outfile.write(zf.read(filename)) + outfile.close() + + return + +############################################################################### +# Return if a number is the NaN number + +def isnan(val): + if val == val: + # Python 2.3 unlike later versions return True for nan == nan + val_str = '%f' % val + if val_str == 'nan': + return True + else: + return False + else: + return True + + +############################################################################### +# Return NaN + +def NaN(): + try: + # Python >= 2.6 + return float('nan') + except: + return 1e400 / 1e400 + +############################################################################### +# Return positive infinity + +def posinf(): + try: + # Python >= 2.6 + return float('inf') + except: + return 1e400 + +############################################################################### +# Return negative infinity + +def neginf(): + try: + # Python >= 2.6 + return float('-inf') + except: + return -1e400 + +############################################################################### +# Has the user requested to run the slow tests + +def run_slow_tests(): + global count_skipped_tests_slow + val = gdal.GetConfigOption('GDAL_RUN_SLOW_TESTS', None) + if val != 'yes' and val != 'YES': + + if count_skipped_tests_slow == 0: + print('As GDAL_RUN_SLOW_TESTS environment variable is not defined, some "slow" tests will be skipped') + count_skipped_tests_slow = count_skipped_tests_slow + 1 + + return False + return True + +############################################################################### +# Return true if the platform support symlinks + +def support_symlink(): + if sys.platform.startswith('linux'): + return True + if sys.platform.find('freebsd') != -1: + return True + if sys.platform == 'darwin': + return True + if sys.platform.find('sunos') != -1: + return True + return False + +############################################################################### +# Return True if the test must be skipped + +def skip_on_travis(): + val = gdal.GetConfigOption('TRAVIS', None) + if val is not None: + post_reason('Test skipped on Travis') + return True + return False + + +############################################################################### +# find_lib_linux() +# Parse /proc/self/maps to find an occurrence of libXXXXX.so.* + +def find_lib_linux(libname): + + f = open('/proc/self/maps') + lines = f.readlines() + f.close() + + for line in lines: + if line.rfind('/lib' + libname) == -1 or line.find('.so') == -1: + continue + + i = line.find(' ') + if i < 0: + continue + line = line[i+1:] + i = line.find(' ') + if i < 0: + continue + line = line[i+1:] + i = line.find(' ') + if i < 0: + continue + line = line[i+1:] + i = line.find(' ') + if i < 0: + continue + line = line[i+1:] + i = line.find(' ') + if i < 0: + continue + line = line[i+1:] + + soname = line.lstrip().rstrip('\n') + if soname.rfind('/lib' + libname) == -1: + continue + + return soname + + return None + +############################################################################### +# find_lib_sunos() +# Parse output of pmap to find an occurrence of libXXX.so.* + +def find_lib_sunos(libname): + + pid = os.getpid() + (lines, err) = runexternal_out_and_err('pmap %d' % pid) + + for line in lines.split('\n'): + if line.rfind('/lib' + libname) == -1 or line.find('.so') == -1: + continue + + i = line.find('/') + if i < 0: + continue + line = line[i:] + + soname = line.lstrip().rstrip('\n') + if soname.rfind('/lib' + libname) == -1: + continue + + return soname + + return None + +############################################################################### +# find_lib_windows() +# use Module32First() / Module32Next() API on the current process + +def find_lib_windows(libname): + + try: + import ctypes + except: + return None + + kernel32 = ctypes.windll.kernel32 + + MAX_MODULE_NAME32 = 255 + MAX_PATH = 260 + + TH32CS_SNAPMODULE = 0x00000008 + + class MODULEENTRY32(ctypes.Structure): + _fields_ = [ + ("dwSize", ctypes.c_int), + ("th32ModuleID", ctypes.c_int), + ("th32ProcessID", ctypes.c_int), + ("GlblcntUsage", ctypes.c_int), + ("ProccntUsage", ctypes.c_int), + ("modBaseAddr", ctypes.c_char_p), + ("modBaseSize", ctypes.c_int), + ("hModule", ctypes.c_void_p), + ("szModule", ctypes.c_char * (MAX_MODULE_NAME32 + 1)), + ("szExePath", ctypes.c_char * MAX_PATH) + ] + + Module32First = kernel32.Module32First + Module32First.argtypes = [ ctypes.c_void_p, ctypes.POINTER(MODULEENTRY32) ] + Module32First.rettypes = ctypes.c_int + + Module32Next = kernel32.Module32Next + Module32Next.argtypes = [ ctypes.c_void_p, ctypes.POINTER(MODULEENTRY32) ] + Module32Next.rettypes = ctypes.c_int + + CreateToolhelp32Snapshot = kernel32.CreateToolhelp32Snapshot + CreateToolhelp32Snapshot.argtypes = [ ctypes.c_int, ctypes.c_int ] + CreateToolhelp32Snapshot.rettypes = ctypes.c_void_p + + CloseHandle = kernel32.CloseHandle + CloseHandle.argtypes = [ ctypes.c_void_p ] + CloseHandle.rettypes = ctypes.c_int + + GetLastError = kernel32.GetLastError + GetLastError.argtypes = [] + GetLastError.rettypes = ctypes.c_int + + snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE,0) + if snapshot is None: + return None + + soname = None + + i = 0 + while True: + entry = MODULEENTRY32() + entry.dwSize = ctypes.sizeof(MODULEENTRY32) + pentry = ctypes.pointer(entry) + if i == 0: + ret = Module32First(snapshot, pentry) + else: + ret = Module32Next(snapshot, pentry) + i = i + 1 + if ret == 0: + break + + try: + path = entry.szExePath.decode('latin1') + except: + continue + + i = path.rfind('\\' + libname) + if i < 0: + continue + if path[i+1:].find('\\') >= 0: + continue + soname = path + break + + CloseHandle(snapshot) + + return soname + +############################################################################### +# find_lib() + +def find_lib(mylib): + if sys.platform.startswith('linux'): + return find_lib_linux(mylib) + elif sys.platform.startswith('sunos'): + return find_lib_sunos(mylib) + elif sys.platform.startswith('win32'): + return find_lib_windows(mylib) + else: + # sorry mac users or other BSDs + # should be doable, but not in a blindless way + return None + +############################################################################### +# get_opened_files() + +def get_opened_files(): + if not sys.platform.startswith('linux'): + return [] + fdpath = '/proc/%d/fd' % os.getpid() + file_numbers = os.listdir(fdpath) + filenames = [] + for fd in file_numbers: + try: + filename = os.readlink('%s/%s' % (fdpath, fd)) + if not filename.startswith('/dev/') and not filename.startswith('pipe:'): + filenames.append(filename) + except: + pass + return filenames + +############################################################################### +# is_file_open() + +def is_file_open(filename): + for got_filename in get_opened_files(): + if got_filename.find(filename) >= 0: + return True + return False diff --git a/autotest/pymod/gdaltest_python2.py b/autotest/pymod/gdaltest_python2.py new file mode 100644 index 000000000000..51c1b91ba10d --- /dev/null +++ b/autotest/pymod/gdaltest_python2.py @@ -0,0 +1,189 @@ +# -*- coding: utf-8 -*- +############################################################################### +# $Id$ +# +# Project: GDAL/OGR Test Suite +# Purpose: Python Library supporting GDAL/OGR Test Suite +# Author: Even Rouault, +# +############################################################################### +# Copyright (c) 2003, Frank Warmerdam +# Copyright (c) 2009-2013, Even Rouault +# +# Permission is hereby granted, free of charge, to any person obtaining a +# copy of this software and associated documentation files (the "Software"), +# to deal in the Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, distribute, sublicense, +# and/or sell copies of the Software, and to permit persons to whom the +# Software is furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included +# in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +# DEALINGS IN THE SOFTWARE. +############################################################################### + +import urllib2 +import socket +import os +import sys +from sys import version_info +from Queue import Queue +from threading import Thread + +def run_func(func): + try: + result = func() + print(result) + return result + except SystemExit, x: + import traceback + traceback.print_exc() + + raise x + except: + result = 'fail (blowup)' + print(result) + + import traceback + traceback.print_exc() + return result + +def urlescape(url): + # Escape any non-ASCII characters + try: + import urllib + url = urllib.quote(url) + except: + pass + return url + +def gdalurlopen(url): + timeout = 10 + old_timeout = socket.getdefaulttimeout() + socket.setdefaulttimeout(timeout) + + if 'GDAL_HTTP_PROXY' in os.environ: + proxy = os.environ['GDAL_HTTP_PROXY'] + + if 'GDAL_HTTP_PROXYUSERPWD' in os.environ: + proxyuserpwd = os.environ['GDAL_HTTP_PROXYUSERPWD'] + proxyHandler = urllib2.ProxyHandler({"http" : \ + "http://%s@%s" % (proxyuserpwd, proxy)}) + else: + proxyuserpwd = None + proxyHandler = urllib2.ProxyHandler({"http" : \ + "http://%s" % (proxy)}) + + opener = urllib2.build_opener(proxyHandler, urllib2.HTTPHandler) + + urllib2.install_opener(opener) + + try: + handle = urllib2.urlopen(url) + socket.setdefaulttimeout(old_timeout) + return handle + except urllib2.HTTPError, e: + print('HTTP service for %s is down (HTTP Error: %d)' % (url, e.code)) + socket.setdefaulttimeout(old_timeout) + return None + except urllib2.URLError, e: + print('HTTP service for %s is down (HTTP Error: %s)' % (url, e.reason)) + socket.setdefaulttimeout(old_timeout) + return None + except: + print('HTTP service for %s is down.' %(url)) + socket.setdefaulttimeout(old_timeout) + return None + +def warn_if_memleak(cmd, out_str): + + # If DEBUG_VSIMALLOC_STATS is defined, this is an easy way + # to catch some memory leaks + if cmd.find('--utility_version') == -1 and \ + out_str.find('VSIMalloc + VSICalloc - VSIFree') != -1 and \ + out_str.find('VSIMalloc + VSICalloc - VSIFree : 0') == -1: + print('memory leak detected') + print(out_str) + +def spawn_async26(cmd): + import shlex + import subprocess + command = shlex.split(cmd) + try: + process = subprocess.Popen(command, stdout=subprocess.PIPE) + return (process, process.stdout) + except: + return (None, None) + +def spawn_async(cmd): + if version_info >= (2,6,0): + return spawn_async26(cmd) + + import popen2 + try: + process = popen2.Popen3(cmd) + except: + return (None, None) + if process is None: + return (None, None) + process.tochild.close() + return (process, process.fromchild) + +def wait_process(process): + process.wait() + +def runexternal(cmd, strin = None, check_memleak = True, display_live_on_parent_stdout = False): + if strin is None: + ret_stdout = os.popen(cmd) + else: + (ret_stdin, ret_stdout) = os.popen2(cmd) + ret_stdin.write(strin) + ret_stdin.close() + + if display_live_on_parent_stdout: + out_str = '' + while True: + c = ret_stdout.read(1) + if c == '': + break + out_str = out_str + c + sys.stdout.write(c) + ret_stdout.close() + else: + out_str = ret_stdout.read() + ret_stdout.close() + + if check_memleak: + warn_if_memleak(cmd, out_str) + + return out_str + +def read_in_thread(f, q): + q.put(f.read()) + f.close() + +def runexternal_out_and_err(cmd, check_memleak = True): + (ret_stdin, ret_stdout, ret_stderr) = os.popen3(cmd) + ret_stdin.close() + + q_stdout = Queue() + t_stdout = Thread(target=read_in_thread, args=(ret_stdout, q_stdout)) + q_stderr = Queue() + t_stderr = Thread(target=read_in_thread, args=(ret_stderr, q_stderr)) + t_stdout.start() + t_stderr.start() + + out_str = q_stdout.get() + err_str = q_stderr.get() + + if check_memleak: + warn_if_memleak(cmd, out_str) + + return (out_str, err_str) diff --git a/autotest/pymod/gdaltest_python3.py b/autotest/pymod/gdaltest_python3.py new file mode 100644 index 000000000000..6ffc7f8e72f9 --- /dev/null +++ b/autotest/pymod/gdaltest_python3.py @@ -0,0 +1,177 @@ +# -*- coding: utf-8 -*- +############################################################################### +# $Id$ +# +# Project: GDAL/OGR Test Suite +# Purpose: Python Library supporting GDAL/OGR Test Suite +# Author: Even Rouault, +# +############################################################################### +# Copyright (c) 2003, Frank Warmerdam +# Copyright (c) 2009-2013, Even Rouault +# +# Permission is hereby granted, free of charge, to any person obtaining a +# copy of this software and associated documentation files (the "Software"), +# to deal in the Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, distribute, sublicense, +# and/or sell copies of the Software, and to permit persons to whom the +# Software is furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included +# in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +# DEALINGS IN THE SOFTWARE. +############################################################################### + +import urllib.request, urllib.error, urllib.parse +import socket +import subprocess +import shlex +import os +import sys +from queue import Queue +from threading import Thread + +def run_func(func): + try: + result = func() + print(result) + return result + except SystemExit as x: + import traceback + traceback.print_exc() + + raise x + except: + result = 'fail (blowup)' + print(result) + + import traceback + traceback.print_exc() + return result + +def urlescape(url): + # Escape any non-ASCII characters + try: + import urllib + url = urllib.parse.quote(url) + except: + pass + return url + +def gdalurlopen(url): + timeout = 10 + old_timeout = socket.getdefaulttimeout() + socket.setdefaulttimeout(timeout) + + if 'GDAL_HTTP_PROXY' in os.environ: + proxy = os.environ['GDAL_HTTP_PROXY'] + + if 'GDAL_HTTP_PROXYUSERPWD' in os.environ: + proxyuserpwd = os.environ['GDAL_HTTP_PROXYUSERPWD'] + proxyHandler = urllib.request.ProxyHandler({"http" : \ + "http://%s@%s" % (proxyuserpwd, proxy)}) + else: + proxyuserpwd = None + proxyHandler = urllib.request.ProxyHandler({"http" : \ + "http://%s" % (proxy)}) + + opener = urllib.request.build_opener(proxyHandler, urllib.request.HTTPHandler) + + urllib.request.install_opener(opener) + + try: + handle = urllib.request.urlopen(url) + socket.setdefaulttimeout(old_timeout) + return handle + except urllib.error.HTTPError as e: + print('HTTP service for %s is down (HTTP Error: %d)' % (url, e.code)) + socket.setdefaulttimeout(old_timeout) + return None + except urllib.error.URLError as e: + print('HTTP service for %s is down (URL Error: %s)' % (url, e.reason)) + socket.setdefaulttimeout(old_timeout) + return None + except: + print('HTTP service for %s is down.' %(url)) + socket.setdefaulttimeout(old_timeout) + return None + +def spawn_async(cmd): + command = shlex.split(cmd) + try: + process = subprocess.Popen(command, stdout=subprocess.PIPE) + return (process, process.stdout) + except: + return (None, None) + +def wait_process(process): + process.wait() + +def runexternal(cmd, strin = None, check_memleak = True, display_live_on_parent_stdout = False): + command = shlex.split(cmd) + if strin is None: + p = subprocess.Popen(command, stdout=subprocess.PIPE) + else: + p = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE) + p.stdin.write(bytes(strin, 'ascii')) + p.stdin.close() + + if p.stdout is not None: + if display_live_on_parent_stdout: + ret = '' + ret_stdout = p.stdout + while True: + c = p.stdout.read(1).decode('ascii') + if c == '': + break + ret = ret + c + sys.stdout.write(c) + else: + ret = p.stdout.read().decode('ascii') + p.stdout.close() + else: + ret = '' + + p.wait() + + return ret + +def read_in_thread(f, q): + q.put(f.read()) + f.close() + +def runexternal_out_and_err(cmd, check_memleak = True): + command = shlex.split(cmd) + p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + + if p.stdout is not None: + q_stdout = Queue() + t_stdout = Thread(target=read_in_thread, args=(p.stdout, q_stdout)) + t_stdout.start() + else: + q_stdout = None + ret_stdout = '' + + if p.stderr is not None: + q_stderr = Queue() + t_stderr = Thread(target=read_in_thread, args=(p.stderr, q_stderr)) + t_stderr.start() + else: + q_stderr = None + ret_stderr = '' + + if q_stdout is not None: + ret_stdout = q_stdout.get().decode('ascii') + if q_stderr is not None: + ret_stderr = q_stderr.get().decode('ascii') + + p.wait() + + return (ret_stdout, ret_stderr) diff --git a/autotest/pymod/ogrtest.py b/autotest/pymod/ogrtest.py new file mode 100644 index 000000000000..1d45b3308e51 --- /dev/null +++ b/autotest/pymod/ogrtest.py @@ -0,0 +1,194 @@ +############################################################################### +# $Id$ +# +# Project: GDAL/OGR Test Suite +# Purpose: Support functions for OGR tests. +# Author: Frank Warmerdam +# +############################################################################### +# Copyright (c) 2003, Frank Warmerdam +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Library General Public +# License as published by the Free Software Foundation; either +# version 2 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Library General Public License for more details. +# +# You should have received a copy of the GNU Library General Public +# License along with this library; if not, write to the +# Free Software Foundation, Inc., 59 Temple Place - Suite 330, +# Boston, MA 02111-1307, USA. +############################################################################### + +import os +import sys + +sys.path.append( '../pymod' ) + +from osgeo import ogr +import gdaltest + +geos_flag = None + + +############################################################################### +def check_features_against_list( layer, field_name, value_list ): + + field_index = layer.GetLayerDefn().GetFieldIndex( field_name ) + if field_index < 0: + gdaltest.post_reason( 'did not find required field ' + field_name ) + return 0 + + for i in range(len(value_list)): + feat = layer.GetNextFeature() + if feat is None: + + gdaltest.post_reason( 'Got only %d features, not the expected %d features.' % (i, len(value_list)) ) + return 0 + + if isinstance(value_list[i],type('str')): + isok = (feat.GetFieldAsString( field_index ) != value_list[i]) + else: + isok = (feat.GetField( field_index ) != value_list[i]) + if isok: + gdaltest.post_reason( 'field %s feature %d did not match expected value %s, got %s.' % (field_name, i, str(value_list[i]), str(feat.GetField(field_index)) ) ) + feat.Destroy() + return 0 + + feat.Destroy() + + feat = layer.GetNextFeature() + if feat is not None: + feat.Destroy() + gdaltest.post_reason( 'got more features than expected' ) + return 0 + + return 1 + +############################################################################### +def check_feature_geometry( feat, geom, max_error = 0.0001 ): + try: + f_geom = feat.GetGeometryRef() + except: + f_geom = feat + + if isinstance(geom,type('a')): + geom = ogr.CreateGeometryFromWkt( geom ) + else: + geom = geom.Clone() + + if (f_geom is not None and geom is None): + gdaltest.post_reason( 'expected NULL geometry but got one.' ) + return 1 + + if (f_geom is None and geom is not None): + gdaltest.post_reason( 'expected geometry but got NULL.' ) + return 1 + + if f_geom.GetGeometryName() != geom.GetGeometryName(): + gdaltest.post_reason( 'geometry names do not match' ) + return 1 + + if f_geom.GetGeometryCount() != geom.GetGeometryCount(): + gdaltest.post_reason( 'sub-geometry counts do not match' ) + return 1 + + if f_geom.GetPointCount() != geom.GetPointCount(): + gdaltest.post_reason( 'point counts do not match' ) + return 1 + + if f_geom.GetGeometryCount() > 0: + count = f_geom.GetGeometryCount() + for i in range(count): + result = check_feature_geometry( f_geom.GetGeometryRef(i), + geom.GetGeometryRef(i), + max_error ) + if result != 0: + return result + + else: + count = f_geom.GetPointCount() + + for i in range(count): + x_dist = abs(f_geom.GetX(i) - geom.GetX(i)) + y_dist = abs(f_geom.GetY(i) - geom.GetY(i)) + z_dist = abs(f_geom.GetZ(i) - geom.GetZ(i)) + + if max(x_dist,y_dist,z_dist) > max_error: + gdaltest.post_reason( 'Error in vertex %d, off by %g.' \ + % (i, max(x_dist,y_dist,z_dist)) ) + return 1 + + geom.Destroy() + return 0 + +############################################################################### +def quick_create_layer_def( lyr, field_list): + # Each field is a tuple of (name, type, width, precision) + # Any of type, width and precision can be skipped. Default type is string. + + for field in field_list: + name = field[0] + if len(field) > 1: + type = field[1] + else: + type = ogr.OFTString + + field_defn = ogr.FieldDefn( name, type ) + + if len(field) > 2: + field_defn.SetWidth( int(field[2]) ) + + if len(field) > 3: + field_defn.SetPrecision( int(field[3]) ) + + lyr.CreateField( field_defn ) + + field_defn.Destroy() + +############################################################################### +def quick_create_feature( layer, field_values, wkt_geometry ): + feature = ogr.Feature( feature_def = layer.GetLayerDefn() ) + + for i in range(len(field_values)): + feature.SetField( i, field_values[i] ) + + if wkt_geometry is not None: + geom = ogr.CreateGeometryFromWkt( wkt_geometry ) + if geom is None: + raise ValueError('Failed to create geometry from: ' + wkt_geometry) + feature.SetGeometryDirectly( geom ) + + result = layer.CreateFeature( feature ) + + feature.Destroy() + + if result != 0: + raise ValueError('CreateFeature() failed in ogrtest.quick_create_feature()') + +############################################################################### +def have_geos(): + global geos_flag + + if geos_flag is None: + pnt1 = ogr.CreateGeometryFromWkt( 'POINT(10 20)' ) + pnt2 = ogr.CreateGeometryFromWkt( 'POINT(30 20)' ) + + try: + result = pnt1.Union( pnt2 ) + except: + result = None + + pnt1.Destroy() + pnt2.Destroy() + + if result is None: + geos_flag = 0 + else: + geos_flag = 1 + + return geos_flag diff --git a/autotest/pymod/test_cli_utilities.py b/autotest/pymod/test_cli_utilities.py new file mode 100644 index 000000000000..cca0e9ef1d21 --- /dev/null +++ b/autotest/pymod/test_cli_utilities.py @@ -0,0 +1,189 @@ +#!/usr/bin/env python +############################################################################### +# $Id$ +# +# Project: GDAL/OGR Test Suite +# Purpose: Helper functions for testing CLI utilities +# Author: Even Rouault +# +############################################################################### +# Copyright (c) 2008-2010, Even Rouault +# +# Permission is hereby granted, free of charge, to any person obtaining a +# copy of this software and associated documentation files (the "Software"), +# to deal in the Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, distribute, sublicense, +# and/or sell copies of the Software, and to permit persons to whom the +# Software is furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included +# in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +# DEALINGS IN THE SOFTWARE. +############################################################################### + +import os +import sys + +from osgeo import gdal + +import gdaltest + +cli_exe_path = { } + +############################################################################### +# +def get_cli_utility_path_internal(cli_utility_name): + + if sys.platform == 'win32': + cli_utility_name = cli_utility_name + '.exe' + + # First try : in the apps directory of the GDAL source tree + # This is the case for the buildbot directory tree + try: + cli_utility_path = os.path.join(os.getcwd(), '..', '..', 'gdal', 'apps', cli_utility_name) + if sys.platform == 'win32': + cli_utility_path = cli_utility_path.replace('\\', '/') + if os.path.isfile(cli_utility_path): + ret = gdaltest.runexternal(cli_utility_path + ' --utility_version') + + if ret.find('GDAL') != -1: + return cli_utility_path + except: + pass + + # Second try : the autotest directory is a subdirectory of gdal/ (FrankW's layout) + try: + cli_utility_path = os.path.join(os.getcwd(), '..', '..', 'apps', cli_utility_name) + if sys.platform == 'win32': + cli_utility_path = cli_utility_path.replace('\\', '/') + if os.path.isfile(cli_utility_path): + ret = gdaltest.runexternal(cli_utility_path + ' --utility_version') + + if ret.find('GDAL') != -1: + return cli_utility_path + except: + pass + + # Otherwise look up in the system path + try: + cli_utility_path = cli_utility_name + ret = gdaltest.runexternal(cli_utility_path + ' --utility_version') + + if ret.find('GDAL') != -1: + return cli_utility_path + except: + pass + + return None + +############################################################################### +# +def get_cli_utility_path(cli_utility_name): + global cli_exe_path + if cli_utility_name in cli_exe_path: + return cli_exe_path[cli_utility_name] + else: + cli_exe_path[cli_utility_name] = get_cli_utility_path_internal(cli_utility_name) + return cli_exe_path[cli_utility_name] + +############################################################################### +# +def get_gdalinfo_path(): + return get_cli_utility_path('gdalinfo') + +############################################################################### +# +def get_gdal_translate_path(): + return get_cli_utility_path('gdal_translate') + +############################################################################### +# +def get_gdalwarp_path(): + return get_cli_utility_path('gdalwarp') + +############################################################################### +# +def get_gdaladdo_path(): + return get_cli_utility_path('gdaladdo') + +############################################################################### +# +def get_gdaltransform_path(): + return get_cli_utility_path('gdaltransform') + +############################################################################### +# +def get_gdaltindex_path(): + return get_cli_utility_path('gdaltindex') + +############################################################################### +# +def get_gdal_grid_path(): + return get_cli_utility_path('gdal_grid') + +############################################################################### +# +def get_ogrinfo_path(): + return get_cli_utility_path('ogrinfo') + +############################################################################### +# +def get_ogr2ogr_path(): + return get_cli_utility_path('ogr2ogr') + +############################################################################### +# +def get_ogrtindex_path(): + return get_cli_utility_path('ogrtindex') + +############################################################################### +# +def get_ogrlineref_path(): + return get_cli_utility_path('ogrlineref') + +############################################################################### +# +def get_gdalbuildvrt_path(): + return get_cli_utility_path('gdalbuildvrt') + +############################################################################### +# +def get_gdal_contour_path(): + return get_cli_utility_path('gdal_contour') + +############################################################################### +# +def get_gdaldem_path(): + return get_cli_utility_path('gdaldem') + +############################################################################### +# +def get_gdal_rasterize_path(): + return get_cli_utility_path('gdal_rasterize') + +############################################################################### +# +def get_nearblack_path(): + return get_cli_utility_path('nearblack') + +############################################################################### +# +def get_test_ogrsf_path(): + return get_cli_utility_path('test_ogrsf') + +############################################################################### +# +def get_gdallocationinfo_path(): + return get_cli_utility_path('gdallocationinfo') + +############################################################################### +# +def get_gdalsrsinfo_path(): + return get_cli_utility_path('gdalsrsinfo') diff --git a/pixelfunctions.c b/pixelfunctions.c new file mode 100644 index 000000000000..9ac7134e5c08 --- /dev/null +++ b/pixelfunctions.c @@ -0,0 +1,818 @@ +/****************************************************************************** + * + * Project: GDAL + * Purpose: Implementation of a set of GDALDerivedPixelFunc(s) to be used + * with source raster band of virtual GDAL datasets. + * Author: Antonio Valentino + * + ****************************************************************************** + * Copyright (c) 2008-2014 Antonio Valentino + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + *****************************************************************************/ + +#include +#include + + +CPLErr RealPixelFunc(void **papoSources, int nSources, void *pData, + int nXSize, int nYSize, + GDALDataType eSrcType, GDALDataType eBufType, + int nPixelSpace, int nLineSpace) +{ + int iLine, nPixelSpaceSrc, nLineSpaceSrc; + + /* ---- Init ---- */ + if (nSources != 1) return CE_Failure; + + nPixelSpaceSrc = GDALGetDataTypeSize( eSrcType ) / 8; + nLineSpaceSrc = nPixelSpaceSrc * nXSize; + + /* ---- Set pixels ---- */ + for( iLine = 0; iLine < nYSize; ++iLine ) { + GDALCopyWords(((GByte *)papoSources[0]) + nLineSpaceSrc * iLine, + eSrcType, nPixelSpaceSrc, + ((GByte *)pData) + nLineSpace * iLine, + eBufType, nPixelSpace, nXSize); + } + + /* ---- Return success ---- */ + return CE_None; +} /* RealPixelFunc */ + + +CPLErr ImagPixelFunc(void **papoSources, int nSources, void *pData, + int nXSize, int nYSize, + GDALDataType eSrcType, GDALDataType eBufType, + int nPixelSpace, int nLineSpace) +{ + int iLine; + + /* ---- Init ---- */ + if (nSources != 1) return CE_Failure; + + if (GDALDataTypeIsComplex( eSrcType )) + { + int nPixelSpaceSrc = GDALGetDataTypeSize( eSrcType ) / 8; + int nLineSpaceSrc = nPixelSpaceSrc * nXSize; + + void* pImag = ((GByte *)papoSources[0]) + + GDALGetDataTypeSize( eSrcType ) / 8 / 2; + + /* ---- Set pixels ---- */ + for( iLine = 0; iLine < nYSize; ++iLine ) { + GDALCopyWords(((GByte *)pImag) + nLineSpaceSrc * iLine, + eSrcType, nPixelSpaceSrc, + ((GByte *)pData) + nLineSpace * iLine, + eBufType, nPixelSpace, nXSize); + } + } else { + double dfImag = 0; + + /* ---- Set pixels ---- */ + for( iLine = 0; iLine < nYSize; ++iLine ) { + /* always copy from the same location */ + GDALCopyWords(&dfImag, eSrcType, 0, + ((GByte *)pData) + nLineSpace * iLine, + eBufType, nPixelSpace, nXSize); + } + } + + /* ---- Return success ---- */ + return CE_None; +} /* ImagPixelFunc */ + + +CPLErr ModulePixelFunc(void **papoSources, int nSources, void *pData, + int nXSize, int nYSize, + GDALDataType eSrcType, GDALDataType eBufType, + int nPixelSpace, int nLineSpace) +{ + int ii, iLine, iCol; + double dfPixVal; + + /* ---- Init ---- */ + if (nSources != 1) return CE_Failure; + + if (GDALDataTypeIsComplex( eSrcType )) + { + double dfReal, dfImag; + void *pReal = papoSources[0]; + void *pImag = ((GByte *)papoSources[0]) + + GDALGetDataTypeSize( eSrcType ) / 8 / 2; + + /* ---- Set pixels ---- */ + for( iLine = 0, ii = 0; iLine < nYSize; ++iLine ) { + for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { + + /* Source raster pixels may be obtained with SRCVAL macro */ + dfReal = SRCVAL(pReal, eSrcType, ii); + dfImag = SRCVAL(pImag, eSrcType, ii); + + dfPixVal = sqrt( dfReal * dfReal + dfImag * dfImag ); + + GDALCopyWords(&dfPixVal, GDT_Float64, 0, + ((GByte *)pData) + nLineSpace * iLine + + iCol * nPixelSpace, eBufType, nPixelSpace, 1); + } + } + } else { + /* ---- Set pixels ---- */ + for( iLine = 0, ii = 0; iLine < nYSize; ++iLine ) { + for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { + + /* Source raster pixels may be obtained with SRCVAL macro */ + dfPixVal = abs(SRCVAL(papoSources[0], eSrcType, ii)); + + GDALCopyWords(&dfPixVal, GDT_Float64, 0, + ((GByte *)pData) + nLineSpace * iLine + + iCol * nPixelSpace, eBufType, nPixelSpace, 1); + } + } + } + + /* ---- Return success ---- */ + return CE_None; +} /* ModulePixelFunc */ + + +CPLErr PhasePixelFunc(void **papoSources, int nSources, void *pData, + int nXSize, int nYSize, + GDALDataType eSrcType, GDALDataType eBufType, + int nPixelSpace, int nLineSpace) +{ + int ii, iLine, iCol; + double dfPixVal, dfReal; + + /* ---- Init ---- */ + if (nSources != 1) return CE_Failure; + + if (GDALDataTypeIsComplex( eSrcType )) + { + double dfImag; + void *pReal = papoSources[0]; + void *pImag = ((GByte *)papoSources[0]) + + GDALGetDataTypeSize( eSrcType ) / 8 / 2; + + /* ---- Set pixels ---- */ + for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { + for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { + + /* Source raster pixels may be obtained with SRCVAL macro */ + dfReal = SRCVAL(pReal, eSrcType, ii); + dfImag = SRCVAL(pImag, eSrcType, ii); + + dfPixVal = atan2(dfImag, dfReal); + + GDALCopyWords(&dfPixVal, GDT_Float64, 0, + ((GByte *)pData) + nLineSpace * iLine + + iCol * nPixelSpace, eBufType, nPixelSpace, 1); + } + } + } else { + /* ---- Set pixels ---- */ + /* + for( iLine = 0; iLine < nYSize; ++iLine ) { + / * always copy from the same location * / + GDALCopyWords(&dfImag, eSrcType, 0, + ((GByte *)pData) + nLineSpace * iLine, + eBufType, nPixelSpace, nXSize); + } + */ + /* ---- Set pixels ---- */ + double pi = atan2(0, -1); + for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { + for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { + + void *pReal = papoSources[0]; + + /* Source raster pixels may be obtained with SRCVAL macro */ + dfReal = SRCVAL(pReal, eSrcType, ii); + dfPixVal = (dfReal < 0) ? pi : 0; + + GDALCopyWords(&dfPixVal, GDT_Float64, dfPixVal, + ((GByte *)pData) + nLineSpace * iLine + + iCol * nPixelSpace, eBufType, nPixelSpace, 1); + } + } + } + + /* ---- Return success ---- */ + return CE_None; +} /* PhasePixelFunc */ + + +CPLErr ConjPixelFunc(void **papoSources, int nSources, void *pData, + int nXSize, int nYSize, + GDALDataType eSrcType, GDALDataType eBufType, + int nPixelSpace, int nLineSpace) +{ + /* ---- Init ---- */ + if (nSources != 1) return CE_Failure; + + if (GDALDataTypeIsComplex( eSrcType ) && GDALDataTypeIsComplex( eBufType )) + { + int iLine, iCol, ii; + double adfPixVal[2]; + int nOffset = GDALGetDataTypeSize( eSrcType ) / 8 / 2; + void *pReal = papoSources[0]; + void *pImag = ((GByte *)papoSources[0]) + nOffset; + + /* ---- Set pixels ---- */ + for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { + for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { + + /* Source raster pixels may be obtained with SRCVAL macro */ + adfPixVal[0] = +SRCVAL(pReal, eSrcType, ii); /* re */ + adfPixVal[1] = -SRCVAL(pImag, eSrcType, ii); /* im */ + + GDALCopyWords(adfPixVal, GDT_CFloat64, 0, + ((GByte *)pData) + nLineSpace * iLine + + iCol * nPixelSpace, eBufType, nPixelSpace, 1); + } + } + } else { + /* no complex data type */ + return RealPixelFunc(papoSources, nSources, pData, nXSize, nYSize, + eSrcType, eBufType, nPixelSpace, nLineSpace); + } + + /* ---- Return success ---- */ + return CE_None; +} /* ConjPixelFunc */ + + +CPLErr SumPixelFunc(void **papoSources, int nSources, void *pData, + int nXSize, int nYSize, + GDALDataType eSrcType, GDALDataType eBufType, + int nPixelSpace, int nLineSpace) +{ + int iLine, iCol, ii, iSrc; + + /* ---- Init ---- */ + if (nSources < 2) return CE_Failure; + + /* ---- Set pixels ---- */ + if (GDALDataTypeIsComplex( eSrcType )) + { + double adfSum[2]; + void *pReal, *pImag; + int nOffset = GDALGetDataTypeSize( eSrcType ) / 8 / 2; + + /* ---- Set pixels ---- */ + for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { + for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { + adfSum[0] = 0; + adfSum[1] = 0; + + for( iSrc = 0; iSrc < nSources; ++iSrc ) { + pReal = papoSources[iSrc]; + pImag = ((GByte *)pReal) + nOffset; + + /* Source raster pixels may be obtained with SRCVAL macro */ + adfSum[0] += SRCVAL(pReal, eSrcType, ii); + adfSum[1] += SRCVAL(pImag, eSrcType, ii); + } + + GDALCopyWords(adfSum, GDT_CFloat64, 0, + ((GByte *)pData) + nLineSpace * iLine + + iCol * nPixelSpace, eBufType, nPixelSpace, 1); + } + } + } else { + /* non complex */ + double dfSum; + + /* ---- Set pixels ---- */ + for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { + for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { + dfSum = 0; + + for( iSrc = 0; iSrc < nSources; ++iSrc ) { + /* Source raster pixels may be obtained with SRCVAL macro */ + dfSum += SRCVAL(papoSources[iSrc], eSrcType, ii); + } + + GDALCopyWords(&dfSum, GDT_Float64, 0, + ((GByte *)pData) + nLineSpace * iLine + + iCol * nPixelSpace, eBufType, nPixelSpace, 1); + } + } + } + + /* ---- Return success ---- */ + return CE_None; +} /* SumPixelFunc */ + + +CPLErr DiffPixelFunc(void **papoSources, int nSources, void *pData, + int nXSize, int nYSize, + GDALDataType eSrcType, GDALDataType eBufType, + int nPixelSpace, int nLineSpace) +{ + int ii, iLine, iCol; + + /* ---- Init ---- */ + if (nSources != 2) return CE_Failure; + + if (GDALDataTypeIsComplex( eSrcType )) + { + + double adfPixVal[2]; + int nOffset = GDALGetDataTypeSize( eSrcType ) / 8 / 2; + void *pReal0 = papoSources[0]; + void *pImag0 = ((GByte *)papoSources[0]) + nOffset; + void *pReal1 = papoSources[1]; + void *pImag1 = ((GByte *)papoSources[1]) + nOffset; + + /* ---- Set pixels ---- */ + for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { + for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { + + /* Source raster pixels may be obtained with SRCVAL macro */ + adfPixVal[0] = SRCVAL(pReal0, eSrcType, ii) + - SRCVAL(pReal1, eSrcType, ii); + adfPixVal[1] = SRCVAL(pImag0, eSrcType, ii) + - SRCVAL(pImag1, eSrcType, ii); + + GDALCopyWords(adfPixVal, GDT_CFloat64, 0, + ((GByte *)pData) + nLineSpace * iLine + + iCol * nPixelSpace, eBufType, nPixelSpace, 1); + } + } + } else { + /* non complex */ + double dfPixVal; + + /* ---- Set pixels ---- */ + for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { + for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { + + /* Source raster pixels may be obtained with SRCVAL macro */ + dfPixVal = SRCVAL(papoSources[0], eSrcType, ii) + - SRCVAL(papoSources[1], eSrcType, ii); + + GDALCopyWords(&dfPixVal, GDT_Float64, 0, + ((GByte *)pData) + nLineSpace * iLine + + iCol * nPixelSpace, eBufType, nPixelSpace, 1); + } + } + } + + /* ---- Return success ---- */ + return CE_None; +} /* DiffPixelFunc */ + + +CPLErr MulPixelFunc(void **papoSources, int nSources, void *pData, + int nXSize, int nYSize, + GDALDataType eSrcType, GDALDataType eBufType, + int nPixelSpace, int nLineSpace) +{ + int iLine, iCol, ii, iSrc; + + /* ---- Init ---- */ + if (nSources < 2) return CE_Failure; + + /* ---- Set pixels ---- */ + if (GDALDataTypeIsComplex( eSrcType )) + { + double adfPixVal[2], dfOldR, dfOldI, dfNewR, dfNewI; + void *pReal, *pImag; + int nOffset = GDALGetDataTypeSize( eSrcType ) / 8 / 2; + + /* ---- Set pixels ---- */ + for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { + for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { + adfPixVal[0] = 1.; + adfPixVal[1] = 0.; + + for( iSrc = 0; iSrc < nSources; ++iSrc ) { + pReal = papoSources[iSrc]; + pImag = ((GByte *)pReal) + nOffset; + + dfOldR = adfPixVal[0]; + dfOldI = adfPixVal[1]; + + /* Source raster pixels may be obtained with SRCVAL macro */ + dfNewR = SRCVAL(pReal, eSrcType, ii); + dfNewI = SRCVAL(pImag, eSrcType, ii); + + adfPixVal[0] = dfOldR * dfNewR - dfOldI * dfNewI; + adfPixVal[1] = dfOldR * dfNewI + dfOldI * dfNewR; + } + + GDALCopyWords(adfPixVal, GDT_CFloat64, 0, + ((GByte *)pData) + nLineSpace * iLine + + iCol * nPixelSpace, eBufType, nPixelSpace, 1); + } + } + } else { + /* non complex */ + double dfPixVal; + + /* ---- Set pixels ---- */ + for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { + for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { + dfPixVal = 1; + + for( iSrc = 0; iSrc < nSources; ++iSrc ) { + /* Source raster pixels may be obtained with SRCVAL macro */ + dfPixVal *= SRCVAL(papoSources[iSrc], eSrcType, ii); + } + + GDALCopyWords(&dfPixVal, GDT_Float64, 0, + ((GByte *)pData) + nLineSpace * iLine + + iCol * nPixelSpace, eBufType, nPixelSpace, 1); + } + } + } + + /* ---- Return success ---- */ + return CE_None; +} /* MulPixelFunc */ + + +CPLErr CMulPixelFunc(void **papoSources, int nSources, void *pData, + int nXSize, int nYSize, + GDALDataType eSrcType, GDALDataType eBufType, + int nPixelSpace, int nLineSpace) +{ + int ii, iLine, iCol; + + /* ---- Init ---- */ + if (nSources != 2) return CE_Failure; + + /* ---- Set pixels ---- */ + if (GDALDataTypeIsComplex( eSrcType )) + { + double adfPixVal[2], dfReal0, dfImag0, dfReal1, dfImag1; + + int nOffset = GDALGetDataTypeSize( eSrcType ) / 8 / 2; + void *pReal0 = papoSources[0]; + void *pImag0 = ((GByte *)papoSources[0]) + nOffset; + void *pReal1 = papoSources[1]; + void *pImag1 = ((GByte *)papoSources[1]) + nOffset; + + for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { + for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { + + /* Source raster pixels may be obtained with SRCVAL macro */ + dfReal0 = SRCVAL(pReal0, eSrcType, ii); + dfReal1 = SRCVAL(pReal1, eSrcType, ii); + dfImag0 = SRCVAL(pImag0, eSrcType, ii); + dfImag1 = SRCVAL(pImag1, eSrcType, ii); + adfPixVal[0] = dfReal0 * dfReal1 + dfImag0 * dfImag1; + adfPixVal[1] = dfReal1 * dfImag0 - dfReal0 * dfImag1; + + GDALCopyWords(adfPixVal, GDT_CFloat64, 0, + ((GByte *)pData) + nLineSpace * iLine + + iCol * nPixelSpace, eBufType, nPixelSpace, 1); + } + } + } else { + /* non complex */ + double adfPixVal[2] = {0, 0}; + + for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { + for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { + + /* Source raster pixels may be obtained with SRCVAL macro */ + adfPixVal[0] = SRCVAL(papoSources[0], eSrcType, ii) + * SRCVAL(papoSources[1], eSrcType, ii); + + GDALCopyWords(adfPixVal, GDT_CFloat64, 0, + ((GByte *)pData) + nLineSpace * iLine + + iCol * nPixelSpace, eBufType, nPixelSpace, 1); + } + } + } + + /* ---- Return success ---- */ + return CE_None; +} /* CMulPixelFunc */ + + +CPLErr InvPixelFunc(void **papoSources, int nSources, void *pData, + int nXSize, int nYSize, + GDALDataType eSrcType, GDALDataType eBufType, + int nPixelSpace, int nLineSpace) +{ + int iLine, iCol, ii; + + /* ---- Init ---- */ + if (nSources != 1) return CE_Failure; + + /* ---- Set pixels ---- */ + if (GDALDataTypeIsComplex( eSrcType )) + { + double adfPixVal[2], dfReal, dfImag, dfAux; + + int nOffset = GDALGetDataTypeSize( eSrcType ) / 8 / 2; + void *pReal = papoSources[0]; + void *pImag = ((GByte *)papoSources[0]) + nOffset; + + for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { + for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { + + /* Source raster pixels may be obtained with SRCVAL macro */ + dfReal = SRCVAL(pReal, eSrcType, ii); + dfImag = SRCVAL(pImag, eSrcType, ii); + dfAux = dfReal * dfReal + dfImag * dfImag; + adfPixVal[0] = +dfReal / dfAux; + adfPixVal[1] = -dfImag / dfAux; + + GDALCopyWords(adfPixVal, GDT_CFloat64, 0, + ((GByte *)pData) + nLineSpace * iLine + + iCol * nPixelSpace, eBufType, nPixelSpace, 1); + } + } + } else { + /* non complex */ + double dfPixVal; + + /* ---- Set pixels ---- */ + for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { + for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { + + /* Source raster pixels may be obtained with SRCVAL macro */ + dfPixVal = 1. / SRCVAL(papoSources[0], eSrcType, ii); + + GDALCopyWords(&dfPixVal, GDT_Float64, 0, + ((GByte *)pData) + nLineSpace * iLine + + iCol * nPixelSpace, eBufType, nPixelSpace, 1); + } + } + } + + /* ---- Return success ---- */ + return CE_None; +} /* InvPixelFunc */ + + +CPLErr IntensityPixelFunc(void **papoSources, int nSources, void *pData, + int nXSize, int nYSize, + GDALDataType eSrcType, GDALDataType eBufType, + int nPixelSpace, int nLineSpace) +{ + int ii, iLine, iCol; + double dfPixVal; + + /* ---- Init ---- */ + if (nSources != 1) return CE_Failure; + + if (GDALDataTypeIsComplex( eSrcType )) + { + double dfReal, dfImag; + int nOffset = GDALGetDataTypeSize( eSrcType ) / 8 / 2; + void *pReal = papoSources[0]; + void *pImag = ((GByte *)papoSources[0]) + nOffset; + + /* ---- Set pixels ---- */ + for( iLine = 0, ii = 0; iLine < nYSize; ++iLine ) { + for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { + + /* Source raster pixels may be obtained with SRCVAL macro */ + dfReal = SRCVAL(pReal, eSrcType, ii); + dfImag = SRCVAL(pImag, eSrcType, ii); + + dfPixVal = dfReal * dfReal + dfImag * dfImag; + + GDALCopyWords(&dfPixVal, GDT_Float64, 0, + ((GByte *)pData) + nLineSpace * iLine + + iCol * nPixelSpace, eBufType, nPixelSpace, 1); + } + } + } else { + /* ---- Set pixels ---- */ + for( iLine = 0, ii = 0; iLine < nYSize; ++iLine ) { + for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { + + /* Source raster pixels may be obtained with SRCVAL macro */ + dfPixVal = SRCVAL(papoSources[0], eSrcType, ii); + dfPixVal *= dfPixVal; + + GDALCopyWords(&dfPixVal, GDT_Float64, 0, + ((GByte *)pData) + nLineSpace * iLine + + iCol * nPixelSpace, eBufType, nPixelSpace, 1); + } + } + } + + /* ---- Return success ---- */ + return CE_None; +} /* IntensityPixelFunc */ + + +CPLErr SqrtPixelFunc(void **papoSources, int nSources, void *pData, + int nXSize, int nYSize, + GDALDataType eSrcType, GDALDataType eBufType, + int nPixelSpace, int nLineSpace) +{ + int iLine, iCol, ii; + double dfPixVal; + + /* ---- Init ---- */ + if (nSources != 1) return CE_Failure; + if (GDALDataTypeIsComplex( eSrcType )) return CE_Failure; + + /* ---- Set pixels ---- */ + for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { + for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { + + /* Source raster pixels may be obtained with SRCVAL macro */; + dfPixVal = sqrt( SRCVAL(papoSources[0], eSrcType, ii) ); + + GDALCopyWords(&dfPixVal, GDT_Float64, 0, + ((GByte *)pData) + nLineSpace * iLine + + iCol * nPixelSpace, eBufType, nPixelSpace, 1); + } + } + + /* ---- Return success ---- */ + return CE_None; +} /* SqrtPixelFunc */ + + +CPLErr Log10PixelFunc(void **papoSources, int nSources, void *pData, + int nXSize, int nYSize, + GDALDataType eSrcType, GDALDataType eBufType, + int nPixelSpace, int nLineSpace) +{ + int ii, iLine, iCol; + + /* ---- Init ---- */ + if (nSources != 1) return CE_Failure; + + if (GDALDataTypeIsComplex( eSrcType )) + { + /* complex input datatype */ + double dfReal, dfImag, dfPixVal; + int nOffset = GDALGetDataTypeSize( eSrcType ) / 8 / 2; + void *pReal = papoSources[0]; + void *pImag = ((GByte *)papoSources[0]) + nOffset; + + /* ---- Set pixels ---- */ + for( iLine = 0, ii = 0; iLine < nYSize; ++iLine ) { + for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { + + /* Source raster pixels may be obtained with SRCVAL macro */ + dfReal = SRCVAL(pReal, eSrcType, ii); + dfImag = SRCVAL(pImag, eSrcType, ii); + + dfPixVal = log10( dfReal * dfReal + dfImag * dfImag ); + + GDALCopyWords(&dfPixVal, GDT_Float64, 0, + ((GByte *)pData) + nLineSpace * iLine + + iCol * nPixelSpace, eBufType, nPixelSpace, 1); + } + } + } else { + double dfPixVal; + + /* ---- Set pixels ---- */ + for( iLine = 0, ii = 0; iLine < nYSize; ++iLine ) { + for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { + + /* Source raster pixels may be obtained with SRCVAL macro */ + dfPixVal = SRCVAL(papoSources[0], eSrcType, ii); + dfPixVal = log10( abs( dfPixVal ) ); + + GDALCopyWords(&dfPixVal, GDT_Float64, 0, + ((GByte *)pData) + nLineSpace * iLine + + iCol * nPixelSpace, eBufType, nPixelSpace, 1); + } + } + } + + /* ---- Return success ---- */ + return CE_None; +} /* Log10PixelFunc */ + + +CPLErr PowPixelFuncHelper(void **papoSources, int nSources, void *pData, + int nXSize, int nYSize, + GDALDataType eSrcType, GDALDataType eBufType, + int nPixelSpace, int nLineSpace, + double base, double fact) +{ + int iLine, iCol, ii; + double dfPixVal; + + /* ---- Init ---- */ + if (nSources != 1) return CE_Failure; + if (GDALDataTypeIsComplex( eSrcType )) return CE_Failure; + + /* ---- Set pixels ---- */ + for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { + for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { + + /* Source raster pixels may be obtained with SRCVAL macro */ + dfPixVal = SRCVAL(papoSources[0], eSrcType, ii); + dfPixVal = pow(base, dfPixVal / fact); + + GDALCopyWords(&dfPixVal, GDT_Float64, 0, + ((GByte *)pData) + nLineSpace * iLine + + iCol * nPixelSpace, eBufType, nPixelSpace, 1); + } + } + + /* ---- Return success ---- */ + return CE_None; +} /* PowPixelFuncHelper */ + +CPLErr dB2AmpPixelFunc(void **papoSources, int nSources, void *pData, + int nXSize, int nYSize, + GDALDataType eSrcType, GDALDataType eBufType, + int nPixelSpace, int nLineSpace) +{ + return PowPixelFuncHelper(papoSources, nSources, pData, + nXSize, nYSize, eSrcType, eBufType, + nPixelSpace, nLineSpace, 10., 20.); +} /* dB2AmpPixelFunc */ + + +CPLErr dB2PowPixelFunc(void **papoSources, int nSources, void *pData, + int nXSize, int nYSize, + GDALDataType eSrcType, GDALDataType eBufType, + int nPixelSpace, int nLineSpace) +{ + return PowPixelFuncHelper(papoSources, nSources, pData, + nXSize, nYSize, eSrcType, eBufType, + nPixelSpace, nLineSpace, 10., 10.); +} /* dB2PowPixelFunc */ + + +/************************************************************************/ +/* GDALRegisterDefaultPixelFunc() */ +/************************************************************************/ + +/** + * This adds a default set of pixel functions to the global list of + * available pixel functions for derived bands: + * + * - "real": extract real part from a single raster band (just a copy if the + * input is non-complex) + * - "imag": extract imaginary part from a single raster band (0 for + * non-complex) + * - "mod": extract module from a single raster band (real or complex) + * - "phase": extract phase from a single raster band (0 for non-complex) + * - "conj": computes the complex conjugate of a single raster band (just a + * copy if the input is non-complex) + * - "sum": sum 2 or more raster bands + * - "diff": computes the difference between 2 raster bands (b1 - b2) + * - "mul": multilpy 2 or more raster bands + * - "cmul": multiply the first band for the complex conjugate of the second + * - "inv": inverse (1./x). Note: no check is performed on zero division + * - "intensity": computes the intensity Re(x*conj(x)) of a single raster band + * (real or complex) + * - "sqrt": perform the square root of a single raster band (real only) + * - "log10": compute the logarithm (base 10) of the abs of a single raster + * band (real or complex): log10( abs( x ) ) + * - "dB2amp": perform scale conversion from logarithmic to linear + * (amplitude) (i.e. 10 ^ ( x / 20 ) ) of a single raster + * band (real only) + * - "dB2pow": perform scale conversion from logarithmic to linear + * (power) (i.e. 10 ^ ( x / 10 ) ) of a single raster + * band (real only) + * + * @see GDALAddDerivedBandPixelFunc + * + * @return CE_None, invalid (NULL) parameters are currently ignored. + */ +CPLErr CPL_STDCALL GDALRegisterDefaultPixelFunc() +{ + GDALAddDerivedBandPixelFunc("real", RealPixelFunc); + GDALAddDerivedBandPixelFunc("imag", ImagPixelFunc); + GDALAddDerivedBandPixelFunc("mod", ModulePixelFunc); + GDALAddDerivedBandPixelFunc("phase", PhasePixelFunc); + GDALAddDerivedBandPixelFunc("conj", ConjPixelFunc); + GDALAddDerivedBandPixelFunc("sum", SumPixelFunc); + GDALAddDerivedBandPixelFunc("diff", DiffPixelFunc); + GDALAddDerivedBandPixelFunc("mul", MulPixelFunc); + GDALAddDerivedBandPixelFunc("cmul", CMulPixelFunc); + GDALAddDerivedBandPixelFunc("inv", InvPixelFunc); + GDALAddDerivedBandPixelFunc("intensity", IntensityPixelFunc); + GDALAddDerivedBandPixelFunc("sqrt", SqrtPixelFunc); + GDALAddDerivedBandPixelFunc("log10", Log10PixelFunc); + GDALAddDerivedBandPixelFunc("dB2amp", dB2AmpPixelFunc); + GDALAddDerivedBandPixelFunc("dB2pow", dB2PowPixelFunc); + + return CE_None; +} diff --git a/pixfunplugin.c b/pixfunplugin.c new file mode 100644 index 000000000000..3eda7602df78 --- /dev/null +++ b/pixfunplugin.c @@ -0,0 +1,56 @@ +/****************************************************************************** + * + * Project: GDAL + * Purpose: Provide a fake GDAL driver to register a small set of pixel + * functions to be used with the virtual driver. + * Indeed it is a dirty hack aimed to enable python users to use + * a small set of custom pixel functions without C++ coding. + * Author: Antonio Valentino + * + ****************************************************************************** + * Copyright (c) 2008-2014 Antonio Valentino + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + *****************************************************************************/ + +#include +#include + +CPL_CVSID("$Id: $"); + +CPL_C_START +CPL_DLL void GDALRegister_PIXFUN(void); +CPL_C_END + +extern CPLErr GDALRegisterDefaultPixelFunc(); + +/************************************************************************/ +/* GDALRegister_PIXFUN() */ +/************************************************************************/ +void GDALRegister_PIXFUN() +{ + const char PLUGINNAME[]="Pixfun"; + + if (! GDAL_CHECK_VERSION(PLUGINNAME)) + return; + + GDALRegisterDefaultPixelFunc(); + CPLDebug("PIXFUN", "Plugin %s %s", PLUGINNAME, "$Revision: $"); + +} /* GDALRegister_PIXFUN */ From 2cfd449db5faa8105ea3c7cb697d992d21fc2fc0 Mon Sep 17 00:00:00 2001 From: Antonio Valentino Date: Sun, 13 Apr 2014 12:36:38 +0200 Subject: [PATCH 02/69] Improved the README.txt file --- README.txt | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/README.txt b/README.txt index ab2a6587022f..449b7400357a 100644 --- a/README.txt +++ b/README.txt @@ -1,10 +1,21 @@ gdal-pixfun-plugin ================== -A small gdal plugin that provides a small set of "pixel functions" (see +A gdal plugin that provides a small set of "pixel functions" (see http://www.gdal.org/gdal_vrttut.html) that can be used to create derived raster bands. +The package provides: + +* the implementation of a set of GDALDerivedPixelFunc(s) to be used with + source raster band of virtual GDAL datasets +* a fake GDAL driver to register pixel functions + +.. note:: + + using the plugin mechanism is a hack aimed to enable python users + to use pixel functions without C++ coding + List of pixel functions ----------------------- @@ -60,7 +71,7 @@ A copy of the latest version of the sources can be obtained ising git_:: How to build, test and install ------------------------------ +------------------------------ The gdal-pixfun-plugin can be built using the following command:: @@ -76,6 +87,11 @@ To run the unit test suite:: To install the plugin just copy the generated shared object (gdal_PIXFUN.so) into the GDAL plugin directory (/usr/lib/gdalplugins/1.XX/ on unix). +The plugin can also be used without installing it. +The user just needs to set the GDAL_DRIVER_PATH environment variable:: + + export GDAL_DRIVER_PATH=:$GDAL_DRIVER_PATH + License ------- From 29f6bd39400a02bcd1e224f049e42827bea378bc Mon Sep 17 00:00:00 2001 From: Antonio Valentino Date: Sun, 13 Apr 2014 12:37:57 +0200 Subject: [PATCH 03/69] Remove unnecessary includes --- pixfunplugin.c | 1 - 1 file changed, 1 deletion(-) diff --git a/pixfunplugin.c b/pixfunplugin.c index 3eda7602df78..ca595ac80340 100644 --- a/pixfunplugin.c +++ b/pixfunplugin.c @@ -29,7 +29,6 @@ * DEALINGS IN THE SOFTWARE. *****************************************************************************/ -#include #include CPL_CVSID("$Id: $"); From 01e325c543c29252f8ee0c1a20e67362d8c44182 Mon Sep 17 00:00:00 2001 From: Antonio Valentino Date: Sun, 13 Apr 2014 12:39:07 +0200 Subject: [PATCH 04/69] Silence compiler warnings --- pixfunplugin.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pixfunplugin.c b/pixfunplugin.c index ca595ac80340..57522e584bda 100644 --- a/pixfunplugin.c +++ b/pixfunplugin.c @@ -42,7 +42,7 @@ extern CPLErr GDALRegisterDefaultPixelFunc(); /************************************************************************/ /* GDALRegister_PIXFUN() */ /************************************************************************/ -void GDALRegister_PIXFUN() +void GDALRegister_PIXFUN(void) { const char PLUGINNAME[]="Pixfun"; From 4038d403a47c430e3bad914849ae6e142707fcaa Mon Sep 17 00:00:00 2001 From: Antonio Valentino Date: Sun, 13 Apr 2014 12:39:30 +0200 Subject: [PATCH 05/69] Minor improvements --- Makefile | 4 ++-- pixfunplugin.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 70c143bb40e6..2362046d1688 100644 --- a/Makefile +++ b/Makefile @@ -27,7 +27,7 @@ # DEALINGS IN THE SOFTWARE. ############################################################################## -.PHONY: all clean dist +.PHONY: all clean dist check OBJS = pixfunplugin.o pixelfunctions.o CFLAGS := -fPIC -Wall -Wno-long-long $(shell gdal-config --cflags) $(CFLAGS) @@ -50,7 +50,7 @@ $(TARGET): $(OBJS) PYTHON=python -check: +check: $(TARGET) cd autotest/gcore && \ env GDAL_DRIVER_PATH=$(PWD):$(GDAL_DRIVER_PATH) \ PYTHONPATH=$(PWD)/autotest/pymod:$(PYTHONPATH) \ diff --git a/pixfunplugin.c b/pixfunplugin.c index 57522e584bda..15c113d10f1a 100644 --- a/pixfunplugin.c +++ b/pixfunplugin.c @@ -3,8 +3,8 @@ * Project: GDAL * Purpose: Provide a fake GDAL driver to register a small set of pixel * functions to be used with the virtual driver. - * Indeed it is a dirty hack aimed to enable python users to use - * a small set of custom pixel functions without C++ coding. + * It is a hack aimed to enable python users to use a small set + * of custom pixel functions without C++ coding. * Author: Antonio Valentino * ****************************************************************************** From d7eaf5fb5080abb443434a6ef2d1c34d032ac58c Mon Sep 17 00:00:00 2001 From: Alessandro Amici Date: Tue, 23 Jun 2015 17:02:43 +0200 Subject: [PATCH 06/69] Add the MakeCompexPixelFunc that make a complex out of a real and imag bands --- README.txt | 2 ++ autotest/gcore/data/pixfun_makecomplex.vrt | 19 ++++++++++++ autotest/gcore/pixfun.py | 26 +++++++++++++++++ pixelfunctions.c | 34 ++++++++++++++++++++++ 4 files changed, 81 insertions(+) create mode 100644 autotest/gcore/data/pixfun_makecomplex.vrt diff --git a/README.txt b/README.txt index 449b7400357a..01a265765490 100644 --- a/README.txt +++ b/README.txt @@ -26,6 +26,8 @@ List of pixel functions :"imag": extract imaginary part from a single raster band (0 for non-complex) +:"makecomplex": + make a complex band merging two bands used as real and imag values :"mod": extract module from a single raster band (real or complex) :"phase": diff --git a/autotest/gcore/data/pixfun_makecomplex.vrt b/autotest/gcore/data/pixfun_makecomplex.vrt new file mode 100644 index 000000000000..f276dca06481 --- /dev/null +++ b/autotest/gcore/data/pixfun_makecomplex.vrt @@ -0,0 +1,19 @@ + + + Make complex + makecomplex + CFloat32 + + int32.tif + 1 + + + + + int32.tif + 1 + + + + + \ No newline at end of file diff --git a/autotest/gcore/pixfun.py b/autotest/gcore/pixfun.py index 991f74c4f410..fcabeb2710d6 100644 --- a/autotest/gcore/pixfun.py +++ b/autotest/gcore/pixfun.py @@ -135,6 +135,31 @@ def pixfun_imag_r(): return 'success' +############################################################################### +# Verify imaginary part extraction from a real dataset. + +def pixfun_makecomplex(): + + filename = 'data/pixfun_makecomplex.vrt' + ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) + if ds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % filename) + return 'fail' + data = ds.GetRasterBand(1).ReadAsArray() + + reffilename = 'data/int32.tif' + refds = gdal.Open(reffilename) + if refds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) + return 'fail' + refdata = refds.GetRasterBand(1).ReadAsArray() + + if not numpy.allclose(data, refdata + 1j * refdata): + return 'fail' + + return 'success' + + ############################################################################### # Verify modulus extraction from a complex (float) dataset. @@ -731,6 +756,7 @@ def pixfun_dB2pow(): pixfun_real_r, pixfun_imag_c, pixfun_imag_r, + pixfun_makecomplex, pixfun_mod_c, pixfun_mod_r, pixfun_phase_c, diff --git a/pixelfunctions.c b/pixelfunctions.c index 9ac7134e5c08..c39b2c760c9d 100644 --- a/pixelfunctions.c +++ b/pixelfunctions.c @@ -99,6 +99,39 @@ CPLErr ImagPixelFunc(void **papoSources, int nSources, void *pData, } /* ImagPixelFunc */ +CPLErr MakeComplexPixelFunc(void **papoSources, int nSources, void *pData, + int nXSize, int nYSize, + GDALDataType eSrcType, GDALDataType eBufType, + int nPixelSpace, int nLineSpace) +{ + int ii, iLine, iCol; + double adfPixVal[2]; + + void *pReal = papoSources[0]; + void *pImag = papoSources[1]; + + /* ---- Init ---- */ + if (nSources != 2) return CE_Failure; + + /* ---- Set pixels ---- */ + for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { + for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { + + /* Source raster pixels may be obtained with SRCVAL macro */ + adfPixVal[0] = SRCVAL(pReal, eSrcType, ii); /* re */ + adfPixVal[1] = SRCVAL(pImag, eSrcType, ii); /* im */ + + GDALCopyWords(adfPixVal, GDT_CFloat64, 0, + ((GByte *)pData) + nLineSpace * iLine + + iCol * nPixelSpace, eBufType, nPixelSpace, 1); + } + } + + /* ---- Return success ---- */ + return CE_None; +} /* MakeComplexPixelFunc */ + + CPLErr ModulePixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, @@ -800,6 +833,7 @@ CPLErr CPL_STDCALL GDALRegisterDefaultPixelFunc() { GDALAddDerivedBandPixelFunc("real", RealPixelFunc); GDALAddDerivedBandPixelFunc("imag", ImagPixelFunc); + GDALAddDerivedBandPixelFunc("makecomplex", MakeComplexPixelFunc); GDALAddDerivedBandPixelFunc("mod", ModulePixelFunc); GDALAddDerivedBandPixelFunc("phase", PhasePixelFunc); GDALAddDerivedBandPixelFunc("conj", ConjPixelFunc); From 4668088df27d2fcb272218bed0f99565cf9ccd31 Mon Sep 17 00:00:00 2001 From: Antonio Valentino Date: Tue, 23 Jun 2015 22:34:00 +0200 Subject: [PATCH 07/69] Rename makecomplex into complex --- README.txt | 4 ++-- .../data/{pixfun_makecomplex.vrt => pixfun_complex.vrt} | 4 ++-- autotest/gcore/pixfun.py | 8 ++++---- pixelfunctions.c | 6 ++++-- 4 files changed, 12 insertions(+), 10 deletions(-) rename autotest/gcore/data/{pixfun_makecomplex.vrt => pixfun_complex.vrt} (91%) diff --git a/README.txt b/README.txt index 01a265765490..7827021cfb28 100644 --- a/README.txt +++ b/README.txt @@ -26,7 +26,7 @@ List of pixel functions :"imag": extract imaginary part from a single raster band (0 for non-complex) -:"makecomplex": +:"complex": make a complex band merging two bands used as real and imag values :"mod": extract module from a single raster band (real or complex) @@ -34,7 +34,7 @@ List of pixel functions extract phase from a single raster band (0 for non-complex) :"conj": computes the complex conjugate of a single raster band (just a - copy if the input is non-complex) + copy if the input is non-complex) :"sum": sum 2 or more raster bands :"diff": diff --git a/autotest/gcore/data/pixfun_makecomplex.vrt b/autotest/gcore/data/pixfun_complex.vrt similarity index 91% rename from autotest/gcore/data/pixfun_makecomplex.vrt rename to autotest/gcore/data/pixfun_complex.vrt index f276dca06481..c26c08cfada1 100644 --- a/autotest/gcore/data/pixfun_makecomplex.vrt +++ b/autotest/gcore/data/pixfun_complex.vrt @@ -1,7 +1,7 @@ Make complex - makecomplex + complex CFloat32 int32.tif @@ -16,4 +16,4 @@ - \ No newline at end of file + diff --git a/autotest/gcore/pixfun.py b/autotest/gcore/pixfun.py index fcabeb2710d6..b8402f4a9641 100644 --- a/autotest/gcore/pixfun.py +++ b/autotest/gcore/pixfun.py @@ -138,9 +138,9 @@ def pixfun_imag_r(): ############################################################################### # Verify imaginary part extraction from a real dataset. -def pixfun_makecomplex(): +def pixfun_complex(): - filename = 'data/pixfun_makecomplex.vrt' + filename = 'data/pixfun_complex.vrt' ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) if ds is None: gdaltest.post_reason('Unable to open "%s" dataset.' % filename) @@ -587,7 +587,7 @@ def pixfun_inv_c(): refdata = refds.GetRasterBand(1).ReadAsArray() refdata = refdata.astype('complex') delta = data - 1./refdata - + if not numpy.alltrue(abs(delta.real) < 1e-13): return 'fail' if not numpy.alltrue(abs(delta.imag) < 1e-13): @@ -756,7 +756,7 @@ def pixfun_dB2pow(): pixfun_real_r, pixfun_imag_c, pixfun_imag_r, - pixfun_makecomplex, + pixfun_complex, pixfun_mod_c, pixfun_mod_r, pixfun_phase_c, diff --git a/pixelfunctions.c b/pixelfunctions.c index c39b2c760c9d..79801ca51468 100644 --- a/pixelfunctions.c +++ b/pixelfunctions.c @@ -99,7 +99,7 @@ CPLErr ImagPixelFunc(void **papoSources, int nSources, void *pData, } /* ImagPixelFunc */ -CPLErr MakeComplexPixelFunc(void **papoSources, int nSources, void *pData, +CPLErr ComplexPixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace) @@ -804,6 +804,8 @@ CPLErr dB2PowPixelFunc(void **papoSources, int nSources, void *pData, * input is non-complex) * - "imag": extract imaginary part from a single raster band (0 for * non-complex) + * - "complex": make a complex band merging two bands used as real and + * imag values * - "mod": extract module from a single raster band (real or complex) * - "phase": extract phase from a single raster band (0 for non-complex) * - "conj": computes the complex conjugate of a single raster band (just a @@ -833,7 +835,7 @@ CPLErr CPL_STDCALL GDALRegisterDefaultPixelFunc() { GDALAddDerivedBandPixelFunc("real", RealPixelFunc); GDALAddDerivedBandPixelFunc("imag", ImagPixelFunc); - GDALAddDerivedBandPixelFunc("makecomplex", MakeComplexPixelFunc); + GDALAddDerivedBandPixelFunc("complex", ComplexPixelFunc); GDALAddDerivedBandPixelFunc("mod", ModulePixelFunc); GDALAddDerivedBandPixelFunc("phase", PhasePixelFunc); GDALAddDerivedBandPixelFunc("conj", ConjPixelFunc); From ac7f70a9bb662cecf731cab5457936ed83d30961 Mon Sep 17 00:00:00 2001 From: Antonio Valentino Date: Mon, 1 Feb 2016 23:48:59 +0100 Subject: [PATCH 08/69] Fix floating point trunation in mod function (closes gh-3) --- pixelfunctions.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pixelfunctions.c b/pixelfunctions.c index 79801ca51468..81396834b1e8 100644 --- a/pixelfunctions.c +++ b/pixelfunctions.c @@ -171,7 +171,7 @@ CPLErr ModulePixelFunc(void **papoSources, int nSources, void *pData, for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { /* Source raster pixels may be obtained with SRCVAL macro */ - dfPixVal = abs(SRCVAL(papoSources[0], eSrcType, ii)); + dfPixVal = fabs(SRCVAL(papoSources[0], eSrcType, ii)); GDALCopyWords(&dfPixVal, GDT_Float64, 0, ((GByte *)pData) + nLineSpace * iLine + @@ -725,7 +725,7 @@ CPLErr Log10PixelFunc(void **papoSources, int nSources, void *pData, /* Source raster pixels may be obtained with SRCVAL macro */ dfPixVal = SRCVAL(papoSources[0], eSrcType, ii); - dfPixVal = log10( abs( dfPixVal ) ); + dfPixVal = log10( fabs( dfPixVal ) ); GDALCopyWords(&dfPixVal, GDT_Float64, 0, ((GByte *)pData) + nLineSpace * iLine + From a984a78fd49314451532966180d6fc773ba36754 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Mon, 4 Jul 2016 14:25:11 +0200 Subject: [PATCH 09/69] ENH: Adding a complex derived dataset to allow for on-the-fly computation of amplitude while reading --- gdal/frmts/cderived/GNUmakefile | 11 ++ gdal/frmts/cderived/cderiveddataset.cpp | 195 ++++++++++++++++++++++++ gdal/frmts/gdalallregister.cpp | 1 + gdal/gcore/gdal_frmts.h | 1 + 4 files changed, 208 insertions(+) create mode 100644 gdal/frmts/cderived/GNUmakefile create mode 100644 gdal/frmts/cderived/cderiveddataset.cpp diff --git a/gdal/frmts/cderived/GNUmakefile b/gdal/frmts/cderived/GNUmakefile new file mode 100644 index 000000000000..dd9b946208a7 --- /dev/null +++ b/gdal/frmts/cderived/GNUmakefile @@ -0,0 +1,11 @@ + +include ../../GDALmake.opt + +OBJ = cderiveddataset.o + +default: $(OBJ:.o=.$(OBJ_EXT)) + +clean: + rm -f *.o $(O_OBJ) + +install-obj: $(O_OBJ:.o=.$(OBJ_EXT)) diff --git a/gdal/frmts/cderived/cderiveddataset.cpp b/gdal/frmts/cderived/cderiveddataset.cpp new file mode 100644 index 000000000000..1fbb8c420acc --- /dev/null +++ b/gdal/frmts/cderived/cderiveddataset.cpp @@ -0,0 +1,195 @@ +#include "../vrt/vrtdataset.h" +#include "gdal_pam.h" +#include "gdal_proxy.h" + +#include + +CPLErr ModulePixelFunc(void **papoSources, int nSources, void *pData, + int nXSize, int nYSize, + GDALDataType eSrcType, GDALDataType eBufType, + int nPixelSpace, int nLineSpace) +{ + int ii, iLine, iCol; + double dfPixVal; + + /* ---- Init ---- */ + if (nSources != 1) return CE_Failure; + + if (GDALDataTypeIsComplex( eSrcType )) + { + double dfReal, dfImag; + void *pReal = papoSources[0]; + void *pImag = ((GByte *)papoSources[0]) + + GDALGetDataTypeSize( eSrcType ) / 8 / 2; + + /* ---- Set pixels ---- */ + for( iLine = 0, ii = 0; iLine < nYSize; ++iLine ) { + for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { + + /* Source raster pixels may be obtained with SRCVAL macro */ + dfReal = SRCVAL(pReal, eSrcType, ii); + dfImag = SRCVAL(pImag, eSrcType, ii); + + dfPixVal = sqrt( dfReal * dfReal + dfImag * dfImag ); + + GDALCopyWords(&dfPixVal, GDT_Float64, 0, + ((GByte *)pData) + nLineSpace * iLine + + iCol * nPixelSpace, eBufType, nPixelSpace, 1); + } + } + } else { + /* ---- Set pixels ---- */ + for( iLine = 0, ii = 0; iLine < nYSize; ++iLine ) { + for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { + + /* Source raster pixels may be obtained with SRCVAL macro */ + dfPixVal = fabs(SRCVAL(papoSources[0], eSrcType, ii)); + + GDALCopyWords(&dfPixVal, GDT_Float64, 0, + ((GByte *)pData) + nLineSpace * iLine + + iCol * nPixelSpace, eBufType, nPixelSpace, 1); + } + } + } + + /* ---- Return success ---- */ + return CE_None; +} /* ModulePixelFunc */ + + +class ComplexDerivedDatasetContainer: public GDALPamDataset +{ + public: + ComplexDerivedDatasetContainer() {} +}; + + +class ComplexDerivedDataset : public VRTDataset +{ + public: + ComplexDerivedDataset(int nXSize, int nYSize); + ~ComplexDerivedDataset(); + + static GDALDataset *Open( GDALOpenInfo * ); + static int Identify( GDALOpenInfo * ); +}; + + +ComplexDerivedDataset::ComplexDerivedDataset(int nXSize, int nYSize) : VRTDataset(nXSize,nYSize) +{ + poDriver = NULL; + SetWritable(FALSE); +} + +ComplexDerivedDataset::~ComplexDerivedDataset() +{ +} + +int ComplexDerivedDataset::Identify(GDALOpenInfo * poOpenInfo) +{ + if(STARTS_WITH_CI(poOpenInfo->pszFilename, "DERIVED_SUBDATASET:COMPLEX_AMPLITUDE:")) + return TRUE; + + return FALSE; +} + +GDALDataset * ComplexDerivedDataset::Open(GDALOpenInfo * poOpenInfo) +{ + if( !Identify(poOpenInfo) ) + { + return NULL; + } + + /* Try to open original dataset */ + CPLString filename(poOpenInfo->pszFilename); + + // TODO: check starts with + + /* DERIVED_SUBDATASET should be first domain */ + size_t dsds_pos = filename.find("DERIVED_SUBDATASET:"); + + if (dsds_pos == std::string::npos) + { + /* Unable to Open in this case */ + return NULL; + } + + /* DERIVED_SUBDATASET should be first domain */ + size_t alg_pos = filename.find(":",dsds_pos+20); + if (alg_pos == std::string::npos) + { + /* Unable to Open in this case */ + return NULL; + } + + VRTDerivedRasterBand::AddPixelFunction("mod",ModulePixelFunc); + + CPLString odFilename = filename.substr(alg_pos+1,filename.size() - alg_pos); + + GDALDataset * poTmpDS = (GDALDataset*)GDALOpen(odFilename, GA_ReadOnly); + + if( poTmpDS == NULL ) + return NULL; + + int nbBands = poTmpDS->GetRasterCount(); + + int nRows = poTmpDS->GetRasterYSize(); + int nCols = poTmpDS->GetRasterXSize(); + + ComplexDerivedDataset * poDS = new ComplexDerivedDataset(nCols,nRows); + + // Transfer metadata + poDS->SetMetadata(poTmpDS->GetMetadata()); + + for(int nBand = 1; nBand <= nbBands; ++nBand) + { + + + VRTDerivedRasterBand * poBand; + + GDALDataType type = GDT_Float64; + + poBand = new VRTDerivedRasterBand(poDS,nBand,type,nCols,nRows); + poBand->SetPixelFunctionName("mod"); + poBand->SetSourceTransferType(poTmpDS->GetRasterBand(nBand)->GetRasterDataType()); + poDS->SetBand(nBand,poBand); + + GDALProxyPoolDataset* proxyDS; + proxyDS = new GDALProxyPoolDataset( odFilename, + poDS->nRasterXSize, + poDS->nRasterYSize, + GA_ReadOnly, + TRUE); + proxyDS->AddSrcBandDescription(poTmpDS->GetRasterBand(nBand)->GetRasterDataType(), 128, 128); + + poBand->AddComplexSource(proxyDS->GetRasterBand(nBand),0,0,nCols,nRows,0,0,nCols,nRows); + proxyDS->Dereference(); + } + + // TODO: do this earlier ? + delete poTmpDS; + + return poDS; +} + +void GDALRegister_ComplexDerived() +{ + if( GDALGetDriverByName( "COMPLEXDERIVED" ) != NULL ) + return; + + GDALDriver *poDriver = new GDALDriver(); + + poDriver->SetDescription( "COMPLEXDERIVED" ); +#ifdef GDAL_DCAP_RASTER + poDriver->SetMetadataItem( GDAL_DCAP_RASTER, "YES" ); +#endif + poDriver->SetMetadataItem( GDAL_DCAP_VIRTUALIO, "YES" ); + poDriver->SetMetadataItem( GDAL_DMD_LONGNAME, "Complex derived bands" ); + poDriver->SetMetadataItem( GDAL_DMD_HELPTOPIC, "frmt_sentinel2.html" ); + poDriver->SetMetadataItem( GDAL_DMD_SUBDATASETS, "YES" ); + + poDriver->pfnOpen = ComplexDerivedDataset::Open; + poDriver->pfnIdentify = ComplexDerivedDataset::Identify; + + GetGDALDriverManager()->RegisterDriver( poDriver ); +} diff --git a/gdal/frmts/gdalallregister.cpp b/gdal/frmts/gdalallregister.cpp index 569ad92c5e31..9c215a35805c 100644 --- a/gdal/frmts/gdalallregister.cpp +++ b/gdal/frmts/gdalallregister.cpp @@ -68,6 +68,7 @@ void CPL_STDCALL GDALAllRegister() #ifdef FRMT_vrt GDALRegister_VRT(); + GDALRegister_ComplexDerived(); #endif #ifdef FRMT_gtiff diff --git a/gdal/gcore/gdal_frmts.h b/gdal/gcore/gdal_frmts.h index 284a7bb45f7c..354177597bdc 100644 --- a/gdal/gcore/gdal_frmts.h +++ b/gdal/gcore/gdal_frmts.h @@ -187,6 +187,7 @@ void CPL_DLL GDALRegister_SAFE(void); void CPL_DLL GDALRegister_SENTINEL2(void); void CPL_DLL GDALRegister_mrf(void); void CPL_DLL GDALRegister_RRASTER(void); +void CPL_DLL GDALRegister_ComplexDerived(void); CPL_C_END #endif /* ndef GDAL_FRMTS_H_INCLUDED */ From e277ccf2a07e73e461532900ee3e18c737ee0e7c Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Mon, 4 Jul 2016 16:16:33 +0200 Subject: [PATCH 10/69] ENH: Comments from review --- gdal/frmts/cderived/cderiveddataset.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/gdal/frmts/cderived/cderiveddataset.cpp b/gdal/frmts/cderived/cderiveddataset.cpp index 1fbb8c420acc..bc9987192876 100644 --- a/gdal/frmts/cderived/cderiveddataset.cpp +++ b/gdal/frmts/cderived/cderiveddataset.cpp @@ -166,8 +166,7 @@ GDALDataset * ComplexDerivedDataset::Open(GDALOpenInfo * poOpenInfo) proxyDS->Dereference(); } - // TODO: do this earlier ? - delete poTmpDS; + GDALClose(poTmpDS); return poDS; } @@ -185,8 +184,8 @@ void GDALRegister_ComplexDerived() #endif poDriver->SetMetadataItem( GDAL_DCAP_VIRTUALIO, "YES" ); poDriver->SetMetadataItem( GDAL_DMD_LONGNAME, "Complex derived bands" ); - poDriver->SetMetadataItem( GDAL_DMD_HELPTOPIC, "frmt_sentinel2.html" ); - poDriver->SetMetadataItem( GDAL_DMD_SUBDATASETS, "YES" ); + poDriver->SetMetadataItem( GDAL_DMD_HELPTOPIC, "TODO" ); + poDriver->SetMetadataItem( GDAL_DMD_SUBDATASETS, "NO" ); poDriver->pfnOpen = ComplexDerivedDataset::Open; poDriver->pfnIdentify = ComplexDerivedDataset::Identify; From 665d8794b8162adefdd296ad7a6a6813eae3ed4d Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Tue, 5 Jul 2016 13:00:25 +0200 Subject: [PATCH 11/69] ENH: Expose derived subdatasets in SetBand() --- gdal/gcore/gdaldataset.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/gdal/gcore/gdaldataset.cpp b/gdal/gcore/gdaldataset.cpp index 819332077fd2..f1eb1721ce69 100644 --- a/gdal/gcore/gdaldataset.cpp +++ b/gdal/gcore/gdaldataset.cpp @@ -581,8 +581,15 @@ void GDALDataset::SetBand( int nNewBand, GDALRasterBand * poBand ) "Cannot allocate band array"); return; } + papoBands = papoNewBands; + // TODO: Filter on complex bands only + SetMetadataItem("DERIVED_SUBDATASET_1_NAME",CPLSPrintf("DERIVED_SUBDATASET:COMPLEX_AMPLITUDE:%s",GetDescription()),"DERIVED_SUBDATASETS"); + + CPLString osDesc(CPLSPrintf("Complex amplitude of bands from %s",GetDescription())); + SetMetadataItem("DERIVED_SUBDATASET_1_DESC",osDesc.c_str(),"DERIVED_SUBDATASETS"); + for( int i = nBands; i < nNewBand; ++i ) papoBands[i] = NULL; From d384b9d44ad82ca015d5e3dcac9d4c71ae5329c0 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Wed, 6 Jul 2016 10:19:20 +0200 Subject: [PATCH 12/69] ENH: Report projectionRef, geoTransform and GCPs for derived subdatasets --- gdal/frmts/cderived/cderiveddataset.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/gdal/frmts/cderived/cderiveddataset.cpp b/gdal/frmts/cderived/cderiveddataset.cpp index bc9987192876..2f56f0ad265b 100644 --- a/gdal/frmts/cderived/cderiveddataset.cpp +++ b/gdal/frmts/cderived/cderiveddataset.cpp @@ -140,11 +140,26 @@ GDALDataset * ComplexDerivedDataset::Open(GDALOpenInfo * poOpenInfo) // Transfer metadata poDS->SetMetadata(poTmpDS->GetMetadata()); + + // Transfer projection + poDS->SetProjection(poTmpDS->GetProjectionRef()); - for(int nBand = 1; nBand <= nbBands; ++nBand) + // Transfer geotransform + double padfTransform[6]; + bool transformOk = poTmpDS->GetGeoTransform(padfTransform); + if(transformOk) { + poDS->SetGeoTransform(padfTransform); + } + + // Transfer GCPs + const char * gcpProjection = poTmpDS->GetGCPProjection(); + int nbGcps = poTmpDS->GetGCPCount(); + poDS->SetGCPs(nbGcps,poTmpDS->GetGCPs(),gcpProjection); - + // Map bands + for(int nBand = 1; nBand <= nbBands; ++nBand) + { VRTDerivedRasterBand * poBand; GDALDataType type = GDT_Float64; From de11bcd3246443390c6b9abe7f92c6b4999adf21 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Wed, 6 Jul 2016 11:53:56 +0200 Subject: [PATCH 13/69] Prepare for integration in Gdal --- Makefile | 57 ---------- README.txt | 101 ------------------ .../{gcore => gdrivers}/data/cfloat64.tif | Bin .../{gcore => gdrivers}/data/cint_sar.tif | Bin autotest/{gcore => gdrivers}/data/float32.tif | Bin autotest/{gcore => gdrivers}/data/int32.tif | Bin .../data/pixfun_cmul_c.vrt | 0 .../data/pixfun_cmul_r.vrt | 0 .../data/pixfun_complex.vrt | 0 .../data/pixfun_conj_c.vrt | 0 .../data/pixfun_conj_r.vrt | 0 .../data/pixfun_dB2amp.vrt | 0 .../data/pixfun_dB2pow.vrt | 0 .../data/pixfun_diff_c.vrt | 0 .../data/pixfun_diff_r.vrt | 0 .../data/pixfun_imag_c.vrt | 0 .../data/pixfun_imag_r.vrt | 0 .../data/pixfun_intensity_c.vrt | 0 .../data/pixfun_intensity_r.vrt | 0 .../{gcore => gdrivers}/data/pixfun_inv_c.vrt | 0 .../{gcore => gdrivers}/data/pixfun_inv_r.vrt | 0 .../{gcore => gdrivers}/data/pixfun_log10.vrt | 0 .../{gcore => gdrivers}/data/pixfun_mod_c.vrt | 0 .../{gcore => gdrivers}/data/pixfun_mod_r.vrt | 0 .../{gcore => gdrivers}/data/pixfun_mul_c.vrt | 0 .../{gcore => gdrivers}/data/pixfun_mul_r.vrt | 0 .../data/pixfun_phase_c.vrt | 0 .../data/pixfun_phase_r.vrt | 0 .../data/pixfun_real_c.vrt | 0 .../data/pixfun_real_r.vrt | 0 .../{gcore => gdrivers}/data/pixfun_sqrt.vrt | 0 .../{gcore => gdrivers}/data/pixfun_sum_c.vrt | 0 .../{gcore => gdrivers}/data/pixfun_sum_r.vrt | 0 autotest/{gcore => gdrivers}/data/uint16.tif | Bin autotest/{gcore => gdrivers}/pixfun.py | 0 .../{gcore => gdrivers}/testnonboundtoswig.py | 0 .../frmts/vrt/pixelfunctions.c | 0 .../frmts/vrt/pixelfunctions.licence | 0 pixfunplugin.c | 55 ---------- 39 files changed, 213 deletions(-) delete mode 100644 Makefile delete mode 100644 README.txt rename autotest/{gcore => gdrivers}/data/cfloat64.tif (100%) rename autotest/{gcore => gdrivers}/data/cint_sar.tif (100%) rename autotest/{gcore => gdrivers}/data/float32.tif (100%) rename autotest/{gcore => gdrivers}/data/int32.tif (100%) rename autotest/{gcore => gdrivers}/data/pixfun_cmul_c.vrt (100%) rename autotest/{gcore => gdrivers}/data/pixfun_cmul_r.vrt (100%) rename autotest/{gcore => gdrivers}/data/pixfun_complex.vrt (100%) rename autotest/{gcore => gdrivers}/data/pixfun_conj_c.vrt (100%) rename autotest/{gcore => gdrivers}/data/pixfun_conj_r.vrt (100%) rename autotest/{gcore => gdrivers}/data/pixfun_dB2amp.vrt (100%) rename autotest/{gcore => gdrivers}/data/pixfun_dB2pow.vrt (100%) rename autotest/{gcore => gdrivers}/data/pixfun_diff_c.vrt (100%) rename autotest/{gcore => gdrivers}/data/pixfun_diff_r.vrt (100%) rename autotest/{gcore => gdrivers}/data/pixfun_imag_c.vrt (100%) rename autotest/{gcore => gdrivers}/data/pixfun_imag_r.vrt (100%) rename autotest/{gcore => gdrivers}/data/pixfun_intensity_c.vrt (100%) rename autotest/{gcore => gdrivers}/data/pixfun_intensity_r.vrt (100%) rename autotest/{gcore => gdrivers}/data/pixfun_inv_c.vrt (100%) rename autotest/{gcore => gdrivers}/data/pixfun_inv_r.vrt (100%) rename autotest/{gcore => gdrivers}/data/pixfun_log10.vrt (100%) rename autotest/{gcore => gdrivers}/data/pixfun_mod_c.vrt (100%) rename autotest/{gcore => gdrivers}/data/pixfun_mod_r.vrt (100%) rename autotest/{gcore => gdrivers}/data/pixfun_mul_c.vrt (100%) rename autotest/{gcore => gdrivers}/data/pixfun_mul_r.vrt (100%) rename autotest/{gcore => gdrivers}/data/pixfun_phase_c.vrt (100%) rename autotest/{gcore => gdrivers}/data/pixfun_phase_r.vrt (100%) rename autotest/{gcore => gdrivers}/data/pixfun_real_c.vrt (100%) rename autotest/{gcore => gdrivers}/data/pixfun_real_r.vrt (100%) rename autotest/{gcore => gdrivers}/data/pixfun_sqrt.vrt (100%) rename autotest/{gcore => gdrivers}/data/pixfun_sum_c.vrt (100%) rename autotest/{gcore => gdrivers}/data/pixfun_sum_r.vrt (100%) rename autotest/{gcore => gdrivers}/data/uint16.tif (100%) rename autotest/{gcore => gdrivers}/pixfun.py (100%) rename autotest/{gcore => gdrivers}/testnonboundtoswig.py (100%) rename pixelfunctions.c => gdal/frmts/vrt/pixelfunctions.c (100%) rename LICENSE.txt => gdal/frmts/vrt/pixelfunctions.licence (100%) delete mode 100644 pixfunplugin.c diff --git a/Makefile b/Makefile deleted file mode 100644 index 2362046d1688..000000000000 --- a/Makefile +++ /dev/null @@ -1,57 +0,0 @@ -############################################################################## -# -# Project: GDAL -# Purpose: Implementation of a set of GDALDerivedPixelFunc(s) to be used -# with source raster band of virtual GDAL datasets. -# Author: Antonio Valentino -# -############################################################################## -# Copyright (c) 2008-2014 Antonio Valentino -# -# Permission is hereby granted, free of charge, to any person obtaining a -# copy of this software and associated documentation files (the "Software"), -# to deal in the Software without restriction, including without limitation -# the rights to use, copy, modify, merge, publish, distribute, sublicense, -# and/or sell copies of the Software, and to permit persons to whom the -# Software is furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included -# in all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -# DEALINGS IN THE SOFTWARE. -############################################################################## - -.PHONY: all clean dist check - -OBJS = pixfunplugin.o pixelfunctions.o -CFLAGS := -fPIC -Wall -Wno-long-long $(shell gdal-config --cflags) $(CFLAGS) -CFLAGS := -pedantic $(CFLAGS) - -CFLAGS := -g $(CFLAGS) -#CFLAGS := -O3 $(CFLAGS) - -TARGET = gdal_PIXFUN.so - -all: $(TARGET) - -clean: - $(RM) $(TARGET) *.o *~ - $(RM) autotest/pymod/*.pyc autotest/gcore/*.pyc - -$(TARGET): $(OBJS) - $(CC) -shared -o $@ $(OBJS) $(shell gdal-config --libs) - - -PYTHON=python - -check: $(TARGET) - cd autotest/gcore && \ - env GDAL_DRIVER_PATH=$(PWD):$(GDAL_DRIVER_PATH) \ - PYTHONPATH=$(PWD)/autotest/pymod:$(PYTHONPATH) \ - $(PYTHON) pixfun.py diff --git a/README.txt b/README.txt deleted file mode 100644 index 7827021cfb28..000000000000 --- a/README.txt +++ /dev/null @@ -1,101 +0,0 @@ -gdal-pixfun-plugin -================== - -A gdal plugin that provides a small set of "pixel functions" (see -http://www.gdal.org/gdal_vrttut.html) that can be used to create derived -raster bands. - -The package provides: - -* the implementation of a set of GDALDerivedPixelFunc(s) to be used with - source raster band of virtual GDAL datasets -* a fake GDAL driver to register pixel functions - -.. note:: - - using the plugin mechanism is a hack aimed to enable python users - to use pixel functions without C++ coding - - -List of pixel functions ------------------------ - -:"real": - extract real part from a single raster band (just a copy if the - input is non-complex) -:"imag": - extract imaginary part from a single raster band (0 for - non-complex) -:"complex": - make a complex band merging two bands used as real and imag values -:"mod": - extract module from a single raster band (real or complex) -:"phase": - extract phase from a single raster band (0 for non-complex) -:"conj": - computes the complex conjugate of a single raster band (just a - copy if the input is non-complex) -:"sum": - sum 2 or more raster bands -:"diff": - computes the difference between 2 raster bands (b1 - b2) -:"mul": - multilpy 2 or more raster bands -:"cmul": - multiply the first band for the complex conjugate of the second -:"inv": - inverse (1./x). Note: no check is performed on zero division -:"intensity": - computes the intensity Re(x*conj(x)) of a single raster band - (real or complex) -:"sqrt": - perform the square root of a single raster band (real only) -:"log10": - compute the logarithm (base 10) of the abs of a single raster - band (real or complex): log10( abs( x ) ) -:"dB2amp": - perform scale conversion from logarithmic to linear - (amplitude) (i.e. 10 ^ ( x / 20 ) ) of a single raster band (real only) -:"dB2pow": - perform scale conversion from logarithmic to linear - (power) (i.e. 10 ^ ( x / 10 ) ) of a single raster band (real only) - - -How to get it -------------- - -The project home page is at https://github.com/avalentino/gdal-pixfun-plugin. -A copy of the latest version of the sources can be obtained ising git_:: - - $ git clone https://github.com/avalentino/gdal-pixfun-plugin.git - -.. _git: http://git-scm.com - - -How to build, test and install ------------------------------- - -The gdal-pixfun-plugin can be built using the following command:: - - $ make - -in the root folder of the source distribution. -It assumes that GDAL is correctly installed on your system. - -To run the unit test suite:: - - $ make check - -To install the plugin just copy the generated shared object (gdal_PIXFUN.so) -into the GDAL plugin directory (/usr/lib/gdalplugins/1.XX/ on unix). - -The plugin can also be used without installing it. -The user just needs to set the GDAL_DRIVER_PATH environment variable:: - - export GDAL_DRIVER_PATH=:$GDAL_DRIVER_PATH - - -License -------- - -See the LICENSE.txt file. diff --git a/autotest/gcore/data/cfloat64.tif b/autotest/gdrivers/data/cfloat64.tif similarity index 100% rename from autotest/gcore/data/cfloat64.tif rename to autotest/gdrivers/data/cfloat64.tif diff --git a/autotest/gcore/data/cint_sar.tif b/autotest/gdrivers/data/cint_sar.tif similarity index 100% rename from autotest/gcore/data/cint_sar.tif rename to autotest/gdrivers/data/cint_sar.tif diff --git a/autotest/gcore/data/float32.tif b/autotest/gdrivers/data/float32.tif similarity index 100% rename from autotest/gcore/data/float32.tif rename to autotest/gdrivers/data/float32.tif diff --git a/autotest/gcore/data/int32.tif b/autotest/gdrivers/data/int32.tif similarity index 100% rename from autotest/gcore/data/int32.tif rename to autotest/gdrivers/data/int32.tif diff --git a/autotest/gcore/data/pixfun_cmul_c.vrt b/autotest/gdrivers/data/pixfun_cmul_c.vrt similarity index 100% rename from autotest/gcore/data/pixfun_cmul_c.vrt rename to autotest/gdrivers/data/pixfun_cmul_c.vrt diff --git a/autotest/gcore/data/pixfun_cmul_r.vrt b/autotest/gdrivers/data/pixfun_cmul_r.vrt similarity index 100% rename from autotest/gcore/data/pixfun_cmul_r.vrt rename to autotest/gdrivers/data/pixfun_cmul_r.vrt diff --git a/autotest/gcore/data/pixfun_complex.vrt b/autotest/gdrivers/data/pixfun_complex.vrt similarity index 100% rename from autotest/gcore/data/pixfun_complex.vrt rename to autotest/gdrivers/data/pixfun_complex.vrt diff --git a/autotest/gcore/data/pixfun_conj_c.vrt b/autotest/gdrivers/data/pixfun_conj_c.vrt similarity index 100% rename from autotest/gcore/data/pixfun_conj_c.vrt rename to autotest/gdrivers/data/pixfun_conj_c.vrt diff --git a/autotest/gcore/data/pixfun_conj_r.vrt b/autotest/gdrivers/data/pixfun_conj_r.vrt similarity index 100% rename from autotest/gcore/data/pixfun_conj_r.vrt rename to autotest/gdrivers/data/pixfun_conj_r.vrt diff --git a/autotest/gcore/data/pixfun_dB2amp.vrt b/autotest/gdrivers/data/pixfun_dB2amp.vrt similarity index 100% rename from autotest/gcore/data/pixfun_dB2amp.vrt rename to autotest/gdrivers/data/pixfun_dB2amp.vrt diff --git a/autotest/gcore/data/pixfun_dB2pow.vrt b/autotest/gdrivers/data/pixfun_dB2pow.vrt similarity index 100% rename from autotest/gcore/data/pixfun_dB2pow.vrt rename to autotest/gdrivers/data/pixfun_dB2pow.vrt diff --git a/autotest/gcore/data/pixfun_diff_c.vrt b/autotest/gdrivers/data/pixfun_diff_c.vrt similarity index 100% rename from autotest/gcore/data/pixfun_diff_c.vrt rename to autotest/gdrivers/data/pixfun_diff_c.vrt diff --git a/autotest/gcore/data/pixfun_diff_r.vrt b/autotest/gdrivers/data/pixfun_diff_r.vrt similarity index 100% rename from autotest/gcore/data/pixfun_diff_r.vrt rename to autotest/gdrivers/data/pixfun_diff_r.vrt diff --git a/autotest/gcore/data/pixfun_imag_c.vrt b/autotest/gdrivers/data/pixfun_imag_c.vrt similarity index 100% rename from autotest/gcore/data/pixfun_imag_c.vrt rename to autotest/gdrivers/data/pixfun_imag_c.vrt diff --git a/autotest/gcore/data/pixfun_imag_r.vrt b/autotest/gdrivers/data/pixfun_imag_r.vrt similarity index 100% rename from autotest/gcore/data/pixfun_imag_r.vrt rename to autotest/gdrivers/data/pixfun_imag_r.vrt diff --git a/autotest/gcore/data/pixfun_intensity_c.vrt b/autotest/gdrivers/data/pixfun_intensity_c.vrt similarity index 100% rename from autotest/gcore/data/pixfun_intensity_c.vrt rename to autotest/gdrivers/data/pixfun_intensity_c.vrt diff --git a/autotest/gcore/data/pixfun_intensity_r.vrt b/autotest/gdrivers/data/pixfun_intensity_r.vrt similarity index 100% rename from autotest/gcore/data/pixfun_intensity_r.vrt rename to autotest/gdrivers/data/pixfun_intensity_r.vrt diff --git a/autotest/gcore/data/pixfun_inv_c.vrt b/autotest/gdrivers/data/pixfun_inv_c.vrt similarity index 100% rename from autotest/gcore/data/pixfun_inv_c.vrt rename to autotest/gdrivers/data/pixfun_inv_c.vrt diff --git a/autotest/gcore/data/pixfun_inv_r.vrt b/autotest/gdrivers/data/pixfun_inv_r.vrt similarity index 100% rename from autotest/gcore/data/pixfun_inv_r.vrt rename to autotest/gdrivers/data/pixfun_inv_r.vrt diff --git a/autotest/gcore/data/pixfun_log10.vrt b/autotest/gdrivers/data/pixfun_log10.vrt similarity index 100% rename from autotest/gcore/data/pixfun_log10.vrt rename to autotest/gdrivers/data/pixfun_log10.vrt diff --git a/autotest/gcore/data/pixfun_mod_c.vrt b/autotest/gdrivers/data/pixfun_mod_c.vrt similarity index 100% rename from autotest/gcore/data/pixfun_mod_c.vrt rename to autotest/gdrivers/data/pixfun_mod_c.vrt diff --git a/autotest/gcore/data/pixfun_mod_r.vrt b/autotest/gdrivers/data/pixfun_mod_r.vrt similarity index 100% rename from autotest/gcore/data/pixfun_mod_r.vrt rename to autotest/gdrivers/data/pixfun_mod_r.vrt diff --git a/autotest/gcore/data/pixfun_mul_c.vrt b/autotest/gdrivers/data/pixfun_mul_c.vrt similarity index 100% rename from autotest/gcore/data/pixfun_mul_c.vrt rename to autotest/gdrivers/data/pixfun_mul_c.vrt diff --git a/autotest/gcore/data/pixfun_mul_r.vrt b/autotest/gdrivers/data/pixfun_mul_r.vrt similarity index 100% rename from autotest/gcore/data/pixfun_mul_r.vrt rename to autotest/gdrivers/data/pixfun_mul_r.vrt diff --git a/autotest/gcore/data/pixfun_phase_c.vrt b/autotest/gdrivers/data/pixfun_phase_c.vrt similarity index 100% rename from autotest/gcore/data/pixfun_phase_c.vrt rename to autotest/gdrivers/data/pixfun_phase_c.vrt diff --git a/autotest/gcore/data/pixfun_phase_r.vrt b/autotest/gdrivers/data/pixfun_phase_r.vrt similarity index 100% rename from autotest/gcore/data/pixfun_phase_r.vrt rename to autotest/gdrivers/data/pixfun_phase_r.vrt diff --git a/autotest/gcore/data/pixfun_real_c.vrt b/autotest/gdrivers/data/pixfun_real_c.vrt similarity index 100% rename from autotest/gcore/data/pixfun_real_c.vrt rename to autotest/gdrivers/data/pixfun_real_c.vrt diff --git a/autotest/gcore/data/pixfun_real_r.vrt b/autotest/gdrivers/data/pixfun_real_r.vrt similarity index 100% rename from autotest/gcore/data/pixfun_real_r.vrt rename to autotest/gdrivers/data/pixfun_real_r.vrt diff --git a/autotest/gcore/data/pixfun_sqrt.vrt b/autotest/gdrivers/data/pixfun_sqrt.vrt similarity index 100% rename from autotest/gcore/data/pixfun_sqrt.vrt rename to autotest/gdrivers/data/pixfun_sqrt.vrt diff --git a/autotest/gcore/data/pixfun_sum_c.vrt b/autotest/gdrivers/data/pixfun_sum_c.vrt similarity index 100% rename from autotest/gcore/data/pixfun_sum_c.vrt rename to autotest/gdrivers/data/pixfun_sum_c.vrt diff --git a/autotest/gcore/data/pixfun_sum_r.vrt b/autotest/gdrivers/data/pixfun_sum_r.vrt similarity index 100% rename from autotest/gcore/data/pixfun_sum_r.vrt rename to autotest/gdrivers/data/pixfun_sum_r.vrt diff --git a/autotest/gcore/data/uint16.tif b/autotest/gdrivers/data/uint16.tif similarity index 100% rename from autotest/gcore/data/uint16.tif rename to autotest/gdrivers/data/uint16.tif diff --git a/autotest/gcore/pixfun.py b/autotest/gdrivers/pixfun.py similarity index 100% rename from autotest/gcore/pixfun.py rename to autotest/gdrivers/pixfun.py diff --git a/autotest/gcore/testnonboundtoswig.py b/autotest/gdrivers/testnonboundtoswig.py similarity index 100% rename from autotest/gcore/testnonboundtoswig.py rename to autotest/gdrivers/testnonboundtoswig.py diff --git a/pixelfunctions.c b/gdal/frmts/vrt/pixelfunctions.c similarity index 100% rename from pixelfunctions.c rename to gdal/frmts/vrt/pixelfunctions.c diff --git a/LICENSE.txt b/gdal/frmts/vrt/pixelfunctions.licence similarity index 100% rename from LICENSE.txt rename to gdal/frmts/vrt/pixelfunctions.licence diff --git a/pixfunplugin.c b/pixfunplugin.c deleted file mode 100644 index 15c113d10f1a..000000000000 --- a/pixfunplugin.c +++ /dev/null @@ -1,55 +0,0 @@ -/****************************************************************************** - * - * Project: GDAL - * Purpose: Provide a fake GDAL driver to register a small set of pixel - * functions to be used with the virtual driver. - * It is a hack aimed to enable python users to use a small set - * of custom pixel functions without C++ coding. - * Author: Antonio Valentino - * - ****************************************************************************** - * Copyright (c) 2008-2014 Antonio Valentino - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - *****************************************************************************/ - -#include - -CPL_CVSID("$Id: $"); - -CPL_C_START -CPL_DLL void GDALRegister_PIXFUN(void); -CPL_C_END - -extern CPLErr GDALRegisterDefaultPixelFunc(); - -/************************************************************************/ -/* GDALRegister_PIXFUN() */ -/************************************************************************/ -void GDALRegister_PIXFUN(void) -{ - const char PLUGINNAME[]="Pixfun"; - - if (! GDAL_CHECK_VERSION(PLUGINNAME)) - return; - - GDALRegisterDefaultPixelFunc(); - CPLDebug("PIXFUN", "Plugin %s %s", PLUGINNAME, "$Revision: $"); - -} /* GDALRegister_PIXFUN */ From 94d8ecc867443e83c3ea0ce1c48f499eb54af45c Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Wed, 6 Jul 2016 15:10:55 +0200 Subject: [PATCH 14/69] ENH: Minor fixes in derived dataset --- gdal/frmts/cderived/cderiveddataset.cpp | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/gdal/frmts/cderived/cderiveddataset.cpp b/gdal/frmts/cderived/cderiveddataset.cpp index 2f56f0ad265b..f766c043a6f1 100644 --- a/gdal/frmts/cderived/cderiveddataset.cpp +++ b/gdal/frmts/cderived/cderiveddataset.cpp @@ -94,7 +94,7 @@ int ComplexDerivedDataset::Identify(GDALOpenInfo * poOpenInfo) } GDALDataset * ComplexDerivedDataset::Open(GDALOpenInfo * poOpenInfo) -{ +{ if( !Identify(poOpenInfo) ) { return NULL; @@ -132,6 +132,12 @@ GDALDataset * ComplexDerivedDataset::Open(GDALOpenInfo * poOpenInfo) return NULL; int nbBands = poTmpDS->GetRasterCount(); + + if(nbBands == 0) + { + GDALClose(poTmpDS); + return NULL; + } int nRows = poTmpDS->GetRasterYSize(); int nCols = poTmpDS->GetRasterXSize(); @@ -165,20 +171,23 @@ GDALDataset * ComplexDerivedDataset::Open(GDALOpenInfo * poOpenInfo) GDALDataType type = GDT_Float64; poBand = new VRTDerivedRasterBand(poDS,nBand,type,nCols,nRows); + poDS->SetBand(nBand,poBand); + poBand->SetPixelFunctionName("mod"); poBand->SetSourceTransferType(poTmpDS->GetRasterBand(nBand)->GetRasterDataType()); - poDS->SetBand(nBand,poBand); - + GDALProxyPoolDataset* proxyDS; proxyDS = new GDALProxyPoolDataset( odFilename, poDS->nRasterXSize, poDS->nRasterYSize, GA_ReadOnly, TRUE); - proxyDS->AddSrcBandDescription(poTmpDS->GetRasterBand(nBand)->GetRasterDataType(), 128, 128); - + for(int j=0;jAddSrcBandDescription(poTmpDS->GetRasterBand(nBand)->GetRasterDataType(), 128, 128); + poBand->AddComplexSource(proxyDS->GetRasterBand(nBand),0,0,nCols,nRows,0,0,nCols,nRows); - proxyDS->Dereference(); + + proxyDS->Dereference(); } GDALClose(poTmpDS); From a7b6a8973b220895835deb018f60be76bb1783e0 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Wed, 6 Jul 2016 15:11:30 +0200 Subject: [PATCH 15/69] ENH: Implement virtual GetMetadata() and GetMetadataDomainList() in GDALDataset --- gdal/gcore/gdal_priv.h | 6 ++++- gdal/gcore/gdaldataset.cpp | 54 +++++++++++++++++++++++++++++++++----- 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/gdal/gcore/gdal_priv.h b/gdal/gcore/gdal_priv.h index 1d07734e6eaa..7bed9eb29a5f 100644 --- a/gdal/gcore/gdal_priv.h +++ b/gdal/gcore/gdal_priv.h @@ -488,6 +488,10 @@ class CPL_DLL GDALDataset : public GDALMajorObject void ReportError(CPLErr eErrClass, CPLErrorNum err_no, const char *fmt, ...) CPL_PRINT_FUNC_FORMAT (4, 5); + virtual char ** GetMetadata(const char * pszDomain = ""); + + virtual char ** GetMetadataDomainList(); + private: void *m_hPrivateData; @@ -495,7 +499,7 @@ class CPL_DLL GDALDataset : public GDALMajorObject OGRGeometry *poSpatialFilter, const char *pszDialect, swq_select_parse_options* poSelectParseOptions); - + CPLStringList * papoDerivedMetadataList; public: virtual int GetLayerCount(); diff --git a/gdal/gcore/gdaldataset.cpp b/gdal/gcore/gdaldataset.cpp index f1eb1721ce69..9c9f9aa9e3f0 100644 --- a/gdal/gcore/gdaldataset.cpp +++ b/gdal/gcore/gdaldataset.cpp @@ -208,6 +208,7 @@ void GDALDataset::Init(int bForceCachedIOIn) bIsInternal = TRUE; bSuppressOnClose = FALSE; papszOpenOptions = NULL; + papoDerivedMetadataList = new CPLStringList(); /* -------------------------------------------------------------------- */ /* Set forced caching flag. */ @@ -328,6 +329,9 @@ GDALDataset::~GDALDataset() CPLFree(psPrivate); CSLDestroy( papszOpenOptions ); + + CPLFree(papoDerivedMetadataList); + papoDerivedMetadataList = NULL; } /************************************************************************/ @@ -584,12 +588,6 @@ void GDALDataset::SetBand( int nNewBand, GDALRasterBand * poBand ) papoBands = papoNewBands; - // TODO: Filter on complex bands only - SetMetadataItem("DERIVED_SUBDATASET_1_NAME",CPLSPrintf("DERIVED_SUBDATASET:COMPLEX_AMPLITUDE:%s",GetDescription()),"DERIVED_SUBDATASETS"); - - CPLString osDesc(CPLSPrintf("Complex amplitude of bands from %s",GetDescription())); - SetMetadataItem("DERIVED_SUBDATASET_1_DESC",osDesc.c_str(),"DERIVED_SUBDATASETS"); - for( int i = nBands; i < nNewBand; ++i ) papoBands[i] = NULL; @@ -3298,6 +3296,50 @@ void GDALDataset::ReportError(CPLErr eErrClass, CPLErrorNum err_no, const char * va_end(args); } +/************************************************************************/ +/* GetMetadata() */ +/************************************************************************/ +char ** GDALDataset::GetMetadata(const char * pszDomain) +{ + if( pszDomain != NULL && EQUAL(pszDomain, "DERIVED_SUBDATASETS") ) + { + papoDerivedMetadataList->Clear(); + + // First condition: at least one raster band + if(GetRasterCount()>0) + { + papoDerivedMetadataList->SetNameValue("DERIVED_SUBDATASET_1_NAME",CPLSPrintf("DERIVED_SUBDATASET:COMPLEX_AMPLITUDE:%s",GetDescription())); + + CPLString osDesc(CPLSPrintf("Complex amplitude of bands from %s",GetDescription())); + papoDerivedMetadataList->SetNameValue("DERIVED_SUBDATASET_1_DESC",osDesc.c_str()); + } + + return papoDerivedMetadataList->List(); + } + else + return GDALMajorObject::GetMetadata(pszDomain); +} + + +/************************************************************************/ +/* GetMetadataDomainList() */ +/************************************************************************/ +char ** GDALDataset::GetMetadataDomainList() +{ + CPLDebug("GDALDataset","GetMetadataDomainList"); + char ** currentDomainList = CSLDuplicate(oMDMD.GetDomainList()); + currentDomainList = CSLAddString(currentDomainList,"DERIVED_SUBDATASETS"); + + int nb_domains = CSLCount(currentDomainList); + + CPLDebug("GDALDataset","Found %i domains",nb_domains); + + for(int i = 0; i< nb_domains;++i) + CPLDebug("GDALDataset","Domain %i: %s",i,currentDomainList[i]); + + return currentDomainList; +} + /************************************************************************/ /* GetDriverName() */ /************************************************************************/ From a5a8adbaa836b0e9af96e814f35b318a83bf933c Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Wed, 6 Jul 2016 15:12:09 +0200 Subject: [PATCH 16/69] ENH: Ensure that geotiff dataset calls base GetMetadata() and GetMetadataDomainList() --- gdal/frmts/gtiff/geotiff.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/gdal/frmts/gtiff/geotiff.cpp b/gdal/frmts/gtiff/geotiff.cpp index d5480a75c065..fc00296bceaf 100644 --- a/gdal/frmts/gtiff/geotiff.cpp +++ b/gdal/frmts/gtiff/geotiff.cpp @@ -15817,10 +15817,22 @@ CPLErr GTiffDataset::SetGCPs( int nGCPCountIn, const GDAL_GCP *pasGCPListIn, char **GTiffDataset::GetMetadataDomainList() { + CPLDebug("GTiffDataset", "GetMetadataDomainList"); + LoadGeoreferencingAndPamIfNeeded(); + char ** domainlist = CSLDuplicate(oGTiffMDMD.GetDomainList()); + char ** baselist = GDALDataset::GetMetadataDomainList(); + + int nbBaseDomains = CSLCount(baselist); + + for(int domainId = 0; domainId Date: Wed, 6 Jul 2016 15:18:44 +0200 Subject: [PATCH 17/69] ENH: Remove useless trace --- gdal/frmts/gtiff/geotiff.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/gdal/frmts/gtiff/geotiff.cpp b/gdal/frmts/gtiff/geotiff.cpp index fc00296bceaf..06d00a073e3b 100644 --- a/gdal/frmts/gtiff/geotiff.cpp +++ b/gdal/frmts/gtiff/geotiff.cpp @@ -15816,9 +15816,7 @@ CPLErr GTiffDataset::SetGCPs( int nGCPCountIn, const GDAL_GCP *pasGCPListIn, /************************************************************************/ char **GTiffDataset::GetMetadataDomainList() -{ - CPLDebug("GTiffDataset", "GetMetadataDomainList"); - +{ LoadGeoreferencingAndPamIfNeeded(); char ** domainlist = CSLDuplicate(oGTiffMDMD.GetDomainList()); From 67373d6992ab6f95c304271d1dc37257e7bcee15 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Wed, 6 Jul 2016 15:42:54 +0200 Subject: [PATCH 18/69] TEST: Removing those tests since they seem unrelated to the pixel functions --- autotest/pymod/gdaltest.py | 1578 -------------------------- autotest/pymod/gdaltest_python2.py | 189 --- autotest/pymod/gdaltest_python3.py | 177 --- autotest/pymod/ogrtest.py | 194 ---- autotest/pymod/test_cli_utilities.py | 189 --- 5 files changed, 2327 deletions(-) delete mode 100644 autotest/pymod/gdaltest.py delete mode 100644 autotest/pymod/gdaltest_python2.py delete mode 100644 autotest/pymod/gdaltest_python3.py delete mode 100644 autotest/pymod/ogrtest.py delete mode 100644 autotest/pymod/test_cli_utilities.py diff --git a/autotest/pymod/gdaltest.py b/autotest/pymod/gdaltest.py deleted file mode 100644 index 4802818fc606..000000000000 --- a/autotest/pymod/gdaltest.py +++ /dev/null @@ -1,1578 +0,0 @@ -# -*- coding: utf-8 -*- -############################################################################### -# $Id$ -# -# Project: GDAL/OGR Test Suite -# Purpose: Python Library supporting GDAL/OGR Test Suite -# Author: Frank Warmerdam -# -############################################################################### -# Copyright (c) 2003, Frank Warmerdam -# Copyright (c) 2008-2013, Even Rouault -# -# Permission is hereby granted, free of charge, to any person obtaining a -# copy of this software and associated documentation files (the "Software"), -# to deal in the Software without restriction, including without limitation -# the rights to use, copy, modify, merge, publish, distribute, sublicense, -# and/or sell copies of the Software, and to permit persons to whom the -# Software is furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included -# in all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -# DEALINGS IN THE SOFTWARE. -############################################################################### - -import sys -import string -import os -import time - -from osgeo import gdal -from osgeo import osr - -cur_name = 'default' - -success_counter = 0 -failure_counter = 0 -expected_failure_counter = 0 -blow_counter = 0 -skip_counter = 0 -failure_summary = [] - -reason = None -count_skipped_tests_download = 0 -count_skipped_tests_slow = 0 -start_time = None -end_time = None - -jp2kak_drv = None -jpeg2000_drv = None -jp2ecw_drv = None -jp2mrsid_drv = None -jp2kak_drv_unregistered = False -jpeg2000_drv_unregistered = False -jp2ecw_drv_unregistered = False -jp2mrsid_drv_unregistered = False - -from sys import version_info -if version_info >= (3,0,0): - from gdaltest_python3 import * -else: - from gdaltest_python2 import * - -# Process commandline arguments for stuff like --debug, --locale, --config - -argv = gdal.GeneralCmdLineProcessor( sys.argv ) - -############################################################################### - -def setup_run( name ): - - if 'APPLY_LOCALE' in os.environ: - import locale - locale.setlocale(locale.LC_ALL, '') - - global cur_name - cur_name = name - -############################################################################### - -def run_tests( test_list ): - global success_counter, failure_counter, expected_failure_counter, blow_counter, skip_counter - global reason, failure_summary, cur_name - global start_time, end_time - - set_time = start_time is None - if set_time: - start_time = time.time() - had_errors_this_script = 0 - - for test_item in test_list: - if test_item is None: - continue - - try: - (func, name) = test_item - if func.__name__[:4] == 'test': - outline = ' TEST: ' + func.__name__[4:] + ': ' + name + ' ... ' - else: - outline = ' TEST: ' + func.__name__ + ': ' + name + ' ... ' - except: - func = test_item - name = func.__name__ - outline = ' TEST: ' + name + ' ... ' - - sys.stdout.write( outline ) - sys.stdout.flush() - - reason = None - result = run_func(func) - - if result[:4] == 'fail': - if had_errors_this_script == 0: - failure_summary.append( 'Script: ' + cur_name ) - had_errors_this_script = 1 - failure_summary.append( outline + result ) - if reason is not None: - failure_summary.append( ' ' + reason ) - - if reason is not None: - print((' ' + reason)) - - if result == 'success': - success_counter = success_counter + 1 - elif result == 'expected_fail': - expected_failure_counter = expected_failure_counter + 1 - elif result == 'fail': - failure_counter = failure_counter + 1 - elif result == 'skip': - skip_counter = skip_counter + 1 - else: - blow_counter = blow_counter + 1 - - if set_time: - end_time = time.time() - -############################################################################### - -def get_lineno_2framesback( frames ): - try: - import inspect - frame = inspect.currentframe() - while frames > 0: - frame = frame.f_back - frames = frames-1 - - return frame.f_lineno - except: - return -1 - -############################################################################### - -def post_reason( msg, frames=2 ): - lineno = get_lineno_2framesback( frames ) - global reason - - if lineno >= 0: - reason = 'line %d: %s' % (lineno, msg) - else: - reason = msg - -############################################################################### - -def summarize(): - global count_skipped_tests_download, count_skipped_tests_slow - global success_counter, failure_counter, blow_counter, skip_counter - global cur_name - global start_time, end_time - - print('') - if cur_name is not None: - print('Test Script: %s' % cur_name) - print('Succeeded: %d' % success_counter) - print('Failed: %d (%d blew exceptions)' \ - % (failure_counter+blow_counter, blow_counter)) - print('Skipped: %d' % skip_counter) - print('Expected fail:%d' % expected_failure_counter) - if start_time is not None: - duration = end_time - start_time - if duration >= 60: - print('Duration: %02dm%02.1fs' % (duration / 60., duration % 60.)) - else: - print('Duration: %02.2fs' % duration) - if count_skipped_tests_download != 0: - print('As GDAL_DOWNLOAD_TEST_DATA environment variable is not defined, %d tests relying on data to downloaded from the Web have been skipped' % count_skipped_tests_download) - if count_skipped_tests_slow != 0: - print('As GDAL_RUN_SLOW_TESTS environment variable is not defined, %d "slow" tests have been skipped' % count_skipped_tests_slow) - print('') - - sys.path.append( 'gcore' ) - sys.path.append( '../gcore' ) - import testnonboundtoswig - # Do it twice to ensure that cleanup routines properly do their jobs - for i in range(2): - testnonboundtoswig.OSRCleanup() - testnonboundtoswig.GDALDestroyDriverManager() - testnonboundtoswig.OGRCleanupAll() - - return failure_counter + blow_counter - -############################################################################### - -def run_all( dirlist, option_list ): - - global start_time, end_time - global cur_name - - start_time = time.time() - - for dir_name in dirlist: - files = os.listdir(dir_name) - - old_path = sys.path - sys.path.append('.') - - for file in files: - if not file[-3:] == '.py': - continue - - module = file[:-3] - try: - wd = os.getcwd() - os.chdir( dir_name ) - - exec("import " + module) - try: - print('Running tests from %s/%s' % (dir_name,file)) - setup_run( '%s/%s' % (dir_name,file) ) - exec("run_tests( " + module + ".gdaltest_list)") - except: - pass - - os.chdir( wd ) - - except: - os.chdir( wd ) - print('... failed to load %s ... skipping.' % file) - - import traceback - traceback.print_exc() - - # We only add the tool directory to the python path long enough - # to load the tool files. - sys.path = old_path - - end_time = time.time() - cur_name = None - - if len(failure_summary) > 0: - print('') - print(' ------------ Failures ------------') - for item in failure_summary: - print(item) - print(' ----------------------------------') - -############################################################################### - -def clean_tmp(): - all_files = os.listdir('tmp') - for file in all_files: - if file == 'CVS' or file == 'do-not-remove': - continue - - try: - os.remove( 'tmp/' + file ) - except: - pass - return 'success' - -############################################################################### -def testCreateCopyInterruptCallback(pct, message, user_data): - if pct > 0.5: - return 0 # to stop - else: - return 1 # to continue - -############################################################################### - -class GDALTest: - def __init__(self, drivername, filename, band, chksum, - xoff = 0, yoff = 0, xsize = 0, ysize = 0, options = [], - filename_absolute = 0 ): - self.driver = None - self.drivername = drivername - self.filename = filename - self.filename_absolute = filename_absolute - self.band = band - self.chksum = chksum - self.xoff = xoff - self.yoff = yoff - self.xsize = xsize - self.ysize = ysize - self.options = options - - def testDriver(self): - if self.driver is None: - self.driver = gdal.GetDriverByName( self.drivername ) - if self.driver is None: - post_reason( self.drivername + ' driver not found!' ) - return 'fail' - - return 'success' - - def testOpen(self, check_prj = None, check_gt = None, gt_epsilon = None, \ - check_stat = None, check_approx_stat = None, \ - stat_epsilon = None, skip_checksum = None): - """check_prj - projection reference, check_gt - geotransformation - matrix (tuple), gt_epsilon - geotransformation tolerance, - check_stat - band statistics (tuple), stat_epsilon - statistics - tolerance.""" - if self.testDriver() == 'fail': - return 'skip' - - if self.filename_absolute: - wrk_filename = self.filename - else: - wrk_filename = 'data/' + self.filename - - ds = gdal.Open( wrk_filename, gdal.GA_ReadOnly ) - - if ds is None: - post_reason( 'Failed to open dataset: ' + wrk_filename ) - return 'fail' - - if ds.GetDriver().ShortName != gdal.GetDriverByName( self.drivername ).ShortName: - post_reason( 'The driver of the returned dataset is %s instead of %s.' % ( ds.GetDriver().ShortName, self.drivername ) ) - return 'fail' - - if self.xsize == 0 and self.ysize == 0: - self.xsize = ds.RasterXSize - self.ysize = ds.RasterYSize - - # Do we need to check projection? - if check_prj is not None: - new_prj = ds.GetProjection() - - src_osr = osr.SpatialReference() - src_osr.SetFromUserInput( check_prj ) - - new_osr = osr.SpatialReference( wkt=new_prj ) - - if not src_osr.IsSame(new_osr): - print('') - print('old = %s' % src_osr.ExportToPrettyWkt()) - print('new = %s' % new_osr.ExportToPrettyWkt()) - post_reason( 'Projections differ' ) - return 'fail' - - # Do we need to check geotransform? - if check_gt: - # Default to 100th of pixel as our test value. - if gt_epsilon is None: - gt_epsilon = (abs(check_gt[1])+abs(check_gt[2])) / 100.0 - - new_gt = ds.GetGeoTransform() - for i in range(6): - if abs(new_gt[i]-check_gt[i]) > gt_epsilon: - print('') - print('old = ', check_gt) - print('new = ', new_gt) - post_reason( 'Geotransform differs.' ) - return 'fail' - - oBand = ds.GetRasterBand(self.band) - if skip_checksum is None: - chksum = oBand.Checksum(self.xoff, self.yoff, self.xsize, self.ysize) - - # Do we need to check approximate statistics? - if check_approx_stat: - # Default to 1000th of pixel value range as our test value. - if stat_epsilon is None: - stat_epsilon = \ - abs(check_approx_stat[1] - check_approx_stat[0]) / 1000.0 - - new_stat = oBand.GetStatistics(1, 1) - for i in range(4): - - # NOTE - mloskot: Poor man Nan/Inf value check. It's poor - # because we need to support old and buggy Python 2.3. - # Tested on Linux, Mac OS X and Windows, with Python 2.3/2.4/2.5. - sv = str(new_stat[i]).lower() - if sv.find('n') >= 0 or sv.find('i') >= 0 or sv.find('#') >= 0: - post_reason( 'NaN or Invinite value encountered '%'.' % sv ) - return 'fail' - - if abs(new_stat[i]-check_approx_stat[i]) > stat_epsilon: - print('') - print('old = ', check_approx_stat) - print('new = ', new_stat) - post_reason( 'Approximate statistics differs.' ) - return 'fail' - - # Do we need to check statistics? - if check_stat: - # Default to 1000th of pixel value range as our test value. - if stat_epsilon is None: - stat_epsilon = abs(check_stat[1] - check_stat[0]) / 1000.0 - - # FIXME: how to test approximate statistic results? - new_stat = oBand.GetStatistics(1, 1) - - new_stat = oBand.GetStatistics(0, 1) - for i in range(4): - - sv = str(new_stat[i]).lower() - if sv.find('n') >= 0 or sv.find('i') >= 0 or sv.find('#') >= 0: - post_reason( 'NaN or Invinite value encountered '%'.' % sv ) - return 'fail' - - if abs(new_stat[i]-check_stat[i]) > stat_epsilon: - print('') - print('old = ', check_stat) - print('new = ', new_stat) - post_reason( 'Statistics differs.' ) - return 'fail' - - ds = None - - if is_file_open(wrk_filename): - post_reason('file still open after dataset closing') - return 'fail' - - if skip_checksum is not None: - return 'success' - elif self.chksum is None or chksum == self.chksum: - return 'success' - else: - post_reason('Checksum for band %d in "%s" is %d, but expected %d.' \ - % (self.band, self.filename, chksum, self.chksum) ) - return 'fail' - - def testCreateCopy(self, check_minmax = 1, check_gt = 0, check_srs = None, - vsimem = 0, new_filename = None, strict_in = 0, - skip_preclose_test = 0, delete_copy = 1, gt_epsilon = None, - check_checksum_not_null = None, interrupt_during_copy = False): - - if self.testDriver() == 'fail': - return 'skip' - - if self.filename_absolute: - wrk_filename = self.filename - else: - wrk_filename = 'data/' + self.filename - - src_ds = gdal.Open( wrk_filename ) - if self.band > 0: - minmax = src_ds.GetRasterBand(self.band).ComputeRasterMinMax() - - src_prj = src_ds.GetProjection() - src_gt = src_ds.GetGeoTransform() - - if new_filename is None: - if vsimem: - new_filename = '/vsimem/' + self.filename + '.tst' - else: - new_filename = 'tmp/' + self.filename + '.tst' - - gdal.PushErrorHandler( 'CPLQuietErrorHandler' ) - if interrupt_during_copy: - new_ds = self.driver.CreateCopy( new_filename, src_ds, - strict = strict_in, - options = self.options, - callback = testCreateCopyInterruptCallback) - else: - new_ds = self.driver.CreateCopy( new_filename, src_ds, - strict = strict_in, - options = self.options ) - gdal.PopErrorHandler() - - if interrupt_during_copy: - if new_ds is None: - gdal.PushErrorHandler( 'CPLQuietErrorHandler' ) - self.driver.Delete( new_filename ) - gdal.PopErrorHandler() - return 'success' - else: - post_reason( 'CreateCopy() should have failed due to interruption') - new_ds = None - self.driver.Delete( new_filename ) - return 'fail' - - if new_ds is None: - post_reason( 'Failed to create test file using CreateCopy method.'\ - + '\n' + gdal.GetLastErrorMsg() ) - return 'fail' - - if new_ds.GetDriver().ShortName != gdal.GetDriverByName( self.drivername ).ShortName: - post_reason( 'The driver of the returned dataset is %s instead of %s.' % ( new_ds.GetDriver().ShortName, self.drivername ) ) - return 'fail' - - if self.band > 0 and skip_preclose_test == 0: - bnd = new_ds.GetRasterBand(self.band) - if check_checksum_not_null is True: - if bnd.Checksum() == 0: - post_reason('Got null checksum on still-open file.') - return 'fail' - elif self.chksum is not None and bnd.Checksum() != self.chksum: - post_reason( - 'Did not get expected checksum on still-open file.\n' \ - ' Got %d instead of %d.' % (bnd.Checksum(),self.chksum)) - return 'fail' - if check_minmax: - got_minmax = bnd.ComputeRasterMinMax() - if got_minmax != minmax: - post_reason( \ - 'Did not get expected min/max values on still-open file.\n' \ - ' Got %g,%g instead of %g,%g.' \ - % ( got_minmax[0], got_minmax[1], minmax[0], minmax[1] ) ) - return 'fail' - - bnd = None - new_ds = None - - # hopefully it's closed now! - - new_ds = gdal.Open( new_filename ) - if new_ds is None: - post_reason( 'Failed to open dataset: ' + new_filename ) - return 'fail' - - if self.band > 0: - bnd = new_ds.GetRasterBand(self.band) - if check_checksum_not_null is True: - if bnd.Checksum() == 0: - post_reason('Got null checksum on reopened file.') - return 'fail' - elif self.chksum is not None and bnd.Checksum() != self.chksum: - post_reason( 'Did not get expected checksum on reopened file.\n' - ' Got %d instead of %d.' \ - % (bnd.Checksum(), self.chksum) ) - return 'fail' - - if check_minmax: - got_minmax = bnd.ComputeRasterMinMax() - if got_minmax != minmax: - post_reason( \ - 'Did not get expected min/max values on reopened file.\n' \ - ' Got %g,%g instead of %g,%g.' \ - % ( got_minmax[0], got_minmax[1], minmax[0], minmax[1] ) ) - return 'fail' - - # Do we need to check the geotransform? - if check_gt: - if gt_epsilon is None: - eps = 0.00000001 - else: - eps = gt_epsilon - new_gt = new_ds.GetGeoTransform() - if abs(new_gt[0] - src_gt[0]) > eps \ - or abs(new_gt[1] - src_gt[1]) > eps \ - or abs(new_gt[2] - src_gt[2]) > eps \ - or abs(new_gt[3] - src_gt[3]) > eps \ - or abs(new_gt[4] - src_gt[4]) > eps \ - or abs(new_gt[5] - src_gt[5]) > eps: - print('') - print('old = ', src_gt) - print('new = ', new_gt) - post_reason( 'Geotransform differs.' ) - return 'fail' - - # Do we need to check the geotransform? - if check_srs is not None: - new_prj = new_ds.GetProjection() - - src_osr = osr.SpatialReference( wkt=src_prj ) - new_osr = osr.SpatialReference( wkt=new_prj ) - - if not src_osr.IsSame(new_osr): - print('') - print('old = %s' % src_osr.ExportToPrettyWkt()) - print('new = %s' % new_osr.ExportToPrettyWkt()) - post_reason( 'Projections differ' ) - return 'fail' - - bnd = None - new_ds = None - src_ds = None - - if gdal.GetConfigOption( 'CPL_DEBUG', 'OFF' ) != 'ON' and delete_copy == 1 : - self.driver.Delete( new_filename ) - - return 'success' - - def testCreate(self, vsimem = 0, new_filename = None, out_bands = 1, - check_minmax = 1 ): - if self.testDriver() == 'fail': - return 'skip' - - if self.filename_absolute: - wrk_filename = self.filename - else: - wrk_filename = 'data/' + self.filename - - src_ds = gdal.Open( wrk_filename ) - xsize = src_ds.RasterXSize - ysize = src_ds.RasterYSize - src_img = src_ds.GetRasterBand(self.band).ReadRaster(0,0,xsize,ysize) - minmax = src_ds.GetRasterBand(self.band).ComputeRasterMinMax() - - if new_filename is None: - if vsimem: - new_filename = '/vsimem/' + self.filename + '.tst' - else: - new_filename = 'tmp/' + self.filename + '.tst' - - new_ds = self.driver.Create( new_filename, xsize, ysize, out_bands, - src_ds.GetRasterBand(self.band).DataType, - options = self.options ) - if new_ds is None: - post_reason( 'Failed to create test file using Create method.' ) - return 'fail' - - src_ds = None - - try: - for band in range(1,out_bands+1): - new_ds.GetRasterBand(band).WriteRaster( 0, 0, xsize, ysize, src_img ) - except: - post_reason( 'Failed to write raster bands to test file.' ) - return 'fail' - - for band in range(1,out_bands+1): - if self.chksum is not None \ - and new_ds.GetRasterBand(band).Checksum() != self.chksum: - post_reason( - 'Did not get expected checksum on still-open file.\n' \ - ' Got %d instead of %d.' \ - % (new_ds.GetRasterBand(band).Checksum(),self.chksum)) - - return 'fail' - - computed_minmax = new_ds.GetRasterBand(band).ComputeRasterMinMax() - if computed_minmax != minmax and check_minmax: - post_reason( 'Did not get expected min/max values on still-open file.' ) - print('expect: ', minmax) - print('got: ', computed_minmax) - return 'fail' - - new_ds = None - - new_ds = gdal.Open( new_filename ) - if new_ds is None: - post_reason( 'Failed to open dataset: ' + new_filename ) - return 'fail' - - for band in range(1,out_bands+1): - if self.chksum is not None \ - and new_ds.GetRasterBand(band).Checksum() != self.chksum: - post_reason( 'Did not get expected checksum on reopened file.' \ - ' Got %d instead of %d.' \ - % (new_ds.GetRasterBand(band).Checksum(),self.chksum)) - return 'fail' - - if new_ds.GetRasterBand(band).ComputeRasterMinMax() != minmax and check_minmax: - post_reason( 'Did not get expected min/max values on reopened file.' ) - return 'fail' - - new_ds = None - - if gdal.GetConfigOption( 'CPL_DEBUG', 'OFF' ) != 'ON': - self.driver.Delete( new_filename ) - - return 'success' - - def testSetGeoTransform(self): - if self.testDriver() == 'fail': - return 'skip' - - src_ds = gdal.Open( 'data/' + self.filename ) - xsize = src_ds.RasterXSize - ysize = src_ds.RasterYSize - - new_filename = 'tmp/' + self.filename + '.tst' - new_ds = self.driver.Create( new_filename, xsize, ysize, 1, - src_ds.GetRasterBand(self.band).DataType, - options = self.options ) - if new_ds is None: - post_reason( 'Failed to create test file using Create method.' ) - return 'fail' - - gt = (123.0, 1.18, 0.0, 456.0, 0.0, -1.18 ) - if new_ds.SetGeoTransform( gt ) is not gdal.CE_None: - post_reason( 'Failed to set geographic transformation.' ) - return 'fail' - - src_ds = None - new_ds = None - - new_ds = gdal.Open( new_filename ) - if new_ds is None: - post_reason( 'Failed to open dataset: ' + new_filename ) - return 'fail' - - eps = 0.00000001 - new_gt = new_ds.GetGeoTransform() - if abs(new_gt[0] - gt[0]) > eps \ - or abs(new_gt[1] - gt[1]) > eps \ - or abs(new_gt[2] - gt[2]) > eps \ - or abs(new_gt[3] - gt[3]) > eps \ - or abs(new_gt[4] - gt[4]) > eps \ - or abs(new_gt[5] - gt[5]) > eps: - print('') - print('old = ', gt) - print('new = ', new_gt) - post_reason( 'Did not get expected geotransform.' ) - return 'fail' - - new_ds = None - - if gdal.GetConfigOption( 'CPL_DEBUG', 'OFF' ) != 'ON': - self.driver.Delete( new_filename ) - - return 'success' - - def testSetProjection(self, prj = None, expected_prj = None ): - if self.testDriver() == 'fail': - return 'skip' - - src_ds = gdal.Open( 'data/' + self.filename ) - xsize = src_ds.RasterXSize - ysize = src_ds.RasterYSize - - new_filename = 'tmp/' + self.filename + '.tst' - new_ds = self.driver.Create( new_filename, xsize, ysize, 1, - src_ds.GetRasterBand(self.band).DataType, - options = self.options ) - if new_ds is None: - post_reason( 'Failed to create test file using Create method.' ) - return 'fail' - - gt = (123.0, 1.18, 0.0, 456.0, 0.0, -1.18 ) - if prj is None: - # This is a challenging SRS since it has non-meter linear units. - prj='PROJCS["NAD83 / Ohio South",GEOGCS["NAD83",DATUM["North_American_Datum_1983",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],AUTHORITY["EPSG","6269"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4269"]],PROJECTION["Lambert_Conformal_Conic_2SP"],PARAMETER["standard_parallel_1",40.03333333333333],PARAMETER["standard_parallel_2",38.73333333333333],PARAMETER["latitude_of_origin",38],PARAMETER["central_meridian",-82.5],PARAMETER["false_easting",1968500],PARAMETER["false_northing",0],UNIT["feet",0.3048006096012192]]' - - src_osr = osr.SpatialReference() - src_osr.ImportFromWkt(prj) - - new_ds.SetGeoTransform( gt ) - if new_ds.SetProjection( prj ) is not gdal.CE_None: - post_reason( 'Failed to set geographic projection string.' ) - return 'fail' - - src_ds = None - new_ds = None - - new_ds = gdal.Open( new_filename ) - if new_ds is None: - post_reason( 'Failed to open dataset: ' + new_filename ) - return 'fail' - - expected_osr = osr.SpatialReference() - if expected_prj is None: - expected_osr = src_osr - else: - expected_osr.ImportFromWkt( expected_prj ) - - new_osr = osr.SpatialReference() - new_osr.ImportFromWkt(new_ds.GetProjection()) - if not new_osr.IsSame(expected_osr): - post_reason( 'Did not get expected projection reference.' ) - print('Got: ') - print(new_osr.ExportToPrettyWkt()) - print('Expected:') - print(expected_osr.ExportToPrettyWkt()) - return 'fail' - - new_ds = None - - if gdal.GetConfigOption( 'CPL_DEBUG', 'OFF' ) != 'ON': - self.driver.Delete( new_filename ) - - return 'success' - - def testSetMetadata(self): - if self.testDriver() == 'fail': - return 'skip' - - src_ds = gdal.Open( 'data/' + self.filename ) - xsize = src_ds.RasterXSize - ysize = src_ds.RasterYSize - - new_filename = 'tmp/' + self.filename + '.tst' - new_ds = self.driver.Create( new_filename, xsize, ysize, 1, - src_ds.GetRasterBand(self.band).DataType, - options = self.options ) - if new_ds is None: - post_reason( 'Failed to create test file using Create method.' ) - return 'fail' - - dict = {} - dict['TEST_KEY'] = 'TestValue' - new_ds.SetMetadata( dict ) -# FIXME - #if new_ds.SetMetadata( dict ) is not gdal.CE_None: - #print new_ds.SetMetadata( dict ) - #post_reason( 'Failed to set metadata item.' ) - #return 'fail' - - src_ds = None - new_ds = None - - new_ds = gdal.Open( new_filename ) - if new_ds is None: - post_reason( 'Failed to open dataset: ' + new_filename ) - return 'fail' - - md_dict = new_ds.GetMetadata() - - if (not 'TEST_KEY' in md_dict): - post_reason( 'Metadata item TEST_KEY does not exist.') - return 'fail' - - if md_dict['TEST_KEY'] != 'TestValue': - post_reason( 'Did not get expected metadata item.' ) - return 'fail' - - new_ds = None - - if gdal.GetConfigOption( 'CPL_DEBUG', 'OFF' ) != 'ON': - self.driver.Delete( new_filename ) - - return 'success' - - def testSetNoDataValue(self): - if self.testDriver() == 'fail': - return 'skip' - - src_ds = gdal.Open( 'data/' + self.filename ) - xsize = src_ds.RasterXSize - ysize = src_ds.RasterYSize - - new_filename = 'tmp/' + self.filename + '.tst' - new_ds = self.driver.Create( new_filename, xsize, ysize, 1, - src_ds.GetRasterBand(self.band).DataType, - options = self.options ) - if new_ds is None: - post_reason( 'Failed to create test file using Create method.' ) - return 'fail' - - nodata = 11 - if new_ds.GetRasterBand(1).SetNoDataValue(nodata) is not gdal.CE_None: - post_reason( 'Failed to set NoData value.' ) - return 'fail' - - src_ds = None - new_ds = None - - new_ds = gdal.Open( new_filename ) - if new_ds is None: - post_reason( 'Failed to open dataset: ' + new_filename ) - return 'fail' - - if nodata != new_ds.GetRasterBand(1).GetNoDataValue(): - post_reason( 'Did not get expected NoData value.' ) - return 'fail' - - new_ds = None - - if gdal.GetConfigOption( 'CPL_DEBUG', 'OFF' ) != 'ON': - self.driver.Delete( new_filename ) - - return 'success' - - def testSetDescription(self): - if self.testDriver() == 'fail': - return 'skip' - - src_ds = gdal.Open( 'data/' + self.filename ) - xsize = src_ds.RasterXSize - ysize = src_ds.RasterYSize - - new_filename = 'tmp/' + self.filename + '.tst' - new_ds = self.driver.Create( new_filename, xsize, ysize, 1, - src_ds.GetRasterBand(self.band).DataType, - options = self.options ) - if new_ds is None: - post_reason( 'Failed to create test file using Create method.' ) - return 'fail' - - description = "Description test string" - new_ds.GetRasterBand(1).SetDescription(description) - - src_ds = None - new_ds = None - - new_ds = gdal.Open( new_filename ) - if new_ds is None: - post_reason( 'Failed to open dataset: ' + new_filename ) - return 'fail' - - if description != new_ds.GetRasterBand(1).GetDescription(): - post_reason( 'Did not get expected description string.' ) - return 'fail' - - new_ds = None - - if gdal.GetConfigOption( 'CPL_DEBUG', 'OFF' ) != 'ON': - self.driver.Delete( new_filename ) - - return 'success' - - -def approx_equal( a, b ): - a = float(a) - b = float(b) - if a == 0 and b != 0: - return 0 - - if abs(b/a - 1.0) > .00000000001: - return 0 - else: - return 1 - - -def user_srs_to_wkt( user_text ): - srs = osr.SpatialReference() - srs.SetFromUserInput( user_text ) - return srs.ExportToWkt() - -def equal_srs_from_wkt( expected_wkt, got_wkt ): - expected_srs = osr.SpatialReference() - expected_srs.ImportFromWkt( expected_wkt ) - - got_srs = osr.SpatialReference() - got_srs.ImportFromWkt( got_wkt ) - - if got_srs.IsSame( expected_srs ): - return 1 - else: - print('Expected:\n%s' % expected_wkt) - print('Got: \n%s' % got_wkt) - - post_reason( 'SRS differs from expected.' ) - return 0 - - -############################################################################### -# Compare two sets of RPC metadata, and establish if they are essentially -# equivelent or not. - -def rpcs_equal( md1, md2 ): - - simple_fields = [ 'LINE_OFF', 'SAMP_OFF', 'LAT_OFF', 'LONG_OFF', - 'HEIGHT_OFF', 'LINE_SCALE', 'SAMP_SCALE', 'LAT_SCALE', - 'LONG_SCALE', 'HEIGHT_SCALE' ] - coef_fields = [ 'LINE_NUM_COEFF', 'LINE_DEN_COEFF', - 'SAMP_NUM_COEFF', 'SAMP_DEN_COEFF' ] - - for sf in simple_fields: - - try: - if not approx_equal(float(md1[sf]),float(md2[sf])): - post_reason( '%s values differ.' % sf ) - print(md1[sf]) - print(md2[sf]) - return 0 - except: - post_reason( '%s value missing or corrupt.' % sf ) - print(md1) - print(md2) - return 0 - - for cf in coef_fields: - - try: - list1 = md1[cf].split() - list2 = md2[cf].split() - - except: - post_reason( '%s value missing or corrupt.' % cf ) - print(md1[cf]) - print(md2[cf]) - return 0 - - if len(list1) != 20: - post_reason( '%s value list length wrong(1)' % cf ) - print(list1) - return 0 - - if len(list2) != 20: - post_reason( '%s value list length wrong(2)' % cf ) - print(list2) - return 0 - - for i in range(20): - if not approx_equal(float(list1[i]),float(list2[i])): - post_reason( '%s[%d] values differ.' % (cf,i) ) - print(list1[i], list2[i]) - return 0 - - return 1 - -############################################################################### -# Test if geotransforms are equal with an epsilon tolerance -# - -def geotransform_equals(gt1, gt2, gt_epsilon): - for i in range(6): - if abs(gt1[i]-gt2[i]) > gt_epsilon: - print('') - print('gt1 = ', gt1) - print('gt2 = ', gt2) - post_reason( 'Geotransform differs.' ) - return False - return True - - -############################################################################### -# Download file at url 'url' and put it as 'filename' in 'tmp/cache/' -# -# If 'filename' already exits in 'tmp/cache/', it is not downloaded -# If GDAL_DOWNLOAD_TEST_DATA is not defined, the function fails -# If GDAL_DOWNLOAD_TEST_DATA is defined, 'url' is downloaded as 'filename' in 'tmp/cache/' - -def download_file(url, filename, download_size = -1): - global count_skipped_tests_download - try: - os.stat( 'tmp/cache/' + filename ) - return True - except: - if 'GDAL_DOWNLOAD_TEST_DATA' in os.environ: - val = None - try: - handle = gdalurlopen(url) - if download_size == -1: - try: - handle_info = handle.info() - content_length = handle_info['content-length'] - print('Downloading %s (length = %s bytes)...' % (url, content_length)) - except: - print('Downloading %s...' % (url)) - val = handle.read() - else: - print('Downloading %d bytes from %s...' % (download_size, url)) - val = handle.read(download_size) - except: - return False - - try: - os.stat( 'tmp/cache' ) - except: - os.mkdir('tmp/cache') - - try: - open( 'tmp/cache/' + filename, 'wb').write(val) - return True - except: - print('Cannot write %s' % (filename)) - return False - else: - if count_skipped_tests_download == 0: - print('As GDAL_DOWNLOAD_TEST_DATA environment variable is not defined, some tests relying on data to downloaded from the Web will be skipped') - count_skipped_tests_download = count_skipped_tests_download + 1 - return False - - -############################################################################### -# GDAL data type to python struct format -def gdal_data_type_to_python_struct_format(datatype): - type_char = 'B' - if datatype == gdal.GDT_Int16: - type_char = 'h' - elif datatype == gdal.GDT_UInt16: - type_char = 'H' - elif datatype == gdal.GDT_Int32: - type_char = 'i' - elif datatype == gdal.GDT_UInt32: - type_char = 'I' - elif datatype == gdal.GDT_Float32: - type_char = 'f' - elif datatype == gdal.GDT_Float64: - type_char = 'd' - return type_char - -############################################################################### -# Compare the values of the pixels - -def compare_ds(ds1, ds2, xoff = 0, yoff = 0, width = 0, height = 0, verbose=1): - import struct - - if width == 0: - width = ds1.RasterXSize - if height == 0: - height = ds1.RasterYSize - data1 = ds1.GetRasterBand(1).ReadRaster(xoff, yoff, width, height) - type_char = gdal_data_type_to_python_struct_format(ds1.GetRasterBand(1).DataType) - val_array1 = struct.unpack(type_char * width * height, data1) - - data2 = ds2.GetRasterBand(1).ReadRaster(xoff, yoff, width, height) - type_char = gdal_data_type_to_python_struct_format(ds2.GetRasterBand(1).DataType) - val_array2 = struct.unpack(type_char * width * height, data2) - - maxdiff = 0.0 - ndiffs = 0 - for i in range(width*height): - diff = val_array1[i] - val_array2[i] - if diff != 0: - #print(val_array1[i]) - #print(val_array2[i]) - ndiffs = ndiffs + 1 - if abs(diff) > maxdiff: - maxdiff = abs(diff) - if verbose: - print("Diff at pixel (%d, %d) : %f" % (i % width, i / width, float(diff))) - elif ndiffs < 10: - if verbose: - print("Diff at pixel (%d, %d) : %f" % (i % width, i / width, float(diff))) - if maxdiff != 0 and verbose: - print("Max diff : %d" % (maxdiff)) - print("Number of diffs : %d" % (ndiffs)) - - return maxdiff - - - -############################################################################### -# Deregister all JPEG2000 drivers, except the one passed as an argument - -def deregister_all_jpeg2000_drivers_but(name_of_driver_to_keep): - global jp2kak_drv, jpeg2000_drv, jp2ecw_drv, jp2mrsid_drv, jp2openjpeg_drv - global jp2kak_drv_unregistered,jpeg2000_drv_unregistered,jp2ecw_drv_unregistered,jp2mrsid_drv_unregistered,jp2openjpeg_drv_unregistered - - # Deregister other potential conflicting JPEG2000 drivers that will - # be re-registered in the cleanup - try: - jp2kak_drv = gdal.GetDriverByName('JP2KAK') - if name_of_driver_to_keep != 'JP2KAK' and jp2kak_drv: - gdal.Debug('gdaltest','Deregistering JP2KAK') - jp2kak_drv.Deregister() - jp2kak_drv_unregistered = True - except: - pass - - try: - jpeg2000_drv = gdal.GetDriverByName('JPEG2000') - if name_of_driver_to_keep != 'JPEG2000' and jpeg2000_drv: - gdal.Debug('gdaltest','Deregistering JPEG2000') - jpeg2000_drv.Deregister() - jpeg2000_drv_unregistered = True - except: - pass - - try: - jp2ecw_drv = gdal.GetDriverByName('JP2ECW') - if name_of_driver_to_keep != 'JP2ECW' and jp2ecw_drv: - gdal.Debug('gdaltest.','Deregistering JP2ECW') - jp2ecw_drv.Deregister() - jp2ecw_drv_unregistered = True - except: - pass - - try: - jp2mrsid_drv = gdal.GetDriverByName('JP2MrSID') - if name_of_driver_to_keep != 'JP2MrSID' and jp2mrsid_drv: - gdal.Debug('gdaltest.','Deregistering JP2MrSID') - jp2mrsid_drv.Deregister() - jp2mrsid_drv_unregistered = True - except: - pass - - try: - jp2openjpeg_drv = gdal.GetDriverByName('JP2OpenJPEG') - if name_of_driver_to_keep != 'JP2OpenJPEG' and jp2openjpeg_drv: - gdal.Debug('gdaltest.','Deregistering JP2OpenJPEG') - jp2openjpeg_drv.Deregister() - jp2openjpeg_drv_unregistered = True - except: - pass - - return True - -############################################################################### -# Re-register all JPEG2000 drivers previously disabled by -# deregister_all_jpeg2000_drivers_but - -def reregister_all_jpeg2000_drivers(): - global jp2kak_drv, jpeg2000_drv, jp2ecw_drv, jp2mrsid_drv, jp2openjpeg_drv - global jp2kak_drv_unregistered,jpeg2000_drv_unregistered,jp2ecw_drv_unregistered,jp2mrsid_drv_unregistered, jp2openjpeg_drv_unregistered - - try: - if jp2kak_drv_unregistered: - jp2kak_drv.Register() - jp2kak_drv_unregistered = False - gdal.Debug('gdaltest','Registering JP2KAK') - except: - pass - - try: - if jpeg2000_drv_unregistered: - jpeg2000_drv.Register() - jpeg2000_drv_unregistered = False - gdal.Debug('gdaltest','Registering JPEG2000') - except: - pass - - try: - if jp2ecw_drv_unregistered: - jp2ecw_drv.Register() - jp2ecw_drv_unregistered = False - gdal.Debug('gdaltest','Registering JP2ECW') - except: - pass - - try: - if jp2mrsid_drv_unregistered: - jp2mrsid_drv.Register() - jp2mrsid_drv_unregistered = False - gdal.Debug('gdaltest','Registering JP2MrSID') - except: - pass - - try: - if jp2openjpeg_drv_unregistered: - jp2openjpeg_drv.Register() - jp2openjpeg_drv = False - gdal.Debug('gdaltest','Registering JP2OpenJPEG') - except: - pass - - return True - -############################################################################### -# Determine if the filesystem supports sparse files. -# Currently, this will only work on Linux (or any *NIX that has the stat -# command line utility) - -def filesystem_supports_sparse_files(path): - - if skip_on_travis(): - return False - - try: - (ret, err) = runexternal_out_and_err('stat -f -c "%T" ' + path) - except: - return False - - if err != '': - post_reason('Cannot determine if filesystem supports sparse files') - return False - - if ret.find('fat32') != -1: - post_reason('File system does not support sparse files') - return False - - # Add here any missing filesystem supporting sparse files - # See http://en.wikipedia.org/wiki/Comparison_of_file_systems - if ret.find('ext3') == -1 and \ - ret.find('ext4') == -1 and \ - ret.find('reiser') == -1 and \ - ret.find('xfs') == -1 and \ - ret.find('jfs') == -1 and \ - ret.find('zfs') == -1 and \ - ret.find('ntfs') == -1 : - post_reason('Filesystem %s is not believed to support sparse files' % ret) - return False - - return True - -############################################################################### -# Unzip a file - -def unzip(target_dir, zipfilename, verbose = False): - - try: - import zipfile - zf = zipfile.ZipFile(zipfilename) - except: - os.system('unzip -d ' + target_dir + ' ' + zipfilename) - return - - for filename in zf.namelist(): - if verbose: - print(filename) - outfilename = os.path.join(target_dir, filename) - if filename.endswith('/'): - if not os.path.exists(outfilename): - os.makedirs(outfilename) - else: - outdirname = os.path.dirname(outfilename) - if not os.path.exists(outdirname): - os.makedirs(outdirname) - - outfile = open(outfilename,'wb') - outfile.write(zf.read(filename)) - outfile.close() - - return - -############################################################################### -# Return if a number is the NaN number - -def isnan(val): - if val == val: - # Python 2.3 unlike later versions return True for nan == nan - val_str = '%f' % val - if val_str == 'nan': - return True - else: - return False - else: - return True - - -############################################################################### -# Return NaN - -def NaN(): - try: - # Python >= 2.6 - return float('nan') - except: - return 1e400 / 1e400 - -############################################################################### -# Return positive infinity - -def posinf(): - try: - # Python >= 2.6 - return float('inf') - except: - return 1e400 - -############################################################################### -# Return negative infinity - -def neginf(): - try: - # Python >= 2.6 - return float('-inf') - except: - return -1e400 - -############################################################################### -# Has the user requested to run the slow tests - -def run_slow_tests(): - global count_skipped_tests_slow - val = gdal.GetConfigOption('GDAL_RUN_SLOW_TESTS', None) - if val != 'yes' and val != 'YES': - - if count_skipped_tests_slow == 0: - print('As GDAL_RUN_SLOW_TESTS environment variable is not defined, some "slow" tests will be skipped') - count_skipped_tests_slow = count_skipped_tests_slow + 1 - - return False - return True - -############################################################################### -# Return true if the platform support symlinks - -def support_symlink(): - if sys.platform.startswith('linux'): - return True - if sys.platform.find('freebsd') != -1: - return True - if sys.platform == 'darwin': - return True - if sys.platform.find('sunos') != -1: - return True - return False - -############################################################################### -# Return True if the test must be skipped - -def skip_on_travis(): - val = gdal.GetConfigOption('TRAVIS', None) - if val is not None: - post_reason('Test skipped on Travis') - return True - return False - - -############################################################################### -# find_lib_linux() -# Parse /proc/self/maps to find an occurrence of libXXXXX.so.* - -def find_lib_linux(libname): - - f = open('/proc/self/maps') - lines = f.readlines() - f.close() - - for line in lines: - if line.rfind('/lib' + libname) == -1 or line.find('.so') == -1: - continue - - i = line.find(' ') - if i < 0: - continue - line = line[i+1:] - i = line.find(' ') - if i < 0: - continue - line = line[i+1:] - i = line.find(' ') - if i < 0: - continue - line = line[i+1:] - i = line.find(' ') - if i < 0: - continue - line = line[i+1:] - i = line.find(' ') - if i < 0: - continue - line = line[i+1:] - - soname = line.lstrip().rstrip('\n') - if soname.rfind('/lib' + libname) == -1: - continue - - return soname - - return None - -############################################################################### -# find_lib_sunos() -# Parse output of pmap to find an occurrence of libXXX.so.* - -def find_lib_sunos(libname): - - pid = os.getpid() - (lines, err) = runexternal_out_and_err('pmap %d' % pid) - - for line in lines.split('\n'): - if line.rfind('/lib' + libname) == -1 or line.find('.so') == -1: - continue - - i = line.find('/') - if i < 0: - continue - line = line[i:] - - soname = line.lstrip().rstrip('\n') - if soname.rfind('/lib' + libname) == -1: - continue - - return soname - - return None - -############################################################################### -# find_lib_windows() -# use Module32First() / Module32Next() API on the current process - -def find_lib_windows(libname): - - try: - import ctypes - except: - return None - - kernel32 = ctypes.windll.kernel32 - - MAX_MODULE_NAME32 = 255 - MAX_PATH = 260 - - TH32CS_SNAPMODULE = 0x00000008 - - class MODULEENTRY32(ctypes.Structure): - _fields_ = [ - ("dwSize", ctypes.c_int), - ("th32ModuleID", ctypes.c_int), - ("th32ProcessID", ctypes.c_int), - ("GlblcntUsage", ctypes.c_int), - ("ProccntUsage", ctypes.c_int), - ("modBaseAddr", ctypes.c_char_p), - ("modBaseSize", ctypes.c_int), - ("hModule", ctypes.c_void_p), - ("szModule", ctypes.c_char * (MAX_MODULE_NAME32 + 1)), - ("szExePath", ctypes.c_char * MAX_PATH) - ] - - Module32First = kernel32.Module32First - Module32First.argtypes = [ ctypes.c_void_p, ctypes.POINTER(MODULEENTRY32) ] - Module32First.rettypes = ctypes.c_int - - Module32Next = kernel32.Module32Next - Module32Next.argtypes = [ ctypes.c_void_p, ctypes.POINTER(MODULEENTRY32) ] - Module32Next.rettypes = ctypes.c_int - - CreateToolhelp32Snapshot = kernel32.CreateToolhelp32Snapshot - CreateToolhelp32Snapshot.argtypes = [ ctypes.c_int, ctypes.c_int ] - CreateToolhelp32Snapshot.rettypes = ctypes.c_void_p - - CloseHandle = kernel32.CloseHandle - CloseHandle.argtypes = [ ctypes.c_void_p ] - CloseHandle.rettypes = ctypes.c_int - - GetLastError = kernel32.GetLastError - GetLastError.argtypes = [] - GetLastError.rettypes = ctypes.c_int - - snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE,0) - if snapshot is None: - return None - - soname = None - - i = 0 - while True: - entry = MODULEENTRY32() - entry.dwSize = ctypes.sizeof(MODULEENTRY32) - pentry = ctypes.pointer(entry) - if i == 0: - ret = Module32First(snapshot, pentry) - else: - ret = Module32Next(snapshot, pentry) - i = i + 1 - if ret == 0: - break - - try: - path = entry.szExePath.decode('latin1') - except: - continue - - i = path.rfind('\\' + libname) - if i < 0: - continue - if path[i+1:].find('\\') >= 0: - continue - soname = path - break - - CloseHandle(snapshot) - - return soname - -############################################################################### -# find_lib() - -def find_lib(mylib): - if sys.platform.startswith('linux'): - return find_lib_linux(mylib) - elif sys.platform.startswith('sunos'): - return find_lib_sunos(mylib) - elif sys.platform.startswith('win32'): - return find_lib_windows(mylib) - else: - # sorry mac users or other BSDs - # should be doable, but not in a blindless way - return None - -############################################################################### -# get_opened_files() - -def get_opened_files(): - if not sys.platform.startswith('linux'): - return [] - fdpath = '/proc/%d/fd' % os.getpid() - file_numbers = os.listdir(fdpath) - filenames = [] - for fd in file_numbers: - try: - filename = os.readlink('%s/%s' % (fdpath, fd)) - if not filename.startswith('/dev/') and not filename.startswith('pipe:'): - filenames.append(filename) - except: - pass - return filenames - -############################################################################### -# is_file_open() - -def is_file_open(filename): - for got_filename in get_opened_files(): - if got_filename.find(filename) >= 0: - return True - return False diff --git a/autotest/pymod/gdaltest_python2.py b/autotest/pymod/gdaltest_python2.py deleted file mode 100644 index 51c1b91ba10d..000000000000 --- a/autotest/pymod/gdaltest_python2.py +++ /dev/null @@ -1,189 +0,0 @@ -# -*- coding: utf-8 -*- -############################################################################### -# $Id$ -# -# Project: GDAL/OGR Test Suite -# Purpose: Python Library supporting GDAL/OGR Test Suite -# Author: Even Rouault, -# -############################################################################### -# Copyright (c) 2003, Frank Warmerdam -# Copyright (c) 2009-2013, Even Rouault -# -# Permission is hereby granted, free of charge, to any person obtaining a -# copy of this software and associated documentation files (the "Software"), -# to deal in the Software without restriction, including without limitation -# the rights to use, copy, modify, merge, publish, distribute, sublicense, -# and/or sell copies of the Software, and to permit persons to whom the -# Software is furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included -# in all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -# DEALINGS IN THE SOFTWARE. -############################################################################### - -import urllib2 -import socket -import os -import sys -from sys import version_info -from Queue import Queue -from threading import Thread - -def run_func(func): - try: - result = func() - print(result) - return result - except SystemExit, x: - import traceback - traceback.print_exc() - - raise x - except: - result = 'fail (blowup)' - print(result) - - import traceback - traceback.print_exc() - return result - -def urlescape(url): - # Escape any non-ASCII characters - try: - import urllib - url = urllib.quote(url) - except: - pass - return url - -def gdalurlopen(url): - timeout = 10 - old_timeout = socket.getdefaulttimeout() - socket.setdefaulttimeout(timeout) - - if 'GDAL_HTTP_PROXY' in os.environ: - proxy = os.environ['GDAL_HTTP_PROXY'] - - if 'GDAL_HTTP_PROXYUSERPWD' in os.environ: - proxyuserpwd = os.environ['GDAL_HTTP_PROXYUSERPWD'] - proxyHandler = urllib2.ProxyHandler({"http" : \ - "http://%s@%s" % (proxyuserpwd, proxy)}) - else: - proxyuserpwd = None - proxyHandler = urllib2.ProxyHandler({"http" : \ - "http://%s" % (proxy)}) - - opener = urllib2.build_opener(proxyHandler, urllib2.HTTPHandler) - - urllib2.install_opener(opener) - - try: - handle = urllib2.urlopen(url) - socket.setdefaulttimeout(old_timeout) - return handle - except urllib2.HTTPError, e: - print('HTTP service for %s is down (HTTP Error: %d)' % (url, e.code)) - socket.setdefaulttimeout(old_timeout) - return None - except urllib2.URLError, e: - print('HTTP service for %s is down (HTTP Error: %s)' % (url, e.reason)) - socket.setdefaulttimeout(old_timeout) - return None - except: - print('HTTP service for %s is down.' %(url)) - socket.setdefaulttimeout(old_timeout) - return None - -def warn_if_memleak(cmd, out_str): - - # If DEBUG_VSIMALLOC_STATS is defined, this is an easy way - # to catch some memory leaks - if cmd.find('--utility_version') == -1 and \ - out_str.find('VSIMalloc + VSICalloc - VSIFree') != -1 and \ - out_str.find('VSIMalloc + VSICalloc - VSIFree : 0') == -1: - print('memory leak detected') - print(out_str) - -def spawn_async26(cmd): - import shlex - import subprocess - command = shlex.split(cmd) - try: - process = subprocess.Popen(command, stdout=subprocess.PIPE) - return (process, process.stdout) - except: - return (None, None) - -def spawn_async(cmd): - if version_info >= (2,6,0): - return spawn_async26(cmd) - - import popen2 - try: - process = popen2.Popen3(cmd) - except: - return (None, None) - if process is None: - return (None, None) - process.tochild.close() - return (process, process.fromchild) - -def wait_process(process): - process.wait() - -def runexternal(cmd, strin = None, check_memleak = True, display_live_on_parent_stdout = False): - if strin is None: - ret_stdout = os.popen(cmd) - else: - (ret_stdin, ret_stdout) = os.popen2(cmd) - ret_stdin.write(strin) - ret_stdin.close() - - if display_live_on_parent_stdout: - out_str = '' - while True: - c = ret_stdout.read(1) - if c == '': - break - out_str = out_str + c - sys.stdout.write(c) - ret_stdout.close() - else: - out_str = ret_stdout.read() - ret_stdout.close() - - if check_memleak: - warn_if_memleak(cmd, out_str) - - return out_str - -def read_in_thread(f, q): - q.put(f.read()) - f.close() - -def runexternal_out_and_err(cmd, check_memleak = True): - (ret_stdin, ret_stdout, ret_stderr) = os.popen3(cmd) - ret_stdin.close() - - q_stdout = Queue() - t_stdout = Thread(target=read_in_thread, args=(ret_stdout, q_stdout)) - q_stderr = Queue() - t_stderr = Thread(target=read_in_thread, args=(ret_stderr, q_stderr)) - t_stdout.start() - t_stderr.start() - - out_str = q_stdout.get() - err_str = q_stderr.get() - - if check_memleak: - warn_if_memleak(cmd, out_str) - - return (out_str, err_str) diff --git a/autotest/pymod/gdaltest_python3.py b/autotest/pymod/gdaltest_python3.py deleted file mode 100644 index 6ffc7f8e72f9..000000000000 --- a/autotest/pymod/gdaltest_python3.py +++ /dev/null @@ -1,177 +0,0 @@ -# -*- coding: utf-8 -*- -############################################################################### -# $Id$ -# -# Project: GDAL/OGR Test Suite -# Purpose: Python Library supporting GDAL/OGR Test Suite -# Author: Even Rouault, -# -############################################################################### -# Copyright (c) 2003, Frank Warmerdam -# Copyright (c) 2009-2013, Even Rouault -# -# Permission is hereby granted, free of charge, to any person obtaining a -# copy of this software and associated documentation files (the "Software"), -# to deal in the Software without restriction, including without limitation -# the rights to use, copy, modify, merge, publish, distribute, sublicense, -# and/or sell copies of the Software, and to permit persons to whom the -# Software is furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included -# in all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -# DEALINGS IN THE SOFTWARE. -############################################################################### - -import urllib.request, urllib.error, urllib.parse -import socket -import subprocess -import shlex -import os -import sys -from queue import Queue -from threading import Thread - -def run_func(func): - try: - result = func() - print(result) - return result - except SystemExit as x: - import traceback - traceback.print_exc() - - raise x - except: - result = 'fail (blowup)' - print(result) - - import traceback - traceback.print_exc() - return result - -def urlescape(url): - # Escape any non-ASCII characters - try: - import urllib - url = urllib.parse.quote(url) - except: - pass - return url - -def gdalurlopen(url): - timeout = 10 - old_timeout = socket.getdefaulttimeout() - socket.setdefaulttimeout(timeout) - - if 'GDAL_HTTP_PROXY' in os.environ: - proxy = os.environ['GDAL_HTTP_PROXY'] - - if 'GDAL_HTTP_PROXYUSERPWD' in os.environ: - proxyuserpwd = os.environ['GDAL_HTTP_PROXYUSERPWD'] - proxyHandler = urllib.request.ProxyHandler({"http" : \ - "http://%s@%s" % (proxyuserpwd, proxy)}) - else: - proxyuserpwd = None - proxyHandler = urllib.request.ProxyHandler({"http" : \ - "http://%s" % (proxy)}) - - opener = urllib.request.build_opener(proxyHandler, urllib.request.HTTPHandler) - - urllib.request.install_opener(opener) - - try: - handle = urllib.request.urlopen(url) - socket.setdefaulttimeout(old_timeout) - return handle - except urllib.error.HTTPError as e: - print('HTTP service for %s is down (HTTP Error: %d)' % (url, e.code)) - socket.setdefaulttimeout(old_timeout) - return None - except urllib.error.URLError as e: - print('HTTP service for %s is down (URL Error: %s)' % (url, e.reason)) - socket.setdefaulttimeout(old_timeout) - return None - except: - print('HTTP service for %s is down.' %(url)) - socket.setdefaulttimeout(old_timeout) - return None - -def spawn_async(cmd): - command = shlex.split(cmd) - try: - process = subprocess.Popen(command, stdout=subprocess.PIPE) - return (process, process.stdout) - except: - return (None, None) - -def wait_process(process): - process.wait() - -def runexternal(cmd, strin = None, check_memleak = True, display_live_on_parent_stdout = False): - command = shlex.split(cmd) - if strin is None: - p = subprocess.Popen(command, stdout=subprocess.PIPE) - else: - p = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE) - p.stdin.write(bytes(strin, 'ascii')) - p.stdin.close() - - if p.stdout is not None: - if display_live_on_parent_stdout: - ret = '' - ret_stdout = p.stdout - while True: - c = p.stdout.read(1).decode('ascii') - if c == '': - break - ret = ret + c - sys.stdout.write(c) - else: - ret = p.stdout.read().decode('ascii') - p.stdout.close() - else: - ret = '' - - p.wait() - - return ret - -def read_in_thread(f, q): - q.put(f.read()) - f.close() - -def runexternal_out_and_err(cmd, check_memleak = True): - command = shlex.split(cmd) - p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - - if p.stdout is not None: - q_stdout = Queue() - t_stdout = Thread(target=read_in_thread, args=(p.stdout, q_stdout)) - t_stdout.start() - else: - q_stdout = None - ret_stdout = '' - - if p.stderr is not None: - q_stderr = Queue() - t_stderr = Thread(target=read_in_thread, args=(p.stderr, q_stderr)) - t_stderr.start() - else: - q_stderr = None - ret_stderr = '' - - if q_stdout is not None: - ret_stdout = q_stdout.get().decode('ascii') - if q_stderr is not None: - ret_stderr = q_stderr.get().decode('ascii') - - p.wait() - - return (ret_stdout, ret_stderr) diff --git a/autotest/pymod/ogrtest.py b/autotest/pymod/ogrtest.py deleted file mode 100644 index 1d45b3308e51..000000000000 --- a/autotest/pymod/ogrtest.py +++ /dev/null @@ -1,194 +0,0 @@ -############################################################################### -# $Id$ -# -# Project: GDAL/OGR Test Suite -# Purpose: Support functions for OGR tests. -# Author: Frank Warmerdam -# -############################################################################### -# Copyright (c) 2003, Frank Warmerdam -# -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Library General Public -# License as published by the Free Software Foundation; either -# version 2 of the License, or (at your option) any later version. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Library General Public License for more details. -# -# You should have received a copy of the GNU Library General Public -# License along with this library; if not, write to the -# Free Software Foundation, Inc., 59 Temple Place - Suite 330, -# Boston, MA 02111-1307, USA. -############################################################################### - -import os -import sys - -sys.path.append( '../pymod' ) - -from osgeo import ogr -import gdaltest - -geos_flag = None - - -############################################################################### -def check_features_against_list( layer, field_name, value_list ): - - field_index = layer.GetLayerDefn().GetFieldIndex( field_name ) - if field_index < 0: - gdaltest.post_reason( 'did not find required field ' + field_name ) - return 0 - - for i in range(len(value_list)): - feat = layer.GetNextFeature() - if feat is None: - - gdaltest.post_reason( 'Got only %d features, not the expected %d features.' % (i, len(value_list)) ) - return 0 - - if isinstance(value_list[i],type('str')): - isok = (feat.GetFieldAsString( field_index ) != value_list[i]) - else: - isok = (feat.GetField( field_index ) != value_list[i]) - if isok: - gdaltest.post_reason( 'field %s feature %d did not match expected value %s, got %s.' % (field_name, i, str(value_list[i]), str(feat.GetField(field_index)) ) ) - feat.Destroy() - return 0 - - feat.Destroy() - - feat = layer.GetNextFeature() - if feat is not None: - feat.Destroy() - gdaltest.post_reason( 'got more features than expected' ) - return 0 - - return 1 - -############################################################################### -def check_feature_geometry( feat, geom, max_error = 0.0001 ): - try: - f_geom = feat.GetGeometryRef() - except: - f_geom = feat - - if isinstance(geom,type('a')): - geom = ogr.CreateGeometryFromWkt( geom ) - else: - geom = geom.Clone() - - if (f_geom is not None and geom is None): - gdaltest.post_reason( 'expected NULL geometry but got one.' ) - return 1 - - if (f_geom is None and geom is not None): - gdaltest.post_reason( 'expected geometry but got NULL.' ) - return 1 - - if f_geom.GetGeometryName() != geom.GetGeometryName(): - gdaltest.post_reason( 'geometry names do not match' ) - return 1 - - if f_geom.GetGeometryCount() != geom.GetGeometryCount(): - gdaltest.post_reason( 'sub-geometry counts do not match' ) - return 1 - - if f_geom.GetPointCount() != geom.GetPointCount(): - gdaltest.post_reason( 'point counts do not match' ) - return 1 - - if f_geom.GetGeometryCount() > 0: - count = f_geom.GetGeometryCount() - for i in range(count): - result = check_feature_geometry( f_geom.GetGeometryRef(i), - geom.GetGeometryRef(i), - max_error ) - if result != 0: - return result - - else: - count = f_geom.GetPointCount() - - for i in range(count): - x_dist = abs(f_geom.GetX(i) - geom.GetX(i)) - y_dist = abs(f_geom.GetY(i) - geom.GetY(i)) - z_dist = abs(f_geom.GetZ(i) - geom.GetZ(i)) - - if max(x_dist,y_dist,z_dist) > max_error: - gdaltest.post_reason( 'Error in vertex %d, off by %g.' \ - % (i, max(x_dist,y_dist,z_dist)) ) - return 1 - - geom.Destroy() - return 0 - -############################################################################### -def quick_create_layer_def( lyr, field_list): - # Each field is a tuple of (name, type, width, precision) - # Any of type, width and precision can be skipped. Default type is string. - - for field in field_list: - name = field[0] - if len(field) > 1: - type = field[1] - else: - type = ogr.OFTString - - field_defn = ogr.FieldDefn( name, type ) - - if len(field) > 2: - field_defn.SetWidth( int(field[2]) ) - - if len(field) > 3: - field_defn.SetPrecision( int(field[3]) ) - - lyr.CreateField( field_defn ) - - field_defn.Destroy() - -############################################################################### -def quick_create_feature( layer, field_values, wkt_geometry ): - feature = ogr.Feature( feature_def = layer.GetLayerDefn() ) - - for i in range(len(field_values)): - feature.SetField( i, field_values[i] ) - - if wkt_geometry is not None: - geom = ogr.CreateGeometryFromWkt( wkt_geometry ) - if geom is None: - raise ValueError('Failed to create geometry from: ' + wkt_geometry) - feature.SetGeometryDirectly( geom ) - - result = layer.CreateFeature( feature ) - - feature.Destroy() - - if result != 0: - raise ValueError('CreateFeature() failed in ogrtest.quick_create_feature()') - -############################################################################### -def have_geos(): - global geos_flag - - if geos_flag is None: - pnt1 = ogr.CreateGeometryFromWkt( 'POINT(10 20)' ) - pnt2 = ogr.CreateGeometryFromWkt( 'POINT(30 20)' ) - - try: - result = pnt1.Union( pnt2 ) - except: - result = None - - pnt1.Destroy() - pnt2.Destroy() - - if result is None: - geos_flag = 0 - else: - geos_flag = 1 - - return geos_flag diff --git a/autotest/pymod/test_cli_utilities.py b/autotest/pymod/test_cli_utilities.py deleted file mode 100644 index cca0e9ef1d21..000000000000 --- a/autotest/pymod/test_cli_utilities.py +++ /dev/null @@ -1,189 +0,0 @@ -#!/usr/bin/env python -############################################################################### -# $Id$ -# -# Project: GDAL/OGR Test Suite -# Purpose: Helper functions for testing CLI utilities -# Author: Even Rouault -# -############################################################################### -# Copyright (c) 2008-2010, Even Rouault -# -# Permission is hereby granted, free of charge, to any person obtaining a -# copy of this software and associated documentation files (the "Software"), -# to deal in the Software without restriction, including without limitation -# the rights to use, copy, modify, merge, publish, distribute, sublicense, -# and/or sell copies of the Software, and to permit persons to whom the -# Software is furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included -# in all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -# DEALINGS IN THE SOFTWARE. -############################################################################### - -import os -import sys - -from osgeo import gdal - -import gdaltest - -cli_exe_path = { } - -############################################################################### -# -def get_cli_utility_path_internal(cli_utility_name): - - if sys.platform == 'win32': - cli_utility_name = cli_utility_name + '.exe' - - # First try : in the apps directory of the GDAL source tree - # This is the case for the buildbot directory tree - try: - cli_utility_path = os.path.join(os.getcwd(), '..', '..', 'gdal', 'apps', cli_utility_name) - if sys.platform == 'win32': - cli_utility_path = cli_utility_path.replace('\\', '/') - if os.path.isfile(cli_utility_path): - ret = gdaltest.runexternal(cli_utility_path + ' --utility_version') - - if ret.find('GDAL') != -1: - return cli_utility_path - except: - pass - - # Second try : the autotest directory is a subdirectory of gdal/ (FrankW's layout) - try: - cli_utility_path = os.path.join(os.getcwd(), '..', '..', 'apps', cli_utility_name) - if sys.platform == 'win32': - cli_utility_path = cli_utility_path.replace('\\', '/') - if os.path.isfile(cli_utility_path): - ret = gdaltest.runexternal(cli_utility_path + ' --utility_version') - - if ret.find('GDAL') != -1: - return cli_utility_path - except: - pass - - # Otherwise look up in the system path - try: - cli_utility_path = cli_utility_name - ret = gdaltest.runexternal(cli_utility_path + ' --utility_version') - - if ret.find('GDAL') != -1: - return cli_utility_path - except: - pass - - return None - -############################################################################### -# -def get_cli_utility_path(cli_utility_name): - global cli_exe_path - if cli_utility_name in cli_exe_path: - return cli_exe_path[cli_utility_name] - else: - cli_exe_path[cli_utility_name] = get_cli_utility_path_internal(cli_utility_name) - return cli_exe_path[cli_utility_name] - -############################################################################### -# -def get_gdalinfo_path(): - return get_cli_utility_path('gdalinfo') - -############################################################################### -# -def get_gdal_translate_path(): - return get_cli_utility_path('gdal_translate') - -############################################################################### -# -def get_gdalwarp_path(): - return get_cli_utility_path('gdalwarp') - -############################################################################### -# -def get_gdaladdo_path(): - return get_cli_utility_path('gdaladdo') - -############################################################################### -# -def get_gdaltransform_path(): - return get_cli_utility_path('gdaltransform') - -############################################################################### -# -def get_gdaltindex_path(): - return get_cli_utility_path('gdaltindex') - -############################################################################### -# -def get_gdal_grid_path(): - return get_cli_utility_path('gdal_grid') - -############################################################################### -# -def get_ogrinfo_path(): - return get_cli_utility_path('ogrinfo') - -############################################################################### -# -def get_ogr2ogr_path(): - return get_cli_utility_path('ogr2ogr') - -############################################################################### -# -def get_ogrtindex_path(): - return get_cli_utility_path('ogrtindex') - -############################################################################### -# -def get_ogrlineref_path(): - return get_cli_utility_path('ogrlineref') - -############################################################################### -# -def get_gdalbuildvrt_path(): - return get_cli_utility_path('gdalbuildvrt') - -############################################################################### -# -def get_gdal_contour_path(): - return get_cli_utility_path('gdal_contour') - -############################################################################### -# -def get_gdaldem_path(): - return get_cli_utility_path('gdaldem') - -############################################################################### -# -def get_gdal_rasterize_path(): - return get_cli_utility_path('gdal_rasterize') - -############################################################################### -# -def get_nearblack_path(): - return get_cli_utility_path('nearblack') - -############################################################################### -# -def get_test_ogrsf_path(): - return get_cli_utility_path('test_ogrsf') - -############################################################################### -# -def get_gdallocationinfo_path(): - return get_cli_utility_path('gdallocationinfo') - -############################################################################### -# -def get_gdalsrsinfo_path(): - return get_cli_utility_path('gdalsrsinfo') From 9d18110619c33736b14958a0c5d6b3e3eecee533 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Wed, 6 Jul 2016 15:55:00 +0200 Subject: [PATCH 19/69] COMP: Adding pixelfunction file for compilation --- gdal/frmts/vrt/GNUmakefile | 1 + 1 file changed, 1 insertion(+) diff --git a/gdal/frmts/vrt/GNUmakefile b/gdal/frmts/vrt/GNUmakefile index b4e3d2427e16..2340fc25f7c1 100644 --- a/gdal/frmts/vrt/GNUmakefile +++ b/gdal/frmts/vrt/GNUmakefile @@ -3,6 +3,7 @@ include ../../GDALmake.opt OBJ := vrtdataset.o vrtrasterband.o vrtdriver.o vrtsources.o OBJ += vrtfilters.o vrtsourcedrasterband.o vrtrawrasterband.o OBJ += vrtwarped.o vrtderivedrasterband.o vrtpansharpened.o +OBJ += pixelfunctions.o CPPFLAGS := -I../raw $(CPPFLAGS) From 22c92a8e2c98d8a9ac7060bdd72bf01a9a39d3be Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Wed, 6 Jul 2016 15:55:27 +0200 Subject: [PATCH 20/69] COMP: Add prototypes for all functions --- gdal/frmts/vrt/pixelfunctions.c | 88 +++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/gdal/frmts/vrt/pixelfunctions.c b/gdal/frmts/vrt/pixelfunctions.c index 81396834b1e8..bbae89801658 100644 --- a/gdal/frmts/vrt/pixelfunctions.c +++ b/gdal/frmts/vrt/pixelfunctions.c @@ -30,6 +30,94 @@ #include #include +CPLErr RealPixelFunc(void **papoSources, int nSources, void *pData, + int nXSize, int nYSize, + GDALDataType eSrcType, GDALDataType eBufType, + int nPixelSpace, int nLineSpace); + +CPLErr ImagPixelFunc(void **papoSources, int nSources, void *pData, + int nXSize, int nYSize, + GDALDataType eSrcType, GDALDataType eBufType, + int nPixelSpace, int nLineSpace); + +CPLErr ComplexPixelFunc(void **papoSources, int nSources, void *pData, + int nXSize, int nYSize, + GDALDataType eSrcType, GDALDataType eBufType, + int nPixelSpace, int nLineSpace); + +CPLErr ModulePixelFunc(void **papoSources, int nSources, void *pData, + int nXSize, int nYSize, + GDALDataType eSrcType, GDALDataType eBufType, + int nPixelSpace, int nLineSpace); + +CPLErr PhasePixelFunc(void **papoSources, int nSources, void *pData, + int nXSize, int nYSize, + GDALDataType eSrcType, GDALDataType eBufType, + int nPixelSpace, int nLineSpace); + +CPLErr ConjPixelFunc(void **papoSources, int nSources, void *pData, + int nXSize, int nYSize, + GDALDataType eSrcType, GDALDataType eBufType, + int nPixelSpace, int nLineSpace); + +CPLErr SumPixelFunc(void **papoSources, int nSources, void *pData, + int nXSize, int nYSize, + GDALDataType eSrcType, GDALDataType eBufType, + int nPixelSpace, int nLineSpace); + +CPLErr DiffPixelFunc(void **papoSources, int nSources, void *pData, + int nXSize, int nYSize, + GDALDataType eSrcType, GDALDataType eBufType, + int nPixelSpace, int nLineSpace); + +CPLErr MulPixelFunc(void **papoSources, int nSources, void *pData, + int nXSize, int nYSize, + GDALDataType eSrcType, GDALDataType eBufType, + int nPixelSpace, int nLineSpace); + +CPLErr CMulPixelFunc(void **papoSources, int nSources, void *pData, + int nXSize, int nYSize, + GDALDataType eSrcType, GDALDataType eBufType, + int nPixelSpace, int nLineSpace); + +CPLErr InvPixelFunc(void **papoSources, int nSources, void *pData, + int nXSize, int nYSize, + GDALDataType eSrcType, GDALDataType eBufType, + int nPixelSpace, int nLineSpace); + +CPLErr IntensityPixelFunc(void **papoSources, int nSources, void *pData, + int nXSize, int nYSize, + GDALDataType eSrcType, GDALDataType eBufType, + int nPixelSpace, int nLineSpace); + +CPLErr SqrtPixelFunc(void **papoSources, int nSources, void *pData, + int nXSize, int nYSize, + GDALDataType eSrcType, GDALDataType eBufType, + int nPixelSpace, int nLineSpace); + +CPLErr Log10PixelFunc(void **papoSources, int nSources, void *pData, + int nXSize, int nYSize, + GDALDataType eSrcType, GDALDataType eBufType, + int nPixelSpace, int nLineSpace); + +CPLErr dB2AmpPixelFunc(void **papoSources, int nSources, void *pData, + int nXSize, int nYSize, + GDALDataType eSrcType, GDALDataType eBufType, + int nPixelSpace, int nLineSpace); + +CPLErr dB2PowPixelFunc(void **papoSources, int nSources, void *pData, + int nXSize, int nYSize, + GDALDataType eSrcType, GDALDataType eBufType, + int nPixelSpace, int nLineSpace); + +CPLErr PowPixelFuncHelper(void **papoSources, int nSources, void *pData, + int nXSize, int nYSize, + GDALDataType eSrcType, GDALDataType eBufType, + int nPixelSpace, int nLineSpace, + double base, double fact); + +CPLErr CPL_STDCALL GDALRegisterDefaultPixelFunc(); + CPLErr RealPixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, From a53f54018962f31568da3a307f044e9b51aeff59 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Wed, 6 Jul 2016 15:55:51 +0200 Subject: [PATCH 21/69] ENH: Register default pixel functions --- gdal/frmts/vrt/vrtdriver.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/gdal/frmts/vrt/vrtdriver.cpp b/gdal/frmts/vrt/vrtdriver.cpp index 82eee925e45f..99d752671ec3 100644 --- a/gdal/frmts/vrt/vrtdriver.cpp +++ b/gdal/frmts/vrt/vrtdriver.cpp @@ -37,6 +37,10 @@ CPL_CVSID("$Id$"); +// Required to register pixel functions +extern CPLErr CPL_STDCALL GDALRegisterDefaultPixelFunc(); + + /************************************************************************/ /* VRTDriver() */ /************************************************************************/ @@ -356,6 +360,9 @@ VRTCreateCopy( const char * pszFilename, void GDALRegister_VRT() { + // First register the pixel functions + GDALRegisterDefaultPixelFunc(); + if( GDALGetDriverByName( "VRT" ) != NULL ) return; From f0f79ef0e432767399cfd27b61b875f81e0d6c6e Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Wed, 6 Jul 2016 16:27:30 +0200 Subject: [PATCH 22/69] ENH: Removing useless files (from PR review) --- autotest/gdrivers/testnonboundtoswig.py | 383 ------------------------ gdal/frmts/vrt/pixelfunctions.licence | 17 -- 2 files changed, 400 deletions(-) delete mode 100644 autotest/gdrivers/testnonboundtoswig.py delete mode 100644 gdal/frmts/vrt/pixelfunctions.licence diff --git a/autotest/gdrivers/testnonboundtoswig.py b/autotest/gdrivers/testnonboundtoswig.py deleted file mode 100644 index f6d8fe90b74b..000000000000 --- a/autotest/gdrivers/testnonboundtoswig.py +++ /dev/null @@ -1,383 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -############################################################################### -# $Id$ -# -# Project: GDAL/OGR Test Suite -# Purpose: Test GDAL functions not bound SWIG with ctypes -# Author: Even Rouault, -# -############################################################################### -# Copyright (c) 2011-2012, Even Rouault -# -# Permission is hereby granted, free of charge, to any person obtaining a -# copy of this software and associated documentation files (the "Software"), -# to deal in the Software without restriction, including without limitation -# the rights to use, copy, modify, merge, publish, distribute, sublicense, -# and/or sell copies of the Software, and to permit persons to whom the -# Software is furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included -# in all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -# DEALINGS IN THE SOFTWARE. -############################################################################### - -from osgeo import gdal -import sys -import os -from sys import version_info - -sys.path.append( '../pymod' ) - -try: - import ctypes -except: - pass - -import gdaltest - -gdal_handle_init = False -gdal_handle = None -gdal_handle_stdcall = None - -############################################################################### -# find_libgdal() - -def find_libgdal(): - return gdaltest.find_lib('gdal') - -############################################################################### -# Init - -def testnonboundtoswig_init(): - - global gdal_handle_init, gdal_handle, gdal_handle_stdcall - - if gdal_handle_init: - if gdal_handle is None: - return 'skip' - return 'success' - - gdal_handle_init = True - - try: - ctypes.cdll - except: - print('cannot find ctypes') - return 'skip' - - name = find_libgdal() - if name is None: - return 'skip' - - print('Found libgdal we are running against : %s' % name) - - static_version = gdal.VersionInfo(None) - short_static_version = static_version[0:2] - - try: - gdal_handle = ctypes.cdll.LoadLibrary(name) - try: - gdal_handle_stdcall = ctypes.windll.LoadLibrary(name) - except: - gdal_handle_stdcall = gdal_handle - - gdal_handle_stdcall.GDALVersionInfo.argtypes = [ ctypes.c_char_p ] - gdal_handle_stdcall.GDALVersionInfo.restype = ctypes.c_char_p - - dynamic_version = gdal_handle_stdcall.GDALVersionInfo(None) - if version_info >= (3,0,0): - dynamic_version = str(dynamic_version, 'utf-8') - - if dynamic_version != static_version: - gdaltest.post_reason('dynamic version(%s) does not match static version (%s)' % (dynamic_version, static_version)) - gdal_handle = None - gdal_handle_stdcall = None - return 'skip' - - return 'success' - except: - print('cannot find gdal shared object') - return 'skip' - -############################################################################### -# Call GDALDestroyDriverManager() - -def GDALDestroyDriverManager(): - - if gdal_handle is None: - testnonboundtoswig_init() - - if gdal_handle is None: - return 'skip' - - gdal_handle_stdcall.GDALDestroyDriverManager.argtypes = [ ] - gdal_handle_stdcall.GDALDestroyDriverManager.restype = None - - gdal_handle_stdcall.GDALDestroyDriverManager() - - return 'success' - -############################################################################### -# Call OGRCleanupAll() - -def OGRCleanupAll(): - - if gdal_handle is None: - testnonboundtoswig_init() - - if gdal_handle is None: - return 'skip' - - gdal_handle_stdcall.OGRCleanupAll.argtypes = [ ] - gdal_handle_stdcall.OGRCleanupAll.restype = None - - gdal_handle_stdcall.OGRCleanupAll() - - return 'success' - -############################################################################### -# Call OSRCleanup() - -def OSRCleanup(): - - if gdal_handle is None: - testnonboundtoswig_init() - - if gdal_handle is None: - return 'skip' - - gdal_handle.OSRCleanup.argtypes = [ ] - gdal_handle.OSRCleanup.restype = None - - gdal_handle.OSRCleanup() - - return 'success' - -############################################################################### -# Test GDALSimpleImageWarp - -def testnonboundtoswig_GDALSimpleImageWarp(): - - if gdal_handle is None: - return 'skip' - - src_ds = gdal.Open('data/byte.tif') - gt = src_ds.GetGeoTransform() - wkt = src_ds.GetProjectionRef() - src_ds = None - - gdal_handle_stdcall.GDALOpen.argtypes = [ ctypes.c_char_p, ctypes.c_int] - gdal_handle_stdcall.GDALOpen.restype = ctypes.c_void_p - - gdal_handle_stdcall.GDALClose.argtypes = [ ctypes.c_void_p ] - gdal_handle_stdcall.GDALClose.restype = None - - gdal_handle.GDALCreateGenImgProjTransformer2.restype = ctypes.c_void_p - gdal_handle.GDALCreateGenImgProjTransformer2.argtypes = [ ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p] - - gdal_handle_stdcall.GDALSimpleImageWarp.argtypes = [ ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p] - gdal_handle_stdcall.GDALSimpleImageWarp.restype = ctypes.c_int - - gdal_handle.GDALDestroyGenImgProjTransformer.argtypes = [ ctypes.c_void_p ] - gdal_handle.GDALDestroyGenImgProjTransformer.restype = None - - out_ds = gdal.GetDriverByName('GTiff').Create('/vsimem/out.tif', 20, 20, 1) - out_ds.SetGeoTransform(gt) - out_ds.SetProjection(wkt) - out_ds = None - - filename = 'data/byte.tif' - if version_info >= (3,0,0): - filename = bytes(filename, 'utf-8') - - native_in_ds = gdal_handle_stdcall.GDALOpen(filename, gdal.GA_ReadOnly) - if native_in_ds is None: - gdaltest.post_reason('fail') - return 'fail' - - filename = '/vsimem/out.tif' - if version_info >= (3,0,0): - filename = bytes(filename, 'utf-8') - - native_out_ds = gdal_handle_stdcall.GDALOpen(filename, gdal.GA_Update) - if native_out_ds is None: - gdaltest.post_reason('fail') - return 'fail' - - pTransformerArg = gdal_handle.GDALCreateGenImgProjTransformer2( native_in_ds, native_out_ds, None ) - if pTransformerArg is None: - gdaltest.post_reason('fail') - return 'fail' - - ret = gdal_handle_stdcall.GDALSimpleImageWarp(native_in_ds, native_out_ds, 0, None, gdal_handle_stdcall.GDALGenImgProjTransform, pTransformerArg, None, None, None) - if ret != 1: - gdaltest.post_reason('fail') - print(ret) - return 'fail' - - gdal_handle.GDALDestroyGenImgProjTransformer(pTransformerArg) - - gdal_handle_stdcall.GDALClose(native_in_ds) - gdal_handle_stdcall.GDALClose(native_out_ds) - - ds = gdal.Open('/vsimem/out.tif') - cs = ds.GetRasterBand(1).Checksum() - ds = None - - gdal.Unlink('/vsimem/out.tif') - - if cs != 4672: - gdaltest.post_reason('fail') - print(cs) - return 'fail' - - return 'success' - -############################################################################### -# Test VRT derived bands with callback functions implemented in Python! - -def GDALTypeToCTypes(gdaltype): - - if gdaltype == gdal.GDT_Byte: - return ctypes.c_ubyte - elif gdaltype == gdal.GDT_Int16: - return ctypes.c_short - elif gdaltype == gdal.GDT_UInt16: - return ctypes.c_ushort - elif gdaltype == gdal.GDT_Int32: - return ctypes.c_int - elif gdaltype == gdal.GDT_UInt32: - return ctypes.c_uint - elif gdaltype == gdal.GDT_Float32: - return ctypes.c_float - elif gdaltype == gdal.GDT_Float64: - return ctypes.c_double - else: - return None - -def my_pyDerivedPixelFunc(papoSources, nSources, pData, nBufXSize, nBufYSize, eSrcType, eBufType, nPixelSpace, nLineSpace): - if nSources != 1: - print(nSources) - gdaltest.post_reason('did not get expected nSources') - return 1 - - srcctype = GDALTypeToCTypes(eSrcType) - if srcctype is None: - print(eSrcType) - gdaltest.post_reason('did not get expected eSrcType') - return 1 - - dstctype = GDALTypeToCTypes(eBufType) - if dstctype is None: - print(eBufType) - gdaltest.post_reason('did not get expected eBufType') - return 1 - - if nPixelSpace != gdal.GetDataTypeSize(eBufType) / 8: - print(nPixelSpace) - gdaltest.post_reason('did not get expected nPixelSpace') - return 1 - - if (nLineSpace % nPixelSpace) != 0: - print(nLineSpace) - gdaltest.post_reason('did not get expected nLineSpace') - return 1 - - nLineStride = (int)(nLineSpace/nPixelSpace) - - srcValues = ctypes.cast(papoSources[0], ctypes.POINTER(srcctype)) - dstValues = ctypes.cast(pData, ctypes.POINTER(dstctype)) - for j in range(nBufYSize): - for i in range(nBufXSize): - dstValues[j * nLineStride + i] = srcValues[j * nBufXSize + i] - - return 0 - -def testnonboundtoswig_VRTDerivedBands(): - - if gdal_handle is None: - return 'skip' - - DerivedPixelFuncType = ctypes.CFUNCTYPE(ctypes.c_int, # ret CPLErr - ctypes.POINTER(ctypes.c_void_p), # void **papoSources - ctypes.c_int, # int nSources - ctypes.c_void_p, #void *pData - ctypes.c_int, #int nBufXSize - ctypes.c_int, #int nBufYSize - ctypes.c_int, # GDALDataType eSrcType - ctypes.c_int, # GDALDataType eBufType - ctypes.c_int, #int nPixelSpace - ctypes.c_int ) #int nLineSpace - - my_cDerivedPixelFunc = DerivedPixelFuncType(my_pyDerivedPixelFunc) - - #CPLErr CPL_DLL CPL_STDCALL GDALAddDerivedBandPixelFunc( const char *pszName, - # GDALDerivedPixelFunc pfnPixelFunc ); - - gdal_handle_stdcall.GDALAddDerivedBandPixelFunc.argtypes = [ ctypes.c_char_p, DerivedPixelFuncType] - gdal_handle_stdcall.GDALAddDerivedBandPixelFunc.restype = ctypes.c_int - - funcName = "pyDerivedPixelFunc" - if version_info >= (3,0,0): - funcName = bytes(funcName, 'utf-8') - ret = gdal_handle_stdcall.GDALAddDerivedBandPixelFunc(funcName, my_cDerivedPixelFunc) - if ret != 0: - gdaltest.post_reason('fail') - return 'fail' - - vrt_xml = """ - - pyDerivedPixelFunc - Byte - - data/byte.tif - 1 - - - - -""" - - src_ds = gdal.Open('data/byte.tif') - ref_cs = src_ds.GetRasterBand(1).Checksum() - ref_data = src_ds.GetRasterBand(1).ReadRaster(0,0,20,20) - src_ds = None - - ds = gdal.Open(vrt_xml) - got_cs = ds.GetRasterBand(1).Checksum() - got_data = ds.GetRasterBand(1).ReadRaster(0,0,20,20) - ds = None - - if ref_cs != got_cs: - gdaltest.post_reason('wrong checksum') - print(got_cs) - return 'fail' - - if ref_data != got_data: - gdaltest.post_reason('wrong data') - print(ref_data) - print(got_data) - return 'fail' - - return 'success' - -gdaltest_list = [ testnonboundtoswig_init, - testnonboundtoswig_GDALSimpleImageWarp, - testnonboundtoswig_VRTDerivedBands ] - -if __name__ == '__main__': - - gdaltest.setup_run( 'testnonboundtoswig' ) - - gdaltest.run_tests( gdaltest_list ) - - gdaltest.summarize() - diff --git a/gdal/frmts/vrt/pixelfunctions.licence b/gdal/frmts/vrt/pixelfunctions.licence deleted file mode 100644 index c1326585c5b6..000000000000 --- a/gdal/frmts/vrt/pixelfunctions.licence +++ /dev/null @@ -1,17 +0,0 @@ - Permission is hereby granted, free of charge, to any person obtaining a - copy of this software and associated documentation files (the "Software"), - to deal in the Software without restriction, including without limitation - the rights to use, copy, modify, merge, publish, distribute, sublicense, - and/or sell copies of the Software, and to permit persons to whom the - Software is furnished to do so, subject to the following conditions: - . - The above copyright notice and this permission notice shall be included - in all copies or substantial portions of the Software. - . - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - DEALINGS IN THE SOFTWARE. From bf13853fa317962de7505f5f3f43fab22100d502 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Wed, 6 Jul 2016 16:28:49 +0200 Subject: [PATCH 23/69] ENH: Remove code that only makes sense as part of a plugin (from PR review) --- autotest/gdrivers/pixfun.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/autotest/gdrivers/pixfun.py b/autotest/gdrivers/pixfun.py index b8402f4a9641..267c0dc60289 100644 --- a/autotest/gdrivers/pixfun.py +++ b/autotest/gdrivers/pixfun.py @@ -36,12 +36,6 @@ import gdaltest -# @TODO: remove in non-plugin configuration -#gdal.SetConfigOption('CPL_DEBUG', 'PIXFUN') -gdal.SetConfigOption('GDAL_DRIVER_PATH', '../..') -gdal.AllRegister() - - ############################################################################### # Verify real part extraction from a complex dataset. From 6f95e7ddd513ce064d3f487331a8add81abff81e Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Wed, 6 Jul 2016 16:32:51 +0200 Subject: [PATCH 24/69] ENH: Avoid ignoring c files (from PR review) --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index e433efa61b1b..599e63a78a14 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ -*.c *.pyc *.o *.so From 5e40669e7bbe738e66d1e4f0d7521c88197cbff3 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Wed, 6 Jul 2016 16:34:33 +0200 Subject: [PATCH 25/69] ENH: Update makefile.vc as well (from PR review) --- gdal/frmts/vrt/makefile.vc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gdal/frmts/vrt/makefile.vc b/gdal/frmts/vrt/makefile.vc index 442da649bb30..99a83150afd6 100644 --- a/gdal/frmts/vrt/makefile.vc +++ b/gdal/frmts/vrt/makefile.vc @@ -2,7 +2,7 @@ OBJ = vrtdataset.obj vrtrasterband.obj vrtdriver.obj \ vrtsources.obj vrtfilters.obj vrtsourcedrasterband.obj \ vrtrawrasterband.obj vrtderivedrasterband.obj vrtwarped.obj \ - vrtpansharpened.obj + vrtpansharpened.obj pixelfunctions.obj GDAL_ROOT = ..\.. From d9fd29b4bd8812d88621a6e41c5c1fd62269fae5 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Wed, 6 Jul 2016 16:44:23 +0200 Subject: [PATCH 26/69] DOC: Adding a section about default pixel functions in vrt tutorial (from PR review) --- gdal/frmts/vrt/vrt_tutorial.dox | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/gdal/frmts/vrt/vrt_tutorial.dox b/gdal/frmts/vrt/vrt_tutorial.dox index 5c294651feb6..d8dbc58da396 100644 --- a/gdal/frmts/vrt/vrt_tutorial.dox +++ b/gdal/frmts/vrt/vrt_tutorial.dox @@ -806,6 +806,30 @@ calling the pixel function, and the imaginary portion would be lost. ... \endcode +

Default Pixel Functions

+ +GDAL provides a set of default pixel functions that can be used without writing new code: + +
    +
  • "real": extract real part from a single raster band (just a copy if the input is non-complex) +
  • "imag": + extract imaginary part from a single raster band (0 for non-complex) +
  • "complex": make a complex band merging two bands used as real and imag values +
  • "mod": extract module from a single raster band (real or complex) +
  • "phase": extract phase from a single raster band (0 for non-complex) +
  • "conj": computes the complex conjugate of a single raster band (just a copy if the input is non-complex) +
  • "sum": sum 2 or more raster bands +
  • "diff": computes the difference between 2 raster bands (b1 - b2) +
  • "mul": multiply 2 or more raster bands +
  • "cmul": multiply the first band for the complex conjugate of the second +
  • "inv": inverse (1./x). Note: no check is performed on zero division +
  • "intensity": computes the intensity Re(x*conj(x)) of a single raster band (real or complex) +
  • "sqrt": perform the square root of a single raster band (real only) +
  • "log10": compute the logarithm (base 10) of the abs of a single raster band (real or complex): log10( abs( x ) ) +
  • "dB2amp": perform scale conversion from logarithmic to linear (amplitude) (i.e. 10 ^ ( x / 20 ) ) of a single raster band (real only) +
  • "dB2pow": perform scale conversion from logarithmic to linear (power) (i.e. 10 ^ ( x / 10 ) ) of a single raster band (real only) +
+

Writing Pixel Functions

To register this function with GDAL (prior to accessing any VRT datasets From 0e14ea18dbbccadc2b06d2f417b0f62fcd5c8d33 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Wed, 6 Jul 2016 17:03:40 +0200 Subject: [PATCH 27/69] COMP: Make pixelfunctions static, and solve link error --- gdal/frmts/vrt/gdal_vrt.h | 2 + gdal/frmts/vrt/pixelfunctions.c | 71 ++++++++++++++++----------------- gdal/frmts/vrt/vrtdriver.cpp | 3 -- 3 files changed, 36 insertions(+), 40 deletions(-) diff --git a/gdal/frmts/vrt/gdal_vrt.h b/gdal/frmts/vrt/gdal_vrt.h index ca8971547619..97bead0f9a97 100644 --- a/gdal/frmts/vrt/gdal_vrt.h +++ b/gdal/frmts/vrt/gdal_vrt.h @@ -46,6 +46,8 @@ CPL_C_START void GDALRegister_VRT(); +CPLErr CPL_STDCALL GDALRegisterDefaultPixelFunc(); + typedef CPLErr (*VRTImageReadFunc)( void *hCBData, int nXOff, int nYOff, int nXSize, int nYSize, diff --git a/gdal/frmts/vrt/pixelfunctions.c b/gdal/frmts/vrt/pixelfunctions.c index bbae89801658..ee5ca2b36d07 100644 --- a/gdal/frmts/vrt/pixelfunctions.c +++ b/gdal/frmts/vrt/pixelfunctions.c @@ -30,96 +30,93 @@ #include #include -CPLErr RealPixelFunc(void **papoSources, int nSources, void *pData, +static CPLErr RealPixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace); -CPLErr ImagPixelFunc(void **papoSources, int nSources, void *pData, +static CPLErr ImagPixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace); -CPLErr ComplexPixelFunc(void **papoSources, int nSources, void *pData, +static CPLErr ComplexPixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace); -CPLErr ModulePixelFunc(void **papoSources, int nSources, void *pData, +static CPLErr ModulePixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace); -CPLErr PhasePixelFunc(void **papoSources, int nSources, void *pData, +static CPLErr PhasePixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace); -CPLErr ConjPixelFunc(void **papoSources, int nSources, void *pData, +static CPLErr ConjPixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace); -CPLErr SumPixelFunc(void **papoSources, int nSources, void *pData, +static CPLErr SumPixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace); -CPLErr DiffPixelFunc(void **papoSources, int nSources, void *pData, +static CPLErr DiffPixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace); -CPLErr MulPixelFunc(void **papoSources, int nSources, void *pData, +static CPLErr MulPixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace); -CPLErr CMulPixelFunc(void **papoSources, int nSources, void *pData, +static CPLErr CMulPixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace); -CPLErr InvPixelFunc(void **papoSources, int nSources, void *pData, +static CPLErr InvPixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace); -CPLErr IntensityPixelFunc(void **papoSources, int nSources, void *pData, +static CPLErr IntensityPixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace); -CPLErr SqrtPixelFunc(void **papoSources, int nSources, void *pData, +static CPLErr SqrtPixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace); -CPLErr Log10PixelFunc(void **papoSources, int nSources, void *pData, +static CPLErr Log10PixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace); -CPLErr dB2AmpPixelFunc(void **papoSources, int nSources, void *pData, +static CPLErr dB2AmpPixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace); -CPLErr dB2PowPixelFunc(void **papoSources, int nSources, void *pData, +static CPLErr dB2PowPixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace); -CPLErr PowPixelFuncHelper(void **papoSources, int nSources, void *pData, +static CPLErr PowPixelFuncHelper(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace, double base, double fact); -CPLErr CPL_STDCALL GDALRegisterDefaultPixelFunc(); - - -CPLErr RealPixelFunc(void **papoSources, int nSources, void *pData, +static CPLErr RealPixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace) @@ -145,7 +142,7 @@ CPLErr RealPixelFunc(void **papoSources, int nSources, void *pData, } /* RealPixelFunc */ -CPLErr ImagPixelFunc(void **papoSources, int nSources, void *pData, +static CPLErr ImagPixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace) @@ -187,7 +184,7 @@ CPLErr ImagPixelFunc(void **papoSources, int nSources, void *pData, } /* ImagPixelFunc */ -CPLErr ComplexPixelFunc(void **papoSources, int nSources, void *pData, +static CPLErr ComplexPixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace) @@ -220,7 +217,7 @@ CPLErr ComplexPixelFunc(void **papoSources, int nSources, void *pData, } /* MakeComplexPixelFunc */ -CPLErr ModulePixelFunc(void **papoSources, int nSources, void *pData, +static CPLErr ModulePixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace) @@ -273,7 +270,7 @@ CPLErr ModulePixelFunc(void **papoSources, int nSources, void *pData, } /* ModulePixelFunc */ -CPLErr PhasePixelFunc(void **papoSources, int nSources, void *pData, +static CPLErr PhasePixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace) @@ -339,7 +336,7 @@ CPLErr PhasePixelFunc(void **papoSources, int nSources, void *pData, } /* PhasePixelFunc */ -CPLErr ConjPixelFunc(void **papoSources, int nSources, void *pData, +static CPLErr ConjPixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace) @@ -379,7 +376,7 @@ CPLErr ConjPixelFunc(void **papoSources, int nSources, void *pData, } /* ConjPixelFunc */ -CPLErr SumPixelFunc(void **papoSources, int nSources, void *pData, +static CPLErr SumPixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace) @@ -442,7 +439,7 @@ CPLErr SumPixelFunc(void **papoSources, int nSources, void *pData, } /* SumPixelFunc */ -CPLErr DiffPixelFunc(void **papoSources, int nSources, void *pData, +static CPLErr DiffPixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace) @@ -501,7 +498,7 @@ CPLErr DiffPixelFunc(void **papoSources, int nSources, void *pData, } /* DiffPixelFunc */ -CPLErr MulPixelFunc(void **papoSources, int nSources, void *pData, +static CPLErr MulPixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace) @@ -570,7 +567,7 @@ CPLErr MulPixelFunc(void **papoSources, int nSources, void *pData, } /* MulPixelFunc */ -CPLErr CMulPixelFunc(void **papoSources, int nSources, void *pData, +static CPLErr CMulPixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace) @@ -630,7 +627,7 @@ CPLErr CMulPixelFunc(void **papoSources, int nSources, void *pData, } /* CMulPixelFunc */ -CPLErr InvPixelFunc(void **papoSources, int nSources, void *pData, +static CPLErr InvPixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace) @@ -687,7 +684,7 @@ CPLErr InvPixelFunc(void **papoSources, int nSources, void *pData, } /* InvPixelFunc */ -CPLErr IntensityPixelFunc(void **papoSources, int nSources, void *pData, +static CPLErr IntensityPixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace) @@ -741,7 +738,7 @@ CPLErr IntensityPixelFunc(void **papoSources, int nSources, void *pData, } /* IntensityPixelFunc */ -CPLErr SqrtPixelFunc(void **papoSources, int nSources, void *pData, +static CPLErr SqrtPixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace) @@ -771,7 +768,7 @@ CPLErr SqrtPixelFunc(void **papoSources, int nSources, void *pData, } /* SqrtPixelFunc */ -CPLErr Log10PixelFunc(void **papoSources, int nSources, void *pData, +static CPLErr Log10PixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace) @@ -827,7 +824,7 @@ CPLErr Log10PixelFunc(void **papoSources, int nSources, void *pData, } /* Log10PixelFunc */ -CPLErr PowPixelFuncHelper(void **papoSources, int nSources, void *pData, +static CPLErr PowPixelFuncHelper(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace, @@ -858,7 +855,7 @@ CPLErr PowPixelFuncHelper(void **papoSources, int nSources, void *pData, return CE_None; } /* PowPixelFuncHelper */ -CPLErr dB2AmpPixelFunc(void **papoSources, int nSources, void *pData, +static CPLErr dB2AmpPixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace) @@ -869,7 +866,7 @@ CPLErr dB2AmpPixelFunc(void **papoSources, int nSources, void *pData, } /* dB2AmpPixelFunc */ -CPLErr dB2PowPixelFunc(void **papoSources, int nSources, void *pData, +static CPLErr dB2PowPixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace) diff --git a/gdal/frmts/vrt/vrtdriver.cpp b/gdal/frmts/vrt/vrtdriver.cpp index 99d752671ec3..2045dc78f817 100644 --- a/gdal/frmts/vrt/vrtdriver.cpp +++ b/gdal/frmts/vrt/vrtdriver.cpp @@ -37,9 +37,6 @@ CPL_CVSID("$Id$"); -// Required to register pixel functions -extern CPLErr CPL_STDCALL GDALRegisterDefaultPixelFunc(); - /************************************************************************/ /* VRTDriver() */ From 07eae9f5876adb4b428a756e386e9485bc269771 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Thu, 7 Jul 2016 09:14:39 +0200 Subject: [PATCH 28/69] ENH: Add derived dataset to GDALmake.opt.in --- gdal/GDALmake.opt.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gdal/GDALmake.opt.in b/gdal/GDALmake.opt.in index 81a21056ace4..9c20e97b4356 100644 --- a/gdal/GDALmake.opt.in +++ b/gdal/GDALmake.opt.in @@ -562,7 +562,7 @@ GDAL_FORMATS += nitf bmp airsar rs2 ilwis rmf leveller sgi srtmhgt GDAL_FORMATS += idrisi gsg ingr ers jaxapalsar dimap gff cosar pds adrg GDAL_FORMATS += coasp tsx terragen blx msgn til r northwood saga xyz hf2 GDAL_FORMATS += kmlsuperoverlay ctg e00grid zmap ngsgeoid iris map cals -GDAL_FORMATS += safe sentinel2 +GDAL_FORMATS += safe sentinel2 cderived GDAL_FORMATS += @OPT_GDAL_FORMATS@ ifneq ($(PCIDSK_SETTING),no) From f2641693bbe39976c51d75d469f743803ac7cf27 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Thu, 7 Jul 2016 09:31:52 +0200 Subject: [PATCH 29/69] ENH: Remove hard-coded pixel function now that we have them within gdal --- gdal/frmts/cderived/cderiveddataset.cpp | 56 +------------------------ 1 file changed, 1 insertion(+), 55 deletions(-) diff --git a/gdal/frmts/cderived/cderiveddataset.cpp b/gdal/frmts/cderived/cderiveddataset.cpp index f766c043a6f1..652af05e0e9a 100644 --- a/gdal/frmts/cderived/cderiveddataset.cpp +++ b/gdal/frmts/cderived/cderiveddataset.cpp @@ -4,58 +4,6 @@ #include -CPLErr ModulePixelFunc(void **papoSources, int nSources, void *pData, - int nXSize, int nYSize, - GDALDataType eSrcType, GDALDataType eBufType, - int nPixelSpace, int nLineSpace) -{ - int ii, iLine, iCol; - double dfPixVal; - - /* ---- Init ---- */ - if (nSources != 1) return CE_Failure; - - if (GDALDataTypeIsComplex( eSrcType )) - { - double dfReal, dfImag; - void *pReal = papoSources[0]; - void *pImag = ((GByte *)papoSources[0]) - + GDALGetDataTypeSize( eSrcType ) / 8 / 2; - - /* ---- Set pixels ---- */ - for( iLine = 0, ii = 0; iLine < nYSize; ++iLine ) { - for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { - - /* Source raster pixels may be obtained with SRCVAL macro */ - dfReal = SRCVAL(pReal, eSrcType, ii); - dfImag = SRCVAL(pImag, eSrcType, ii); - - dfPixVal = sqrt( dfReal * dfReal + dfImag * dfImag ); - - GDALCopyWords(&dfPixVal, GDT_Float64, 0, - ((GByte *)pData) + nLineSpace * iLine + - iCol * nPixelSpace, eBufType, nPixelSpace, 1); - } - } - } else { - /* ---- Set pixels ---- */ - for( iLine = 0, ii = 0; iLine < nYSize; ++iLine ) { - for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { - - /* Source raster pixels may be obtained with SRCVAL macro */ - dfPixVal = fabs(SRCVAL(papoSources[0], eSrcType, ii)); - - GDALCopyWords(&dfPixVal, GDT_Float64, 0, - ((GByte *)pData) + nLineSpace * iLine + - iCol * nPixelSpace, eBufType, nPixelSpace, 1); - } - } - } - - /* ---- Return success ---- */ - return CE_None; -} /* ModulePixelFunc */ - class ComplexDerivedDatasetContainer: public GDALPamDataset { @@ -121,9 +69,7 @@ GDALDataset * ComplexDerivedDataset::Open(GDALOpenInfo * poOpenInfo) /* Unable to Open in this case */ return NULL; } - - VRTDerivedRasterBand::AddPixelFunction("mod",ModulePixelFunc); - + CPLString odFilename = filename.substr(alg_pos+1,filename.size() - alg_pos); GDALDataset * poTmpDS = (GDALDataset*)GDALOpen(odFilename, GA_ReadOnly); From ec88c34a6294851f88d0127d748fae866ba4732a Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Thu, 7 Jul 2016 10:50:12 +0200 Subject: [PATCH 30/69] ENH: Support several types of derived datasets, all declared in one place --- gdal/frmts/cderived/cderiveddataset.cpp | 49 ++++++++++++++----------- gdal/frmts/cderived/derivedlist.h | 25 +++++++++++++ gdal/gcore/gdaldataset.cpp | 15 ++++++-- 3 files changed, 63 insertions(+), 26 deletions(-) create mode 100644 gdal/frmts/cderived/derivedlist.h diff --git a/gdal/frmts/cderived/cderiveddataset.cpp b/gdal/frmts/cderived/cderiveddataset.cpp index 652af05e0e9a..c63d2000f535 100644 --- a/gdal/frmts/cderived/cderiveddataset.cpp +++ b/gdal/frmts/cderived/cderiveddataset.cpp @@ -1,10 +1,10 @@ #include "../vrt/vrtdataset.h" #include "gdal_pam.h" #include "gdal_proxy.h" +#include "derivedlist.h" #include - class ComplexDerivedDatasetContainer: public GDALPamDataset { public: @@ -19,7 +19,6 @@ class ComplexDerivedDataset : public VRTDataset ~ComplexDerivedDataset(); static GDALDataset *Open( GDALOpenInfo * ); - static int Identify( GDALOpenInfo * ); }; @@ -33,26 +32,11 @@ ComplexDerivedDataset::~ComplexDerivedDataset() { } -int ComplexDerivedDataset::Identify(GDALOpenInfo * poOpenInfo) -{ - if(STARTS_WITH_CI(poOpenInfo->pszFilename, "DERIVED_SUBDATASET:COMPLEX_AMPLITUDE:")) - return TRUE; - - return FALSE; -} - GDALDataset * ComplexDerivedDataset::Open(GDALOpenInfo * poOpenInfo) -{ - if( !Identify(poOpenInfo) ) - { - return NULL; - } - +{ /* Try to open original dataset */ CPLString filename(poOpenInfo->pszFilename); - - // TODO: check starts with - + /* DERIVED_SUBDATASET should be first domain */ size_t dsds_pos = filename.find("DERIVED_SUBDATASET:"); @@ -62,14 +46,35 @@ GDALDataset * ComplexDerivedDataset::Open(GDALOpenInfo * poOpenInfo) return NULL; } - /* DERIVED_SUBDATASET should be first domain */ + /* Next, we need to now which derived dataset to compute */ size_t alg_pos = filename.find(":",dsds_pos+20); if (alg_pos == std::string::npos) { - /* Unable to Open in this case */ + /* Unable to Open if we do not find the name of the derived dataset */ return NULL; } - + + CPLString odDerivedName = filename.substr(dsds_pos+19,alg_pos-dsds_pos-19); + + CPLDebug("ComplexDerivedDataset::Open","Derived dataset identified: %s",odDerivedName.c_str()); + + CPLString pixelFunctionName = ""; + bool datasetFound = false; + + for(unsigned int derivedId = 0; derivedIdClear(); + + // First condition: at least one raster band if(GetRasterCount()>0) { - papoDerivedMetadataList->SetNameValue("DERIVED_SUBDATASET_1_NAME",CPLSPrintf("DERIVED_SUBDATASET:COMPLEX_AMPLITUDE:%s",GetDescription())); - - CPLString osDesc(CPLSPrintf("Complex amplitude of bands from %s",GetDescription())); - papoDerivedMetadataList->SetNameValue("DERIVED_SUBDATASET_1_DESC",osDesc.c_str()); + CPLDebug("GDALDataset::GetMetadata","Number of derived datasets to report: %i",(int)NB_DERIVED_DATASETS); + for(unsigned int derivedId = 0; derivedIdSetNameValue(CPLSPrintf("DERIVED_SUBDATASET_%i_NAME",derivedId),CPLSPrintf("DERIVED_SUBDATASET:%s:%s",asDDSDesc[derivedId].pszDatasetName,GetDescription())); + + CPLString osDesc(CPLSPrintf("%s from %s",asDDSDesc[derivedId].pszDatasetDescritpion,GetDescription())); + papoDerivedMetadataList->SetNameValue(CPLSPrintf("DERIVED_SUBDATASET_%i_DESC",derivedId),osDesc.c_str()); + } } return papoDerivedMetadataList->List(); From 97124fc8c8433efb41721cf16641f221635117b2 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Thu, 7 Jul 2016 10:53:46 +0200 Subject: [PATCH 31/69] DOC: Add copyright notices --- gdal/frmts/cderived/cderiveddataset.cpp | 27 +++++++++++++++++++++ gdal/frmts/cderived/derivedlist.h | 31 +++++++++++++++++++++++-- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/gdal/frmts/cderived/cderiveddataset.cpp b/gdal/frmts/cderived/cderiveddataset.cpp index c63d2000f535..971ae4559596 100644 --- a/gdal/frmts/cderived/cderiveddataset.cpp +++ b/gdal/frmts/cderived/cderiveddataset.cpp @@ -1,3 +1,30 @@ +/****************************************************************************** + * + * Project: GDAL + * Purpose: Implementation of derived subdatasets + * Author: Julien Michel + * + ****************************************************************************** + * Copyright (c) 2008-2014 Antonio Valentino + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + *****************************************************************************/ #include "../vrt/vrtdataset.h" #include "gdal_pam.h" #include "gdal_proxy.h" diff --git a/gdal/frmts/cderived/derivedlist.h b/gdal/frmts/cderived/derivedlist.h index da929a08f68b..8b68b325742d 100644 --- a/gdal/frmts/cderived/derivedlist.h +++ b/gdal/frmts/cderived/derivedlist.h @@ -1,5 +1,32 @@ -#ifndef DERIVEDLIST_H_INCLUDEd -#define DERIVEDLIST_H_INCLUDEd +/****************************************************************************** + * + * Project: GDAL + * Purpose: Implementation of derived subdatasets + * Author: Julien Michel + * + ****************************************************************************** + * Copyright (c) 2008-2014 Antonio Valentino + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + *****************************************************************************/ +#ifndef DERIVEDLIST_H_INCLUDED +#define DERIVEDLIST_H_INCLUDED typedef struct { From f7fbcc290eba3676f959c8354a46742b10b39ed8 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Thu, 7 Jul 2016 10:59:32 +0200 Subject: [PATCH 32/69] BUG: Use the correct pixel function --- gdal/frmts/cderived/cderiveddataset.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gdal/frmts/cderived/cderiveddataset.cpp b/gdal/frmts/cderived/cderiveddataset.cpp index 971ae4559596..368114fcdef3 100644 --- a/gdal/frmts/cderived/cderiveddataset.cpp +++ b/gdal/frmts/cderived/cderiveddataset.cpp @@ -83,13 +83,13 @@ GDALDataset * ComplexDerivedDataset::Open(GDALOpenInfo * poOpenInfo) CPLString odDerivedName = filename.substr(dsds_pos+19,alg_pos-dsds_pos-19); - CPLDebug("ComplexDerivedDataset::Open","Derived dataset identified: %s",odDerivedName.c_str()); + CPLDebug("ComplexDerivedDataset::Open","Derived dataset requested: %s",odDerivedName.c_str()); CPLString pixelFunctionName = ""; bool datasetFound = false; for(unsigned int derivedId = 0; derivedIdSetBand(nBand,poBand); - poBand->SetPixelFunctionName("mod"); + poBand->SetPixelFunctionName(pixelFunctionName); poBand->SetSourceTransferType(poTmpDS->GetRasterBand(nBand)->GetRasterDataType()); GDALProxyPoolDataset* proxyDS; From 318ed3d23fd75be42a8b48624d24449f8adc93cf Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Thu, 7 Jul 2016 11:02:41 +0200 Subject: [PATCH 33/69] COMP: Adding make file for windows --- gdal/frmts/cderived/makefile.vc | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 gdal/frmts/cderived/makefile.vc diff --git a/gdal/frmts/cderived/makefile.vc b/gdal/frmts/cderived/makefile.vc new file mode 100644 index 000000000000..5fd83953a1ee --- /dev/null +++ b/gdal/frmts/cderived/makefile.vc @@ -0,0 +1,13 @@ + +OBJ = cderiveddataset.obj + +GDAL_ROOT = ..\.. + +!INCLUDE $(GDAL_ROOT)\nmake.opt + +default: $(OBJ) + xcopy /D /Y *.obj ..\o + +clean: + -del *.obj + From 66e338857047d508dc4ca3f782d57e96b95c81c3 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Thu, 7 Jul 2016 11:06:30 +0200 Subject: [PATCH 34/69] COMP: Add gdal_vrt.h include to get declaration of prototype for GDALRegisterDefaultPixelFunc --- gdal/frmts/vrt/pixelfunctions.c | 1 + 1 file changed, 1 insertion(+) diff --git a/gdal/frmts/vrt/pixelfunctions.c b/gdal/frmts/vrt/pixelfunctions.c index ee5ca2b36d07..45a20913a56c 100644 --- a/gdal/frmts/vrt/pixelfunctions.c +++ b/gdal/frmts/vrt/pixelfunctions.c @@ -29,6 +29,7 @@ #include #include +#include static CPLErr RealPixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, From 8783995a215fd09a4bdd785f94580dd2203b8713 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Thu, 7 Jul 2016 11:25:12 +0200 Subject: [PATCH 35/69] COMP: Fix include of gdal_vrt.h --- gdal/frmts/vrt/pixelfunctions.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gdal/frmts/vrt/pixelfunctions.c b/gdal/frmts/vrt/pixelfunctions.c index 45a20913a56c..0dd7a44a9e65 100644 --- a/gdal/frmts/vrt/pixelfunctions.c +++ b/gdal/frmts/vrt/pixelfunctions.c @@ -29,7 +29,7 @@ #include #include -#include +#include "gdal_vrt.h" static CPLErr RealPixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, From 21620491c7c5d1a626f6053cc3f0210603265ce3 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Thu, 7 Jul 2016 11:29:19 +0200 Subject: [PATCH 36/69] TEST: Adding simple opening tests for cderived datasets --- autotest/gdrivers/cderived.py | 94 +++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100755 autotest/gdrivers/cderived.py diff --git a/autotest/gdrivers/cderived.py b/autotest/gdrivers/cderived.py new file mode 100755 index 000000000000..be3b394010e7 --- /dev/null +++ b/autotest/gdrivers/cderived.py @@ -0,0 +1,94 @@ +#!/usr/bin/env python +############################################################################### +# $Id$ +# +# Project: GDAL/OGR Test Suite +# Purpose: Test cderived driver +# Author: Julien Michel +# +############################################################################### +# Copyright (c) 2016, Julien Michel, +# +# Permission is hereby granted, free of charge, to any person obtaining a +# copy of this software and associated documentation files (the "Software"), +# to deal in the Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, distribute, sublicense, +# and/or sell copies of the Software, and to permit persons to whom the +# Software is furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included +# in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +# DEALINGS IN THE SOFTWARE. +############################################################################### + +import os +import sys +from osgeo import gdal + +sys.path.append( '../pymod' ) + +import gdaltest + +############################################################################### +# Test opening a L1C product + + +def cderived_test(): + filename = "data/cfloat64.tif" + gdal.ErrorReset() + ds = gdal.Open(filename) + if ds is None or gdal.GetLastErrorMsg() != '': + gdaltest.post_reason('fail') + return 'fail' + got_dsds = ds.GetMetadata('DERIVED_SUBDATASETS') + + expected_dsds = {'DERIVED_SUBDATASET_0_NAME' : 'DERIVED_SUBDATASET:AMPLITUDE:data/cfloat64.tif', + 'DERIVED_SUBDATASET_0_DESC' : 'Amplitude of input bands from data/cfloat64.tif', + 'DERIVED_SUBDATASET_1_NAME' : 'DERIVED_SUBDATASET:PHASE:data/cfloat64.tif', + 'DERIVED_SUBDATASET_1_DESC' : 'Phase of input bands from data/cfloat64.tif', + 'DERIVED_SUBDATASET_2_NAME' : 'DERIVED_SUBDATASET:REAL:data/cfloat64.tif', + 'DERIVED_SUBDATASET_2_DESC' : 'Real part of input bands from data/cfloat64.tif', + 'DERIVED_SUBDATASET_3_NAME' : 'DERIVED_SUBDATASET:IMAG:data/cfloat64.tif', + 'DERIVED_SUBDATASET_3_DESC' : 'Imaginary part of input bands from data/cfloat64.tif', + 'DERIVED_SUBDATASET_4_NAME' : 'DERIVED_SUBDATASET:CONJ:data/cfloat64.tif', + 'DERIVED_SUBDATASET_4_DESC' : 'Conjugate of input bands from data/cfloat64.tif', + 'DERIVED_SUBDATASET_5_NAME' : 'DERIVED_SUBDATASET:INTENSITY:data/cfloat64.tif', + 'DERIVED_SUBDATASET_5_DESC' : 'Intensity (squared amplitude) of input bands from data/cfloat64.tif', + 'DERIVED_SUBDATASET_6_NAME' : 'DERIVED_SUBDATASET:LOGAMPLITUDE:data/cfloat64.tif', + 'DERIVED_SUBDATASET_6_DESC' : 'log10 of amplitude of input bands from data/cfloat64.tif'} + + if got_dsds != expected_dsds: + gdaltest.post_reason('fail') + import pprint + pprint.pprint(got_dsds) + return 'fail' + + + for (key,val) in expected_dsds.iteritems(): + if key.endswith('_NAME'): + ds = gdal.Open(val) + if ds is None or gdal.GetLastErrorMsg() != '': + gdaltest.post_reason('fail') + return 'fail' + + return 'success' + + +gdaltest_list = [ + cderived_test + ] + +if __name__ == '__main__': + + gdaltest.setup_run( 'cderived' ) + + gdaltest.run_tests( gdaltest_list ) + + gdaltest.summarize() From de301c3e17bd3a6df7d048941bed2a46f6e084bd Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Thu, 7 Jul 2016 11:39:52 +0200 Subject: [PATCH 37/69] TEST: Make numpy dependency optional (from PR review) --- autotest/gdrivers/pixfun.py | 117 +++++++++++++++++++----------------- 1 file changed, 63 insertions(+), 54 deletions(-) diff --git a/autotest/gdrivers/pixfun.py b/autotest/gdrivers/pixfun.py index 267c0dc60289..707a7abcfbde 100644 --- a/autotest/gdrivers/pixfun.py +++ b/autotest/gdrivers/pixfun.py @@ -29,7 +29,13 @@ ############################################################################### import sys -import numpy + +try: + import numpy + numpy_available = True +except ImportError: + numpy_available = False + from osgeo import gdal sys.path.append('../pymod') @@ -55,7 +61,7 @@ def pixfun_real_c(): return 'fail' refdata = refds.GetRasterBand(1).ReadAsArray() - if not numpy.alltrue(data == refdata.real): + if numpy_available and not numpy.alltrue(data == refdata.real): return 'fail' return 'success' @@ -80,7 +86,7 @@ def pixfun_real_r(): return 'fail' refdata = refds.GetRasterBand(1).ReadAsArray() - if not numpy.alltrue(data == refdata.real): + if numpy_available and not numpy.alltrue(data == refdata.real): return 'fail' return 'success' @@ -105,7 +111,7 @@ def pixfun_imag_c(): return 'fail' refdata = refds.GetRasterBand(1).ReadAsArray() - if not numpy.alltrue(data == refdata.imag): + if numpy_available and not numpy.alltrue(data == refdata.imag): return 'fail' return 'success' @@ -123,7 +129,7 @@ def pixfun_imag_r(): return 'fail' data = ds.GetRasterBand(1).ReadAsArray() - if not numpy.alltrue(data == 0): + if numpy_available and not numpy.alltrue(data == 0): return 'fail' return 'success' @@ -148,7 +154,7 @@ def pixfun_complex(): return 'fail' refdata = refds.GetRasterBand(1).ReadAsArray() - if not numpy.allclose(data, refdata + 1j * refdata): + if numpy_available and not numpy.allclose(data, refdata + 1j * refdata): return 'fail' return 'success' @@ -173,7 +179,7 @@ def pixfun_mod_c(): return 'fail' refdata = refds.GetRasterBand(1).ReadAsArray() - if not numpy.alltrue(data == numpy.abs(refdata)): + if numpy_available and not numpy.alltrue(data == numpy.abs(refdata)): return 'fail' return 'success' @@ -198,7 +204,7 @@ def pixfun_mod_r(): return 'fail' refdata = refds.GetRasterBand(1).ReadAsArray() - if not numpy.alltrue(data == numpy.abs(refdata)): + if numpy_available and not numpy.alltrue(data == numpy.abs(refdata)): return 'fail' return 'success' @@ -225,7 +231,7 @@ def pixfun_phase_c(): refdata = refdata.astype('complex128') #if not numpy.allclose(data, numpy.arctan2(refdata.imag, refdata.real)): - if not numpy.alltrue(data == numpy.arctan2(refdata.imag, refdata.real)): + if numpy_available and not numpy.alltrue(data == numpy.arctan2(refdata.imag, refdata.real)): return 'fail' return 'success' @@ -250,7 +256,7 @@ def pixfun_phase_r(): return 'fail' refdata = refds.GetRasterBand(1).ReadAsArray() - if not numpy.alltrue(data == numpy.arctan2(0, refdata)): + if numpy_available and not numpy.alltrue(data == numpy.arctan2(0, refdata)): return 'fail' return 'success' @@ -275,7 +281,7 @@ def pixfun_conj_c(): return 'fail' refdata = refds.GetRasterBand(1).ReadAsArray() - if not numpy.alltrue(data == numpy.conj(refdata)): + if numpy_available and not numpy.alltrue(data == numpy.conj(refdata)): return 'fail' return 'success' @@ -300,7 +306,7 @@ def pixfun_conj_r(): return 'fail' refdata = refds.GetRasterBand(1).ReadAsArray() - if not numpy.alltrue(data == numpy.conj(refdata)): + if numpy_available and not numpy.alltrue(data == numpy.conj(refdata)): return 'fail' return 'success' @@ -318,17 +324,18 @@ def pixfun_sum_r(): return 'fail' data = ds.GetRasterBand(1).ReadAsArray() - refdata = numpy.zeros(data.shape, 'float') - for reffilename in ('data/uint16.tif', 'data/int32.tif', - 'data/float32.tif'): - refds = gdal.Open(reffilename) - if refds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) + if numpy_available: + refdata = numpy.zeros(data.shape, 'float') + for reffilename in ('data/uint16.tif', 'data/int32.tif', + 'data/float32.tif'): + refds = gdal.Open(reffilename) + if refds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) + return 'fail' + refdata += refds.GetRasterBand(1).ReadAsArray() + + if not numpy.alltrue(data == refdata): return 'fail' - refdata += refds.GetRasterBand(1).ReadAsArray() - - if not numpy.alltrue(data == refdata): - return 'fail' return 'success' @@ -345,17 +352,18 @@ def pixfun_sum_c(): return 'fail' data = ds.GetRasterBand(1).ReadAsArray() - refdata = numpy.zeros(data.shape, 'complex') - for reffilename in ('data/uint16.tif', 'data/cint_sar.tif', - 'data/cfloat64.tif'): - refds = gdal.Open(reffilename) - if refds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) - return 'fail' + if numpy_available: + refdata = numpy.zeros(data.shape, 'complex') + for reffilename in ('data/uint16.tif', 'data/cint_sar.tif', + 'data/cfloat64.tif'): + refds = gdal.Open(reffilename) + if refds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) + return 'fail' refdata += refds.GetRasterBand(1).ReadAsArray(0, 0, 5, 6) - if not numpy.alltrue(data == refdata): - return 'fail' + if not numpy.alltrue(data == refdata): + return 'fail' return 'success' @@ -386,7 +394,7 @@ def pixfun_diff_r(): return 'fail' refdata2 = refds.GetRasterBand(1).ReadAsArray(10, 10, 5, 6) - if not numpy.alltrue(data == refdata1-refdata2): + if numpy_available and not numpy.alltrue(data == refdata1-refdata2): return 'fail' return 'success' @@ -418,7 +426,7 @@ def pixfun_diff_c(): return 'fail' refdata2 = refds.GetRasterBand(1).ReadAsArray(0, 0, 5, 6) - if not numpy.alltrue(data == refdata1-refdata2): + if numpy_available and not numpy.alltrue(data == refdata1-refdata2): return 'fail' return 'success' @@ -436,17 +444,18 @@ def pixfun_mul_r(): return 'fail' data = ds.GetRasterBand(1).ReadAsArray() - refdata = numpy.ones(data.shape, 'float') - for reffilename in ('data/uint16.tif', 'data/int32.tif', - 'data/float32.tif'): - refds = gdal.Open(reffilename) - if refds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) - return 'fail' + if numpy_available: + refdata = numpy.ones(data.shape, 'float') + for reffilename in ('data/uint16.tif', 'data/int32.tif', + 'data/float32.tif'): + refds = gdal.Open(reffilename) + if refds is None: + gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) + return 'fail' refdata *= refds.GetRasterBand(1).ReadAsArray() - if not numpy.alltrue(data == refdata): - return 'fail' + if not numpy.alltrue(data == refdata): + return 'fail' return 'success' @@ -470,7 +479,7 @@ def pixfun_mul_c(): return 'fail' refdata = refds.GetRasterBand(1).ReadAsArray() - if not numpy.alltrue(data == refdata*refdata): + if numpy_available and not numpy.alltrue(data == refdata*refdata): return 'fail' return 'success' @@ -495,7 +504,7 @@ def pixfun_cmul_c(): return 'fail' refdata = refds.GetRasterBand(1).ReadAsArray() - if not numpy.alltrue(data == refdata*refdata.conj()): + if numpy_available and not numpy.alltrue(data == refdata*refdata.conj()): return 'fail' return 'success' @@ -529,7 +538,7 @@ def pixfun_cmul_r(): refdata2 = refds.GetRasterBand(1).ReadAsArray() refdata2 = refdata2.astype('float64') - if not numpy.alltrue(data == refdata1 * refdata2.conj()): + if numpy_available and not numpy.alltrue(data == refdata1 * refdata2.conj()): return 'fail' return 'success' @@ -555,7 +564,7 @@ def pixfun_inv_r(): refdata = refds.GetRasterBand(1).ReadAsArray() refdata = refdata.astype('float64') - if not numpy.alltrue(data == 1./refdata): + if numpy_available and not numpy.alltrue(data == 1./refdata): return 'fail' return 'success' @@ -582,9 +591,9 @@ def pixfun_inv_c(): refdata = refdata.astype('complex') delta = data - 1./refdata - if not numpy.alltrue(abs(delta.real) < 1e-13): + if numpy_available and not numpy.alltrue(abs(delta.real) < 1e-13): return 'fail' - if not numpy.alltrue(abs(delta.imag) < 1e-13): + if numpy_available and not numpy.alltrue(abs(delta.imag) < 1e-13): return 'fail' return 'success' @@ -609,7 +618,7 @@ def pixfun_intensity_c(): return 'fail' refdata = refds.GetRasterBand(1).ReadAsArray() - if not numpy.alltrue(data == (refdata*refdata.conj()).real): + if numpy_available and not numpy.alltrue(data == (refdata*refdata.conj()).real): return 'fail' return 'success' @@ -634,7 +643,7 @@ def pixfun_intensity_r(): return 'fail' refdata = refds.GetRasterBand(1).ReadAsArray() - if not numpy.alltrue(data == (refdata*refdata.conj()).real): + if numpy_available and not numpy.alltrue(data == (refdata*refdata.conj()).real): return 'fail' return 'success' @@ -659,7 +668,7 @@ def pixfun_sqrt(): return 'fail' refdata = refds.GetRasterBand(1).ReadAsArray() - if not numpy.alltrue(data == numpy.sqrt(refdata)): + if numpy_available and not numpy.alltrue(data == numpy.sqrt(refdata)): return 'fail' return 'success' @@ -684,7 +693,7 @@ def pixfun_log10(): return 'fail' refdata = refds.GetRasterBand(1).ReadAsArray() - if not numpy.alltrue(data == numpy.log10(refdata)): + if numpy_available and not numpy.alltrue(data == numpy.log10(refdata)): return 'fail' return 'success' @@ -710,7 +719,7 @@ def pixfun_dB2amp(): refdata = refds.GetRasterBand(1).ReadAsArray() #if not numpy.alltrue(data == 10.**(refdata/20.)): - if not numpy.allclose(data, 10.**(refdata/20.)): + if numpy_available and not numpy.allclose(data, 10.**(refdata/20.)): return 'fail' return 'success' @@ -737,7 +746,7 @@ def pixfun_dB2pow(): refdata = refdata.astype('float64') #if not numpy.allclose(data, 10.**(refdata/10.)): - if not numpy.alltrue(data == 10.**(refdata/10.)): + if numpy_available and not numpy.alltrue(data == 10.**(refdata/10.)): return 'fail' return 'success' From 0345b9e74f655ea75fba57387e0d950b79f266f7 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Thu, 7 Jul 2016 11:45:29 +0200 Subject: [PATCH 38/69] COMP: Trying to fix travis-ci compilation error --- gdal/frmts/vrt/gdal_vrt.h | 2 +- gdal/frmts/vrt/pixelfunctions.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/gdal/frmts/vrt/gdal_vrt.h b/gdal/frmts/vrt/gdal_vrt.h index 97bead0f9a97..642280a55614 100644 --- a/gdal/frmts/vrt/gdal_vrt.h +++ b/gdal/frmts/vrt/gdal_vrt.h @@ -46,7 +46,7 @@ CPL_C_START void GDALRegister_VRT(); -CPLErr CPL_STDCALL GDALRegisterDefaultPixelFunc(); +CPLErr CPL_STDCALL GDALRegisterDefaultPixelFunc(void); typedef CPLErr (*VRTImageReadFunc)( void *hCBData, diff --git a/gdal/frmts/vrt/pixelfunctions.c b/gdal/frmts/vrt/pixelfunctions.c index 0dd7a44a9e65..677cd95694c0 100644 --- a/gdal/frmts/vrt/pixelfunctions.c +++ b/gdal/frmts/vrt/pixelfunctions.c @@ -917,7 +917,7 @@ static CPLErr dB2PowPixelFunc(void **papoSources, int nSources, void *pData, * * @return CE_None, invalid (NULL) parameters are currently ignored. */ -CPLErr CPL_STDCALL GDALRegisterDefaultPixelFunc() +CPLErr CPL_STDCALL GDALRegisterDefaultPixelFunc(void) { GDALAddDerivedBandPixelFunc("real", RealPixelFunc); GDALAddDerivedBandPixelFunc("imag", ImagPixelFunc); From e8cb1aed547cdf61a23132f7c4bf506487023f62 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Thu, 7 Jul 2016 12:07:56 +0200 Subject: [PATCH 39/69] TEST: Fixing test failing due to bad indent --- autotest/gdrivers/pixfun.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/autotest/gdrivers/pixfun.py b/autotest/gdrivers/pixfun.py index 707a7abcfbde..9450085372ac 100644 --- a/autotest/gdrivers/pixfun.py +++ b/autotest/gdrivers/pixfun.py @@ -360,7 +360,7 @@ def pixfun_sum_c(): if refds is None: gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) return 'fail' - refdata += refds.GetRasterBand(1).ReadAsArray(0, 0, 5, 6) + refdata += refds.GetRasterBand(1).ReadAsArray(0, 0, 5, 6) if not numpy.alltrue(data == refdata): return 'fail' @@ -452,7 +452,7 @@ def pixfun_mul_r(): if refds is None: gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) return 'fail' - refdata *= refds.GetRasterBand(1).ReadAsArray() + refdata *= refds.GetRasterBand(1).ReadAsArray() if not numpy.alltrue(data == refdata): return 'fail' From f0147487f735e5dde5e9000cd1831a28af985e06 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Thu, 7 Jul 2016 13:47:15 +0200 Subject: [PATCH 40/69] TEST: travis-ci failing due to python package imported but not used --- autotest/gdrivers/cderived.py | 1 - 1 file changed, 1 deletion(-) diff --git a/autotest/gdrivers/cderived.py b/autotest/gdrivers/cderived.py index be3b394010e7..65ac49b83b90 100755 --- a/autotest/gdrivers/cderived.py +++ b/autotest/gdrivers/cderived.py @@ -28,7 +28,6 @@ # DEALINGS IN THE SOFTWARE. ############################################################################### -import os import sys from osgeo import gdal From 96ff920bb971ca446d8e1d64add10107fc2738b0 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Thu, 7 Jul 2016 15:32:10 +0200 Subject: [PATCH 41/69] ENH: CPLStringList * -> CPLStringList (from PR review) --- gdal/gcore/gdal_priv.h | 2 +- gdal/gcore/gdaldataset.cpp | 12 ++++-------- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/gdal/gcore/gdal_priv.h b/gdal/gcore/gdal_priv.h index 7bed9eb29a5f..6853f475c586 100644 --- a/gdal/gcore/gdal_priv.h +++ b/gdal/gcore/gdal_priv.h @@ -499,7 +499,7 @@ class CPL_DLL GDALDataset : public GDALMajorObject OGRGeometry *poSpatialFilter, const char *pszDialect, swq_select_parse_options* poSelectParseOptions); - CPLStringList * papoDerivedMetadataList; + CPLStringList papoDerivedMetadataList; public: virtual int GetLayerCount(); diff --git a/gdal/gcore/gdaldataset.cpp b/gdal/gcore/gdaldataset.cpp index 34e787e993ca..2f9cddc35e55 100644 --- a/gdal/gcore/gdaldataset.cpp +++ b/gdal/gcore/gdaldataset.cpp @@ -209,7 +209,6 @@ void GDALDataset::Init(int bForceCachedIOIn) bIsInternal = TRUE; bSuppressOnClose = FALSE; papszOpenOptions = NULL; - papoDerivedMetadataList = new CPLStringList(); /* -------------------------------------------------------------------- */ /* Set forced caching flag. */ @@ -330,9 +329,6 @@ GDALDataset::~GDALDataset() CPLFree(psPrivate); CSLDestroy( papszOpenOptions ); - - CPLFree(papoDerivedMetadataList); - papoDerivedMetadataList = NULL; } /************************************************************************/ @@ -3304,7 +3300,7 @@ char ** GDALDataset::GetMetadata(const char * pszDomain) { if( pszDomain != NULL && EQUAL(pszDomain, "DERIVED_SUBDATASETS") ) { - papoDerivedMetadataList->Clear(); + papoDerivedMetadataList.Clear(); @@ -3314,14 +3310,14 @@ char ** GDALDataset::GetMetadata(const char * pszDomain) CPLDebug("GDALDataset::GetMetadata","Number of derived datasets to report: %i",(int)NB_DERIVED_DATASETS); for(unsigned int derivedId = 0; derivedIdSetNameValue(CPLSPrintf("DERIVED_SUBDATASET_%i_NAME",derivedId),CPLSPrintf("DERIVED_SUBDATASET:%s:%s",asDDSDesc[derivedId].pszDatasetName,GetDescription())); + papoDerivedMetadataList.SetNameValue(CPLSPrintf("DERIVED_SUBDATASET_%i_NAME",derivedId),CPLSPrintf("DERIVED_SUBDATASET:%s:%s",asDDSDesc[derivedId].pszDatasetName,GetDescription())); CPLString osDesc(CPLSPrintf("%s from %s",asDDSDesc[derivedId].pszDatasetDescritpion,GetDescription())); - papoDerivedMetadataList->SetNameValue(CPLSPrintf("DERIVED_SUBDATASET_%i_DESC",derivedId),osDesc.c_str()); + papoDerivedMetadataList.SetNameValue(CPLSPrintf("DERIVED_SUBDATASET_%i_DESC",derivedId),osDesc.c_str()); } } - return papoDerivedMetadataList->List(); + return papoDerivedMetadataList.List(); } else return GDALMajorObject::GetMetadata(pszDomain); From 27ab9f5ce68e2fad8e44d3f49c235008c4dc278f Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Thu, 7 Jul 2016 15:33:23 +0200 Subject: [PATCH 42/69] ENH: Clear debug traces (from PR review) --- gdal/gcore/gdaldataset.cpp | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/gdal/gcore/gdaldataset.cpp b/gdal/gcore/gdaldataset.cpp index 2f9cddc35e55..4a13426a1b7b 100644 --- a/gdal/gcore/gdaldataset.cpp +++ b/gdal/gcore/gdaldataset.cpp @@ -3307,7 +3307,6 @@ char ** GDALDataset::GetMetadata(const char * pszDomain) // First condition: at least one raster band if(GetRasterCount()>0) { - CPLDebug("GDALDataset::GetMetadata","Number of derived datasets to report: %i",(int)NB_DERIVED_DATASETS); for(unsigned int derivedId = 0; derivedId Date: Thu, 7 Jul 2016 15:35:06 +0200 Subject: [PATCH 43/69] STY: Fix if indentation (from PR review) --- gdal/gcore/gdaldataset.cpp | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/gdal/gcore/gdaldataset.cpp b/gdal/gcore/gdaldataset.cpp index 4a13426a1b7b..f2431f4634b6 100644 --- a/gdal/gcore/gdaldataset.cpp +++ b/gdal/gcore/gdaldataset.cpp @@ -3299,25 +3299,22 @@ void GDALDataset::ReportError(CPLErr eErrClass, CPLErrorNum err_no, const char * char ** GDALDataset::GetMetadata(const char * pszDomain) { if( pszDomain != NULL && EQUAL(pszDomain, "DERIVED_SUBDATASETS") ) - { - papoDerivedMetadataList.Clear(); - - - - // First condition: at least one raster band - if(GetRasterCount()>0) + { + papoDerivedMetadataList.Clear(); + + // First condition: at least one raster band + if(GetRasterCount()>0) { - for(unsigned int derivedId = 0; derivedId Date: Thu, 7 Jul 2016 15:36:01 +0200 Subject: [PATCH 44/69] ENH: Remove unused code (from PR review) --- gdal/frmts/cderived/cderiveddataset.cpp | 9 --------- 1 file changed, 9 deletions(-) diff --git a/gdal/frmts/cderived/cderiveddataset.cpp b/gdal/frmts/cderived/cderiveddataset.cpp index 368114fcdef3..a0dac2259466 100644 --- a/gdal/frmts/cderived/cderiveddataset.cpp +++ b/gdal/frmts/cderived/cderiveddataset.cpp @@ -30,15 +30,6 @@ #include "gdal_proxy.h" #include "derivedlist.h" -#include - -class ComplexDerivedDatasetContainer: public GDALPamDataset -{ - public: - ComplexDerivedDatasetContainer() {} -}; - - class ComplexDerivedDataset : public VRTDataset { public: From 842bff598c764f16101a52afd1c6ffa60f4104d2 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Thu, 7 Jul 2016 15:36:43 +0200 Subject: [PATCH 45/69] DOC: Fix copyright --- gdal/frmts/cderived/cderiveddataset.cpp | 2 +- gdal/frmts/cderived/derivedlist.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/gdal/frmts/cderived/cderiveddataset.cpp b/gdal/frmts/cderived/cderiveddataset.cpp index a0dac2259466..5fa691f819ad 100644 --- a/gdal/frmts/cderived/cderiveddataset.cpp +++ b/gdal/frmts/cderived/cderiveddataset.cpp @@ -5,7 +5,7 @@ * Author: Julien Michel * ****************************************************************************** - * Copyright (c) 2008-2014 Antonio Valentino + * Copyright (c) 2016 Julien Michel * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), diff --git a/gdal/frmts/cderived/derivedlist.h b/gdal/frmts/cderived/derivedlist.h index 8b68b325742d..7f21005c9713 100644 --- a/gdal/frmts/cderived/derivedlist.h +++ b/gdal/frmts/cderived/derivedlist.h @@ -5,7 +5,7 @@ * Author: Julien Michel * ****************************************************************************** - * Copyright (c) 2008-2014 Antonio Valentino + * Copyright (c) 2016 Julien Michel * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), From 47137853a304bad48c3799088281054c60d97c18 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Thu, 7 Jul 2016 15:39:15 +0200 Subject: [PATCH 46/69] ENH: Replace magic number by appropriate definition (from PR review) --- gdal/frmts/cderived/cderiveddataset.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/gdal/frmts/cderived/cderiveddataset.cpp b/gdal/frmts/cderived/cderiveddataset.cpp index 5fa691f819ad..6c97b0f82d13 100644 --- a/gdal/frmts/cderived/cderiveddataset.cpp +++ b/gdal/frmts/cderived/cderiveddataset.cpp @@ -56,7 +56,8 @@ GDALDataset * ComplexDerivedDataset::Open(GDALOpenInfo * poOpenInfo) CPLString filename(poOpenInfo->pszFilename); /* DERIVED_SUBDATASET should be first domain */ - size_t dsds_pos = filename.find("DERIVED_SUBDATASET:"); + const size_t dsds_pos = filename.find("DERIVED_SUBDATASET:"); + const size_t nPrefixLen = strlen("DERIVED_SUBDATASET:"); if (dsds_pos == std::string::npos) { @@ -65,14 +66,14 @@ GDALDataset * ComplexDerivedDataset::Open(GDALOpenInfo * poOpenInfo) } /* Next, we need to now which derived dataset to compute */ - size_t alg_pos = filename.find(":",dsds_pos+20); + const size_t alg_pos = filename.find(":",dsds_pos+20); if (alg_pos == std::string::npos) { /* Unable to Open if we do not find the name of the derived dataset */ return NULL; } - CPLString odDerivedName = filename.substr(dsds_pos+19,alg_pos-dsds_pos-19); + CPLString odDerivedName = filename.substr(dsds_pos+nPrefixLen,alg_pos-dsds_pos-nPrefixLen); CPLDebug("ComplexDerivedDataset::Open","Derived dataset requested: %s",odDerivedName.c_str()); From 44f6f4d2da0d29c19956aabc04e7d3a32e905e32 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Thu, 7 Jul 2016 15:43:50 +0200 Subject: [PATCH 47/69] STY: papo -> o (from PR review) --- gdal/gcore/gdal_priv.h | 2 +- gdal/gcore/gdaldataset.cpp | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/gdal/gcore/gdal_priv.h b/gdal/gcore/gdal_priv.h index 6853f475c586..0c5d9e5811d7 100644 --- a/gdal/gcore/gdal_priv.h +++ b/gdal/gcore/gdal_priv.h @@ -499,7 +499,7 @@ class CPL_DLL GDALDataset : public GDALMajorObject OGRGeometry *poSpatialFilter, const char *pszDialect, swq_select_parse_options* poSelectParseOptions); - CPLStringList papoDerivedMetadataList; + CPLStringList oDerivedMetadataList; public: virtual int GetLayerCount(); diff --git a/gdal/gcore/gdaldataset.cpp b/gdal/gcore/gdaldataset.cpp index f2431f4634b6..378896c55be3 100644 --- a/gdal/gcore/gdaldataset.cpp +++ b/gdal/gcore/gdaldataset.cpp @@ -3300,20 +3300,20 @@ char ** GDALDataset::GetMetadata(const char * pszDomain) { if( pszDomain != NULL && EQUAL(pszDomain, "DERIVED_SUBDATASETS") ) { - papoDerivedMetadataList.Clear(); + oDerivedMetadataList.Clear(); // First condition: at least one raster band if(GetRasterCount()>0) { for(unsigned int derivedId = 0; derivedId Date: Thu, 7 Jul 2016 16:20:05 +0200 Subject: [PATCH 48/69] ENH: Hide static const DerivedDatasetDescription asDDSDesc [] in a compiled file to avoid duplication in binaries (from PR review) --- gdal/frmts/cderived/GNUmakefile | 2 +- gdal/frmts/cderived/cderiveddataset.cpp | 12 ++++-- gdal/frmts/cderived/derivedlist.c | 56 +++++++++++++++++++++++++ gdal/frmts/cderived/derivedlist.h | 14 +------ gdal/frmts/cderived/makefile.vc | 2 +- gdal/gcore/gdaldataset.cpp | 12 ++++-- 6 files changed, 76 insertions(+), 22 deletions(-) create mode 100644 gdal/frmts/cderived/derivedlist.c diff --git a/gdal/frmts/cderived/GNUmakefile b/gdal/frmts/cderived/GNUmakefile index dd9b946208a7..91a8866efa9e 100644 --- a/gdal/frmts/cderived/GNUmakefile +++ b/gdal/frmts/cderived/GNUmakefile @@ -1,7 +1,7 @@ include ../../GDALmake.opt -OBJ = cderiveddataset.o +OBJ = cderiveddataset.o derivedlist.o default: $(OBJ:.o=.$(OBJ_EXT)) diff --git a/gdal/frmts/cderived/cderiveddataset.cpp b/gdal/frmts/cderived/cderiveddataset.cpp index 6c97b0f82d13..4356028fc1be 100644 --- a/gdal/frmts/cderived/cderiveddataset.cpp +++ b/gdal/frmts/cderived/cderiveddataset.cpp @@ -79,13 +79,17 @@ GDALDataset * ComplexDerivedDataset::Open(GDALOpenInfo * poOpenInfo) CPLString pixelFunctionName = ""; bool datasetFound = false; + + const unsigned int nbSupportedDerivedDS = GDALGetNumberOfDerivedDatasetDecriptions(); - for(unsigned int derivedId = 0; derivedIdpszDatasetName) { datasetFound = true; - pixelFunctionName = asDDSDesc[derivedId].pszPixelFunction; + pixelFunctionName = poCurrentDerivedDatasetDescription->pszPixelFunction; } } diff --git a/gdal/frmts/cderived/derivedlist.c b/gdal/frmts/cderived/derivedlist.c new file mode 100644 index 000000000000..5c1a8ce11986 --- /dev/null +++ b/gdal/frmts/cderived/derivedlist.c @@ -0,0 +1,56 @@ +/****************************************************************************** + * + * Project: GDAL + * Purpose: Implementation of derived subdatasets + * Author: Julien Michel + * + ****************************************************************************** + * Copyright (c) 2016 Julien Michel + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + *****************************************************************************/ +#include "derivedlist.h" +#include "gdal.h" + +static const DerivedDatasetDescription asDDSDesc [] = +{ + { "AMPLITUDE", "Amplitude of input bands", "mod"}, + { "PHASE", "Phase of input bands", "phase"}, + { "REAL", "Real part of input bands", "real"}, + { "IMAG", "Imaginary part of input bands", "imag"}, + { "CONJ", "Conjugate of input bands", "conj"}, + { "INTENSITY", "Intensity (squared amplitude) of input bands", "intensity"}, + { "LOGAMPLITUDE", "log10 of amplitude of input bands", "log10"} +}; + +#define NB_DERIVED_DATASETS (sizeof(asDDSDesc)/sizeof(asDDSDesc[0])) + +const DerivedDatasetDescription* GDALGetDerivedDatasetDescription(const unsigned int * pnDescriptionCount) +{ + if(*pnDescriptionCount < (int)NB_DERIVED_DATASETS) + { + return &asDDSDesc[*pnDescriptionCount]; + } + return NULL; +} + +unsigned int GDALGetNumberOfDerivedDatasetDecriptions(void) +{ + return (unsigned int)NB_DERIVED_DATASETS; +} diff --git a/gdal/frmts/cderived/derivedlist.h b/gdal/frmts/cderived/derivedlist.h index 7f21005c9713..5726e818cc36 100644 --- a/gdal/frmts/cderived/derivedlist.h +++ b/gdal/frmts/cderived/derivedlist.h @@ -35,18 +35,8 @@ typedef struct const char * pszPixelFunction; } DerivedDatasetDescription; -static const DerivedDatasetDescription asDDSDesc [] = -{ - { "AMPLITUDE", "Amplitude of input bands", "mod"}, - { "PHASE", "Phase of input bands", "phase"}, - { "REAL", "Real part of input bands", "real"}, - { "IMAG", "Imaginary part of input bands", "imag"}, - { "CONJ", "Conjugate of input bands", "conj"}, - { "INTENSITY", "Intensity (squared amplitude) of input bands", "intensity"}, - { "LOGAMPLITUDE", "log10 of amplitude of input bands", "log10"} -}; - -#define NB_DERIVED_DATASETS (sizeof(asDDSDesc)/sizeof(asDDSDesc[0])) +const DerivedDatasetDescription* GDALGetDerivedDatasetDescription(const unsigned int * pnDescriptionCount); +unsigned int GDALGetNumberOfDerivedDatasetDecriptions(void); #endif diff --git a/gdal/frmts/cderived/makefile.vc b/gdal/frmts/cderived/makefile.vc index 5fd83953a1ee..b2ab580b3a1f 100644 --- a/gdal/frmts/cderived/makefile.vc +++ b/gdal/frmts/cderived/makefile.vc @@ -1,5 +1,5 @@ -OBJ = cderiveddataset.obj +OBJ = cderiveddataset.obj derivedlist.obj GDAL_ROOT = ..\.. diff --git a/gdal/gcore/gdaldataset.cpp b/gdal/gcore/gdaldataset.cpp index 378896c55be3..b17908c1690d 100644 --- a/gdal/gcore/gdaldataset.cpp +++ b/gdal/gcore/gdaldataset.cpp @@ -3305,11 +3305,15 @@ char ** GDALDataset::GetMetadata(const char * pszDomain) // First condition: at least one raster band if(GetRasterCount()>0) { - for(unsigned int derivedId = 0; derivedIdpszDatasetName,GetDescription())); - CPLString osDesc(CPLSPrintf("%s from %s",asDDSDesc[derivedId].pszDatasetDescritpion,GetDescription())); + CPLString osDesc(CPLSPrintf("%s from %s",poCurrentDerivedDatasetDescription->pszDatasetDescritpion,GetDescription())); oDerivedMetadataList.SetNameValue(CPLSPrintf("DERIVED_SUBDATASET_%i_DESC",derivedId),osDesc.c_str()); } } From c32128a99fbb91870c298a6f9a183336c2c3b839 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Thu, 7 Jul 2016 16:25:33 +0200 Subject: [PATCH 49/69] ENH: Renamed cderived -> derived (as it is not limited to complex datasets) --- autotest/gdrivers/{cderived.py => derived.py} | 4 +-- gdal/GDALmake.opt.in | 2 +- gdal/frmts/{cderived => derived}/GNUmakefile | 2 +- .../deriveddataset.cpp} | 28 +++++++++---------- .../frmts/{cderived => derived}/derivedlist.c | 0 .../frmts/{cderived => derived}/derivedlist.h | 0 gdal/frmts/{cderived => derived}/makefile.vc | 2 +- gdal/frmts/gdalallregister.cpp | 2 +- gdal/gcore/gdal_frmts.h | 2 +- gdal/gcore/gdaldataset.cpp | 2 +- 10 files changed, 22 insertions(+), 22 deletions(-) rename autotest/gdrivers/{cderived.py => derived.py} (98%) rename gdal/frmts/{cderived => derived}/GNUmakefile (76%) rename gdal/frmts/{cderived/cderiveddataset.cpp => derived/deriveddataset.cpp} (86%) rename gdal/frmts/{cderived => derived}/derivedlist.c (100%) rename gdal/frmts/{cderived => derived}/derivedlist.h (100%) rename gdal/frmts/{cderived => derived}/makefile.vc (73%) diff --git a/autotest/gdrivers/cderived.py b/autotest/gdrivers/derived.py similarity index 98% rename from autotest/gdrivers/cderived.py rename to autotest/gdrivers/derived.py index 65ac49b83b90..8d80968c4143 100755 --- a/autotest/gdrivers/cderived.py +++ b/autotest/gdrivers/derived.py @@ -39,7 +39,7 @@ # Test opening a L1C product -def cderived_test(): +def derived_test(): filename = "data/cfloat64.tif" gdal.ErrorReset() ds = gdal.Open(filename) @@ -86,7 +86,7 @@ def cderived_test(): if __name__ == '__main__': - gdaltest.setup_run( 'cderived' ) + gdaltest.setup_run( 'derived' ) gdaltest.run_tests( gdaltest_list ) diff --git a/gdal/GDALmake.opt.in b/gdal/GDALmake.opt.in index 9c20e97b4356..776ba71093ac 100644 --- a/gdal/GDALmake.opt.in +++ b/gdal/GDALmake.opt.in @@ -562,7 +562,7 @@ GDAL_FORMATS += nitf bmp airsar rs2 ilwis rmf leveller sgi srtmhgt GDAL_FORMATS += idrisi gsg ingr ers jaxapalsar dimap gff cosar pds adrg GDAL_FORMATS += coasp tsx terragen blx msgn til r northwood saga xyz hf2 GDAL_FORMATS += kmlsuperoverlay ctg e00grid zmap ngsgeoid iris map cals -GDAL_FORMATS += safe sentinel2 cderived +GDAL_FORMATS += safe sentinel2 derived GDAL_FORMATS += @OPT_GDAL_FORMATS@ ifneq ($(PCIDSK_SETTING),no) diff --git a/gdal/frmts/cderived/GNUmakefile b/gdal/frmts/derived/GNUmakefile similarity index 76% rename from gdal/frmts/cderived/GNUmakefile rename to gdal/frmts/derived/GNUmakefile index 91a8866efa9e..179f487c6cbf 100644 --- a/gdal/frmts/cderived/GNUmakefile +++ b/gdal/frmts/derived/GNUmakefile @@ -1,7 +1,7 @@ include ../../GDALmake.opt -OBJ = cderiveddataset.o derivedlist.o +OBJ = deriveddataset.o derivedlist.o default: $(OBJ:.o=.$(OBJ_EXT)) diff --git a/gdal/frmts/cderived/cderiveddataset.cpp b/gdal/frmts/derived/deriveddataset.cpp similarity index 86% rename from gdal/frmts/cderived/cderiveddataset.cpp rename to gdal/frmts/derived/deriveddataset.cpp index 4356028fc1be..f6d268c27fc2 100644 --- a/gdal/frmts/cderived/cderiveddataset.cpp +++ b/gdal/frmts/derived/deriveddataset.cpp @@ -30,27 +30,27 @@ #include "gdal_proxy.h" #include "derivedlist.h" -class ComplexDerivedDataset : public VRTDataset +class DerivedDataset : public VRTDataset { public: - ComplexDerivedDataset(int nXSize, int nYSize); - ~ComplexDerivedDataset(); + DerivedDataset(int nXSize, int nYSize); + ~DerivedDataset(); static GDALDataset *Open( GDALOpenInfo * ); }; -ComplexDerivedDataset::ComplexDerivedDataset(int nXSize, int nYSize) : VRTDataset(nXSize,nYSize) +DerivedDataset::DerivedDataset(int nXSize, int nYSize) : VRTDataset(nXSize,nYSize) { poDriver = NULL; SetWritable(FALSE); } -ComplexDerivedDataset::~ComplexDerivedDataset() +DerivedDataset::~DerivedDataset() { } -GDALDataset * ComplexDerivedDataset::Open(GDALOpenInfo * poOpenInfo) +GDALDataset * DerivedDataset::Open(GDALOpenInfo * poOpenInfo) { /* Try to open original dataset */ CPLString filename(poOpenInfo->pszFilename); @@ -75,7 +75,7 @@ GDALDataset * ComplexDerivedDataset::Open(GDALOpenInfo * poOpenInfo) CPLString odDerivedName = filename.substr(dsds_pos+nPrefixLen,alg_pos-dsds_pos-nPrefixLen); - CPLDebug("ComplexDerivedDataset::Open","Derived dataset requested: %s",odDerivedName.c_str()); + CPLDebug("DerivedDataset::Open","Derived dataset requested: %s",odDerivedName.c_str()); CPLString pixelFunctionName = ""; bool datasetFound = false; @@ -116,7 +116,7 @@ GDALDataset * ComplexDerivedDataset::Open(GDALOpenInfo * poOpenInfo) int nRows = poTmpDS->GetRasterYSize(); int nCols = poTmpDS->GetRasterXSize(); - ComplexDerivedDataset * poDS = new ComplexDerivedDataset(nCols,nRows); + DerivedDataset * poDS = new DerivedDataset(nCols,nRows); // Transfer metadata poDS->SetMetadata(poTmpDS->GetMetadata()); @@ -169,24 +169,24 @@ GDALDataset * ComplexDerivedDataset::Open(GDALOpenInfo * poOpenInfo) return poDS; } -void GDALRegister_ComplexDerived() +void GDALRegister_Derived() { - if( GDALGetDriverByName( "COMPLEXDERIVED" ) != NULL ) + if( GDALGetDriverByName( "DERIVED" ) != NULL ) return; GDALDriver *poDriver = new GDALDriver(); - poDriver->SetDescription( "COMPLEXDERIVED" ); + poDriver->SetDescription( "DERIVED" ); #ifdef GDAL_DCAP_RASTER poDriver->SetMetadataItem( GDAL_DCAP_RASTER, "YES" ); #endif poDriver->SetMetadataItem( GDAL_DCAP_VIRTUALIO, "YES" ); - poDriver->SetMetadataItem( GDAL_DMD_LONGNAME, "Complex derived bands" ); + poDriver->SetMetadataItem( GDAL_DMD_LONGNAME, "Derived datasets using VRT pixel functions" ); poDriver->SetMetadataItem( GDAL_DMD_HELPTOPIC, "TODO" ); poDriver->SetMetadataItem( GDAL_DMD_SUBDATASETS, "NO" ); - poDriver->pfnOpen = ComplexDerivedDataset::Open; - poDriver->pfnIdentify = ComplexDerivedDataset::Identify; + poDriver->pfnOpen = DerivedDataset::Open; + poDriver->pfnIdentify = DerivedDataset::Identify; GetGDALDriverManager()->RegisterDriver( poDriver ); } diff --git a/gdal/frmts/cderived/derivedlist.c b/gdal/frmts/derived/derivedlist.c similarity index 100% rename from gdal/frmts/cderived/derivedlist.c rename to gdal/frmts/derived/derivedlist.c diff --git a/gdal/frmts/cderived/derivedlist.h b/gdal/frmts/derived/derivedlist.h similarity index 100% rename from gdal/frmts/cderived/derivedlist.h rename to gdal/frmts/derived/derivedlist.h diff --git a/gdal/frmts/cderived/makefile.vc b/gdal/frmts/derived/makefile.vc similarity index 73% rename from gdal/frmts/cderived/makefile.vc rename to gdal/frmts/derived/makefile.vc index b2ab580b3a1f..9a2d14af3289 100644 --- a/gdal/frmts/cderived/makefile.vc +++ b/gdal/frmts/derived/makefile.vc @@ -1,5 +1,5 @@ -OBJ = cderiveddataset.obj derivedlist.obj +OBJ = deriveddataset.obj derivedlist.obj GDAL_ROOT = ..\.. diff --git a/gdal/frmts/gdalallregister.cpp b/gdal/frmts/gdalallregister.cpp index 9c215a35805c..a8e969f63e19 100644 --- a/gdal/frmts/gdalallregister.cpp +++ b/gdal/frmts/gdalallregister.cpp @@ -68,7 +68,7 @@ void CPL_STDCALL GDALAllRegister() #ifdef FRMT_vrt GDALRegister_VRT(); - GDALRegister_ComplexDerived(); + GDALRegister_Derived(); #endif #ifdef FRMT_gtiff diff --git a/gdal/gcore/gdal_frmts.h b/gdal/gcore/gdal_frmts.h index 354177597bdc..4f7a62f7d952 100644 --- a/gdal/gcore/gdal_frmts.h +++ b/gdal/gcore/gdal_frmts.h @@ -187,7 +187,7 @@ void CPL_DLL GDALRegister_SAFE(void); void CPL_DLL GDALRegister_SENTINEL2(void); void CPL_DLL GDALRegister_mrf(void); void CPL_DLL GDALRegister_RRASTER(void); -void CPL_DLL GDALRegister_ComplexDerived(void); +void CPL_DLL GDALRegister_Derived(void); CPL_C_END #endif /* ndef GDAL_FRMTS_H_INCLUDED */ diff --git a/gdal/gcore/gdaldataset.cpp b/gdal/gcore/gdaldataset.cpp index b17908c1690d..ca871f3a6654 100644 --- a/gdal/gcore/gdaldataset.cpp +++ b/gdal/gcore/gdaldataset.cpp @@ -40,7 +40,7 @@ #include "ograpispy.h" #include "ogrunionlayer.h" #include "swq.h" -#include "../frmts/cderived/derivedlist.h" +#include "../frmts/derived/derivedlist.h" #ifdef SQLITE_ENABLED #include "../sqlite/ogrsqliteexecutesql.h" From 03ca1aa681c53fcea54f8935d62fb50df3bbe805 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Thu, 7 Jul 2016 16:58:35 +0200 Subject: [PATCH 50/69] COMP: Adding C wrapping stuff to make it compile --- gdal/frmts/derived/derivedlist.c | 22 +++++++++++++--------- gdal/frmts/derived/derivedlist.h | 11 +++++++++-- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/gdal/frmts/derived/derivedlist.c b/gdal/frmts/derived/derivedlist.c index 5c1a8ce11986..31a4579a1968 100644 --- a/gdal/frmts/derived/derivedlist.c +++ b/gdal/frmts/derived/derivedlist.c @@ -28,20 +28,22 @@ #include "derivedlist.h" #include "gdal.h" +CPL_C_START + static const DerivedDatasetDescription asDDSDesc [] = { - { "AMPLITUDE", "Amplitude of input bands", "mod"}, - { "PHASE", "Phase of input bands", "phase"}, - { "REAL", "Real part of input bands", "real"}, - { "IMAG", "Imaginary part of input bands", "imag"}, - { "CONJ", "Conjugate of input bands", "conj"}, - { "INTENSITY", "Intensity (squared amplitude) of input bands", "intensity"}, - { "LOGAMPLITUDE", "log10 of amplitude of input bands", "log10"} + { "AMPLITUDE", "Amplitude of input bands", "mod", "complex"}, + { "PHASE", "Phase of input bands", "phase", "complex"}, + { "REAL", "Real part of input bands", "real", "complex"}, + { "IMAG", "Imaginary part of input bands", "imag", "complex"}, + { "CONJ", "Conjugate of input bands", "conj", "complex"}, + { "INTENSITY", "Intensity (squared amplitude) of input bands", "intensity", "complex"}, + { "LOGAMPLITUDE", "log10 of amplitude of input bands", "log10", "all"} }; #define NB_DERIVED_DATASETS (sizeof(asDDSDesc)/sizeof(asDDSDesc[0])) -const DerivedDatasetDescription* GDALGetDerivedDatasetDescription(const unsigned int * pnDescriptionCount) +const DerivedDatasetDescription* CPL_DLL CPL_STDCALL GDALGetDerivedDatasetDescription(const unsigned int * pnDescriptionCount) { if(*pnDescriptionCount < (int)NB_DERIVED_DATASETS) { @@ -50,7 +52,9 @@ const DerivedDatasetDescription* GDALGetDerivedDatasetDescription(const unsigned return NULL; } -unsigned int GDALGetNumberOfDerivedDatasetDecriptions(void) +unsigned int CPL_DLL CPL_STDCALL GDALGetNumberOfDerivedDatasetDecriptions(void) { return (unsigned int)NB_DERIVED_DATASETS; } + +CPL_C_END diff --git a/gdal/frmts/derived/derivedlist.h b/gdal/frmts/derived/derivedlist.h index 5726e818cc36..2f0b2620fca4 100644 --- a/gdal/frmts/derived/derivedlist.h +++ b/gdal/frmts/derived/derivedlist.h @@ -28,15 +28,22 @@ #ifndef DERIVEDLIST_H_INCLUDED #define DERIVEDLIST_H_INCLUDED +#include "cpl_port.h" + +CPL_C_START + typedef struct { const char * pszDatasetName; const char * pszDatasetDescritpion; const char * pszPixelFunction; + const char * pszTargetPixelType; } DerivedDatasetDescription; -const DerivedDatasetDescription* GDALGetDerivedDatasetDescription(const unsigned int * pnDescriptionCount); +const DerivedDatasetDescription* CPL_DLL CPL_STDCALL GDALGetDerivedDatasetDescription(const unsigned int * pnDescriptionCount); + +unsigned int CPL_DLL CPL_STDCALL GDALGetNumberOfDerivedDatasetDecriptions(void); -unsigned int GDALGetNumberOfDerivedDatasetDecriptions(void); +CPL_C_END #endif From a65e6efdefd2d450319f014355738ea9f0dd9b9b Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Thu, 7 Jul 2016 17:12:40 +0200 Subject: [PATCH 51/69] ENH: Avoid exposing complex relevant derived datasets if there is not at least one complex raster band --- gdal/gcore/gdaldataset.cpp | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/gdal/gcore/gdaldataset.cpp b/gdal/gcore/gdaldataset.cpp index ca871f3a6654..bdcd6f16de13 100644 --- a/gdal/gcore/gdaldataset.cpp +++ b/gdal/gcore/gdaldataset.cpp @@ -3305,16 +3305,31 @@ char ** GDALDataset::GetMetadata(const char * pszDomain) // First condition: at least one raster band if(GetRasterCount()>0) { + // Check if there is at least one complex band + bool hasAComplexBand = false; + + for(int rasterId = 1; rasterId <= GetRasterCount();++rasterId) + { + if(GDALDataTypeIsComplex(GetRasterBand(rasterId)->GetRasterDataType())) + { + hasAComplexBand = true; + break; + } + } + const unsigned int nbSupportedDerivedDS = GDALGetNumberOfDerivedDatasetDecriptions(); for(unsigned int derivedId = 0; derivedIdpszDatasetName,GetDescription())); - - CPLString osDesc(CPLSPrintf("%s from %s",poCurrentDerivedDatasetDescription->pszDatasetDescritpion,GetDescription())); - oDerivedMetadataList.SetNameValue(CPLSPrintf("DERIVED_SUBDATASET_%i_DESC",derivedId),osDesc.c_str()); + + if(hasAComplexBand || CPLString(poCurrentDerivedDatasetDescription->pszTargetPixelType) != "complex") + { + oDerivedMetadataList.SetNameValue(CPLSPrintf("DERIVED_SUBDATASET_%i_NAME",derivedId),CPLSPrintf("DERIVED_SUBDATASET:%s:%s",poCurrentDerivedDatasetDescription->pszDatasetName,GetDescription())); + + CPLString osDesc(CPLSPrintf("%s from %s",poCurrentDerivedDatasetDescription->pszDatasetDescritpion,GetDescription())); + oDerivedMetadataList.SetNameValue(CPLSPrintf("DERIVED_SUBDATASET_%i_DESC",derivedId),osDesc.c_str()); + } } } return oDerivedMetadataList.List(); From 57e607da7ff9d059db1a0f4d0670ae3652cf3328 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Thu, 7 Jul 2016 17:15:44 +0200 Subject: [PATCH 52/69] ENH: Report correct block size (from PR review) --- gdal/frmts/derived/deriveddataset.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/gdal/frmts/derived/deriveddataset.cpp b/gdal/frmts/derived/deriveddataset.cpp index f6d268c27fc2..bee7b185868a 100644 --- a/gdal/frmts/derived/deriveddataset.cpp +++ b/gdal/frmts/derived/deriveddataset.cpp @@ -157,7 +157,11 @@ GDALDataset * DerivedDataset::Open(GDALOpenInfo * poOpenInfo) GA_ReadOnly, TRUE); for(int j=0;jAddSrcBandDescription(poTmpDS->GetRasterBand(nBand)->GetRasterDataType(), 128, 128); + { + int blockXSize, blockYSize; + poTmpDS->GetRasterBand(nBand)->GetBlockSize(&blockXSize,&blockYSize); + proxyDS->AddSrcBandDescription(poTmpDS->GetRasterBand(nBand)->GetRasterDataType(), blockXSize, blockYSize); + } poBand->AddComplexSource(proxyDS->GetRasterBand(nBand),0,0,nCols,nRows,0,0,nCols,nRows); From 2490c3d6d0613e65fc68d47c095fe36e6846c417 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Fri, 8 Jul 2016 13:40:59 +0200 Subject: [PATCH 53/69] ENH: Better implementation of access to available subdatasets description --- gdal/frmts/derived/deriveddataset.cpp | 12 ++++++------ gdal/frmts/derived/derivedlist.c | 14 +++----------- gdal/frmts/derived/derivedlist.h | 4 +--- gdal/gcore/gdaldataset.cpp | 12 ++++++------ 4 files changed, 16 insertions(+), 26 deletions(-) diff --git a/gdal/frmts/derived/deriveddataset.cpp b/gdal/frmts/derived/deriveddataset.cpp index bee7b185868a..891185350449 100644 --- a/gdal/frmts/derived/deriveddataset.cpp +++ b/gdal/frmts/derived/deriveddataset.cpp @@ -80,16 +80,16 @@ GDALDataset * DerivedDataset::Open(GDALOpenInfo * poOpenInfo) CPLString pixelFunctionName = ""; bool datasetFound = false; - const unsigned int nbSupportedDerivedDS = GDALGetNumberOfDerivedDatasetDecriptions(); + unsigned int nbSupportedDerivedDS(0); + + const DerivedDatasetDescription * poDDSDesc = GDALGetDerivedDatasetDescriptions(&nbSupportedDerivedDS); for(unsigned int derivedId = 0; derivedIdpszDatasetName) + { + if(odDerivedName == poDDSDesc[derivedId].pszDatasetName) { datasetFound = true; - pixelFunctionName = poCurrentDerivedDatasetDescription->pszPixelFunction; + pixelFunctionName = poDDSDesc[derivedId].pszPixelFunction; } } diff --git a/gdal/frmts/derived/derivedlist.c b/gdal/frmts/derived/derivedlist.c index 31a4579a1968..7cfcbe5313ef 100644 --- a/gdal/frmts/derived/derivedlist.c +++ b/gdal/frmts/derived/derivedlist.c @@ -43,18 +43,10 @@ static const DerivedDatasetDescription asDDSDesc [] = #define NB_DERIVED_DATASETS (sizeof(asDDSDesc)/sizeof(asDDSDesc[0])) -const DerivedDatasetDescription* CPL_DLL CPL_STDCALL GDALGetDerivedDatasetDescription(const unsigned int * pnDescriptionCount) +const DerivedDatasetDescription* CPL_DLL CPL_STDCALL GDALGetDerivedDatasetDescriptions(unsigned int * pnDescriptionCount) { - if(*pnDescriptionCount < (int)NB_DERIVED_DATASETS) - { - return &asDDSDesc[*pnDescriptionCount]; - } - return NULL; -} - -unsigned int CPL_DLL CPL_STDCALL GDALGetNumberOfDerivedDatasetDecriptions(void) -{ - return (unsigned int)NB_DERIVED_DATASETS; + *pnDescriptionCount = (unsigned int)NB_DERIVED_DATASETS; + return asDDSDesc; } CPL_C_END diff --git a/gdal/frmts/derived/derivedlist.h b/gdal/frmts/derived/derivedlist.h index 2f0b2620fca4..3202f8860e6c 100644 --- a/gdal/frmts/derived/derivedlist.h +++ b/gdal/frmts/derived/derivedlist.h @@ -40,9 +40,7 @@ typedef struct const char * pszTargetPixelType; } DerivedDatasetDescription; -const DerivedDatasetDescription* CPL_DLL CPL_STDCALL GDALGetDerivedDatasetDescription(const unsigned int * pnDescriptionCount); - -unsigned int CPL_DLL CPL_STDCALL GDALGetNumberOfDerivedDatasetDecriptions(void); +const DerivedDatasetDescription* CPL_DLL CPL_STDCALL GDALGetDerivedDatasetDescriptions(unsigned int * pnDescriptionCount); CPL_C_END diff --git a/gdal/gcore/gdaldataset.cpp b/gdal/gcore/gdaldataset.cpp index 98d9f546c6cc..24652f95783f 100644 --- a/gdal/gcore/gdaldataset.cpp +++ b/gdal/gcore/gdaldataset.cpp @@ -3316,17 +3316,17 @@ char ** GDALDataset::GetMetadata(const char * pszDomain) } } - const unsigned int nbSupportedDerivedDS = GDALGetNumberOfDerivedDatasetDecriptions(); + unsigned int nbSupportedDerivedDS; + const DerivedDatasetDescription * poDDSDesc = GDALGetDerivedDatasetDescriptions(&nbSupportedDerivedDS); for(unsigned int derivedId = 0; derivedIdpszTargetPixelType) != "complex") + + if(hasAComplexBand || CPLString(poDDSDesc[derivedId].pszTargetPixelType) != "complex") { - oDerivedMetadataList.SetNameValue(CPLSPrintf("DERIVED_SUBDATASET_%i_NAME",derivedId),CPLSPrintf("DERIVED_SUBDATASET:%s:%s",poCurrentDerivedDatasetDescription->pszDatasetName,GetDescription())); + oDerivedMetadataList.SetNameValue(CPLSPrintf("DERIVED_SUBDATASET_%i_NAME",derivedId),CPLSPrintf("DERIVED_SUBDATASET:%s:%s",poDDSDesc[derivedId].pszDatasetName,GetDescription())); - CPLString osDesc(CPLSPrintf("%s from %s",poCurrentDerivedDatasetDescription->pszDatasetDescritpion,GetDescription())); + CPLString osDesc(CPLSPrintf("%s from %s",poDDSDesc[derivedId].pszDatasetDescritpion,GetDescription())); oDerivedMetadataList.SetNameValue(CPLSPrintf("DERIVED_SUBDATASET_%i_DESC",derivedId),osDesc.c_str()); } } From 17516f6d58f332197ca7c4db810a1b2662c9dca7 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Fri, 8 Jul 2016 13:42:14 +0200 Subject: [PATCH 54/69] TEST: Fix autotest for derived datasets --- autotest/gdrivers/derived.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/autotest/gdrivers/derived.py b/autotest/gdrivers/derived.py index 8d80968c4143..34e5f13e1156 100755 --- a/autotest/gdrivers/derived.py +++ b/autotest/gdrivers/derived.py @@ -3,7 +3,7 @@ # $Id$ # # Project: GDAL/OGR Test Suite -# Purpose: Test cderived driver +# Purpose: Test derived driver # Author: Julien Michel # ############################################################################### @@ -81,7 +81,7 @@ def derived_test(): gdaltest_list = [ - cderived_test + derived_test ] if __name__ == '__main__': From 153e215eee53673a86e1ebfbcd27ec1e13ed94c3 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Fri, 8 Jul 2016 13:48:45 +0200 Subject: [PATCH 55/69] TEST: Fix tests failing due to incorrect number of metadata domains --- autotest/gcore/tiff_read.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/autotest/gcore/tiff_read.py b/autotest/gcore/tiff_read.py index e5a89a53feb5..6f8c1d876ea2 100755 --- a/autotest/gcore/tiff_read.py +++ b/autotest/gcore/tiff_read.py @@ -1764,7 +1764,7 @@ def tiff_read_md1(): return 'fail' metadata = ds.GetMetadataDomainList() - if len(metadata) != 5: + if len(metadata) != 6: gdaltest.post_reason( 'did not get expected metadata list.' ) return 'fail' @@ -1813,7 +1813,7 @@ def tiff_read_md2(): return 'fail' metadata = ds.GetMetadataDomainList() - if len(metadata) != 5: + if len(metadata) != 6: gdaltest.post_reason( 'did not get expected metadata list.' ) return 'fail' @@ -1862,7 +1862,7 @@ def tiff_read_md3(): return 'fail' metadata = ds.GetMetadataDomainList() - if len(metadata) != 5: + if len(metadata) != 6: gdaltest.post_reason( 'did not get expected metadata list.' ) return 'fail' @@ -1911,7 +1911,7 @@ def tiff_read_md4(): return 'fail' metadata = ds.GetMetadataDomainList() - if len(metadata) != 5: + if len(metadata) != 6: gdaltest.post_reason( 'did not get expected metadata list.' ) return 'fail' @@ -1960,7 +1960,8 @@ def tiff_read_md5(): return 'fail' metadata = ds.GetMetadataDomainList() - if len(metadata) != 4: + print metadata + if len(metadata) != 5: gdaltest.post_reason( 'did not get expected metadata list.' ) return 'fail' @@ -2009,7 +2010,7 @@ def tiff_read_md6(): return 'fail' metadata = ds.GetMetadataDomainList() - if len(metadata) != 4: + if len(metadata) != 5: gdaltest.post_reason( 'did not get expected metadata list.' ) return 'fail' @@ -2058,7 +2059,7 @@ def tiff_read_md7(): return 'fail' metadata = ds.GetMetadataDomainList() - if len(metadata) != 4: + if len(metadata) != 5: gdaltest.post_reason( 'did not get expected metadata list.' ) return 'fail' @@ -2107,7 +2108,7 @@ def tiff_read_md8(): return 'fail' metadata = ds.GetMetadataDomainList() - if len(metadata) != 4: + if len(metadata) != 5: gdaltest.post_reason( 'did not get expected metadata list.' ) return 'fail' @@ -2156,7 +2157,7 @@ def tiff_read_md9(): return 'fail' metadata = ds.GetMetadataDomainList() - if len(metadata) != 5: + if len(metadata) != 6: gdaltest.post_reason( 'did not get expected metadata list.' ) return 'fail' @@ -2202,7 +2203,7 @@ def tiff_read_md10(): return 'fail' metadata = ds.GetMetadataDomainList() - if len(metadata) != 5: + if len(metadata) != 6: gdaltest.post_reason( 'did not get expected metadata list.' ) return 'fail' @@ -2251,7 +2252,7 @@ def tiff_read_md11(): return 'fail' metadata = ds.GetMetadataDomainList() - if len(metadata) != 5: + if len(metadata) != 6: gdaltest.post_reason( 'did not get expected metadata list.' ) return 'fail' @@ -2295,7 +2296,7 @@ def tiff_read_md12(): return 'fail' metadata = ds.GetMetadataDomainList() - if len(metadata) != 5: + if len(metadata) != 6: gdaltest.post_reason( 'did not get expected metadata list.' ) return 'fail' From b0abc50f3117c1d3c1ba083844bcf9c2659872af Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Fri, 8 Jul 2016 14:23:11 +0200 Subject: [PATCH 56/69] BUG: Fix geotransform setting --- gdal/frmts/derived/deriveddataset.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/gdal/frmts/derived/deriveddataset.cpp b/gdal/frmts/derived/deriveddataset.cpp index 891185350449..bc1c59347605 100644 --- a/gdal/frmts/derived/deriveddataset.cpp +++ b/gdal/frmts/derived/deriveddataset.cpp @@ -126,8 +126,9 @@ GDALDataset * DerivedDataset::Open(GDALOpenInfo * poOpenInfo) // Transfer geotransform double padfTransform[6]; - bool transformOk = poTmpDS->GetGeoTransform(padfTransform); - if(transformOk) + CPLErr transformOk = poTmpDS->GetGeoTransform(padfTransform); + + if(transformOk == CE_None) { poDS->SetGeoTransform(padfTransform); } From e1c072abb56fe533ba2b3e509501c2b968a83334 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Fri, 8 Jul 2016 14:44:14 +0200 Subject: [PATCH 57/69] BUG: We must also now the return type of the pixel function (for instance conj is CFloat64, but mod is Float64) --- gdal/frmts/derived/deriveddataset.cpp | 4 ++-- gdal/frmts/derived/derivedlist.c | 14 +++++++------- gdal/frmts/derived/derivedlist.h | 3 ++- gdal/gcore/gdaldataset.cpp | 2 +- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/gdal/frmts/derived/deriveddataset.cpp b/gdal/frmts/derived/deriveddataset.cpp index bc1c59347605..9f3505105ab1 100644 --- a/gdal/frmts/derived/deriveddataset.cpp +++ b/gdal/frmts/derived/deriveddataset.cpp @@ -81,6 +81,7 @@ GDALDataset * DerivedDataset::Open(GDALOpenInfo * poOpenInfo) bool datasetFound = false; unsigned int nbSupportedDerivedDS(0); + GDALDataType type = GDT_Float64; const DerivedDatasetDescription * poDDSDesc = GDALGetDerivedDatasetDescriptions(&nbSupportedDerivedDS); @@ -90,6 +91,7 @@ GDALDataset * DerivedDataset::Open(GDALOpenInfo * poOpenInfo) { datasetFound = true; pixelFunctionName = poDDSDesc[derivedId].pszPixelFunction; + type = GDALGetDataTypeByName(poDDSDesc[derivedId].pszOutputPixelType); } } @@ -142,8 +144,6 @@ GDALDataset * DerivedDataset::Open(GDALOpenInfo * poOpenInfo) for(int nBand = 1; nBand <= nbBands; ++nBand) { VRTDerivedRasterBand * poBand; - - GDALDataType type = GDT_Float64; poBand = new VRTDerivedRasterBand(poDS,nBand,type,nCols,nRows); poDS->SetBand(nBand,poBand); diff --git a/gdal/frmts/derived/derivedlist.c b/gdal/frmts/derived/derivedlist.c index 7cfcbe5313ef..cfc60a47ed58 100644 --- a/gdal/frmts/derived/derivedlist.c +++ b/gdal/frmts/derived/derivedlist.c @@ -32,13 +32,13 @@ CPL_C_START static const DerivedDatasetDescription asDDSDesc [] = { - { "AMPLITUDE", "Amplitude of input bands", "mod", "complex"}, - { "PHASE", "Phase of input bands", "phase", "complex"}, - { "REAL", "Real part of input bands", "real", "complex"}, - { "IMAG", "Imaginary part of input bands", "imag", "complex"}, - { "CONJ", "Conjugate of input bands", "conj", "complex"}, - { "INTENSITY", "Intensity (squared amplitude) of input bands", "intensity", "complex"}, - { "LOGAMPLITUDE", "log10 of amplitude of input bands", "log10", "all"} + { "AMPLITUDE", "Amplitude of input bands", "mod", "complex","Float64"}, + { "PHASE", "Phase of input bands", "phase", "complex","Float64"}, + { "REAL", "Real part of input bands", "real", "complex","Float64"}, + { "IMAG", "Imaginary part of input bands", "imag", "complex","Float64"}, + { "CONJ", "Conjugate of input bands", "conj", "complex","CFloat64"}, + { "INTENSITY", "Intensity (squared amplitude) of input bands", "intensity", "complex","Float64"}, + { "LOGAMPLITUDE", "log10 of amplitude of input bands", "log10", "all","Float64"} }; #define NB_DERIVED_DATASETS (sizeof(asDDSDesc)/sizeof(asDDSDesc[0])) diff --git a/gdal/frmts/derived/derivedlist.h b/gdal/frmts/derived/derivedlist.h index 3202f8860e6c..4d67c11d0457 100644 --- a/gdal/frmts/derived/derivedlist.h +++ b/gdal/frmts/derived/derivedlist.h @@ -37,7 +37,8 @@ typedef struct const char * pszDatasetName; const char * pszDatasetDescritpion; const char * pszPixelFunction; - const char * pszTargetPixelType; + const char * pszInputPixelType; + const char * pszOutputPixelType; } DerivedDatasetDescription; const DerivedDatasetDescription* CPL_DLL CPL_STDCALL GDALGetDerivedDatasetDescriptions(unsigned int * pnDescriptionCount); diff --git a/gdal/gcore/gdaldataset.cpp b/gdal/gcore/gdaldataset.cpp index 24652f95783f..83039473c6d9 100644 --- a/gdal/gcore/gdaldataset.cpp +++ b/gdal/gcore/gdaldataset.cpp @@ -3322,7 +3322,7 @@ char ** GDALDataset::GetMetadata(const char * pszDomain) for(unsigned int derivedId = 0; derivedId Date: Fri, 8 Jul 2016 14:44:42 +0200 Subject: [PATCH 58/69] TEST: More complete tests, including geoTransform, Proj and Checksum tests (from PR review) --- autotest/gdrivers/derived.py | 73 +++++++++++++++++++++++++++++++++--- 1 file changed, 68 insertions(+), 5 deletions(-) diff --git a/autotest/gdrivers/derived.py b/autotest/gdrivers/derived.py index 34e5f13e1156..770b3cd3bb5d 100755 --- a/autotest/gdrivers/derived.py +++ b/autotest/gdrivers/derived.py @@ -39,7 +39,7 @@ # Test opening a L1C product -def derived_test(): +def derived_test1(): filename = "data/cfloat64.tif" gdal.ErrorReset() ds = gdal.Open(filename) @@ -47,7 +47,8 @@ def derived_test(): gdaltest.post_reason('fail') return 'fail' got_dsds = ds.GetMetadata('DERIVED_SUBDATASETS') - + expected_gt = ds.GetGeoTransform() + expected_prj = ds.GetProjection() expected_dsds = {'DERIVED_SUBDATASET_0_NAME' : 'DERIVED_SUBDATASET:AMPLITUDE:data/cfloat64.tif', 'DERIVED_SUBDATASET_0_DESC' : 'Amplitude of input bands from data/cfloat64.tif', 'DERIVED_SUBDATASET_1_NAME' : 'DERIVED_SUBDATASET:PHASE:data/cfloat64.tif', @@ -69,19 +70,81 @@ def derived_test(): pprint.pprint(got_dsds) return 'fail' - for (key,val) in expected_dsds.iteritems(): if key.endswith('_NAME'): ds = gdal.Open(val) if ds is None or gdal.GetLastErrorMsg() != '': gdaltest.post_reason('fail') return 'fail' - + gt = ds.GetGeoTransform() + if gt != expected_gt: + gdaltest.post_reason('fail') + import pprint + pprint.pprint("Expected geotransform: "+str(expected_gt)+", got "+str(gt)) + return 'fail' + prj = ds.GetProjection() + if prj != expected_prj: + gdaltest.post_reason('fail') + import pprint + pprint.pprint("Expected projection: "+str(expected_prj)+", got: "+str(gt)) + return 'fail' return 'success' +def derived_test2(): + filename = "data/cint_sar.tif" + gdal.ErrorReset() + ds = gdal.Open(filename) + if ds is None or gdal.GetLastErrorMsg() != '': + gdaltest.post_reason('fail') + return 'fail' + got_dsds = ds.GetMetadata('DERIVED_SUBDATASETS') + expected_dsds = {'DERIVED_SUBDATASET_0_NAME' : 'DERIVED_SUBDATASET:AMPLITUDE:data/cint_sar.tif', + 'DERIVED_SUBDATASET_0_DESC' : 'Amplitude of input bands from data/cint_sar.tif', + 'DERIVED_SUBDATASET_1_NAME' : 'DERIVED_SUBDATASET:PHASE:data/cint_sar.tif', + 'DERIVED_SUBDATASET_1_DESC' : 'Phase of input bands from data/cint_sar.tif', + 'DERIVED_SUBDATASET_2_NAME' : 'DERIVED_SUBDATASET:REAL:data/cint_sar.tif', + 'DERIVED_SUBDATASET_2_DESC' : 'Real part of input bands from data/cint_sar.tif', + 'DERIVED_SUBDATASET_3_NAME' : 'DERIVED_SUBDATASET:IMAG:data/cint_sar.tif', + 'DERIVED_SUBDATASET_3_DESC' : 'Imaginary part of input bands from data/cint_sar.tif', + 'DERIVED_SUBDATASET_4_NAME' : 'DERIVED_SUBDATASET:CONJ:data/cint_sar.tif', + 'DERIVED_SUBDATASET_4_DESC' : 'Conjugate of input bands from data/cint_sar.tif', + 'DERIVED_SUBDATASET_5_NAME' : 'DERIVED_SUBDATASET:INTENSITY:data/cint_sar.tif', + 'DERIVED_SUBDATASET_5_DESC' : 'Intensity (squared amplitude) of input bands from data/cint_sar.tif', + 'DERIVED_SUBDATASET_6_NAME' : 'DERIVED_SUBDATASET:LOGAMPLITUDE:data/cint_sar.tif', + 'DERIVED_SUBDATASET_6_DESC' : 'log10 of amplitude of input bands from data/cint_sar.tif'} + + expected_cs = { 'DERIVED_SUBDATASET_0_NAME' : 345, + 'DERIVED_SUBDATASET_5_NAME' : 314, + 'DERIVED_SUBDATASET_3_NAME' : 142, + 'DERIVED_SUBDATASET_1_NAME' : 10, + 'DERIVED_SUBDATASET_6_NAME' : 99, + 'DERIVED_SUBDATASET_4_NAME' : 110, + 'DERIVED_SUBDATASET_2_NAME' : 159} + + if got_dsds != expected_dsds: + gdaltest.post_reason('fail') + import pprint + pprint.pprint(got_dsds) + return 'fail' + + for (key,val) in expected_dsds.iteritems(): + if key.endswith('_NAME'): + ds = gdal.Open(val) + if ds is None or gdal.GetLastErrorMsg() != '': + gdaltest.post_reason('fail') + return 'fail' + cs = ds.GetRasterBand(1).Checksum() + if expected_cs[key] != cs: + gdaltest.post_reason('fail') + import pprint + pprint.pprint("Expected checksum "+str(expected_cs[key])+", got "+str(cs)) + return 'fail' + + return 'success' gdaltest_list = [ - derived_test + derived_test1, + derived_test2 ] if __name__ == '__main__': From f6aaf6dbfce9c32a571bf54d3b82d69fe9789daf Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Fri, 8 Jul 2016 15:05:56 +0200 Subject: [PATCH 59/69] BUG: Ensure that DERIVED mdd is not reported twice --- gdal/gcore/gdaldataset.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/gdal/gcore/gdaldataset.cpp b/gdal/gcore/gdaldataset.cpp index 83039473c6d9..d15bfbb57291 100644 --- a/gdal/gcore/gdaldataset.cpp +++ b/gdal/gcore/gdaldataset.cpp @@ -3344,7 +3344,12 @@ char ** GDALDataset::GetMetadata(const char * pszDomain) char ** GDALDataset::GetMetadataDomainList() { char ** currentDomainList = CSLDuplicate(oMDMD.GetDomainList()); - currentDomainList = CSLAddString(currentDomainList,"DERIVED_SUBDATASETS"); + + // Ensure that we do not duplicate DERIVED domain + if(CSLFindString(currentDomainList,"DERIVED_SUBDATASETS")==-1) + { + currentDomainList = CSLAddString(currentDomainList,"DERIVED_SUBDATASETS"); + } return currentDomainList; } From 8b7793eb1ce6ba3bd9a9437786d58f2be973912b Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Fri, 8 Jul 2016 15:43:59 +0200 Subject: [PATCH 60/69] BUG: Only report DERIVED metadata domain if there is at least one raster band --- gdal/gcore/gdaldataset.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gdal/gcore/gdaldataset.cpp b/gdal/gcore/gdaldataset.cpp index d15bfbb57291..d03948cfaa9a 100644 --- a/gdal/gcore/gdaldataset.cpp +++ b/gdal/gcore/gdaldataset.cpp @@ -3346,7 +3346,7 @@ char ** GDALDataset::GetMetadataDomainList() char ** currentDomainList = CSLDuplicate(oMDMD.GetDomainList()); // Ensure that we do not duplicate DERIVED domain - if(CSLFindString(currentDomainList,"DERIVED_SUBDATASETS")==-1) + if(GetRasterCount()>0 && CSLFindString(currentDomainList,"DERIVED_SUBDATASETS")==-1) { currentDomainList = CSLAddString(currentDomainList,"DERIVED_SUBDATASETS"); } From 84afb22ce2d352fa6e944d5fd1a8d29069222a71 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Tue, 12 Jul 2016 12:17:44 +0200 Subject: [PATCH 61/69] TEST: Fixing tests failing because of new metadata domain DERIVED_SUBDATASETS --- autotest/gdrivers/gpkg.py | 2 +- autotest/gdrivers/jp2openjpeg.py | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/autotest/gdrivers/gpkg.py b/autotest/gdrivers/gpkg.py index 224fa7a0ddbe..ccf3b495d5b0 100755 --- a/autotest/gdrivers/gpkg.py +++ b/autotest/gdrivers/gpkg.py @@ -2069,7 +2069,7 @@ def gpkg_21(): out_ds = gdaltest.gpkg_dr.Create('tmp/tmp.gpkg', 1, 1) out_ds.SetGeoTransform([0,1,0,0,0,-1]) mddlist = out_ds.GetMetadataDomainList() - if len(mddlist) != 2: + if len(mddlist) != 3: gdaltest.post_reason('fail') print(mddlist) return 'fail' diff --git a/autotest/gdrivers/jp2openjpeg.py b/autotest/gdrivers/jp2openjpeg.py index dcb7dc5a667d..f2637cca6001 100755 --- a/autotest/gdrivers/jp2openjpeg.py +++ b/autotest/gdrivers/jp2openjpeg.py @@ -2094,7 +2094,7 @@ def jp2openjpeg_42(): if ds.GetGCPCount() != 1: gdaltest.post_reason('fail') return 'fail' - if len(ds.GetMetadataDomainList()) != 1 : + if len(ds.GetMetadataDomainList()) != 2 : gdaltest.post_reason('fail') print(ds.GetMetadataDomainList()) return 'fail' @@ -2115,7 +2115,7 @@ def jp2openjpeg_42(): if ds.GetGCPCount() != 0: gdaltest.post_reason('fail') return 'fail' - if ds.GetMetadataDomainList() is not None : + if ds.GetMetadataDomainList() != ['', 'DERIVED_SUBDATASETS']: gdaltest.post_reason('fail') print(ds.GetMetadataDomainList()) return 'fail' @@ -2138,7 +2138,7 @@ def jp2openjpeg_42(): print(ds.GetGeoTransform()) return 'fail' # Check that we have a GMLJP2 box - if ds.GetMetadataDomainList() != ['xml:gml.root-instance'] : + if ds.GetMetadataDomainList() != ['xml:gml.root-instance','', 'DERIVED_SUBDATASETS'] : gdaltest.post_reason('fail') print(ds.GetMetadataDomainList()) return 'fail' @@ -2160,7 +2160,7 @@ def jp2openjpeg_42(): gdaltest.post_reason('fail') print(ds.GetGeoTransform()) return 'fail' - if ds.GetMetadataDomainList() is not None: + if ds.GetMetadataDomainList() != ['', 'DERIVED_SUBDATASETS']: gdaltest.post_reason('fail') print(ds.GetMetadataDomainList()) return 'fail' @@ -2187,7 +2187,7 @@ def jp2openjpeg_42(): gdaltest.post_reason('fail') print(ds.GetGeoTransform()) return 'fail' - if ds.GetMetadataDomainList() is not None: + if ds.GetMetadataDomainList() != ['', 'DERIVED_SUBDATASETS']: gdaltest.post_reason('fail') print(ds.GetMetadataDomainList()) return 'fail' @@ -2218,7 +2218,7 @@ def jp2openjpeg_42(): gdaltest.post_reason('fail') print(ds.GetGeoTransform()) return 'fail' - if ds.GetMetadataDomainList() is None: + if ds.GetMetadataDomainList() != ['xml:gml.root-instance', '', 'DERIVED_SUBDATASETS']: gdaltest.post_reason('fail') print(ds.GetMetadataDomainList()) return 'fail' @@ -2235,7 +2235,7 @@ def jp2openjpeg_42(): if len(ds.GetGCPs()) == 0: gdaltest.post_reason('fail') return 'fail' - if ds.GetMetadataDomainList() is not None: + if ds.GetMetadataDomainList() != ['', 'DERIVED_SUBDATASETS']: gdaltest.post_reason('fail') print(ds.GetMetadataDomainList()) return 'fail' From 3fcc55249cda7ed40d08321c07c57caefd0a1195 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Tue, 12 Jul 2016 14:20:24 +0200 Subject: [PATCH 62/69] ENH: Add the DERIVED_SUBDATASETS domain to the filtered domain in gdaljp2metadata --- gdal/gcore/gdaljp2metadata.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/gdal/gcore/gdaljp2metadata.cpp b/gdal/gcore/gdaljp2metadata.cpp index 2dd7cd0ee49e..326a378ba673 100644 --- a/gdal/gcore/gdaljp2metadata.cpp +++ b/gdal/gcore/gdaljp2metadata.cpp @@ -3040,6 +3040,7 @@ CPLXMLNode* GDALJP2Metadata::CreateGDALMultiDomainMetadataXML( { if( !EQUAL(*papszMDListIter, "") && !EQUAL(*papszMDListIter, "IMAGE_STRUCTURE") && + !EQUAL(*papszMDListIter, "DERIVED_SUBDATASETS") && !EQUAL(*papszMDListIter, "JPEG2000") && !STARTS_WITH_CI(*papszMDListIter, "xml:BOX_") && !EQUAL(*papszMDListIter, "xml:gml.root-instance") && From 5b785c481c22c257f46ab1fc3ee6c8930dcdcad6 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Tue, 12 Jul 2016 14:20:40 +0200 Subject: [PATCH 63/69] TEST: Fix remaining jp2openjpeg failing tests --- autotest/gdrivers/jp2openjpeg.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/autotest/gdrivers/jp2openjpeg.py b/autotest/gdrivers/jp2openjpeg.py index f2637cca6001..76c87240b875 100755 --- a/autotest/gdrivers/jp2openjpeg.py +++ b/autotest/gdrivers/jp2openjpeg.py @@ -2115,7 +2115,7 @@ def jp2openjpeg_42(): if ds.GetGCPCount() != 0: gdaltest.post_reason('fail') return 'fail' - if ds.GetMetadataDomainList() != ['', 'DERIVED_SUBDATASETS']: + if ds.GetMetadataDomainList() != ['DERIVED_SUBDATASETS']: gdaltest.post_reason('fail') print(ds.GetMetadataDomainList()) return 'fail' @@ -2138,7 +2138,7 @@ def jp2openjpeg_42(): print(ds.GetGeoTransform()) return 'fail' # Check that we have a GMLJP2 box - if ds.GetMetadataDomainList() != ['xml:gml.root-instance','', 'DERIVED_SUBDATASETS'] : + if ds.GetMetadataDomainList() != ['xml:gml.root-instance', 'DERIVED_SUBDATASETS'] : gdaltest.post_reason('fail') print(ds.GetMetadataDomainList()) return 'fail' @@ -2160,7 +2160,7 @@ def jp2openjpeg_42(): gdaltest.post_reason('fail') print(ds.GetGeoTransform()) return 'fail' - if ds.GetMetadataDomainList() != ['', 'DERIVED_SUBDATASETS']: + if ds.GetMetadataDomainList() != ['DERIVED_SUBDATASETS']: gdaltest.post_reason('fail') print(ds.GetMetadataDomainList()) return 'fail' @@ -2187,7 +2187,7 @@ def jp2openjpeg_42(): gdaltest.post_reason('fail') print(ds.GetGeoTransform()) return 'fail' - if ds.GetMetadataDomainList() != ['', 'DERIVED_SUBDATASETS']: + if ds.GetMetadataDomainList() != ['DERIVED_SUBDATASETS']: gdaltest.post_reason('fail') print(ds.GetMetadataDomainList()) return 'fail' @@ -2218,7 +2218,7 @@ def jp2openjpeg_42(): gdaltest.post_reason('fail') print(ds.GetGeoTransform()) return 'fail' - if ds.GetMetadataDomainList() != ['xml:gml.root-instance', '', 'DERIVED_SUBDATASETS']: + if ds.GetMetadataDomainList() != ['xml:gml.root-instance', 'DERIVED_SUBDATASETS']: gdaltest.post_reason('fail') print(ds.GetMetadataDomainList()) return 'fail' @@ -2235,7 +2235,7 @@ def jp2openjpeg_42(): if len(ds.GetGCPs()) == 0: gdaltest.post_reason('fail') return 'fail' - if ds.GetMetadataDomainList() != ['', 'DERIVED_SUBDATASETS']: + if ds.GetMetadataDomainList() != ['DERIVED_SUBDATASETS']: gdaltest.post_reason('fail') print(ds.GetMetadataDomainList()) return 'fail' From eba1603f4737cb5d24e884c7b3a6a477b2b8329d Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Tue, 12 Jul 2016 15:09:53 +0200 Subject: [PATCH 64/69] COMP: Remove pixel function C file, as it has been renamed to .cpp --- gdal/frmts/vrt/pixelfunctions.c | 940 -------------------------------- 1 file changed, 940 deletions(-) delete mode 100644 gdal/frmts/vrt/pixelfunctions.c diff --git a/gdal/frmts/vrt/pixelfunctions.c b/gdal/frmts/vrt/pixelfunctions.c deleted file mode 100644 index 677cd95694c0..000000000000 --- a/gdal/frmts/vrt/pixelfunctions.c +++ /dev/null @@ -1,940 +0,0 @@ -/****************************************************************************** - * - * Project: GDAL - * Purpose: Implementation of a set of GDALDerivedPixelFunc(s) to be used - * with source raster band of virtual GDAL datasets. - * Author: Antonio Valentino - * - ****************************************************************************** - * Copyright (c) 2008-2014 Antonio Valentino - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - *****************************************************************************/ - -#include -#include -#include "gdal_vrt.h" - -static CPLErr RealPixelFunc(void **papoSources, int nSources, void *pData, - int nXSize, int nYSize, - GDALDataType eSrcType, GDALDataType eBufType, - int nPixelSpace, int nLineSpace); - -static CPLErr ImagPixelFunc(void **papoSources, int nSources, void *pData, - int nXSize, int nYSize, - GDALDataType eSrcType, GDALDataType eBufType, - int nPixelSpace, int nLineSpace); - -static CPLErr ComplexPixelFunc(void **papoSources, int nSources, void *pData, - int nXSize, int nYSize, - GDALDataType eSrcType, GDALDataType eBufType, - int nPixelSpace, int nLineSpace); - -static CPLErr ModulePixelFunc(void **papoSources, int nSources, void *pData, - int nXSize, int nYSize, - GDALDataType eSrcType, GDALDataType eBufType, - int nPixelSpace, int nLineSpace); - -static CPLErr PhasePixelFunc(void **papoSources, int nSources, void *pData, - int nXSize, int nYSize, - GDALDataType eSrcType, GDALDataType eBufType, - int nPixelSpace, int nLineSpace); - -static CPLErr ConjPixelFunc(void **papoSources, int nSources, void *pData, - int nXSize, int nYSize, - GDALDataType eSrcType, GDALDataType eBufType, - int nPixelSpace, int nLineSpace); - -static CPLErr SumPixelFunc(void **papoSources, int nSources, void *pData, - int nXSize, int nYSize, - GDALDataType eSrcType, GDALDataType eBufType, - int nPixelSpace, int nLineSpace); - -static CPLErr DiffPixelFunc(void **papoSources, int nSources, void *pData, - int nXSize, int nYSize, - GDALDataType eSrcType, GDALDataType eBufType, - int nPixelSpace, int nLineSpace); - -static CPLErr MulPixelFunc(void **papoSources, int nSources, void *pData, - int nXSize, int nYSize, - GDALDataType eSrcType, GDALDataType eBufType, - int nPixelSpace, int nLineSpace); - -static CPLErr CMulPixelFunc(void **papoSources, int nSources, void *pData, - int nXSize, int nYSize, - GDALDataType eSrcType, GDALDataType eBufType, - int nPixelSpace, int nLineSpace); - -static CPLErr InvPixelFunc(void **papoSources, int nSources, void *pData, - int nXSize, int nYSize, - GDALDataType eSrcType, GDALDataType eBufType, - int nPixelSpace, int nLineSpace); - -static CPLErr IntensityPixelFunc(void **papoSources, int nSources, void *pData, - int nXSize, int nYSize, - GDALDataType eSrcType, GDALDataType eBufType, - int nPixelSpace, int nLineSpace); - -static CPLErr SqrtPixelFunc(void **papoSources, int nSources, void *pData, - int nXSize, int nYSize, - GDALDataType eSrcType, GDALDataType eBufType, - int nPixelSpace, int nLineSpace); - -static CPLErr Log10PixelFunc(void **papoSources, int nSources, void *pData, - int nXSize, int nYSize, - GDALDataType eSrcType, GDALDataType eBufType, - int nPixelSpace, int nLineSpace); - -static CPLErr dB2AmpPixelFunc(void **papoSources, int nSources, void *pData, - int nXSize, int nYSize, - GDALDataType eSrcType, GDALDataType eBufType, - int nPixelSpace, int nLineSpace); - -static CPLErr dB2PowPixelFunc(void **papoSources, int nSources, void *pData, - int nXSize, int nYSize, - GDALDataType eSrcType, GDALDataType eBufType, - int nPixelSpace, int nLineSpace); - -static CPLErr PowPixelFuncHelper(void **papoSources, int nSources, void *pData, - int nXSize, int nYSize, - GDALDataType eSrcType, GDALDataType eBufType, - int nPixelSpace, int nLineSpace, - double base, double fact); - -static CPLErr RealPixelFunc(void **papoSources, int nSources, void *pData, - int nXSize, int nYSize, - GDALDataType eSrcType, GDALDataType eBufType, - int nPixelSpace, int nLineSpace) -{ - int iLine, nPixelSpaceSrc, nLineSpaceSrc; - - /* ---- Init ---- */ - if (nSources != 1) return CE_Failure; - - nPixelSpaceSrc = GDALGetDataTypeSize( eSrcType ) / 8; - nLineSpaceSrc = nPixelSpaceSrc * nXSize; - - /* ---- Set pixels ---- */ - for( iLine = 0; iLine < nYSize; ++iLine ) { - GDALCopyWords(((GByte *)papoSources[0]) + nLineSpaceSrc * iLine, - eSrcType, nPixelSpaceSrc, - ((GByte *)pData) + nLineSpace * iLine, - eBufType, nPixelSpace, nXSize); - } - - /* ---- Return success ---- */ - return CE_None; -} /* RealPixelFunc */ - - -static CPLErr ImagPixelFunc(void **papoSources, int nSources, void *pData, - int nXSize, int nYSize, - GDALDataType eSrcType, GDALDataType eBufType, - int nPixelSpace, int nLineSpace) -{ - int iLine; - - /* ---- Init ---- */ - if (nSources != 1) return CE_Failure; - - if (GDALDataTypeIsComplex( eSrcType )) - { - int nPixelSpaceSrc = GDALGetDataTypeSize( eSrcType ) / 8; - int nLineSpaceSrc = nPixelSpaceSrc * nXSize; - - void* pImag = ((GByte *)papoSources[0]) - + GDALGetDataTypeSize( eSrcType ) / 8 / 2; - - /* ---- Set pixels ---- */ - for( iLine = 0; iLine < nYSize; ++iLine ) { - GDALCopyWords(((GByte *)pImag) + nLineSpaceSrc * iLine, - eSrcType, nPixelSpaceSrc, - ((GByte *)pData) + nLineSpace * iLine, - eBufType, nPixelSpace, nXSize); - } - } else { - double dfImag = 0; - - /* ---- Set pixels ---- */ - for( iLine = 0; iLine < nYSize; ++iLine ) { - /* always copy from the same location */ - GDALCopyWords(&dfImag, eSrcType, 0, - ((GByte *)pData) + nLineSpace * iLine, - eBufType, nPixelSpace, nXSize); - } - } - - /* ---- Return success ---- */ - return CE_None; -} /* ImagPixelFunc */ - - -static CPLErr ComplexPixelFunc(void **papoSources, int nSources, void *pData, - int nXSize, int nYSize, - GDALDataType eSrcType, GDALDataType eBufType, - int nPixelSpace, int nLineSpace) -{ - int ii, iLine, iCol; - double adfPixVal[2]; - - void *pReal = papoSources[0]; - void *pImag = papoSources[1]; - - /* ---- Init ---- */ - if (nSources != 2) return CE_Failure; - - /* ---- Set pixels ---- */ - for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { - for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { - - /* Source raster pixels may be obtained with SRCVAL macro */ - adfPixVal[0] = SRCVAL(pReal, eSrcType, ii); /* re */ - adfPixVal[1] = SRCVAL(pImag, eSrcType, ii); /* im */ - - GDALCopyWords(adfPixVal, GDT_CFloat64, 0, - ((GByte *)pData) + nLineSpace * iLine + - iCol * nPixelSpace, eBufType, nPixelSpace, 1); - } - } - - /* ---- Return success ---- */ - return CE_None; -} /* MakeComplexPixelFunc */ - - -static CPLErr ModulePixelFunc(void **papoSources, int nSources, void *pData, - int nXSize, int nYSize, - GDALDataType eSrcType, GDALDataType eBufType, - int nPixelSpace, int nLineSpace) -{ - int ii, iLine, iCol; - double dfPixVal; - - /* ---- Init ---- */ - if (nSources != 1) return CE_Failure; - - if (GDALDataTypeIsComplex( eSrcType )) - { - double dfReal, dfImag; - void *pReal = papoSources[0]; - void *pImag = ((GByte *)papoSources[0]) - + GDALGetDataTypeSize( eSrcType ) / 8 / 2; - - /* ---- Set pixels ---- */ - for( iLine = 0, ii = 0; iLine < nYSize; ++iLine ) { - for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { - - /* Source raster pixels may be obtained with SRCVAL macro */ - dfReal = SRCVAL(pReal, eSrcType, ii); - dfImag = SRCVAL(pImag, eSrcType, ii); - - dfPixVal = sqrt( dfReal * dfReal + dfImag * dfImag ); - - GDALCopyWords(&dfPixVal, GDT_Float64, 0, - ((GByte *)pData) + nLineSpace * iLine + - iCol * nPixelSpace, eBufType, nPixelSpace, 1); - } - } - } else { - /* ---- Set pixels ---- */ - for( iLine = 0, ii = 0; iLine < nYSize; ++iLine ) { - for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { - - /* Source raster pixels may be obtained with SRCVAL macro */ - dfPixVal = fabs(SRCVAL(papoSources[0], eSrcType, ii)); - - GDALCopyWords(&dfPixVal, GDT_Float64, 0, - ((GByte *)pData) + nLineSpace * iLine + - iCol * nPixelSpace, eBufType, nPixelSpace, 1); - } - } - } - - /* ---- Return success ---- */ - return CE_None; -} /* ModulePixelFunc */ - - -static CPLErr PhasePixelFunc(void **papoSources, int nSources, void *pData, - int nXSize, int nYSize, - GDALDataType eSrcType, GDALDataType eBufType, - int nPixelSpace, int nLineSpace) -{ - int ii, iLine, iCol; - double dfPixVal, dfReal; - - /* ---- Init ---- */ - if (nSources != 1) return CE_Failure; - - if (GDALDataTypeIsComplex( eSrcType )) - { - double dfImag; - void *pReal = papoSources[0]; - void *pImag = ((GByte *)papoSources[0]) - + GDALGetDataTypeSize( eSrcType ) / 8 / 2; - - /* ---- Set pixels ---- */ - for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { - for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { - - /* Source raster pixels may be obtained with SRCVAL macro */ - dfReal = SRCVAL(pReal, eSrcType, ii); - dfImag = SRCVAL(pImag, eSrcType, ii); - - dfPixVal = atan2(dfImag, dfReal); - - GDALCopyWords(&dfPixVal, GDT_Float64, 0, - ((GByte *)pData) + nLineSpace * iLine + - iCol * nPixelSpace, eBufType, nPixelSpace, 1); - } - } - } else { - /* ---- Set pixels ---- */ - /* - for( iLine = 0; iLine < nYSize; ++iLine ) { - / * always copy from the same location * / - GDALCopyWords(&dfImag, eSrcType, 0, - ((GByte *)pData) + nLineSpace * iLine, - eBufType, nPixelSpace, nXSize); - } - */ - /* ---- Set pixels ---- */ - double pi = atan2(0, -1); - for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { - for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { - - void *pReal = papoSources[0]; - - /* Source raster pixels may be obtained with SRCVAL macro */ - dfReal = SRCVAL(pReal, eSrcType, ii); - dfPixVal = (dfReal < 0) ? pi : 0; - - GDALCopyWords(&dfPixVal, GDT_Float64, dfPixVal, - ((GByte *)pData) + nLineSpace * iLine + - iCol * nPixelSpace, eBufType, nPixelSpace, 1); - } - } - } - - /* ---- Return success ---- */ - return CE_None; -} /* PhasePixelFunc */ - - -static CPLErr ConjPixelFunc(void **papoSources, int nSources, void *pData, - int nXSize, int nYSize, - GDALDataType eSrcType, GDALDataType eBufType, - int nPixelSpace, int nLineSpace) -{ - /* ---- Init ---- */ - if (nSources != 1) return CE_Failure; - - if (GDALDataTypeIsComplex( eSrcType ) && GDALDataTypeIsComplex( eBufType )) - { - int iLine, iCol, ii; - double adfPixVal[2]; - int nOffset = GDALGetDataTypeSize( eSrcType ) / 8 / 2; - void *pReal = papoSources[0]; - void *pImag = ((GByte *)papoSources[0]) + nOffset; - - /* ---- Set pixels ---- */ - for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { - for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { - - /* Source raster pixels may be obtained with SRCVAL macro */ - adfPixVal[0] = +SRCVAL(pReal, eSrcType, ii); /* re */ - adfPixVal[1] = -SRCVAL(pImag, eSrcType, ii); /* im */ - - GDALCopyWords(adfPixVal, GDT_CFloat64, 0, - ((GByte *)pData) + nLineSpace * iLine + - iCol * nPixelSpace, eBufType, nPixelSpace, 1); - } - } - } else { - /* no complex data type */ - return RealPixelFunc(papoSources, nSources, pData, nXSize, nYSize, - eSrcType, eBufType, nPixelSpace, nLineSpace); - } - - /* ---- Return success ---- */ - return CE_None; -} /* ConjPixelFunc */ - - -static CPLErr SumPixelFunc(void **papoSources, int nSources, void *pData, - int nXSize, int nYSize, - GDALDataType eSrcType, GDALDataType eBufType, - int nPixelSpace, int nLineSpace) -{ - int iLine, iCol, ii, iSrc; - - /* ---- Init ---- */ - if (nSources < 2) return CE_Failure; - - /* ---- Set pixels ---- */ - if (GDALDataTypeIsComplex( eSrcType )) - { - double adfSum[2]; - void *pReal, *pImag; - int nOffset = GDALGetDataTypeSize( eSrcType ) / 8 / 2; - - /* ---- Set pixels ---- */ - for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { - for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { - adfSum[0] = 0; - adfSum[1] = 0; - - for( iSrc = 0; iSrc < nSources; ++iSrc ) { - pReal = papoSources[iSrc]; - pImag = ((GByte *)pReal) + nOffset; - - /* Source raster pixels may be obtained with SRCVAL macro */ - adfSum[0] += SRCVAL(pReal, eSrcType, ii); - adfSum[1] += SRCVAL(pImag, eSrcType, ii); - } - - GDALCopyWords(adfSum, GDT_CFloat64, 0, - ((GByte *)pData) + nLineSpace * iLine + - iCol * nPixelSpace, eBufType, nPixelSpace, 1); - } - } - } else { - /* non complex */ - double dfSum; - - /* ---- Set pixels ---- */ - for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { - for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { - dfSum = 0; - - for( iSrc = 0; iSrc < nSources; ++iSrc ) { - /* Source raster pixels may be obtained with SRCVAL macro */ - dfSum += SRCVAL(papoSources[iSrc], eSrcType, ii); - } - - GDALCopyWords(&dfSum, GDT_Float64, 0, - ((GByte *)pData) + nLineSpace * iLine + - iCol * nPixelSpace, eBufType, nPixelSpace, 1); - } - } - } - - /* ---- Return success ---- */ - return CE_None; -} /* SumPixelFunc */ - - -static CPLErr DiffPixelFunc(void **papoSources, int nSources, void *pData, - int nXSize, int nYSize, - GDALDataType eSrcType, GDALDataType eBufType, - int nPixelSpace, int nLineSpace) -{ - int ii, iLine, iCol; - - /* ---- Init ---- */ - if (nSources != 2) return CE_Failure; - - if (GDALDataTypeIsComplex( eSrcType )) - { - - double adfPixVal[2]; - int nOffset = GDALGetDataTypeSize( eSrcType ) / 8 / 2; - void *pReal0 = papoSources[0]; - void *pImag0 = ((GByte *)papoSources[0]) + nOffset; - void *pReal1 = papoSources[1]; - void *pImag1 = ((GByte *)papoSources[1]) + nOffset; - - /* ---- Set pixels ---- */ - for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { - for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { - - /* Source raster pixels may be obtained with SRCVAL macro */ - adfPixVal[0] = SRCVAL(pReal0, eSrcType, ii) - - SRCVAL(pReal1, eSrcType, ii); - adfPixVal[1] = SRCVAL(pImag0, eSrcType, ii) - - SRCVAL(pImag1, eSrcType, ii); - - GDALCopyWords(adfPixVal, GDT_CFloat64, 0, - ((GByte *)pData) + nLineSpace * iLine + - iCol * nPixelSpace, eBufType, nPixelSpace, 1); - } - } - } else { - /* non complex */ - double dfPixVal; - - /* ---- Set pixels ---- */ - for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { - for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { - - /* Source raster pixels may be obtained with SRCVAL macro */ - dfPixVal = SRCVAL(papoSources[0], eSrcType, ii) - - SRCVAL(papoSources[1], eSrcType, ii); - - GDALCopyWords(&dfPixVal, GDT_Float64, 0, - ((GByte *)pData) + nLineSpace * iLine + - iCol * nPixelSpace, eBufType, nPixelSpace, 1); - } - } - } - - /* ---- Return success ---- */ - return CE_None; -} /* DiffPixelFunc */ - - -static CPLErr MulPixelFunc(void **papoSources, int nSources, void *pData, - int nXSize, int nYSize, - GDALDataType eSrcType, GDALDataType eBufType, - int nPixelSpace, int nLineSpace) -{ - int iLine, iCol, ii, iSrc; - - /* ---- Init ---- */ - if (nSources < 2) return CE_Failure; - - /* ---- Set pixels ---- */ - if (GDALDataTypeIsComplex( eSrcType )) - { - double adfPixVal[2], dfOldR, dfOldI, dfNewR, dfNewI; - void *pReal, *pImag; - int nOffset = GDALGetDataTypeSize( eSrcType ) / 8 / 2; - - /* ---- Set pixels ---- */ - for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { - for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { - adfPixVal[0] = 1.; - adfPixVal[1] = 0.; - - for( iSrc = 0; iSrc < nSources; ++iSrc ) { - pReal = papoSources[iSrc]; - pImag = ((GByte *)pReal) + nOffset; - - dfOldR = adfPixVal[0]; - dfOldI = adfPixVal[1]; - - /* Source raster pixels may be obtained with SRCVAL macro */ - dfNewR = SRCVAL(pReal, eSrcType, ii); - dfNewI = SRCVAL(pImag, eSrcType, ii); - - adfPixVal[0] = dfOldR * dfNewR - dfOldI * dfNewI; - adfPixVal[1] = dfOldR * dfNewI + dfOldI * dfNewR; - } - - GDALCopyWords(adfPixVal, GDT_CFloat64, 0, - ((GByte *)pData) + nLineSpace * iLine + - iCol * nPixelSpace, eBufType, nPixelSpace, 1); - } - } - } else { - /* non complex */ - double dfPixVal; - - /* ---- Set pixels ---- */ - for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { - for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { - dfPixVal = 1; - - for( iSrc = 0; iSrc < nSources; ++iSrc ) { - /* Source raster pixels may be obtained with SRCVAL macro */ - dfPixVal *= SRCVAL(papoSources[iSrc], eSrcType, ii); - } - - GDALCopyWords(&dfPixVal, GDT_Float64, 0, - ((GByte *)pData) + nLineSpace * iLine + - iCol * nPixelSpace, eBufType, nPixelSpace, 1); - } - } - } - - /* ---- Return success ---- */ - return CE_None; -} /* MulPixelFunc */ - - -static CPLErr CMulPixelFunc(void **papoSources, int nSources, void *pData, - int nXSize, int nYSize, - GDALDataType eSrcType, GDALDataType eBufType, - int nPixelSpace, int nLineSpace) -{ - int ii, iLine, iCol; - - /* ---- Init ---- */ - if (nSources != 2) return CE_Failure; - - /* ---- Set pixels ---- */ - if (GDALDataTypeIsComplex( eSrcType )) - { - double adfPixVal[2], dfReal0, dfImag0, dfReal1, dfImag1; - - int nOffset = GDALGetDataTypeSize( eSrcType ) / 8 / 2; - void *pReal0 = papoSources[0]; - void *pImag0 = ((GByte *)papoSources[0]) + nOffset; - void *pReal1 = papoSources[1]; - void *pImag1 = ((GByte *)papoSources[1]) + nOffset; - - for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { - for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { - - /* Source raster pixels may be obtained with SRCVAL macro */ - dfReal0 = SRCVAL(pReal0, eSrcType, ii); - dfReal1 = SRCVAL(pReal1, eSrcType, ii); - dfImag0 = SRCVAL(pImag0, eSrcType, ii); - dfImag1 = SRCVAL(pImag1, eSrcType, ii); - adfPixVal[0] = dfReal0 * dfReal1 + dfImag0 * dfImag1; - adfPixVal[1] = dfReal1 * dfImag0 - dfReal0 * dfImag1; - - GDALCopyWords(adfPixVal, GDT_CFloat64, 0, - ((GByte *)pData) + nLineSpace * iLine + - iCol * nPixelSpace, eBufType, nPixelSpace, 1); - } - } - } else { - /* non complex */ - double adfPixVal[2] = {0, 0}; - - for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { - for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { - - /* Source raster pixels may be obtained with SRCVAL macro */ - adfPixVal[0] = SRCVAL(papoSources[0], eSrcType, ii) - * SRCVAL(papoSources[1], eSrcType, ii); - - GDALCopyWords(adfPixVal, GDT_CFloat64, 0, - ((GByte *)pData) + nLineSpace * iLine + - iCol * nPixelSpace, eBufType, nPixelSpace, 1); - } - } - } - - /* ---- Return success ---- */ - return CE_None; -} /* CMulPixelFunc */ - - -static CPLErr InvPixelFunc(void **papoSources, int nSources, void *pData, - int nXSize, int nYSize, - GDALDataType eSrcType, GDALDataType eBufType, - int nPixelSpace, int nLineSpace) -{ - int iLine, iCol, ii; - - /* ---- Init ---- */ - if (nSources != 1) return CE_Failure; - - /* ---- Set pixels ---- */ - if (GDALDataTypeIsComplex( eSrcType )) - { - double adfPixVal[2], dfReal, dfImag, dfAux; - - int nOffset = GDALGetDataTypeSize( eSrcType ) / 8 / 2; - void *pReal = papoSources[0]; - void *pImag = ((GByte *)papoSources[0]) + nOffset; - - for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { - for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { - - /* Source raster pixels may be obtained with SRCVAL macro */ - dfReal = SRCVAL(pReal, eSrcType, ii); - dfImag = SRCVAL(pImag, eSrcType, ii); - dfAux = dfReal * dfReal + dfImag * dfImag; - adfPixVal[0] = +dfReal / dfAux; - adfPixVal[1] = -dfImag / dfAux; - - GDALCopyWords(adfPixVal, GDT_CFloat64, 0, - ((GByte *)pData) + nLineSpace * iLine + - iCol * nPixelSpace, eBufType, nPixelSpace, 1); - } - } - } else { - /* non complex */ - double dfPixVal; - - /* ---- Set pixels ---- */ - for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { - for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { - - /* Source raster pixels may be obtained with SRCVAL macro */ - dfPixVal = 1. / SRCVAL(papoSources[0], eSrcType, ii); - - GDALCopyWords(&dfPixVal, GDT_Float64, 0, - ((GByte *)pData) + nLineSpace * iLine + - iCol * nPixelSpace, eBufType, nPixelSpace, 1); - } - } - } - - /* ---- Return success ---- */ - return CE_None; -} /* InvPixelFunc */ - - -static CPLErr IntensityPixelFunc(void **papoSources, int nSources, void *pData, - int nXSize, int nYSize, - GDALDataType eSrcType, GDALDataType eBufType, - int nPixelSpace, int nLineSpace) -{ - int ii, iLine, iCol; - double dfPixVal; - - /* ---- Init ---- */ - if (nSources != 1) return CE_Failure; - - if (GDALDataTypeIsComplex( eSrcType )) - { - double dfReal, dfImag; - int nOffset = GDALGetDataTypeSize( eSrcType ) / 8 / 2; - void *pReal = papoSources[0]; - void *pImag = ((GByte *)papoSources[0]) + nOffset; - - /* ---- Set pixels ---- */ - for( iLine = 0, ii = 0; iLine < nYSize; ++iLine ) { - for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { - - /* Source raster pixels may be obtained with SRCVAL macro */ - dfReal = SRCVAL(pReal, eSrcType, ii); - dfImag = SRCVAL(pImag, eSrcType, ii); - - dfPixVal = dfReal * dfReal + dfImag * dfImag; - - GDALCopyWords(&dfPixVal, GDT_Float64, 0, - ((GByte *)pData) + nLineSpace * iLine + - iCol * nPixelSpace, eBufType, nPixelSpace, 1); - } - } - } else { - /* ---- Set pixels ---- */ - for( iLine = 0, ii = 0; iLine < nYSize; ++iLine ) { - for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { - - /* Source raster pixels may be obtained with SRCVAL macro */ - dfPixVal = SRCVAL(papoSources[0], eSrcType, ii); - dfPixVal *= dfPixVal; - - GDALCopyWords(&dfPixVal, GDT_Float64, 0, - ((GByte *)pData) + nLineSpace * iLine + - iCol * nPixelSpace, eBufType, nPixelSpace, 1); - } - } - } - - /* ---- Return success ---- */ - return CE_None; -} /* IntensityPixelFunc */ - - -static CPLErr SqrtPixelFunc(void **papoSources, int nSources, void *pData, - int nXSize, int nYSize, - GDALDataType eSrcType, GDALDataType eBufType, - int nPixelSpace, int nLineSpace) -{ - int iLine, iCol, ii; - double dfPixVal; - - /* ---- Init ---- */ - if (nSources != 1) return CE_Failure; - if (GDALDataTypeIsComplex( eSrcType )) return CE_Failure; - - /* ---- Set pixels ---- */ - for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { - for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { - - /* Source raster pixels may be obtained with SRCVAL macro */; - dfPixVal = sqrt( SRCVAL(papoSources[0], eSrcType, ii) ); - - GDALCopyWords(&dfPixVal, GDT_Float64, 0, - ((GByte *)pData) + nLineSpace * iLine + - iCol * nPixelSpace, eBufType, nPixelSpace, 1); - } - } - - /* ---- Return success ---- */ - return CE_None; -} /* SqrtPixelFunc */ - - -static CPLErr Log10PixelFunc(void **papoSources, int nSources, void *pData, - int nXSize, int nYSize, - GDALDataType eSrcType, GDALDataType eBufType, - int nPixelSpace, int nLineSpace) -{ - int ii, iLine, iCol; - - /* ---- Init ---- */ - if (nSources != 1) return CE_Failure; - - if (GDALDataTypeIsComplex( eSrcType )) - { - /* complex input datatype */ - double dfReal, dfImag, dfPixVal; - int nOffset = GDALGetDataTypeSize( eSrcType ) / 8 / 2; - void *pReal = papoSources[0]; - void *pImag = ((GByte *)papoSources[0]) + nOffset; - - /* ---- Set pixels ---- */ - for( iLine = 0, ii = 0; iLine < nYSize; ++iLine ) { - for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { - - /* Source raster pixels may be obtained with SRCVAL macro */ - dfReal = SRCVAL(pReal, eSrcType, ii); - dfImag = SRCVAL(pImag, eSrcType, ii); - - dfPixVal = log10( dfReal * dfReal + dfImag * dfImag ); - - GDALCopyWords(&dfPixVal, GDT_Float64, 0, - ((GByte *)pData) + nLineSpace * iLine + - iCol * nPixelSpace, eBufType, nPixelSpace, 1); - } - } - } else { - double dfPixVal; - - /* ---- Set pixels ---- */ - for( iLine = 0, ii = 0; iLine < nYSize; ++iLine ) { - for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { - - /* Source raster pixels may be obtained with SRCVAL macro */ - dfPixVal = SRCVAL(papoSources[0], eSrcType, ii); - dfPixVal = log10( fabs( dfPixVal ) ); - - GDALCopyWords(&dfPixVal, GDT_Float64, 0, - ((GByte *)pData) + nLineSpace * iLine + - iCol * nPixelSpace, eBufType, nPixelSpace, 1); - } - } - } - - /* ---- Return success ---- */ - return CE_None; -} /* Log10PixelFunc */ - - -static CPLErr PowPixelFuncHelper(void **papoSources, int nSources, void *pData, - int nXSize, int nYSize, - GDALDataType eSrcType, GDALDataType eBufType, - int nPixelSpace, int nLineSpace, - double base, double fact) -{ - int iLine, iCol, ii; - double dfPixVal; - - /* ---- Init ---- */ - if (nSources != 1) return CE_Failure; - if (GDALDataTypeIsComplex( eSrcType )) return CE_Failure; - - /* ---- Set pixels ---- */ - for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { - for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { - - /* Source raster pixels may be obtained with SRCVAL macro */ - dfPixVal = SRCVAL(papoSources[0], eSrcType, ii); - dfPixVal = pow(base, dfPixVal / fact); - - GDALCopyWords(&dfPixVal, GDT_Float64, 0, - ((GByte *)pData) + nLineSpace * iLine + - iCol * nPixelSpace, eBufType, nPixelSpace, 1); - } - } - - /* ---- Return success ---- */ - return CE_None; -} /* PowPixelFuncHelper */ - -static CPLErr dB2AmpPixelFunc(void **papoSources, int nSources, void *pData, - int nXSize, int nYSize, - GDALDataType eSrcType, GDALDataType eBufType, - int nPixelSpace, int nLineSpace) -{ - return PowPixelFuncHelper(papoSources, nSources, pData, - nXSize, nYSize, eSrcType, eBufType, - nPixelSpace, nLineSpace, 10., 20.); -} /* dB2AmpPixelFunc */ - - -static CPLErr dB2PowPixelFunc(void **papoSources, int nSources, void *pData, - int nXSize, int nYSize, - GDALDataType eSrcType, GDALDataType eBufType, - int nPixelSpace, int nLineSpace) -{ - return PowPixelFuncHelper(papoSources, nSources, pData, - nXSize, nYSize, eSrcType, eBufType, - nPixelSpace, nLineSpace, 10., 10.); -} /* dB2PowPixelFunc */ - - -/************************************************************************/ -/* GDALRegisterDefaultPixelFunc() */ -/************************************************************************/ - -/** - * This adds a default set of pixel functions to the global list of - * available pixel functions for derived bands: - * - * - "real": extract real part from a single raster band (just a copy if the - * input is non-complex) - * - "imag": extract imaginary part from a single raster band (0 for - * non-complex) - * - "complex": make a complex band merging two bands used as real and - * imag values - * - "mod": extract module from a single raster band (real or complex) - * - "phase": extract phase from a single raster band (0 for non-complex) - * - "conj": computes the complex conjugate of a single raster band (just a - * copy if the input is non-complex) - * - "sum": sum 2 or more raster bands - * - "diff": computes the difference between 2 raster bands (b1 - b2) - * - "mul": multilpy 2 or more raster bands - * - "cmul": multiply the first band for the complex conjugate of the second - * - "inv": inverse (1./x). Note: no check is performed on zero division - * - "intensity": computes the intensity Re(x*conj(x)) of a single raster band - * (real or complex) - * - "sqrt": perform the square root of a single raster band (real only) - * - "log10": compute the logarithm (base 10) of the abs of a single raster - * band (real or complex): log10( abs( x ) ) - * - "dB2amp": perform scale conversion from logarithmic to linear - * (amplitude) (i.e. 10 ^ ( x / 20 ) ) of a single raster - * band (real only) - * - "dB2pow": perform scale conversion from logarithmic to linear - * (power) (i.e. 10 ^ ( x / 10 ) ) of a single raster - * band (real only) - * - * @see GDALAddDerivedBandPixelFunc - * - * @return CE_None, invalid (NULL) parameters are currently ignored. - */ -CPLErr CPL_STDCALL GDALRegisterDefaultPixelFunc(void) -{ - GDALAddDerivedBandPixelFunc("real", RealPixelFunc); - GDALAddDerivedBandPixelFunc("imag", ImagPixelFunc); - GDALAddDerivedBandPixelFunc("complex", ComplexPixelFunc); - GDALAddDerivedBandPixelFunc("mod", ModulePixelFunc); - GDALAddDerivedBandPixelFunc("phase", PhasePixelFunc); - GDALAddDerivedBandPixelFunc("conj", ConjPixelFunc); - GDALAddDerivedBandPixelFunc("sum", SumPixelFunc); - GDALAddDerivedBandPixelFunc("diff", DiffPixelFunc); - GDALAddDerivedBandPixelFunc("mul", MulPixelFunc); - GDALAddDerivedBandPixelFunc("cmul", CMulPixelFunc); - GDALAddDerivedBandPixelFunc("inv", InvPixelFunc); - GDALAddDerivedBandPixelFunc("intensity", IntensityPixelFunc); - GDALAddDerivedBandPixelFunc("sqrt", SqrtPixelFunc); - GDALAddDerivedBandPixelFunc("log10", Log10PixelFunc); - GDALAddDerivedBandPixelFunc("dB2amp", dB2AmpPixelFunc); - GDALAddDerivedBandPixelFunc("dB2pow", dB2PowPixelFunc); - - return CE_None; -} From 72f37d679181f1eb3b5d5b8450127ee85396c2fd Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Tue, 12 Jul 2016 15:44:02 +0200 Subject: [PATCH 65/69] TEST: Remove duplicate pixfun.py --- autotest/gdrivers/pixfun.py | 791 ------------------------------------ 1 file changed, 791 deletions(-) delete mode 100644 autotest/gdrivers/pixfun.py diff --git a/autotest/gdrivers/pixfun.py b/autotest/gdrivers/pixfun.py deleted file mode 100644 index 9450085372ac..000000000000 --- a/autotest/gdrivers/pixfun.py +++ /dev/null @@ -1,791 +0,0 @@ -#!/usr/bin/env python -############################################################################### -# $Id$ -# -# Project: GDAL/OGR Test Suite -# Purpose: Test pixel functions support. -# Author: Antonio Valentino -# -############################################################################### -# Copyright (c) 2010-2014, Antonio Valentino -# -# Permission is hereby granted, free of charge, to any person obtaining a -# copy of this software and associated documentation files (the "Software"), -# to deal in the Software without restriction, including without limitation -# the rights to use, copy, modify, merge, publish, distribute, sublicense, -# and/or sell copies of the Software, and to permit persons to whom the -# Software is furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included -# in all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -# DEALINGS IN THE SOFTWARE. -############################################################################### - -import sys - -try: - import numpy - numpy_available = True -except ImportError: - numpy_available = False - -from osgeo import gdal - -sys.path.append('../pymod') - -import gdaltest - -############################################################################### -# Verify real part extraction from a complex dataset. - -def pixfun_real_c(): - - filename = 'data/pixfun_real_c.vrt' - ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) - if ds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % filename) - return 'fail' - data = ds.GetRasterBand(1).ReadAsArray() - - reffilename = 'data/cint_sar.tif' - refds = gdal.Open(reffilename) - if refds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) - return 'fail' - refdata = refds.GetRasterBand(1).ReadAsArray() - - if numpy_available and not numpy.alltrue(data == refdata.real): - return 'fail' - - return 'success' - - -############################################################################### -# Verify real part extraction from a complex dataset. - -def pixfun_real_r(): - - filename = 'data/pixfun_real_r.vrt' - ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) - if ds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % filename) - return 'fail' - data = ds.GetRasterBand(1).ReadAsArray() - - reffilename = 'data/int32.tif' - refds = gdal.Open(reffilename) - if refds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) - return 'fail' - refdata = refds.GetRasterBand(1).ReadAsArray() - - if numpy_available and not numpy.alltrue(data == refdata.real): - return 'fail' - - return 'success' - - -############################################################################### -# Verify imaginary part extraction from a complex dataset. - -def pixfun_imag_c(): - - filename = 'data/pixfun_imag_c.vrt' - ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) - if ds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % filename) - return 'fail' - data = ds.GetRasterBand(1).ReadAsArray() - - reffilename = 'data/cint_sar.tif' - refds = gdal.Open(reffilename) - if refds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) - return 'fail' - refdata = refds.GetRasterBand(1).ReadAsArray() - - if numpy_available and not numpy.alltrue(data == refdata.imag): - return 'fail' - - return 'success' - - -############################################################################### -# Verify imaginary part extraction from a real dataset. - -def pixfun_imag_r(): - - filename = 'data/pixfun_imag_r.vrt' - ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) - if ds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % filename) - return 'fail' - data = ds.GetRasterBand(1).ReadAsArray() - - if numpy_available and not numpy.alltrue(data == 0): - return 'fail' - - return 'success' - - -############################################################################### -# Verify imaginary part extraction from a real dataset. - -def pixfun_complex(): - - filename = 'data/pixfun_complex.vrt' - ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) - if ds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % filename) - return 'fail' - data = ds.GetRasterBand(1).ReadAsArray() - - reffilename = 'data/int32.tif' - refds = gdal.Open(reffilename) - if refds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) - return 'fail' - refdata = refds.GetRasterBand(1).ReadAsArray() - - if numpy_available and not numpy.allclose(data, refdata + 1j * refdata): - return 'fail' - - return 'success' - - -############################################################################### -# Verify modulus extraction from a complex (float) dataset. - -def pixfun_mod_c(): - - filename = 'data/pixfun_mod_c.vrt' - ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) - if ds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % filename) - return 'fail' - data = ds.GetRasterBand(1).ReadAsArray() - - reffilename = 'data/cint_sar.tif' - refds = gdal.Open(reffilename) - if refds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) - return 'fail' - refdata = refds.GetRasterBand(1).ReadAsArray() - - if numpy_available and not numpy.alltrue(data == numpy.abs(refdata)): - return 'fail' - - return 'success' - - -############################################################################### -# Verify modulus extraction from a real (integer type) dataset. - -def pixfun_mod_r(): - - filename = 'data/pixfun_mod_r.vrt' - ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) - if ds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % filename) - return 'fail' - data = ds.GetRasterBand(1).ReadAsArray() - - reffilename = 'data/int32.tif' - refds = gdal.Open(reffilename) - if refds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) - return 'fail' - refdata = refds.GetRasterBand(1).ReadAsArray() - - if numpy_available and not numpy.alltrue(data == numpy.abs(refdata)): - return 'fail' - - return 'success' - - -############################################################################### -# Verify phase extraction from a complex dataset. - -def pixfun_phase_c(): - - filename = 'data/pixfun_phase_c.vrt' - ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) - if ds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % filename) - return 'fail' - data = ds.GetRasterBand(1).ReadAsArray() - - reffilename = 'data/cint_sar.tif' - refds = gdal.Open(reffilename) - if refds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) - return 'fail' - refdata = refds.GetRasterBand(1).ReadAsArray() - refdata = refdata.astype('complex128') - - #if not numpy.allclose(data, numpy.arctan2(refdata.imag, refdata.real)): - if numpy_available and not numpy.alltrue(data == numpy.arctan2(refdata.imag, refdata.real)): - return 'fail' - - return 'success' - - -############################################################################### -# Verify phase extraction from a real dataset. - -def pixfun_phase_r(): - - filename = 'data/pixfun_phase_r.vrt' - ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) - if ds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % filename) - return 'fail' - data = ds.GetRasterBand(1).ReadAsArray() - - reffilename = 'data/pixfun_imag_c.vrt' - refds = gdal.Open(reffilename) - if refds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) - return 'fail' - refdata = refds.GetRasterBand(1).ReadAsArray() - - if numpy_available and not numpy.alltrue(data == numpy.arctan2(0, refdata)): - return 'fail' - - return 'success' - - -############################################################################### -# Verify cmplex conjugare computation on a complex dataset. - -def pixfun_conj_c(): - - filename = 'data/pixfun_conj_c.vrt' - ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) - if ds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % filename) - return 'fail' - data = ds.GetRasterBand(1).ReadAsArray() - - reffilename = 'data/cint_sar.tif' - refds = gdal.Open(reffilename) - if refds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) - return 'fail' - refdata = refds.GetRasterBand(1).ReadAsArray() - - if numpy_available and not numpy.alltrue(data == numpy.conj(refdata)): - return 'fail' - - return 'success' - - -############################################################################### -# Verify cmplex conjugare computation on a real dataset. - -def pixfun_conj_r(): - - filename = 'data/pixfun_conj_r.vrt' - ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) - if ds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % filename) - return 'fail' - data = ds.GetRasterBand(1).ReadAsArray() - - reffilename = 'data/int32.tif' - refds = gdal.Open(reffilename) - if refds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) - return 'fail' - refdata = refds.GetRasterBand(1).ReadAsArray() - - if numpy_available and not numpy.alltrue(data == numpy.conj(refdata)): - return 'fail' - - return 'success' - - -############################################################################### -# Verify the sum of 3 (real) datasets. - -def pixfun_sum_r(): - - filename = 'data/pixfun_sum_r.vrt' - ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) - if ds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % filename) - return 'fail' - data = ds.GetRasterBand(1).ReadAsArray() - - if numpy_available: - refdata = numpy.zeros(data.shape, 'float') - for reffilename in ('data/uint16.tif', 'data/int32.tif', - 'data/float32.tif'): - refds = gdal.Open(reffilename) - if refds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) - return 'fail' - refdata += refds.GetRasterBand(1).ReadAsArray() - - if not numpy.alltrue(data == refdata): - return 'fail' - - return 'success' - - -############################################################################### -# Verify the sum of 3 (two complex and one real) datasets. - -def pixfun_sum_c(): - - filename = 'data/pixfun_sum_c.vrt' - ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) - if ds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % filename) - return 'fail' - data = ds.GetRasterBand(1).ReadAsArray() - - if numpy_available: - refdata = numpy.zeros(data.shape, 'complex') - for reffilename in ('data/uint16.tif', 'data/cint_sar.tif', - 'data/cfloat64.tif'): - refds = gdal.Open(reffilename) - if refds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) - return 'fail' - refdata += refds.GetRasterBand(1).ReadAsArray(0, 0, 5, 6) - - if not numpy.alltrue(data == refdata): - return 'fail' - - return 'success' - - -############################################################################### -# Verify the difference of 2 (real) datasets. - -def pixfun_diff_r(): - - filename = 'data/pixfun_diff_r.vrt' - ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) - if ds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % filename) - return 'fail' - data = ds.GetRasterBand(1).ReadAsArray() - - reffilename = 'data/int32.tif' - refds = gdal.Open(reffilename) - if refds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) - return 'fail' - refdata1 = refds.GetRasterBand(1).ReadAsArray(0, 0, 5, 6) - - reffilename = 'data/float32.tif' - refds = gdal.Open(reffilename) - if refds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) - return 'fail' - refdata2 = refds.GetRasterBand(1).ReadAsArray(10, 10, 5, 6) - - if numpy_available and not numpy.alltrue(data == refdata1-refdata2): - return 'fail' - - return 'success' - - -############################################################################### -# Verify the difference of 2 (complex) datasets. - -def pixfun_diff_c(): - - filename = 'data/pixfun_diff_c.vrt' - ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) - if ds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % filename) - return 'fail' - data = ds.GetRasterBand(1).ReadAsArray() - - reffilename = 'data/cint_sar.tif' - refds = gdal.Open(reffilename) - if refds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) - return 'fail' - refdata1 = refds.GetRasterBand(1).ReadAsArray() - - reffilename = 'data/cfloat64.tif' - refds = gdal.Open(reffilename) - if refds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) - return 'fail' - refdata2 = refds.GetRasterBand(1).ReadAsArray(0, 0, 5, 6) - - if numpy_available and not numpy.alltrue(data == refdata1-refdata2): - return 'fail' - - return 'success' - - -############################################################################### -# Verify the product of 3 (real) datasets. - -def pixfun_mul_r(): - - filename = 'data/pixfun_mul_r.vrt' - ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) - if ds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % filename) - return 'fail' - data = ds.GetRasterBand(1).ReadAsArray() - - if numpy_available: - refdata = numpy.ones(data.shape, 'float') - for reffilename in ('data/uint16.tif', 'data/int32.tif', - 'data/float32.tif'): - refds = gdal.Open(reffilename) - if refds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) - return 'fail' - refdata *= refds.GetRasterBand(1).ReadAsArray() - - if not numpy.alltrue(data == refdata): - return 'fail' - - return 'success' - - -############################################################################### -# Verify the product of 2 (complex) datasets. - -def pixfun_mul_c(): - - filename = 'data/pixfun_mul_c.vrt' - ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) - if ds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % filename) - return 'fail' - data = ds.GetRasterBand(1).ReadAsArray() - - reffilename = 'data/cint_sar.tif' - refds = gdal.Open(reffilename) - if refds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) - return 'fail' - refdata = refds.GetRasterBand(1).ReadAsArray() - - if numpy_available and not numpy.alltrue(data == refdata*refdata): - return 'fail' - - return 'success' - - -############################################################################### -# Verify the product with complex conjugate of a complex datasets. - -def pixfun_cmul_c(): - - filename = 'data/pixfun_cmul_c.vrt' - ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) - if ds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % filename) - return 'fail' - data = ds.GetRasterBand(1).ReadAsArray() - - reffilename = 'data/cint_sar.tif' - refds = gdal.Open(reffilename) - if refds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) - return 'fail' - refdata = refds.GetRasterBand(1).ReadAsArray() - - if numpy_available and not numpy.alltrue(data == refdata*refdata.conj()): - return 'fail' - - return 'success' - - -############################################################################### -# Verify the product with complex conjugate of two real datasets. - -def pixfun_cmul_r(): - - filename = 'data/pixfun_cmul_r.vrt' - ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) - if ds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % filename) - return 'fail' - data = ds.GetRasterBand(1).ReadAsArray() - - reffilename = 'data/uint16.tif' - refds = gdal.Open(reffilename) - if refds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) - return 'fail' - refdata1 = refds.GetRasterBand(1).ReadAsArray() - refdata1 = refdata1.astype('float64') - - reffilename = 'data/int32.tif' - refds = gdal.Open(reffilename) - if refds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) - return 'fail' - refdata2 = refds.GetRasterBand(1).ReadAsArray() - refdata2 = refdata2.astype('float64') - - if numpy_available and not numpy.alltrue(data == refdata1 * refdata2.conj()): - return 'fail' - - return 'success' - - -############################################################################### -# Verify computation of the inverse of a real datasets. - -def pixfun_inv_r(): - - filename = 'data/pixfun_inv_r.vrt' - ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) - if ds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % filename) - return 'fail' - data = ds.GetRasterBand(1).ReadAsArray() - - reffilename = 'data/uint16.tif' - refds = gdal.Open(reffilename) - if refds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) - return 'fail' - refdata = refds.GetRasterBand(1).ReadAsArray() - refdata = refdata.astype('float64') - - if numpy_available and not numpy.alltrue(data == 1./refdata): - return 'fail' - - return 'success' - - -############################################################################### -# Verify computation of the inverse of a complex datasets. - -def pixfun_inv_c(): - - filename = 'data/pixfun_inv_c.vrt' - ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) - if ds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % filename) - return 'fail' - data = ds.GetRasterBand(1).ReadAsArray() - - reffilename = 'data/cint_sar.tif' - refds = gdal.Open(reffilename) - if refds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) - return 'fail' - refdata = refds.GetRasterBand(1).ReadAsArray() - refdata = refdata.astype('complex') - delta = data - 1./refdata - - if numpy_available and not numpy.alltrue(abs(delta.real) < 1e-13): - return 'fail' - if numpy_available and not numpy.alltrue(abs(delta.imag) < 1e-13): - return 'fail' - - return 'success' - - -############################################################################### -# Verify intensity computation of a complex dataset. - -def pixfun_intensity_c(): - - filename = 'data/pixfun_intensity_c.vrt' - ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) - if ds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % filename) - return 'fail' - data = ds.GetRasterBand(1).ReadAsArray() - - reffilename = 'data/cint_sar.tif' - refds = gdal.Open(reffilename) - if refds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) - return 'fail' - refdata = refds.GetRasterBand(1).ReadAsArray() - - if numpy_available and not numpy.alltrue(data == (refdata*refdata.conj()).real): - return 'fail' - - return 'success' - - -############################################################################### -# Verify intensity computation of real dataset. - -def pixfun_intensity_r(): - - filename = 'data/pixfun_intensity_r.vrt' - ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) - if ds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % filename) - return 'fail' - data = ds.GetRasterBand(1).ReadAsArray() - - reffilename = 'data/float32.tif' - refds = gdal.Open(reffilename) - if refds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) - return 'fail' - refdata = refds.GetRasterBand(1).ReadAsArray() - - if numpy_available and not numpy.alltrue(data == (refdata*refdata.conj()).real): - return 'fail' - - return 'success' - - -############################################################################### -# Verify square root computation. - -def pixfun_sqrt(): - - filename = 'data/pixfun_sqrt.vrt' - ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) - if ds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % filename) - return 'fail' - data = ds.GetRasterBand(1).ReadAsArray() - - reffilename = 'data/float32.tif' - refds = gdal.Open(reffilename) - if refds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) - return 'fail' - refdata = refds.GetRasterBand(1).ReadAsArray() - - if numpy_available and not numpy.alltrue(data == numpy.sqrt(refdata)): - return 'fail' - - return 'success' - - -############################################################################### -# Verify logarithm computation. - -def pixfun_log10(): - - filename = 'data/pixfun_log10.vrt' - ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) - if ds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % filename) - return 'fail' - data = ds.GetRasterBand(1).ReadAsArray() - - reffilename = 'data/float32.tif' - refds = gdal.Open(reffilename) - if refds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) - return 'fail' - refdata = refds.GetRasterBand(1).ReadAsArray() - - if numpy_available and not numpy.alltrue(data == numpy.log10(refdata)): - return 'fail' - - return 'success' - - -############################################################################### -# Verify conversion from dB to amplitude. - -def pixfun_dB2amp(): - - filename = 'data/pixfun_dB2amp.vrt' - ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) - if ds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % filename) - return 'fail' - data = ds.GetRasterBand(1).ReadAsArray() - - reffilename = 'data/float32.tif' - refds = gdal.Open(reffilename) - if refds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) - return 'fail' - refdata = refds.GetRasterBand(1).ReadAsArray() - - #if not numpy.alltrue(data == 10.**(refdata/20.)): - if numpy_available and not numpy.allclose(data, 10.**(refdata/20.)): - return 'fail' - - return 'success' - - -############################################################################### -# Verify conversion from dB to power. - -def pixfun_dB2pow(): - - filename = 'data/pixfun_dB2pow.vrt' - ds = gdal.OpenShared(filename, gdal.GA_ReadOnly) - if ds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % filename) - return 'fail' - data = ds.GetRasterBand(1).ReadAsArray() - - reffilename = 'data/float32.tif' - refds = gdal.Open(reffilename) - if refds is None: - gdaltest.post_reason('Unable to open "%s" dataset.' % reffilename) - return 'fail' - refdata = refds.GetRasterBand(1).ReadAsArray() - refdata = refdata.astype('float64') - - #if not numpy.allclose(data, 10.**(refdata/10.)): - if numpy_available and not numpy.alltrue(data == 10.**(refdata/10.)): - return 'fail' - - return 'success' - - -############################################################################### - -gdaltest_list = [ - pixfun_real_c, - pixfun_real_r, - pixfun_imag_c, - pixfun_imag_r, - pixfun_complex, - pixfun_mod_c, - pixfun_mod_r, - pixfun_phase_c, - pixfun_phase_r, - pixfun_conj_c, - pixfun_conj_r, - pixfun_sum_r, - pixfun_sum_c, - pixfun_diff_r, - pixfun_diff_c, - pixfun_mul_r, - pixfun_mul_c, - pixfun_cmul_c, - pixfun_cmul_r, - pixfun_inv_r, - pixfun_inv_c, - pixfun_intensity_c, - pixfun_intensity_r, - pixfun_sqrt, - pixfun_log10, - pixfun_dB2amp, - pixfun_dB2pow, -] - - -if __name__ == '__main__': - gdaltest.setup_run('pixfun') - gdaltest.run_tests(gdaltest_list) - gdaltest.summarize() From 3a8ff493d8d31a96d0284fdf9c168432a0882def Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Tue, 12 Jul 2016 15:44:21 +0200 Subject: [PATCH 66/69] TEST: Check expected checksum following modification of intensity function --- autotest/gdrivers/derived.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autotest/gdrivers/derived.py b/autotest/gdrivers/derived.py index 770b3cd3bb5d..fb05e4858a59 100755 --- a/autotest/gdrivers/derived.py +++ b/autotest/gdrivers/derived.py @@ -117,7 +117,7 @@ def derived_test2(): 'DERIVED_SUBDATASET_5_NAME' : 314, 'DERIVED_SUBDATASET_3_NAME' : 142, 'DERIVED_SUBDATASET_1_NAME' : 10, - 'DERIVED_SUBDATASET_6_NAME' : 99, + 'DERIVED_SUBDATASET_6_NAME' : 55, 'DERIVED_SUBDATASET_4_NAME' : 110, 'DERIVED_SUBDATASET_2_NAME' : 159} From 1a9a5facfeddbb46398d78964da552c3de2555da Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Tue, 12 Jul 2016 16:57:56 +0200 Subject: [PATCH 67/69] ENH: Remove last magic number --- gdal/frmts/derived/deriveddataset.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gdal/frmts/derived/deriveddataset.cpp b/gdal/frmts/derived/deriveddataset.cpp index 9f3505105ab1..b0a7a470aa3f 100644 --- a/gdal/frmts/derived/deriveddataset.cpp +++ b/gdal/frmts/derived/deriveddataset.cpp @@ -66,7 +66,7 @@ GDALDataset * DerivedDataset::Open(GDALOpenInfo * poOpenInfo) } /* Next, we need to now which derived dataset to compute */ - const size_t alg_pos = filename.find(":",dsds_pos+20); + const size_t alg_pos = filename.find(":",dsds_pos+nPrefixLen+1); if (alg_pos == std::string::npos) { /* Unable to Open if we do not find the name of the derived dataset */ From bf03eed84049f5711489210f72edb545ec05b925 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Tue, 12 Jul 2016 17:31:13 +0200 Subject: [PATCH 68/69] DOC: Adding documentation derived datasets --- gdal/frmts/derived/deriveddataset.cpp | 2 +- gdal/frmts/derived/frmt_devided.html | 236 ++++++++++++++++++++++++++ gdal/frmts/formats_list.html | 8 + 3 files changed, 245 insertions(+), 1 deletion(-) create mode 100644 gdal/frmts/derived/frmt_devided.html diff --git a/gdal/frmts/derived/deriveddataset.cpp b/gdal/frmts/derived/deriveddataset.cpp index b0a7a470aa3f..ed116c4dc969 100644 --- a/gdal/frmts/derived/deriveddataset.cpp +++ b/gdal/frmts/derived/deriveddataset.cpp @@ -187,7 +187,7 @@ void GDALRegister_Derived() #endif poDriver->SetMetadataItem( GDAL_DCAP_VIRTUALIO, "YES" ); poDriver->SetMetadataItem( GDAL_DMD_LONGNAME, "Derived datasets using VRT pixel functions" ); - poDriver->SetMetadataItem( GDAL_DMD_HELPTOPIC, "TODO" ); + poDriver->SetMetadataItem( GDAL_DMD_HELPTOPIC, "frmt_derived.html" ); poDriver->SetMetadataItem( GDAL_DMD_SUBDATASETS, "NO" ); poDriver->pfnOpen = DerivedDataset::Open; diff --git a/gdal/frmts/derived/frmt_devided.html b/gdal/frmts/derived/frmt_devided.html new file mode 100644 index 000000000000..4414c6a161f1 --- /dev/null +++ b/gdal/frmts/derived/frmt_devided.html @@ -0,0 +1,236 @@ + + +DERIVED -- Derived subdatasets driver + + + + +

DERIVED -- Derived subdatasets driver

+ +

This driver allows to access to subdatasets derived from a given +dataset. Those derived datasets have the same projection reference, +geo-transform and metadata than the original dataset, but derives new +pixel values using gdal pixel functions.

+ +

Available functions

+ +

Avaiable derived datasets are: +

    +
  • AMPLITUDE: Amplitude of pixels from input bands
  • +
  • PHASE: Phase of pixels from input bands
  • +
  • REAL: Real part of pixels from input bands
  • +
  • IMAG: Imaginary part of pixels from input bands
  • +
  • CONJ: Conjugate of pixels from input bands
  • +
  • INTENSITY: Intensity (squared amplitude) of pixels from input bands
  • +
  • LOGAMPLITUDE: Log10 of amplitude of pixels from input bands
  • +
+

+ +

A typical use is to directly access amplitude, phase or log-amplitude of any complex dataset.

+ +

Accessing derived subdatasets

+ +

Derived subdatasets are stored in the DERIVED_SUBDATASETS metadata domain, and can be accessed using the following syntax: +

+ +
+  DERIVED_SUBDATASET:FUNCTION:dataset_name
+
+ +

where function is one of AMPLITUDE, PHASE, REAL, IMAG, CONJ, INTENSITY, LOGAMPLITUDE. So as to ensure numerical precision, all derived subdatasets bands will have Float64 or CFloat64 precision (depending on the function used).

+ +

For instance:

+

+  $ gdalinfo cint_sar.tif
+
+ +
  
+Driver: GTiff/GeoTIFF
+Files: cint_sar.tif
+Size is 5, 6
+Coordinate System is `'
+GCP Projection = 
+GEOGCS["WGS 84",
+    DATUM["WGS_1984",
+        SPHEROID["WGS 84",6378137,298.257223563,
+            AUTHORITY["EPSG","7030"]],
+        AUTHORITY["EPSG","6326"]],
+    PRIMEM["Greenwich",0],
+    UNIT["degree",0.0174532925199433],
+    AUTHORITY["EPSG","4326"]]
+GCP[  0]: Id=1, Info=
+          (-1910.5,-7430.5) -> (297.507,16.368,0)
+GCP[  1]: Id=2, Info=
+          (588.5,-7430.5) -> (297.938,16.455,0)
+GCP[  2]: Id=3, Info=
+          (588.5,7363.5) -> (297.824,16.977,0)
+GCP[  3]: Id=4, Info=
+          (-1910.5,7363.5) -> (297.393,16.89,0)
+Metadata:
+  AREA_OR_POINT=Area
+  CEOS_ACQUISITION_TIME=19970718024119087               
+  CEOS_ELLIPSOID=GEM6            
+  CEOS_INC_ANGLE=24.824
+  CEOS_LINE_SPACING_METERS=3.9900000
+  CEOS_LOGICAL_VOLUME_ID=0001667400297672
+  CEOS_PIXEL_SPACING_METERS=7.9040000
+  CEOS_PIXEL_TIME_DIR=INCREASE
+  CEOS_PLATFORM_HEADING=347.339
+  CEOS_PLATFORM_LATITUDE=16.213
+  CEOS_PLATFORM_LONGITUDE=-65.311
+  CEOS_PROCESSING_AGENCY=ESA     
+  CEOS_PROCESSING_COUNTRY=ITALY       
+  CEOS_PROCESSING_FACILITY=ES          
+  CEOS_SEMI_MAJOR=6378.1440000
+  CEOS_SEMI_MINOR=6356.7590000
+  CEOS_SENSOR_CLOCK_ANGLE=90.000
+  CEOS_SOFTWARE_ID=ERS2-SLC-6.1
+  CEOS_TRUE_HEADING=345.5885834
+Image Structure Metadata:
+  INTERLEAVE=BAND
+Corner Coordinates:
+Upper Left  (    0.0,    0.0)
+Lower Left  (    0.0,    6.0)
+Upper Right (    5.0,    0.0)
+Lower Right (    5.0,    6.0)
+Center      (    2.5,    3.0)
+Band 1 Block=5x6 Type=CInt16, ColorInterp=Gray
+
+ +
+  $ gdalinfo DERIVED_SUBDATASET:LOGAMPLITUDE:cint_sar.tif
+
+Driver: DERIVED/Derived datasets using VRT pixel functions
+Files: cint_sar.tif
+Size is 5, 6
+Coordinate System is `'
+GCP Projection = 
+GEOGCS["WGS 84",
+    DATUM["WGS_1984",
+        SPHEROID["WGS 84",6378137,298.257223563,
+            AUTHORITY["EPSG","7030"]],
+        AUTHORITY["EPSG","6326"]],
+    PRIMEM["Greenwich",0],
+    UNIT["degree",0.0174532925199433],
+    AUTHORITY["EPSG","4326"]]
+GCP[  0]: Id=1, Info=
+          (-1910.5,-7430.5) -> (297.507,16.368,0)
+GCP[  1]: Id=2, Info=
+          (588.5,-7430.5) -> (297.938,16.455,0)
+GCP[  2]: Id=3, Info=
+          (588.5,7363.5) -> (297.824,16.977,0)
+GCP[  3]: Id=4, Info=
+          (-1910.5,7363.5) -> (297.393,16.89,0)
+Metadata:
+  AREA_OR_POINT=Area
+  CEOS_ACQUISITION_TIME=19970718024119087               
+  CEOS_ELLIPSOID=GEM6            
+  CEOS_INC_ANGLE=24.824
+  CEOS_LINE_SPACING_METERS=3.9900000
+  CEOS_LOGICAL_VOLUME_ID=0001667400297672
+  CEOS_PIXEL_SPACING_METERS=7.9040000
+  CEOS_PIXEL_TIME_DIR=INCREASE
+  CEOS_PLATFORM_HEADING=347.339
+  CEOS_PLATFORM_LATITUDE=16.213
+  CEOS_PLATFORM_LONGITUDE=-65.311
+  CEOS_PROCESSING_AGENCY=ESA     
+  CEOS_PROCESSING_COUNTRY=ITALY       
+  CEOS_PROCESSING_FACILITY=ES          
+  CEOS_SEMI_MAJOR=6378.1440000
+  CEOS_SEMI_MINOR=6356.7590000
+  CEOS_SENSOR_CLOCK_ANGLE=90.000
+  CEOS_SOFTWARE_ID=ERS2-SLC-6.1
+  CEOS_TRUE_HEADING=345.5885834
+Corner Coordinates:
+Upper Left  (    0.0,    0.0)
+Lower Left  (    0.0,    6.0)
+Upper Right (    5.0,    0.0)
+Lower Right (    5.0,    6.0)
+Center      (    2.5,    3.0)
+Band 1 Block=5x6 Type=Float64, ColorInterp=Undefined
+
+ +

Listing available subdatasets

+ +

Available subdatasets are reported in the DERIVED_SUBDATASETS metadata domain. Only functions that make sense will be reported for a given dataset, which means that AMPLITUDE, PHASE, REAL, IMAG, CONJ and INTENSITY will only be reported if the dataset has at least one complex band. Nevertheless, even if not reported, those derived datasets are still reachable with the syntax presented above. + +

+    $ gdalinfo -mdd DERIVED_SUBDATASETS cint_sar.tif
+  
+ +
Driver: GTiff/GeoTIFF
+Files: cint_sar.tif
+Size is 5, 6
+Coordinate System is `'
+GCP Projection = 
+GEOGCS["WGS 84",
+    DATUM["WGS_1984",
+        SPHEROID["WGS 84",6378137,298.257223563,
+            AUTHORITY["EPSG","7030"]],
+        AUTHORITY["EPSG","6326"]],
+    PRIMEM["Greenwich",0],
+    UNIT["degree",0.0174532925199433],
+    AUTHORITY["EPSG","4326"]]
+GCP[  0]: Id=1, Info=
+          (-1910.5,-7430.5) -> (297.507,16.368,0)
+GCP[  1]: Id=2, Info=
+          (588.5,-7430.5) -> (297.938,16.455,0)
+GCP[  2]: Id=3, Info=
+          (588.5,7363.5) -> (297.824,16.977,0)
+GCP[  3]: Id=4, Info=
+          (-1910.5,7363.5) -> (297.393,16.89,0)
+Metadata:
+  AREA_OR_POINT=Area
+  CEOS_ACQUISITION_TIME=19970718024119087               
+  CEOS_ELLIPSOID=GEM6            
+  CEOS_INC_ANGLE=24.824
+  CEOS_LINE_SPACING_METERS=3.9900000
+  CEOS_LOGICAL_VOLUME_ID=0001667400297672
+  CEOS_PIXEL_SPACING_METERS=7.9040000
+  CEOS_PIXEL_TIME_DIR=INCREASE
+  CEOS_PLATFORM_HEADING=347.339
+  CEOS_PLATFORM_LATITUDE=16.213
+  CEOS_PLATFORM_LONGITUDE=-65.311
+  CEOS_PROCESSING_AGENCY=ESA     
+  CEOS_PROCESSING_COUNTRY=ITALY       
+  CEOS_PROCESSING_FACILITY=ES          
+  CEOS_SEMI_MAJOR=6378.1440000
+  CEOS_SEMI_MINOR=6356.7590000
+  CEOS_SENSOR_CLOCK_ANGLE=90.000
+  CEOS_SOFTWARE_ID=ERS2-SLC-6.1
+  CEOS_TRUE_HEADING=345.5885834
+Metadata (DERIVED_SUBDATASETS):
+  DERIVED_SUBDATASET_0_NAME=DERIVED_SUBDATASET:AMPLITUDE:cint_sar.tif
+  DERIVED_SUBDATASET_0_DESC=Amplitude of input bands from cint_sar.tif
+  DERIVED_SUBDATASET_1_NAME=DERIVED_SUBDATASET:PHASE:cint_sar.tif
+  DERIVED_SUBDATASET_1_DESC=Phase of input bands from cint_sar.tif
+  DERIVED_SUBDATASET_2_NAME=DERIVED_SUBDATASET:REAL:cint_sar.tif
+  DERIVED_SUBDATASET_2_DESC=Real part of input bands from cint_sar.tif
+  DERIVED_SUBDATASET_3_NAME=DERIVED_SUBDATASET:IMAG:cint_sar.tif
+  DERIVED_SUBDATASET_3_DESC=Imaginary part of input bands from cint_sar.tif
+  DERIVED_SUBDATASET_4_NAME=DERIVED_SUBDATASET:CONJ:cint_sar.tif
+  DERIVED_SUBDATASET_4_DESC=Conjugate of input bands from cint_sar.tif
+  DERIVED_SUBDATASET_5_NAME=DERIVED_SUBDATASET:INTENSITY:cint_sar.tif
+  DERIVED_SUBDATASET_5_DESC=Intensity (squared amplitude) of input bands from cint_sar.tif
+  DERIVED_SUBDATASET_6_NAME=DERIVED_SUBDATASET:LOGAMPLITUDE:cint_sar.tif
+  DERIVED_SUBDATASET_6_DESC=log10 of amplitude of input bands from cint_sar.tif
+Image Structure Metadata:
+  INTERLEAVE=BAND
+Corner Coordinates:
+Upper Left  (    0.0,    0.0)
+Lower Left  (    0.0,    6.0)
+Upper Right (    5.0,    0.0)
+Lower Right (    5.0,    6.0)
+Center      (    2.5,    3.0)
+Band 1 Block=5x6 Type=CInt16, ColorInterp=Gray
+
+ +

See Also:

+ + + + + + diff --git a/gdal/frmts/formats_list.html b/gdal/frmts/formats_list.html index abd4739d34da..eaf3465deb80 100644 --- a/gdal/frmts/formats_list.html +++ b/gdal/frmts/formats_list.html @@ -1139,6 +1139,14 @@

GDAL Raster Formats

Yes + Derived + DERIVED + No + Yes + -- + Yes + + OGC Web Coverage Service WCS No From acd8343539861804e50941693b4a30ced1824a4e Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Wed, 13 Jul 2016 09:04:15 +0200 Subject: [PATCH 69/69] DOC: Moving DERIVED dataset entry at the correct location in supported formats table --- gdal/frmts/formats_list.html | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/gdal/frmts/formats_list.html b/gdal/frmts/formats_list.html index eaf3465deb80..9e13d02716c3 100644 --- a/gdal/frmts/formats_list.html +++ b/gdal/frmts/formats_list.html @@ -179,6 +179,14 @@

GDAL Raster Formats

No, needs Crunch Lib + Derived + DERIVED + No + Yes + -- + Yes + + Spot DIMAP (metadata.dim) DIMAP No @@ -1139,14 +1147,6 @@

GDAL Raster Formats

Yes - Derived - DERIVED - No - Yes - -- - Yes - - OGC Web Coverage Service WCS No