File: rt_union.sql

package info (click to toggle)
postgis 2.5.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 75,792 kB
  • sloc: ansic: 139,314; sql: 136,281; xml: 48,954; sh: 4,906; perl: 4,509; makefile: 2,897; python: 1,198; yacc: 441; cpp: 305; lex: 132
file content (459 lines) | stat: -rw-r--r-- 10,233 bytes parent folder | download
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
DROP TABLE IF EXISTS raster_union_in;
CREATE TABLE raster_union_in (
	rid integer,
	rast raster
);
DROP TABLE IF EXISTS raster_union_out;
CREATE TABLE raster_union_out (
	uniontype text,
	rast raster
);

INSERT INTO raster_union_in
	SELECT 0, NULL::raster AS rast UNION ALL
	SELECT 1, ST_AddBand(ST_MakeEmptyRaster(2, 2, 0, 0, 1, -1, 0, 0, 0), 1, '8BUI', 1, 0) AS rast UNION ALL
	SELECT 2, ST_AddBand(ST_MakeEmptyRaster(2, 2, 1, -1, 1, -1, 0, 0, 0), 1, '8BUI', 2, 0) AS rast
;

INSERT INTO raster_union_out
	SELECT
		'LAST',
		ST_Union(rast, 1) AS rast
	FROM raster_union_in;

INSERT INTO raster_union_out
	SELECT
		'FIRST',
		ST_Union(rast, 1, 'FIRST') AS rast
	FROM raster_union_in;

INSERT INTO raster_union_out
	SELECT
		'MIN',
		ST_Union(rast, 1, 'MIN') AS rast
	FROM raster_union_in;

INSERT INTO raster_union_out
	SELECT
		'MAX',
		ST_Union(rast, 1, 'MAX') AS rast
	FROM raster_union_in;

INSERT INTO raster_union_out
	SELECT
		'COUNT',
		ST_Union(rast, 1, 'COUNT') AS rast
	FROM raster_union_in;

INSERT INTO raster_union_out
	SELECT
		'SUM',
		ST_Union(rast, 1, 'SUM') AS rast
	FROM raster_union_in;

INSERT INTO raster_union_out
	SELECT
		'MEAN',
		ST_Union(rast, 'mean') AS rast
	FROM raster_union_in;

INSERT INTO raster_union_out
	SELECT
		'RANGE',
		ST_Union(rast, 'range') AS rast
	FROM raster_union_in;

SELECT
	uniontype,
	x,
	y,
	val
FROM (
	SELECT
		uniontype,
		(ST_PixelAsPoints(rast)).*
	FROM raster_union_out
) foo
ORDER BY uniontype, y, x;

TRUNCATE raster_union_out;
TRUNCATE raster_union_in;

INSERT INTO raster_union_in
	SELECT 10, ST_AddBand(ST_MakeEmptyRaster(2, 2, 0, 0, 1, -1, 0, 0, 0), 1, '8BUI', 1, 0) AS rast UNION ALL
	SELECT 11, ST_AddBand(ST_MakeEmptyRaster(2, 2, 2, 0, 1, -1, 0, 0, 0), 1, '8BUI', 1, 0) AS rast UNION ALL
	SELECT 12, ST_AddBand(ST_MakeEmptyRaster(2, 2, 4, 0, 1, -1, 0, 0, 0), 1, '8BUI', 1, 0) AS rast UNION ALL

	SELECT 13, ST_AddBand(ST_MakeEmptyRaster(2, 2, 0, -2, 1, -1, 0, 0, 0), 1, '8BUI', 1, 0) AS rast UNION ALL
	SELECT 14, ST_AddBand(ST_MakeEmptyRaster(2, 2, 2, -2, 1, -1, 0, 0, 0), 1, '8BUI', 1, 0) AS rast UNION ALL
	SELECT 15, ST_AddBand(ST_MakeEmptyRaster(2, 2, 4, -2, 1, -1, 0, 0, 0), 1, '8BUI', 1, 0) AS rast UNION ALL

	SELECT 16, ST_AddBand(ST_MakeEmptyRaster(2, 2, 0, -4, 1, -1, 0, 0, 0), 1, '8BUI', 1, 0) AS rast UNION ALL
	SELECT 17, ST_AddBand(ST_MakeEmptyRaster(2, 2, 2, -4, 1, -1, 0, 0, 0), 1, '8BUI', 1, 0) AS rast UNION ALL
	SELECT 18, ST_AddBand(ST_MakeEmptyRaster(2, 2, 4, -4, 1, -1, 0, 0, 0), 1, '8BUI', 1, 0) AS rast
