-
Notifications
You must be signed in to change notification settings - Fork 5
/
tmp_bdexplo_structure_from_cluster_autan.sql
12508 lines (10559 loc) · 399 KB
/
tmp_bdexplo_structure_from_cluster_autan.sql
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- _______________ENCOURS_______________GEOLLLIBRE
-- D'abord, les tables.
/* vu: {{{
CREATE SCHEMA checks;
ALTER SCHEMA checks OWNER TO pierre;
COMMENT ON SCHEMA checks IS 'Views selecting unconsistent, incoherent, unprobable data';
CREATE SCHEMA gdm;
ALTER SCHEMA gdm OWNER TO pierre;
CREATE SCHEMA input;
ALTER SCHEMA input OWNER TO pierre;
COMMENT ON SCHEMA input IS 'Tables with same structure as in public schema, for data input before validation and dump into final tables (apparently unused on 2013_08_03__11_40_18)';
CREATE SCHEMA pierre;
ALTER SCHEMA pierre OWNER TO pierre;
CREATE SCHEMA stats_reports;
ALTER SCHEMA stats_reports OWNER TO pierre;
COMMENT ON SCHEMA stats_reports IS 'Views with statistics and reports, for daily/weekly/monthly statistics';
CREATE SCHEMA tmp_a_traiter;
ALTER SCHEMA tmp_a_traiter OWNER TO pierre;
CREATE SCHEMA tmp_imports;
ALTER SCHEMA tmp_imports OWNER TO pierre;
CREATE SCHEMA tmp_ntoto;
ALTER SCHEMA tmp_ntoto OWNER TO pierre;
CREATE SCHEMA topology;
ALTER SCHEMA topology OWNER TO postgres;
CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;
COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';
CREATE OR REPLACE PROCEDURAL LANGUAGE plpythonu;
ALTER PROCEDURAL LANGUAGE plpythonu OWNER TO postgres;
CREATE EXTENSION IF NOT EXISTS postgis WITH SCHEMA public;
COMMENT ON EXTENSION postgis IS 'PostGIS geometry, geography, and raster spatial types and functions';
CREATE EXTENSION IF NOT EXISTS postgis_topology WITH SCHEMA topology;
COMMENT ON EXTENSION postgis_topology IS 'PostGIS topology spatial types and functions';
SET search_path = public, pg_catalog;
}}}
vu, du postgis:{{{
--
-- Name: box3d_extent; Type: TYPE; Schema: public; Owner: postgres
--
CREATE TYPE box3d_extent;
ALTER TYPE box3d_extent OWNER TO postgres;
CREATE TYPE chip;
ALTER TYPE chip OWNER TO postgres;
CREATE FUNCTION addgeometrycolumn(character varying, character varying, integer, character varying, integer) RETURNS text
LANGUAGE plpgsql STRICT
AS $_$
DECLARE
ret text;
BEGIN
SELECT AddGeometryColumn('','',$1,$2,$3,$4,$5) into ret;
RETURN ret;
END;
$_$;
ALTER FUNCTION public.addgeometrycolumn(character varying, character varying, integer, character varying, integer) OWNER TO postgres;
CREATE FUNCTION addgeometrycolumn(character varying, character varying, character varying, integer, character varying, integer) RETURNS text
LANGUAGE plpgsql STABLE STRICT
AS $_$
DECLARE
ret text;
BEGIN
SELECT AddGeometryColumn('',$1,$2,$3,$4,$5,$6) into ret;
RETURN ret;
END;
$_$;
ALTER FUNCTION public.addgeometrycolumn(character varying, character varying, character varying, integer, character varying, integer) OWNER TO postgres;
--
-- Name: addgeometrycolumn(character varying, character varying, character varying, character varying, integer, character varying, integer); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION addgeometrycolumn(character varying, character varying, character varying, character varying, integer, character varying, integer) RETURNS text
LANGUAGE plpgsql STRICT
AS $_$
DECLARE
catalog_name alias for $1;
schema_name alias for $2;
table_name alias for $3;
column_name alias for $4;
new_srid alias for $5;
new_type alias for $6;
new_dim alias for $7;
rec RECORD;
sr text;
real_schema name;
sql text;
BEGIN
-- Verify geometry type
IF ( NOT ( (new_type = 'GEOMETRY') OR
(new_type = 'GEOMETRYCOLLECTION') OR
(new_type = 'POINT') OR
(new_type = 'MULTIPOINT') OR
(new_type = 'POLYGON') OR
(new_type = 'MULTIPOLYGON') OR
(new_type = 'LINESTRING') OR
(new_type = 'MULTILINESTRING') OR
(new_type = 'GEOMETRYCOLLECTIONM') OR
(new_type = 'POINTM') OR
(new_type = 'MULTIPOINTM') OR
(new_type = 'POLYGONM') OR
(new_type = 'MULTIPOLYGONM') OR
(new_type = 'LINESTRINGM') OR
(new_type = 'MULTILINESTRINGM') OR
(new_type = 'CIRCULARSTRING') OR
(new_type = 'CIRCULARSTRINGM') OR
(new_type = 'COMPOUNDCURVE') OR
(new_type = 'COMPOUNDCURVEM') OR
(new_type = 'CURVEPOLYGON') OR
(new_type = 'CURVEPOLYGONM') OR
(new_type = 'MULTICURVE') OR
(new_type = 'MULTICURVEM') OR
(new_type = 'MULTISURFACE') OR
(new_type = 'MULTISURFACEM')) )
THEN
RAISE EXCEPTION 'Invalid type name - valid ones are:
POINT, MULTIPOINT,
LINESTRING, MULTILINESTRING,
POLYGON, MULTIPOLYGON,
CIRCULARSTRING, COMPOUNDCURVE, MULTICURVE,
CURVEPOLYGON, MULTISURFACE,
GEOMETRY, GEOMETRYCOLLECTION,
POINTM, MULTIPOINTM,
LINESTRINGM, MULTILINESTRINGM,
POLYGONM, MULTIPOLYGONM,
CIRCULARSTRINGM, COMPOUNDCURVEM, MULTICURVEM
CURVEPOLYGONM, MULTISURFACEM,
or GEOMETRYCOLLECTIONM';
RETURN 'fail';
END IF;
-- Verify dimension
IF ( (new_dim >4) OR (new_dim <0) ) THEN
RAISE EXCEPTION 'invalid dimension';
RETURN 'fail';
END IF;
IF ( (new_type LIKE '%M') AND (new_dim!=3) ) THEN
RAISE EXCEPTION 'TypeM needs 3 dimensions';
RETURN 'fail';
END IF;
-- Verify SRID
IF ( new_srid != -1 ) THEN
SELECT SRID INTO sr FROM spatial_ref_sys WHERE SRID = new_srid;
IF NOT FOUND THEN
RAISE EXCEPTION 'AddGeometryColumns() - invalid SRID';
RETURN 'fail';
END IF;
END IF;
-- Verify schema
IF ( schema_name IS NOT NULL AND schema_name != '' ) THEN
sql := 'SELECT nspname FROM pg_namespace ' ||
'WHERE text(nspname) = ' || quote_literal(schema_name) ||
'LIMIT 1';
RAISE DEBUG '%', sql;
EXECUTE sql INTO real_schema;
IF ( real_schema IS NULL ) THEN
RAISE EXCEPTION 'Schema % is not a valid schemaname', quote_literal(schema_name);
RETURN 'fail';
END IF;
END IF;
IF ( real_schema IS NULL ) THEN
RAISE DEBUG 'Detecting schema';
sql := 'SELECT n.nspname AS schemaname ' ||
'FROM pg_catalog.pg_class c ' ||
'JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace ' ||
'WHERE c.relkind = ' || quote_literal('r') ||
' AND n.nspname NOT IN (' || quote_literal('pg_catalog') || ', ' || quote_literal('pg_toast') || ')' ||
' AND pg_catalog.pg_table_is_visible(c.oid)' ||
' AND c.relname = ' || quote_literal(table_name);
RAISE DEBUG '%', sql;
EXECUTE sql INTO real_schema;
IF ( real_schema IS NULL ) THEN
RAISE EXCEPTION 'Table % does not occur in the search_path', quote_literal(table_name);
RETURN 'fail';
END IF;
END IF;
-- Add geometry column to table
sql := 'ALTER TABLE ' ||
quote_ident(real_schema) || '.' || quote_ident(table_name)
|| ' ADD COLUMN ' || quote_ident(column_name) ||
' geometry ';
RAISE DEBUG '%', sql;
EXECUTE sql;
-- Delete stale record in geometry_columns (if any)
sql := 'DELETE FROM geometry_columns WHERE
f_table_catalog = ' || quote_literal('') ||
' AND f_table_schema = ' ||
quote_literal(real_schema) ||
' AND f_table_name = ' || quote_literal(table_name) ||
' AND f_geometry_column = ' || quote_literal(column_name);
RAISE DEBUG '%', sql;
EXECUTE sql;
-- Add record in geometry_columns
sql := 'INSERT INTO geometry_columns (f_table_catalog,f_table_schema,f_table_name,' ||
'f_geometry_column,coord_dimension,srid,type)' ||
' VALUES (' ||
quote_literal('') || ',' ||
quote_literal(real_schema) || ',' ||
quote_literal(table_name) || ',' ||
quote_literal(column_name) || ',' ||
new_dim::text || ',' ||
new_srid::text || ',' ||
quote_literal(new_type) || ')';
RAISE DEBUG '%', sql;
EXECUTE sql;
-- Add table CHECKs
sql := 'ALTER TABLE ' ||
quote_ident(real_schema) || '.' || quote_ident(table_name)
|| ' ADD CONSTRAINT '
|| quote_ident('enforce_srid_' || column_name)
|| ' CHECK (ST_SRID(' || quote_ident(column_name) ||
') = ' || new_srid::text || ')' ;
RAISE DEBUG '%', sql;
EXECUTE sql;
sql := 'ALTER TABLE ' ||
quote_ident(real_schema) || '.' || quote_ident(table_name)
|| ' ADD CONSTRAINT '
|| quote_ident('enforce_dims_' || column_name)
|| ' CHECK (ST_NDims(' || quote_ident(column_name) ||
') = ' || new_dim::text || ')' ;
RAISE DEBUG '%', sql;
EXECUTE sql;
IF ( NOT (new_type = 'GEOMETRY')) THEN
sql := 'ALTER TABLE ' ||
quote_ident(real_schema) || '.' || quote_ident(table_name) || ' ADD CONSTRAINT ' ||
quote_ident('enforce_geotype_' || column_name) ||
' CHECK (GeometryType(' ||
quote_ident(column_name) || ')=' ||
quote_literal(new_type) || ' OR (' ||
quote_ident(column_name) || ') is null)';
RAISE DEBUG '%', sql;
EXECUTE sql;
END IF;
RETURN
real_schema || '.' ||
table_name || '.' || column_name ||
' SRID:' || new_srid::text ||
' TYPE:' || new_type ||
' DIMS:' || new_dim::text || ' ';
END;
$_$;
ALTER FUNCTION public.addgeometrycolumn(character varying, character varying, character varying, character varying, integer, character varying, integer) OWNER TO postgres;
CREATE FUNCTION fix_geometry_columns() RETURNS text
LANGUAGE plpgsql
AS $$
DECLARE
mislinked record;
result text;
linked integer;
deleted integer;
foundschema integer;
BEGIN
-- Since 7.3 schema support has been added.
-- Previous postgis versions used to put the database name in
-- the schema column. This needs to be fixed, so we try to
-- set the correct schema for each geometry_colums record
-- looking at table, column, type and srid.
UPDATE geometry_columns SET f_table_schema = n.nspname
FROM pg_namespace n, pg_class c, pg_attribute a,
pg_constraint sridcheck, pg_constraint typecheck
WHERE ( f_table_schema is NULL
OR f_table_schema = ''
OR f_table_schema NOT IN (
SELECT nspname::text
FROM pg_namespace nn, pg_class cc, pg_attribute aa
WHERE cc.relnamespace = nn.oid
AND cc.relname = f_table_name::name
AND aa.attrelid = cc.oid
AND aa.attname = f_geometry_column::name))
AND f_table_name::name = c.relname
AND c.oid = a.attrelid
AND c.relnamespace = n.oid
AND f_geometry_column::name = a.attname
AND sridcheck.conrelid = c.oid
AND sridcheck.consrc LIKE '(srid(% = %)'
AND sridcheck.consrc ~ textcat(' = ', srid::text)
AND typecheck.conrelid = c.oid
AND typecheck.consrc LIKE
'((geometrytype(%) = ''%''::text) OR (% IS NULL))'
AND typecheck.consrc ~ textcat(' = ''', type::text)
AND NOT EXISTS (
SELECT oid FROM geometry_columns gc
WHERE c.relname::text = gc.f_table_name
AND n.nspname::text = gc.f_table_schema
AND a.attname::text = gc.f_geometry_column
);
GET DIAGNOSTICS foundschema = ROW_COUNT;
-- no linkage to system table needed
return 'fixed:'||foundschema::text;
END;
$$;
ALTER FUNCTION public.fix_geometry_columns() OWNER TO postgres;
CREATE TABLE spatial_ref_sys_old (
srid integer NOT NULL,
auth_name character varying(256),
auth_srid integer,
srtext character varying(2048),
proj4text character varying(2048)
);
ALTER TABLE spatial_ref_sys_old OWNER TO postgres;
}}}
vu, fonction:{{{
--
-- Name: generate_cross_sections_array(); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION generate_cross_sections_array() RETURNS trigger
LANGUAGE plpythonu
AS $$
#{{{
#{{{
# Cette fonction est appele depuis un TRIGGER de la
# table sections_definition, sans arguments, et elle
# renvoie un trigger.
# dfinition du TRIGGER:
# CREATE TRIGGER sections_definition_change
# AFTER INSERT OR UPDATE ON sections_definition
# FOR EACH ROW
# EXECUTE PROCEDURE generate_cross_sections_array();
#
# @#faire le pendant, qui dtruise les enregistrements de sections_array quand on supprime un enregistrement de sections_definition
#}}}
from math import sin, cos, pi
import string
sep = "," #"\t" #dfinition du sparateur: la virgule
sepchar = "\'" #dfinition du sparateur de chane de caractres: le ', en SQL
#Cherchons les paramtres dfinissant les coupes, dans la table sections_definition:
res = plpy.execute("SELECT opid, id, location, srid, ll_corner_x, ll_corner_y, ll_corner_z, azim_ng, interval, num_start, count, length, title FROM sections_definition
--WHERE opid IN (SELECT opid FROM operation_active)
;")
sql_insert = "" #on btit une chane sql_insert qui contiendra tout ce qu'il faudra faire, des insertions essentiellement
#on enlve d'abord les coupes existantes dans la table sections_array{{{
sql_insert += "DELETE FROM sections_array WHERE sections_array.opid IN (SELECT opid FROM operation_active) AND sections_array.id IS NOT NULL; "
#non: plutt, on enlve seulement les coupes existantes pour le mme id que celui qui vient d'tre affect:
#sql_insert +="DELETE FROM sections_array WHERE substr(sections_array.title, 1, " + str(NEW["title"].len) + ") = " + sepchar + NEW["title"] + sepchar + ";"
# => marche pas:
# ERREUR: PL/python : NameError: global name 'NEW' is not defined
# CONTEXTE : fonction PL/ generate_cross_sections_array Python
# =>@#reprendre
#}}}
i = 0
for line in res:
#le rsultat est fourni sous forme de tuples de dictionnaires:
opid = line["opid"]
id = line["id"]
srid = line["srid"]
location = line["location"]
ll_corner_x = line["ll_corner_x"]
ll_corner_y = line["ll_corner_y"]
ll_corner_z = line["ll_corner_z"]
azim_ng = line["azim_ng"]
interval_ = line["interval"]
num_start = line["num_start"]
count = line["count"]
length = line["length"]
title = line["title"]
#num = 1 #pas besoin, on a mis un autoincrment dans la table
indice_coupe = num_start #l'indice de la coupe
sql_insert += "INSERT INTO sections_array (opid, location, id, title, srid, length, x1, y1, z1, x2, y2, z2) VALUES \n"
for j in range(count):
#out = str(opid) + sep + sepchar + location + sepchar + sep + sepchar + location+'_'+str(indice_coupe).zfill(3) + sepchar + sep + sepchar + title + " - section # "+str(indice_coupe) + sepchar + sep + str(srid) + sep
out = str(opid) + sep + sepchar + location + sepchar + sep + sepchar + location+'_'+str(indice_coupe).zfill(3) + sepchar + sep + sepchar + title + " - section " + location+'_'+str(indice_coupe).zfill(3) + sepchar + sep + str(srid) + sep
x2 = ll_corner_x+interval_*(j) * cos((90.0-azim_ng)/180*pi)
y2 = ll_corner_y+interval_*(j) * sin((90.0-azim_ng)/180*pi)
x1 = x2 - length * sin((90.0-azim_ng)/180*pi)
y1 = y2 + length * cos((90.0-azim_ng)/180*pi)
z = ll_corner_z
out += str(length) + sep + str(x1) + sep + str(y1) + sep + str(z) + sep + str(x2) + sep + str(y2) + sep + str(z)
sql_insert += "("+out+"),\n"
indice_coupe += 1
sql_insert = sql_insert[0:len(sql_insert)-2] #pour enlever le dernier ",\n"
sql_insert += ";\n"
i += 1
# au lieu de retourner la chaine (comme dans le scr
res = plpy.execute(sql_insert)
return 'OK'
#}}}
$$;
ALTER FUNCTION public.generate_cross_sections_array() OWNER TO postgres;
CREATE FUNCTION lab_ana_results_sample_id_default_value_num() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
--UPDATE public.lab_ana_results SET sample_id = lab_sampleid WHERE (sample_id IS NULL OR sample_id = '') AND (lab_sampleid IS NOT NULL OR lab_sampleid <> '');
UPDATE public.lab_ana_results SET sample_id_lab = sample_id;
UPDATE public.lab_ana_results SET sample_id = REPLACE(sample_id, 'STD:', '') WHERE sample_id ILIKE 'STD%';
UPDATE public.lab_ana_results SET value_num =
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(value, 'IS', '-999'),
'NSS', '-999'),
'LNR', '-9999'),
'NA', '-99'),
'<', '-'),
'>', ''),
'Not Received', '-9999'),
'Bag Empty', '-9999')::numeric WHERE value <> 'NULL' AND value IS NOT NULL AND value_num IS NULL;
RETURN NULL;
END;
$$;
ALTER FUNCTION public.lab_ana_results_sample_id_default_value_num() OWNER TO pierre;
}}}
vu, du postgis:{{{
--
-- Name: populate_geometry_columns(); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION populate_geometry_columns() RETURNS text
LANGUAGE plpgsql
AS $$
DECLARE
inserted integer;
oldcount integer;
probed integer;
stale integer;
gcs RECORD;
gc RECORD;
gsrid integer;
gndims integer;
gtype text;
query text;
gc_is_valid boolean;
BEGIN
SELECT count(*) INTO oldcount FROM geometry_columns;
inserted := 0;
EXECUTE 'TRUNCATE geometry_columns';
-- Count the number of geometry columns in all tables and views
SELECT count(DISTINCT c.oid) INTO probed
FROM pg_class c,
pg_attribute a,
pg_type t,
pg_namespace n
WHERE (c.relkind = 'r' OR c.relkind = 'v')
AND t.typname = 'geometry'
AND a.attisdropped = false
AND a.atttypid = t.oid
AND a.attrelid = c.oid
AND c.relnamespace = n.oid
AND n.nspname NOT ILIKE 'pg_temp%';
-- Iterate through all non-dropped geometry columns
RAISE DEBUG 'Processing Tables.....';
FOR gcs IN
SELECT DISTINCT ON (c.oid) c.oid, n.nspname, c.relname
FROM pg_class c,
pg_attribute a,
pg_type t,
pg_namespace n
WHERE c.relkind = 'r'
AND t.typname = 'geometry'
AND a.attisdropped = false
AND a.atttypid = t.oid
AND a.attrelid = c.oid
AND c.relnamespace = n.oid
AND n.nspname NOT ILIKE 'pg_temp%'
LOOP
inserted := inserted + populate_geometry_columns(gcs.oid);
END LOOP;
-- Add views to geometry columns table
RAISE DEBUG 'Processing Views.....';
FOR gcs IN
SELECT DISTINCT ON (c.oid) c.oid, n.nspname, c.relname
FROM pg_class c,
pg_attribute a,
pg_type t,
pg_namespace n
WHERE c.relkind = 'v'
AND t.typname = 'geometry'
AND a.attisdropped = false
AND a.atttypid = t.oid
AND a.attrelid = c.oid
AND c.relnamespace = n.oid
LOOP
inserted := inserted + populate_geometry_columns(gcs.oid);
END LOOP;
IF oldcount > inserted THEN
stale = oldcount-inserted;
ELSE
stale = 0;
END IF;
RETURN 'probed:' ||probed|| ' inserted:'||inserted|| ' conflicts:'||probed-inserted|| ' deleted:'||stale;
END
$$;
ALTER FUNCTION public.populate_geometry_columns() OWNER TO postgres;
CREATE FUNCTION populate_geometry_columns(tbl_oid oid) RETURNS integer
LANGUAGE plpgsql
AS $$
DECLARE
gcs RECORD;
gc RECORD;
gsrid integer;
gndims integer;
gtype text;
query text;
gc_is_valid boolean;
inserted integer;
BEGIN
inserted := 0;
-- Iterate through all geometry columns in this table
FOR gcs IN
SELECT n.nspname, c.relname, a.attname
FROM pg_class c,
pg_attribute a,
pg_type t,
pg_namespace n
WHERE c.relkind = 'r'
AND t.typname = 'geometry'
AND a.attisdropped = false
AND a.atttypid = t.oid
AND a.attrelid = c.oid
AND c.relnamespace = n.oid
AND n.nspname NOT ILIKE 'pg_temp%'
AND c.oid = tbl_oid
LOOP
RAISE DEBUG 'Processing table %.%.%', gcs.nspname, gcs.relname, gcs.attname;
DELETE FROM geometry_columns
WHERE f_table_schema = quote_ident(gcs.nspname)
AND f_table_name = quote_ident(gcs.relname)
AND f_geometry_column = quote_ident(gcs.attname);
gc_is_valid := true;
-- Try to find srid check from system tables (pg_constraint)
gsrid :=
(SELECT replace(replace(split_part(s.consrc, ' = ', 2), ')', ''), '(', '')
FROM pg_class c, pg_namespace n, pg_attribute a, pg_constraint s
WHERE n.nspname = gcs.nspname
AND c.relname = gcs.relname
AND a.attname = gcs.attname
AND a.attrelid = c.oid
AND s.connamespace = n.oid
AND s.conrelid = c.oid
AND a.attnum = ANY (s.conkey)
AND s.consrc LIKE '%srid(% = %');
IF (gsrid IS NULL) THEN
-- Try to find srid from the geometry itself
EXECUTE 'SELECT srid(' || quote_ident(gcs.attname) || ')
FROM ONLY ' || quote_ident(gcs.nspname) || '.' || quote_ident(gcs.relname) || '
WHERE ' || quote_ident(gcs.attname) || ' IS NOT NULL LIMIT 1'
INTO gc;
gsrid := gc.srid;
-- Try to apply srid check to column
IF (gsrid IS NOT NULL) THEN
BEGIN
EXECUTE 'ALTER TABLE ONLY ' || quote_ident(gcs.nspname) || '.' || quote_ident(gcs.relname) || '
ADD CONSTRAINT ' || quote_ident('enforce_srid_' || gcs.attname) || '
CHECK (srid(' || quote_ident(gcs.attname) || ') = ' || gsrid || ')';
EXCEPTION
WHEN check_violation THEN
RAISE WARNING 'Not inserting ''%'' in ''%.%'' into geometry_columns: could not apply constraint CHECK (srid(%) = %)', quote_ident(gcs.attname), quote_ident(gcs.nspname), quote_ident(gcs.relname), quote_ident(gcs.attname), gsrid;
gc_is_valid := false;
END;
END IF;
END IF;
-- Try to find ndims check from system tables (pg_constraint)
gndims :=
(SELECT replace(split_part(s.consrc, ' = ', 2), ')', '')
FROM pg_class c, pg_namespace n, pg_attribute a, pg_constraint s
WHERE n.nspname = gcs.nspname
AND c.relname = gcs.relname
AND a.attname = gcs.attname
AND a.attrelid = c.oid
AND s.connamespace = n.oid
AND s.conrelid = c.oid
AND a.attnum = ANY (s.conkey)
AND s.consrc LIKE '%ndims(% = %');
IF (gndims IS NULL) THEN
-- Try to find ndims from the geometry itself
EXECUTE 'SELECT ndims(' || quote_ident(gcs.attname) || ')
FROM ONLY ' || quote_ident(gcs.nspname) || '.' || quote_ident(gcs.relname) || '
WHERE ' || quote_ident(gcs.attname) || ' IS NOT NULL LIMIT 1'
INTO gc;
gndims := gc.ndims;
-- Try to apply ndims check to column
IF (gndims IS NOT NULL) THEN
BEGIN
EXECUTE 'ALTER TABLE ONLY ' || quote_ident(gcs.nspname) || '.' || quote_ident(gcs.relname) || '
ADD CONSTRAINT ' || quote_ident('enforce_dims_' || gcs.attname) || '
CHECK (ndims(' || quote_ident(gcs.attname) || ') = '||gndims||')';
EXCEPTION
WHEN check_violation THEN
RAISE WARNING 'Not inserting ''%'' in ''%.%'' into geometry_columns: could not apply constraint CHECK (ndims(%) = %)', quote_ident(gcs.attname), quote_ident(gcs.nspname), quote_ident(gcs.relname), quote_ident(gcs.attname), gndims;
gc_is_valid := false;
END;
END IF;
END IF;
-- Try to find geotype check from system tables (pg_constraint)
gtype :=
(SELECT replace(split_part(s.consrc, '''', 2), ')', '')
FROM pg_class c, pg_namespace n, pg_attribute a, pg_constraint s
WHERE n.nspname = gcs.nspname
AND c.relname = gcs.relname
AND a.attname = gcs.attname
AND a.attrelid = c.oid
AND s.connamespace = n.oid
AND s.conrelid = c.oid
AND a.attnum = ANY (s.conkey)
AND s.consrc LIKE '%geometrytype(% = %');
IF (gtype IS NULL) THEN
-- Try to find geotype from the geometry itself
EXECUTE 'SELECT geometrytype(' || quote_ident(gcs.attname) || ')
FROM ONLY ' || quote_ident(gcs.nspname) || '.' || quote_ident(gcs.relname) || '
WHERE ' || quote_ident(gcs.attname) || ' IS NOT NULL LIMIT 1'
INTO gc;
gtype := gc.geometrytype;
--IF (gtype IS NULL) THEN
-- gtype := 'GEOMETRY';
--END IF;
-- Try to apply geometrytype check to column
IF (gtype IS NOT NULL) THEN
BEGIN
EXECUTE 'ALTER TABLE ONLY ' || quote_ident(gcs.nspname) || '.' || quote_ident(gcs.relname) || '
ADD CONSTRAINT ' || quote_ident('enforce_geotype_' || gcs.attname) || '
CHECK ((geometrytype(' || quote_ident(gcs.attname) || ') = ' || quote_literal(gtype) || ') OR (' || quote_ident(gcs.attname) || ' IS NULL))';
EXCEPTION
WHEN check_violation THEN
-- No geometry check can be applied. This column contains a number of geometry types.
RAISE WARNING 'Could not add geometry type check (%) to table column: %.%.%', gtype, quote_ident(gcs.nspname),quote_ident(gcs.relname),quote_ident(gcs.attname);
END;
END IF;
END IF;
IF (gsrid IS NULL) THEN
RAISE WARNING 'Not inserting ''%'' in ''%.%'' into geometry_columns: could not determine the srid', quote_ident(gcs.attname), quote_ident(gcs.nspname), quote_ident(gcs.relname);
ELSIF (gndims IS NULL) THEN
RAISE WARNING 'Not inserting ''%'' in ''%.%'' into geometry_columns: could not determine the number of dimensions', quote_ident(gcs.attname), quote_ident(gcs.nspname), quote_ident(gcs.relname);
ELSIF (gtype IS NULL) THEN
RAISE WARNING 'Not inserting ''%'' in ''%.%'' into geometry_columns: could not determine the geometry type', quote_ident(gcs.attname), quote_ident(gcs.nspname), quote_ident(gcs.relname);
ELSE
-- Only insert into geometry_columns if table constraints could be applied.
IF (gc_is_valid) THEN
INSERT INTO geometry_columns (f_table_catalog,f_table_schema, f_table_name, f_geometry_column, coord_dimension, srid, type)
VALUES ('', gcs.nspname, gcs.relname, gcs.attname, gndims, gsrid, gtype);
inserted := inserted + 1;
END IF;
END IF;
END LOOP;
-- Add views to geometry columns table
FOR gcs IN
SELECT n.nspname, c.relname, a.attname
FROM pg_class c,
pg_attribute a,
pg_type t,
pg_namespace n
WHERE c.relkind = 'v'
AND t.typname = 'geometry'
AND a.attisdropped = false
AND a.atttypid = t.oid
AND a.attrelid = c.oid
AND c.relnamespace = n.oid
AND n.nspname NOT ILIKE 'pg_temp%'
AND c.oid = tbl_oid
LOOP
RAISE DEBUG 'Processing view %.%.%', gcs.nspname, gcs.relname, gcs.attname;
EXECUTE 'SELECT ndims(' || quote_ident(gcs.attname) || ')
FROM ' || quote_ident(gcs.nspname) || '.' || quote_ident(gcs.relname) || '
WHERE ' || quote_ident(gcs.attname) || ' IS NOT NULL LIMIT 1'
INTO gc;
gndims := gc.ndims;
EXECUTE 'SELECT srid(' || quote_ident(gcs.attname) || ')
FROM ' || quote_ident(gcs.nspname) || '.' || quote_ident(gcs.relname) || '
WHERE ' || quote_ident(gcs.attname) || ' IS NOT NULL LIMIT 1'
INTO gc;
gsrid := gc.srid;
EXECUTE 'SELECT geometrytype(' || quote_ident(gcs.attname) || ')
FROM ' || quote_ident(gcs.nspname) || '.' || quote_ident(gcs.relname) || '
WHERE ' || quote_ident(gcs.attname) || ' IS NOT NULL LIMIT 1'
INTO gc;
gtype := gc.geometrytype;
IF (gndims IS NULL) THEN
RAISE WARNING 'Not inserting ''%'' in ''%.%'' into geometry_columns: could not determine ndims', quote_ident(gcs.attname), quote_ident(gcs.nspname), quote_ident(gcs.relname);
ELSIF (gsrid IS NULL) THEN
RAISE WARNING 'Not inserting ''%'' in ''%.%'' into geometry_columns: could not determine srid', quote_ident(gcs.attname), quote_ident(gcs.nspname), quote_ident(gcs.relname);
ELSIF (gtype IS NULL) THEN
RAISE WARNING 'Not inserting ''%'' in ''%.%'' into geometry_columns: could not determine gtype', quote_ident(gcs.attname), quote_ident(gcs.nspname), quote_ident(gcs.relname);
ELSE
query := 'INSERT INTO geometry_columns (f_table_catalog,f_table_schema, f_table_name, f_geometry_column, coord_dimension, srid, type) ' ||
'VALUES ('''', ' || quote_literal(gcs.nspname) || ',' || quote_literal(gcs.relname) || ',' || quote_literal(gcs.attname) || ',' || gndims || ',' || gsrid || ',' || quote_literal(gtype) || ')';
EXECUTE query;
inserted := inserted + 1;
END IF;
END LOOP;
RETURN inserted;
END
$$;
ALTER FUNCTION public.populate_geometry_columns(tbl_oid oid) OWNER TO postgres;
CREATE FUNCTION probe_geometry_columns() RETURNS text
LANGUAGE plpgsql
AS $$
DECLARE
inserted integer;
oldcount integer;
probed integer;
stale integer;
BEGIN
SELECT count(*) INTO oldcount FROM geometry_columns;
SELECT count(*) INTO probed
FROM pg_class c, pg_attribute a, pg_type t,
pg_namespace n,
pg_constraint sridcheck, pg_constraint typecheck
WHERE t.typname = 'geometry'
AND a.atttypid = t.oid
AND a.attrelid = c.oid
AND c.relnamespace = n.oid
AND sridcheck.connamespace = n.oid
AND typecheck.connamespace = n.oid
AND sridcheck.conrelid = c.oid
AND sridcheck.consrc LIKE '(srid('||a.attname||') = %)'
AND typecheck.conrelid = c.oid
AND typecheck.consrc LIKE
'((geometrytype('||a.attname||') = ''%''::text) OR (% IS NULL))'
;
INSERT INTO geometry_columns SELECT
''::text as f_table_catalogue,
n.nspname::text as f_table_schema,
c.relname::text as f_table_name,
a.attname::text as f_geometry_column,
2 as coord_dimension,
trim(both ' =)' from
replace(replace(split_part(
sridcheck.consrc, ' = ', 2), ')', ''), '(', ''))::integer AS srid,
trim(both ' =)''' from substr(typecheck.consrc,
strpos(typecheck.consrc, '='),
strpos(typecheck.consrc, '::')-
strpos(typecheck.consrc, '=')
))::text as type
FROM pg_class c, pg_attribute a, pg_type t,
pg_namespace n,
pg_constraint sridcheck, pg_constraint typecheck
WHERE t.typname = 'geometry'
AND a.atttypid = t.oid
AND a.attrelid = c.oid
AND c.relnamespace = n.oid
AND sridcheck.connamespace = n.oid
AND typecheck.connamespace = n.oid
AND sridcheck.conrelid = c.oid
AND sridcheck.consrc LIKE '(st_srid('||a.attname||') = %)'
AND typecheck.conrelid = c.oid
AND typecheck.consrc LIKE
'((geometrytype('||a.attname||') = ''%''::text) OR (% IS NULL))'
AND NOT EXISTS (
SELECT oid FROM geometry_columns gc
WHERE c.relname::text = gc.f_table_name
AND n.nspname::text = gc.f_table_schema
AND a.attname::text = gc.f_geometry_column
);
GET DIAGNOSTICS inserted = ROW_COUNT;
if OLDCOUNT > PROBED theN
stale = oldcount-probed;
ELSE
stale = 0;
END IF;
RETURN 'probed:'||probed::text||
' inserted:'||inserted::text||
' conflicts:'||(probed-inserted)::text||
' stale:'||stale::text;
END
$$;
ALTER FUNCTION public.probe_geometry_columns() OWNER TO postgres;
CREATE FUNCTION rename_geometry_table_constraints() RETURNS text
LANGUAGE sql IMMUTABLE
AS $$
SELECT 'rename_geometry_table_constraint() is obsoleted'::text
$$;
ALTER FUNCTION public.rename_geometry_table_constraints() OWNER TO postgres;
CREATE FUNCTION st_asbinary(text) RETURNS bytea
LANGUAGE sql IMMUTABLE STRICT
AS $_$ SELECT ST_AsBinary($1::geometry); $_$;
ALTER FUNCTION public.st_asbinary(text) OWNER TO postgres;
SET search_path = backups, pg_catalog;
SET default_tablespace = '';
SET default_with_oids = false;
}}}
TABLES VUES:{{{
SET search_path = public, pg_catalog;
CREATE TABLE public.dh_collars (
id text NOT NULL,
shid text,
location text,
profile text,
srid integer,
x numeric(12,3),
y numeric(12,3),
z numeric(12,3),
azim_ng numeric(10,2),
azim_nm numeric(10,2),
dip_hz numeric(10,2),
dh_type text,
date_start date,
contractor text,
geologist text,
length numeric(10,2),
nb_samples integer,
comments text,
completed boolean DEFAULT false,
numauto integer NOT NULL,
date_completed date,
opid integer NOT NULL,
purpose text DEFAULT 'EXPLO'::text,
x_local numeric(12,3),
y_local numeric(12,3),
z_local numeric(12,3),
accusum numeric(10,2),
id_pject text,
x_pject numeric(10,3),
y_pject numeric(10,3),
z_pject numeric(10,3),
topo_survey_type text,
creation_ts timestamp without time zone DEFAULT now(),
username text DEFAULT "current_user"(),
datasource integer,
campaign text
);
ALTER TABLE dh_collars OWNER TO data_admin;
COMMENT ON TABLE dh_collars IS 'Drill holes collars or trenches starting points';
COMMENT ON COLUMN dh_collars.id IS 'Full identifier for borehole or trench, may include zone code, type and sequential number';
COMMENT ON COLUMN dh_collars.shid IS 'Short identifier: type _ sequential number';
COMMENT ON COLUMN dh_collars.location IS 'Investigated area code, refers to occurrences table';
COMMENT ON COLUMN dh_collars.profile IS 'Profile number';
COMMENT ON COLUMN dh_collars.srid IS 'Spatial Reference Identifier, or coordinate reference system: see spatial_ref_sys from postgis extension';
COMMENT ON COLUMN dh_collars.x IS 'X coordinate (Easting), in coordinate system srid';
COMMENT ON COLUMN dh_collars.y IS 'Y coordinate (Northing), in coordinate system srid';
COMMENT ON COLUMN dh_collars.azim_ng IS 'Hole or trench azimuth (°) relative to geographic North';
COMMENT ON COLUMN dh_collars.azim_nm IS 'Hole or trench azimuth (°) relative to Magnetic North';
COMMENT ON COLUMN dh_collars.dip_hz IS 'Drill hole or trench dip relative to horizontal (°)';
COMMENT ON COLUMN dh_collars.dh_type IS 'Type: D for Diamond drill hole, R for RC drill hole, T for Trench, A for Auger drill hole';
COMMENT ON COLUMN dh_collars.date_start IS 'Work start date';
COMMENT ON COLUMN dh_collars.contractor IS 'Drilling contractor';
COMMENT ON COLUMN dh_collars.geologist IS 'Geologist name';
COMMENT ON COLUMN dh_collars.length IS 'Total length (m)';
COMMENT ON COLUMN dh_collars.nb_samples IS 'Number of samples';
COMMENT ON COLUMN dh_collars.comments IS 'Comments';
COMMENT ON COLUMN dh_collars.completed IS 'True: completed; False: planned';
COMMENT ON COLUMN dh_collars.numauto IS 'Automatic integer primary key';
COMMENT ON COLUMN dh_collars.date_completed IS 'Work finish date';
COMMENT ON COLUMN dh_collars.opid IS 'Operation identifier';
COMMENT ON COLUMN dh_collars.purpose IS 'Purpose of hole: exploration, delineation, estimation, grade control, etc.';
COMMENT ON COLUMN dh_collars.x_local IS 'Local x coordinate';
COMMENT ON COLUMN dh_collars.y_local IS 'Local y coordinate';
COMMENT ON COLUMN dh_collars.z_local IS 'Local z coordinate';
COMMENT ON COLUMN dh_collars.accusum IS 'Accumulation sum over various mineralised intervals intersected by drill hole or trench (purpose: quick visualisation on maps (at wide scale ONLY), quick ranking of interesting holes)';
COMMENT ON COLUMN dh_collars.id_pject IS 'PJ for ProJect identifier: provisional identifier; aka peg number';
COMMENT ON COLUMN dh_collars.x_pject IS 'Planned x coordinate';
COMMENT ON COLUMN dh_collars.y_pject IS 'Planned y coordinate';
COMMENT ON COLUMN dh_collars.z_pject IS 'Planned z coordinate';
COMMENT ON COLUMN dh_collars.topo_survey_type IS 'Topographic collar survey type: GPS, GPSD, geometry, theodolite, relative, computed from local coordinate system, etc.';
COMMENT ON COLUMN dh_collars.creation_ts IS 'Current date and time stamp when data is loaded in table';
COMMENT ON COLUMN dh_collars.username IS 'User (role) which created data record';
COMMENT ON COLUMN dh_collars.datasource IS 'Datasource identifier, refers to lex_datasource';
COMMENT ON COLUMN dh_collars.campaign IS 'Campaign: year, type, etc. i.e. DDH exploration 1967';
CREATE TABLE operation_active (
opid integer,
creation_ts timestamp without time zone DEFAULT now(),
username text DEFAULT "current_user"(),
numauto integer NOT NULL
);
ALTER TABLE operation_active OWNER TO data_admin;
COMMENT ON COLUMN operation_active.opid IS 'Operation identifier';
COMMENT ON COLUMN operation_active.creation_ts IS 'Current date and time stamp when data is loaded in table';