-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-dem-gdal.scm
executable file
·223 lines (184 loc) · 7.27 KB
/
test-dem-gdal.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/bin/bash
#| -*- mode: scheme; coding: utf-8; -*-
NO_RUNTIME_COMPILE="$1"
[ "$NO_RUNTIME_COMPILE" = "-Fno-runtime-compile" ] || NO_RUNTIME_COMPILE=""
# |#
:; exec gosh -I. $NO_RUNTIME_COMPILE -- $0 "$@"
;;disable debug print
;; (define-syntax debug-print
;; (syntax-rules ()
;; ((_ ?form)
;; ?form)))
;; (let1 x debug-print
;; (with-module gauche.vm.debugger (set! debug-print x)))
;; (use gc-hack)
;; (gc-set-warn-proc
;; (lambda(msg arg)
;; (with-output-to-port (current-error-port)
;; (lambda()
;; (set! called #t)
;; (print "SCHEME GC WARNING HANDLER HACK:\n" msg arg)
;; (%vm-show-stack-trace (vm-get-stack-trace-lite))))))
(use dem-gdal)
(use gauche.test)
(use rfc.http)
(use gauche.process)
(use srfi-1)
(use gauche.collection)
(test-start "dem-gdal")
(define *test-files* '("N48E008.hgt" "N48E009.hgt"))
(define *vrt-file* "all.vrt")
(define (download host path dest)
(call-with-output-file dest
(lambda (out)
(http-get host path :sink out :flusher (lambda _ #t)))))
(define (run l)
(let1 p (run-process l)
(process-wait p #f #t)))
(define (unzip x)
(run `(unzip ,x))
(sys-unlink x))
;;; download and unzip data files if needed
(for-each
(lambda(file)
(when (not (file-exists? file))
(with-output-to-port (current-error-port)
(cut print "downloading " file))
(download "dds.cr.usgs.gov"
(string-append "/srtm/version2_1/SRTM3/Eurasia/"
file
".zip")
(string-append file ".zip"))
(unzip (string-append file ".zip"))))
*test-files*)
;;; build test vrt file
(when (not (file-exists? *vrt-file*))
(with-output-to-port (current-error-port)
(cut print "build vrt file " *vrt-file*))
(run (append `(gdalbuildvrt ,|*vrt-file*|)
*test-files*)))
;;; project N48E008 to utm 32N
(when (not (file-exists? "N48E008_utm.tif"))
(run `(gdalwarp -dstnodata -32748 -t_srs ,#`"epsg:,(+ 32600 32)"
"N48E008.hgt"
"N48E008_utm.tif")))
;; ;;; void filled
;; (when (not (file-exists? "N48E008_filled.tif"))
;; (run `(gdal_fillnodata.py "N48E008.hgt" "N48E008_filled.tif")))
;;; low resolution averaged version
(when (not (file-exists? "lores.tif"))
(run `(gdaladdo -r average ,|*vrt-file*| 2 4))
(run `(gdal_translate -outsize 25% 25% ,|*vrt-file*| "lores.tif")))
;; (run `(gdaldem hillshade -s 111120 ,|*vrt-file*| hillshade.tif))
;;; the real tests
;; make sure find works as expected on collections
;; see also gauche mailing list:
;; Message-ID: <87d34i9smp.fsf@karme.de>
(test* "find"
-10000.0
(find (cut = -10000.0 <>) #f32(-10000.0 -10000.0)))
(test* "dem->xy->z"
735
(let1 z (dem->xy->z *vrt-file*)
(round->exact (z 8.5 48.5))))
(test* "dem->xy-project->z"
735
(let1 z (dem->xy-project->z "epsg:4326" *vrt-file*)
(round->exact (z 8.5 48.5))))
;; (subseq ((osr-transform (osr-from-user-input "epsg:4326") (osr-from-user-input "epsg:25832")) '(8.5 48.5)) 0 2)
(test* "dem->xy-project->z (utm32)"
735
(let1 z (dem->xy-project->z "epsg:25832" *vrt-file*)
(round->exact (z 463064.1314 5371996.2822))))
;; (subseq ((osr-transform (osr-from-user-input "epsg:4326") (osr-from-user-input "epsg:25833")) '(8.5 48.5)) 0 2)
;; (19959.274411356135 5392310.583687477)
(test* "dem->xy-project->z (utm33->utm32)"
735
(let1 z (dem->xy-project->z "epsg:25833" "N48E008_utm.tif")
(round->exact (z 19959.274411356135 5392310.583687477))))
(test* "dem->xy-project->z nodata(1)?"
#f
(let1 z (dem->xy-project->z "epsg:4326" *vrt-file*)
(nan? (z 8.5 48.5))))
(test* "dem->xy-project->z nodata(2)?"
#t
(let1 z (dem->xy-project->z "epsg:4326" *vrt-file*)
(nan? (z 8.29151 48.87847))))
(define-condition-type <nodata-error> <error>
nodata-error?)
(test* "dem->xy-project->z test error"
(test-error <nodata-error>)
((dem->xy-project->z "epsg:4326" *vrt-file* :next (lambda _ (error <nodata-error>)))
8.29151 48.87847))
(test* "nearest"
'(158.0 158.0 155.0 560.0 738.0 649.0)
(let1 z (dem->xy-project->z "epsg:4326" *vrt-file*
:next (lambda _ (error <nodata-error>))
:interpolation 'nearest)
(map (cut apply z <>) '((8 49) (8.000416666666666 49) (8.000833333333333 49)
(8 48) (9 48) (10 48)))))
(test* "dem->xy-project->z :next (simple)"
#f
(let1 z (dem->xy-project->z "epsg:4326" *vrt-file* :next (lambda _ 0))
(nan? (z 8.29151 48.87847))))
(test* "dem->xy-project->z (stacked)"
#t
(let* ((z1 (dem->xy-project->z "epsg:4326" "lores.tif" :next (lambda _ (error "foo"))))
(z (dem->xy-project->z "epsg:4326" "N48E008_utm.tif" :next z1))
(d (abs (apply - (map (cut apply <> '(8.29151 48.87847)) (list z z1))))))
(and (> d 0) (< d 1))))
(test* "dem-stack->xy->z"
#t
(let* ((z1 (dem->xy-project->z "epsg:4326" "lores.tif" :next (lambda _ (error "foo"))))
(z (dem->xy-project->z "epsg:4326" "N48E008_utm.tif" :next z1))
(zs (dem-stack->xy->z "epsg:4326" '(("N48E008_utm.tif")
("lores.tif" :next (lambda _ (error "foo")))))))
(apply = (map (cut apply <> '(8.29151 48.87847)) (list z zs)))))
(test* "dem-stack->xy->z (2)"
(test-error <nodata-error>)
((dem-stack->xy->z "epsg:4326" `(("N48E008_utm.tif")
("lores.tif" :next ,(lambda _ (error <nodata-error>)))))
0 0))
(test* "wrap-around"
(list 2801 2801 2801 2801 2801
0 0 0 0 0 0
0 0
242 242
163 163
95 95
0
676)
(map (compose round->exact
(cute apply (dem->xy-project->z "epsg:4326" "gmted2010_mn30@480.tif" :next (lambda _ 0)) <>))
'((0 -90) (0 -90.0001) (180.0001 -90.00001) (-180.0001 -90.00001) (360.0001 -90.00001)
(0 90) (0 90.0001) (180 90) (180.0001 90) (720 90) (-720 90)
(180 0) (181 0)
(180 66) (-180 66)
(180.1 66) (-179.9 66)
(179 66) (-181 66)
(47 90.00001)
(8.5 48.5))))
(test* "wrap-around (stack)"
(list 2801 2801 2801 2801 2801
0 0 0 0 0 0
0 0
242 242
163 163
95 95
0
735)
(map (compose round->exact
(cute apply (dem-stack->xy->z "epsg:4326"
`(("N48E008_utm.tif")
("gmted2010_mn30@480.tif" :next ,(lambda _ 0)))) <>))
'((0 -90) (0 -90.0001) (180.0001 -90.00001) (-180.0001 -90.00001) (360.0001 -90.00001)
(0 90) (0 90.0001) (180 90) (180.0001 90) (720 90) (-720 90)
(180 0) (181 0)
(180 66) (-180 66)
(180.1 66) (-179.9 66)
(179 66) (-181 66)
(47 90.00001)
(8.5 48.5))))
;; old gauche versions don't support keywords
;; (test-end :exit-on-failure #t)
(test-end)