;

INSERT INTO raster_union_out
	SELECT
		'LAST',
		ST_Union(rast, 1) AS rast
	FROM raster_union_in;

INSERT INTO raster_union_out
	SELECT
		'FIRST',
		ST_Union(rast, 1, 'FIRST') AS rast
	FROM raster_union_in;

INSERT INTO raster_union_out
	SELECT
		'MIN',
		ST_Union(rast, 1, 'MIN') AS rast
	FROM raster_union_in;

INSERT INTO raster_union_out
	SELECT
		'MAX',
		ST_Union(rast, 1, 'MAX') AS rast
	FROM raster_union_in;

INSERT INTO raster_union_out
	SELECT
		'COUNT',
		ST_Union(rast, 1, 'COUNT') AS rast
	FROM raster_union_in;

INSERT INTO raster_union_out
	SELECT
		'SUM',
		ST_Union(rast, 1, 'SUM') AS rast
	FROM raster_union_in;

INSERT INTO raster_union_out
	SELECT
		'MEAN',
		ST_Union(rast, 1, 'mean') AS rast
	FROM raster_union_in;

INSERT INTO raster_union_out
	SELECT
		'RANGE',
		ST_Union(rast, 1, 'RANGE') AS rast
	FROM raster_union_in;

SELECT
	uniontype,
	(pp).x,
	(pp).y,
	(pp).val
FROM (
	SELECT
		uniontype,
		(ST_PixelAsPoints(rast)) AS pp
	FROM raster_union_out
) foo
ORDER BY uniontype, y, x;

TRUNCATE raster_union_out;
TRUNCATE raster_union_in;

INSERT INTO raster_union_in
	SELECT 20, ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(2, 2, 0, 0, 1, -1, 0, 0, 0), 1, '8BUI', 1, 0), 2, '32BF', 1, -9999) AS rast UNION ALL
	SELECT 21, ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(2, 2, 1, -1, 1, -1, 0, 0, 0), 1, '8BUI', 2, 0), 2, '32BF', 2, -9999) AS rast
;

INSERT INTO raster_union_out
	SELECT
		'LAST',
		ST_Union(rast, 2) AS rast
	FROM raster_union_in;

INSERT INTO raster_union_out
	SELECT
		'FIRST',
		ST_Union(rast, 2, 'FIRST') AS rast
	FROM raster_union_in;

INSERT INTO raster_union_out
	SELECT
		'MIN',
		ST_Union(rast, 2, 'MIN') AS rast
	FROM raster_union_in;

INSERT INTO raster_union_out
	SELECT
		'MAX',
		ST_Union(rast, 2, 'MAX') AS rast
	FROM raster_union_in;

INSERT INTO raster_union_out
	SELECT
		'COUNT',
		ST_Union(rast, 2, 'COUNT') AS rast
	FROM raster_union_in;

INSERT INTO raster_union_out
	SELECT
		'SUM',
		ST_Union(rast, 2, 'SUM') AS rast
	FROM raster_union_in;

INSERT INTO raster_union_out
	SELECT
		'MEAN',
		ST_Union(rast, 2, 'mean') AS rast
	FROM raster_union_in;

INSERT INTO raster_union_out
	SELECT
		'RANGE',
		ST_Union(rast, 2, 'RANGE') AS rast
	FROM raster_union_in;

SELECT
	uniontype,
	x,
	y,
	val
FROM (
	SELECT
		uniontype,
		(ST_PixelAsPoints(rast)).*
	FROM raster_union_out
) foo
ORDER BY uniontype, y, x;

TRUNCATE raster_union_out;
TRUNCATE raster_union_in;

INSERT INTO raster_union_in
	SELECT 30, ST_AddBand(ST_MakeEmptyRaster(2, 2, 0, 0, 1, -1, 0, 0, 0), 1, '8BUI', 1, NULL) AS rast UNION ALL
	SELECT 31, ST_AddBand(ST_MakeEmptyRaster(2, 2, 1, -1, 1, -1, 0, 0, 0), 1, '8BUI', 2, NULL) AS rast
;

INSERT INTO raster_union_out
	SELECT
		'LAST',
		ST_Union(rast, 1) AS rast
	FROM raster_union_in;

SELECT
	uniontype,
	x,
	y,
	val
FROM (
	SELECT
		uniontype,
		(ST_PixelAsPoints(rast)).*
	FROM raster_union_out
) foo
ORDER BY uniontype, y, x;

TRUNCATE raster_union_out;
TRUNCATE raster_union_in;

INSERT INTO raster_union_in
	SELECT 40, NULL::raster AS rast UNION ALL
	SELECT 41, NULL::raster AS rast UNION ALL
	SELECT 42, ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(2, 2, 0, 0, 1, -1, 0, 0, 0), 1, '8BUI', 1, 0), 2, '32BF', 100, -9999) AS rast UNION ALL
	SELECT 43, ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(2, 2, 1, -1, 1, -1, 0, 0, 0), 1, '8BUI', 2, 0), 2, '32BF', 200, -9999) AS rast
;

INSERT INTO raster_union_out
	SELECT
		'LAST',
		ST_Union(rast, ARRAY[ROW(1, 'LAST'), ROW(2, 'LAST')]::unionarg[]) AS rast
	FROM raster_union_in;

INSERT INTO raster_union_out
	SELECT
		'FIRST',
		ST_Union(rast, ARRAY[ROW(1, 'FIRST'), ROW(2, 'FIRST')]::unionarg[]) AS rast
	FROM raster_union_in;

INSERT INTO raster_union_out
	SELECT
		'MIN',
		ST_Union(rast, ARRAY[ROW(1, 'MIN'), ROW(2, 'MIN')]::unionarg[]) AS rast
	FROM raster_union_in;

INSERT INTO raster_union_out
	SELECT
		'MAX',
		ST_Union(rast, ARRAY[ROW(1, 'MAX'), ROW(2, 'MAX')]::unionarg[]) AS rast
	FROM raster_union_in;

INSERT INTO raster_union_out
	SELECT
		'COUNT',
		ST_Union(rast, ARRAY[ROW(1, 'COUNT'), ROW(2, 'COUNT')]::unionarg[]) AS rast
	FROM raster_union_in;

INSERT INTO raster_union_out
	SELECT
		'SUM',
		ST_Union(rast, ARRAY[ROW(1, 'SUM'), ROW(2, 'SUM')]::unionarg[]) AS rast
	FROM raster_union_in;

INSERT INTO raster_union_out
	SELECT
		'MEAN',
		ST_Union(rast, ARRAY[ROW(1, 'MEAN'), ROW(2, 'MEAN')]::unionarg[]) AS rast
	FROM raster_union_in;

INSERT INTO raster_union_out
	SELECT
		'RANGE',
		ST_Union(rast, ARRAY[ROW(1, 'RANGE'), ROW(2, 'RANGE')]::unionarg[]) AS rast
	FROM raster_union_in;

SELECT
	uniontype,
	x,
	y,
	val
FROM (
	SELECT
		uniontype,
		(ST_PixelAsPoints(rast)).*
	FROM raster_union_out
) foo
ORDER BY uniontype, y, x;

SELECT
	uniontype,
	x,
	y,
	val
FROM (
	SELECT
		uniontype,
		(ST_PixelAsPoints(rast, 2)).*
	FROM raster_union_out
) foo
ORDER BY uniontype, y, x;

TRUNCATE raster_union_out;
TRUNCATE raster_union_in;

INSERT INTO raster_union_in
	SELECT 50, NULL::raster AS rast UNION ALL
	SELECT 51, NULL::raster AS rast UNION ALL
	SELECT 52, ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(2, 2, 0, 0, 1, -1, 0, 0, 0), 1, '8BUI', 1, 0), 2, '32BF', 100, -9999) AS rast UNION ALL
	SELECT 53, ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(2, 2, 1, -1, 1, -1, 0, 0, 0), 1, '8BUI', 2, 0), 2, '32BF', 200, -9999) AS rast UNION ALL
	SELECT 54, NULL::raster AS rast UNION ALL
	SELECT 55, ST_AddBand(ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(2, 2, -1, 1, 1, -1, 0, 0, 0), 1, '8BUI', 3, 0), 2, '32BF', 300, -9999), 3, '8BSI', -1, -10) AS rast UNION ALL
	SELECT 56, ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(2, 2, 1, 1, 1, -1, 0, 0, 0), 1, '8BUI', 4, 0), 2, '32BF', 400, -9999) AS rast
;

INSERT INTO raster_union_out
	SELECT
		'LAST-1',
		ST_Union(rast) AS rast
	FROM raster_union_in;

INSERT INTO raster_union_out
	SELECT
		'LAST-2',
		ST_Union(rast, 'last') AS rast
	FROM raster_union_in;

INSERT INTO raster_union_out
	SELECT
		'FIRST-2',
		ST_Union(rast, 'first') AS rast
	FROM raster_union_in;

INSERT INTO raster_union_out
	SELECT
		'MEAN-2',
		ST_Union(rast, 'mean') AS rast
	FROM raster_union_in;

SELECT
	uniontype,
	(ST_Metadata(rast)).*
FROM raster_union_out
ORDER BY uniontype;

SELECT
	uniontype,
	x,
	y,
	val
FROM (
	SELECT
		uniontype,
		(ST_PixelAsPoints(rast)).*
	FROM raster_union_out
) foo
ORDER BY uniontype, y, x;

SELECT
	uniontype,
	x,
	y,
	val
FROM (
	SELECT
		uniontype,
		(ST_PixelAsPoints(rast, 2)).*
	FROM raster_union_out
) foo
ORDER BY uniontype, y, x;

SELECT
	uniontype,
	x,
	y,
	val
FROM (
	SELECT
		uniontype,
		(ST_PixelAsPoints(rast, 3)).*
	FROM raster_union_out
) foo
ORDER BY uniontype, y, x;

TRUNCATE raster_union_out;
TRUNCATE raster_union_in;

INSERT INTO raster_union_in
	SELECT 60, ST_AddBand(ST_MakeEmptyRaster(2, 2, 2, -2, 1, -1, 0, 0, 0), 1, '8BUI', 1, 0) AS rast UNION ALL
	SELECT 61, ST_AddBand(ST_MakeEmptyRaster(2, 2, 0, 0, 1, -1, 0, 0, 0), 1, '8BUI', 2, 0) AS rast UNION ALL
	SELECT 62, ST_AddBand(ST_MakeEmptyRaster(2, 2, 2, 0, 1, -1, 0, 0, 0), 1, '8BUI', 3, 0) AS rast UNION ALL
	SELECT 63, ST_AddBand(ST_MakeEmptyRaster(2, 2, -1, -3, 1, -1, 0, 0, 0), 1, '8BUI', 4, 0) AS rast UNION ALL
	SELECT  0, NULL::raster UNION ALL -- toxic element embedded
	SELECT 64, ST_AddBand(ST_MakeEmptyRaster(2, 2, -2, 4, 1, -1, 0, 0, 0), 1, '8BUI', 5, 0) AS rast
;

INSERT INTO raster_union_out
	SELECT
		'LAST',
		ST_Union(rast) AS rast
	FROM raster_union_in;

SELECT
	(ST_Metadata(rast)).*
FROM raster_union_out;

SELECT
	uniontype,
	x,
	y,
	val
FROM (
	SELECT
		uniontype,
		(ST_PixelAsPoints(rast)).*
	FROM raster_union_out
) foo
ORDER BY uniontype, y, x;

DROP TABLE IF EXISTS raster_union_in;
DROP TABLE IF EXISTS raster_union_out;

-- Some toxic input
SELECT 'none', ST_Union(r) from ( select null::raster r where false ) f;
SELECT 'null', ST_Union(null::raster);