-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdz_json_deploy.sql
8954 lines (7237 loc) · 291 KB
/
dz_json_deploy.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
WHENEVER SQLERROR EXIT -99;
WHENEVER OSERROR EXIT -98;
SET DEFINE OFF;
--******************************--
PROMPT Packages/DZ_JSON_CONSTANTS.pks
CREATE OR REPLACE PACKAGE dz_json_constants
AUTHID CURRENT_USER
AS
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
/*
Constant: dz_json_constants.c_json_unescaped_slashes
Flag to control whether to escape forward slashes. Certain libraries may
or may not require such escaping.
*/
c_json_unescaped_slashes CONSTANT BOOLEAN := TRUE;
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
/*
Constant: dz_json_constants.c_json_unescaped_unicode
Flag to control escaping of unicode characters into /u format. Set to TRUE
to leave all unicode text as received. Note this is not recommended for
most JSON handling.
*/
c_json_unescaped_unicode CONSTANT BOOLEAN := FALSE;
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
/*
Constant: dz_json_constants.c_json_escape_all_nonascii
Flag to control whether to escape all non-ascii characters into /u format.
May be helpful for certain character sets or use cases.
*/
c_json_escape_all_nonascii CONSTANT BOOLEAN := FALSE;
END dz_json_constants;
/
GRANT EXECUTE ON dz_json_constants TO PUBLIC;
--******************************--
PROMPT Packages/DZ_JSON_UTIL.pks
CREATE OR REPLACE PACKAGE dz_json_util
AUTHID CURRENT_USER
AS
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
FUNCTION get_guid
RETURN VARCHAR2;
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
FUNCTION gz_split(
p_str IN VARCHAR2
,p_regex IN VARCHAR2
,p_match IN VARCHAR2 DEFAULT NULL
,p_end IN NUMBER DEFAULT 0
,p_trim IN VARCHAR2 DEFAULT 'FALSE'
) RETURN MDSYS.SDO_STRING2_ARRAY DETERMINISTIC;
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
FUNCTION safe_to_number(
p_input IN VARCHAR2
,p_null_replacement IN NUMBER DEFAULT NULL
) RETURN NUMBER;
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
FUNCTION strings2numbers(
p_input IN MDSYS.SDO_STRING2_ARRAY
) RETURN MDSYS.SDO_NUMBER_ARRAY;
----------------------------------------------------------------------------
-----------------------------------------------------------------------------
FUNCTION true_point(
p_input IN MDSYS.SDO_GEOMETRY
) RETURN MDSYS.SDO_GEOMETRY;
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
FUNCTION downsize_2d(
p_input IN MDSYS.SDO_GEOMETRY
) RETURN MDSYS.SDO_GEOMETRY;
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
FUNCTION downsize_2dM(
p_input IN MDSYS.SDO_GEOMETRY
) RETURN MDSYS.SDO_GEOMETRY;
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
FUNCTION downsize_3d(
p_input IN MDSYS.SDO_GEOMETRY
) RETURN MDSYS.SDO_GEOMETRY;
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
FUNCTION prune_number(
p_input IN NUMBER
,p_trunc IN NUMBER DEFAULT NULL
) RETURN NUMBER;
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
FUNCTION prune_number_varchar2(
p_input IN NUMBER
,p_trunc IN NUMBER DEFAULT NULL
) RETURN VARCHAR2;
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
FUNCTION prune_number_clob(
p_input IN NUMBER
,p_trunc IN NUMBER DEFAULT NULL
) RETURN CLOB;
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
FUNCTION pretty_str(
p_input IN VARCHAR2
,p_level IN NUMBER
,p_amount IN VARCHAR2 DEFAULT ' '
,p_linefeed IN VARCHAR2 DEFAULT CHR(10)
) RETURN VARCHAR2;
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
FUNCTION pretty(
p_input IN CLOB
,p_level IN NUMBER
,p_amount IN VARCHAR2 DEFAULT ' '
,p_linefeed IN VARCHAR2 DEFAULT CHR(10)
) RETURN CLOB;
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
PROCEDURE append2(
p_input IN OUT MDSYS.SDO_NUMBER_ARRAY
,p_value IN MDSYS.SDO_NUMBER_ARRAY
,p_unique IN VARCHAR2 DEFAULT 'FALSE'
);
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
PROCEDURE append2(
p_input IN OUT MDSYS.SDO_NUMBER_ARRAY
,p_value IN NUMBER
,p_unique IN VARCHAR2 DEFAULT 'FALSE'
);
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
PROCEDURE append2(
p_input IN OUT MDSYS.SDO_STRING2_ARRAY
,p_value IN MDSYS.SDO_STRING2_ARRAY
,p_unique IN VARCHAR2 DEFAULT 'FALSE'
);
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
PROCEDURE append2(
p_input IN OUT MDSYS.SDO_STRING2_ARRAY
,p_value IN VARCHAR2
,p_unique IN VARCHAR2 DEFAULT 'FALSE'
);
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
FUNCTION a_in_b(
p_input_a IN NUMBER
,p_input_b IN MDSYS.SDO_NUMBER_ARRAY
) RETURN VARCHAR2;
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
FUNCTION a_in_b(
p_input_a IN MDSYS.SDO_NUMBER_ARRAY
,p_input_b IN MDSYS.SDO_NUMBER_ARRAY
) RETURN VARCHAR2;
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
FUNCTION a_in_b(
p_input_a IN VARCHAR2
,p_input_b IN MDSYS.SDO_STRING2_ARRAY
) RETURN VARCHAR2;
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
FUNCTION a_in_b(
p_input_a IN MDSYS.SDO_STRING2_ARRAY
,p_input_b IN MDSYS.SDO_STRING2_ARRAY
) RETURN VARCHAR2;
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
FUNCTION sort_string(
p_input_array IN MDSYS.SDO_STRING2_ARRAY
,p_direction IN VARCHAR2 DEFAULT 'ASC'
,p_unique IN VARCHAR2 DEFAULT 'FALSE'
) RETURN MDSYS.SDO_STRING2_ARRAY;
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
FUNCTION sort_number(
p_input_array IN MDSYS.SDO_NUMBER_ARRAY
,p_direction IN VARCHAR2 DEFAULT 'ASC'
,p_unique IN VARCHAR2 DEFAULT 'FALSE'
) RETURN MDSYS.SDO_NUMBER_ARRAY;
END dz_json_util;
/
GRANT EXECUTE ON dz_json_util TO PUBLIC;
--******************************--
PROMPT Packages/DZ_JSON_UTIL.pkb
CREATE OR REPLACE PACKAGE BODY dz_json_util
AS
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
FUNCTION get_guid
RETURN VARCHAR2
AS
str_sysguid VARCHAR2(40 Char);
BEGIN
str_sysguid := UPPER(RAWTOHEX(SYS_GUID()));
RETURN '{'
|| SUBSTR(str_sysguid,1,8) || '-'
|| SUBSTR(str_sysguid,9,4) || '-'
|| SUBSTR(str_sysguid,13,4) || '-'
|| SUBSTR(str_sysguid,17,4) || '-'
|| SUBSTR(str_sysguid,21,12)|| '}';
END get_guid;
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
FUNCTION gz_split(
p_str IN VARCHAR2
,p_regex IN VARCHAR2
,p_match IN VARCHAR2 DEFAULT NULL
,p_end IN NUMBER DEFAULT 0
,p_trim IN VARCHAR2 DEFAULT 'FALSE'
) RETURN MDSYS.SDO_STRING2_ARRAY DETERMINISTIC
AS
int_delim PLS_INTEGER;
int_position PLS_INTEGER := 1;
int_counter PLS_INTEGER := 1;
ary_output MDSYS.SDO_STRING2_ARRAY;
num_end NUMBER := p_end;
str_trim VARCHAR2(5 Char) := UPPER(p_trim);
FUNCTION trim_varray(
p_input IN MDSYS.SDO_STRING2_ARRAY
) RETURN MDSYS.SDO_STRING2_ARRAY
AS
ary_output MDSYS.SDO_STRING2_ARRAY := MDSYS.SDO_STRING2_ARRAY();
int_index PLS_INTEGER := 1;
str_check VARCHAR2(4000 Char);
BEGIN
--------------------------------------------------------------------------
-- Step 10
-- Exit if input is empty
--------------------------------------------------------------------------
IF p_input IS NULL
OR p_input.COUNT = 0
THEN
RETURN ary_output;
END IF;
--------------------------------------------------------------------------
-- Step 20
-- Trim the strings removing anything utterly trimmed away
--------------------------------------------------------------------------
FOR i IN 1 .. p_input.COUNT
LOOP
str_check := TRIM(p_input(i));
IF str_check IS NULL
OR str_check = ''
THEN
NULL;
ELSE
ary_output.EXTEND(1);
ary_output(int_index) := str_check;
int_index := int_index + 1;
END IF;
END LOOP;
--------------------------------------------------------------------------
-- Step 10
-- Return the results
--------------------------------------------------------------------------
RETURN ary_output;
END trim_varray;
BEGIN
--------------------------------------------------------------------------
-- Step 10
-- Create the output array and check parameters
--------------------------------------------------------------------------
ary_output := MDSYS.SDO_STRING2_ARRAY();
IF str_trim IS NULL
THEN
str_trim := 'FALSE';
ELSIF str_trim NOT IN ('TRUE','FALSE')
THEN
RAISE_APPLICATION_ERROR(-20001,'boolean error');
END IF;
IF num_end IS NULL
THEN
num_end := 0;
END IF;
--------------------------------------------------------------------------
-- Step 20
-- Exit early if input is empty
--------------------------------------------------------------------------
IF p_str IS NULL
OR p_str = ''
THEN
RETURN ary_output;
END IF;
--------------------------------------------------------------------------
-- Step 30
-- Account for weird instance of pure character breaking
--------------------------------------------------------------------------
IF p_regex IS NULL
OR p_regex = ''
THEN
FOR i IN 1 .. LENGTH(p_str)
LOOP
ary_output.EXTEND(1);
ary_output(i) := SUBSTR(p_str,i,1);
END LOOP;
RETURN ary_output;
END IF;
--------------------------------------------------------------------------
-- Step 40
-- Break string using the usual REGEXP functions
--------------------------------------------------------------------------
LOOP
EXIT WHEN int_position = 0;
int_delim := REGEXP_INSTR(p_str,p_regex,int_position,1,0,p_match);
IF int_delim = 0
THEN
-- no more matches found
ary_output.EXTEND(1);
ary_output(int_counter) := SUBSTR(p_str,int_position);
int_position := 0;
ELSE
IF int_counter = num_end
THEN
-- take the rest as is
ary_output.EXTEND(1);
ary_output(int_counter) := SUBSTR(p_str,int_position);
int_position := 0;
ELSE
--dbms_output.put_line(ary_output.COUNT);
ary_output.EXTEND(1);
ary_output(int_counter) := SUBSTR(p_str,int_position,int_delim-int_position);
int_counter := int_counter + 1;
int_position := REGEXP_INSTR(p_str,p_regex,int_position,1,1,p_match);
END IF;
END IF;
END LOOP;
--------------------------------------------------------------------------
-- Step 50
-- Trim results if so desired
--------------------------------------------------------------------------
IF str_trim = 'TRUE'
THEN
RETURN trim_varray(
p_input => ary_output
);
END IF;
--------------------------------------------------------------------------
-- Step 60
-- Cough out the results
--------------------------------------------------------------------------
RETURN ary_output;
END gz_split;
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
FUNCTION safe_to_number(
p_input IN VARCHAR2
,p_null_replacement IN NUMBER DEFAULT NULL
) RETURN NUMBER
AS
BEGIN
RETURN TO_NUMBER(
REPLACE(
REPLACE(
p_input,
CHR(10),
''
),
CHR(13),
''
)
);
EXCEPTION
WHEN VALUE_ERROR
THEN
RETURN p_null_replacement;
END safe_to_number;
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
FUNCTION strings2numbers(
p_input IN MDSYS.SDO_STRING2_ARRAY
) RETURN MDSYS.SDO_NUMBER_ARRAY
AS
ary_output MDSYS.SDO_NUMBER_ARRAY := MDSYS.SDO_NUMBER_ARRAY();
num_tester NUMBER;
int_index PLS_INTEGER := 1;
BEGIN
--------------------------------------------------------------------------
-- Step 10
-- Exit if input is empty
--------------------------------------------------------------------------
IF p_input IS NULL
OR p_input.COUNT = 0
THEN
RETURN ary_output;
END IF;
--------------------------------------------------------------------------
-- Step 20
-- Convert anything that is a valid number to a number, dump the rest
--------------------------------------------------------------------------
FOR i IN 1 .. p_input.COUNT
LOOP
IF p_input(i) IS NOT NULL
THEN
num_tester := safe_to_number(
p_input => p_input(i)
);
IF num_tester IS NOT NULL
THEN
ary_output.EXTEND();
ary_output(int_index) := num_tester;
int_index := int_index + 1;
END IF;
END IF;
END LOOP;
RETURN ary_output;
END strings2numbers;
----------------------------------------------------------------------------
-----------------------------------------------------------------------------
FUNCTION true_point(
p_input IN MDSYS.SDO_GEOMETRY
) RETURN MDSYS.SDO_GEOMETRY
AS
BEGIN
IF p_input.SDO_POINT IS NOT NULL
THEN
RETURN p_input;
END IF;
IF p_input.get_gtype() = 1
THEN
IF p_input.get_dims() = 2
THEN
RETURN MDSYS.SDO_GEOMETRY(
p_input.SDO_GTYPE
,p_input.SDO_SRID
,MDSYS.SDO_POINT_TYPE(
p_input.SDO_ORDINATES(1)
,p_input.SDO_ORDINATES(2)
,NULL
)
,NULL
,NULL
);
ELSIF p_input.get_dims() = 3
THEN
RETURN MDSYS.SDO_GEOMETRY(
p_input.SDO_GTYPE
,p_input.SDO_SRID
,MDSYS.SDO_POINT_TYPE(
p_input.SDO_ORDINATES(1)
,p_input.SDO_ORDINATES(2)
,p_input.SDO_ORDINATES(3)
)
,NULL
,NULL
);
ELSE
RAISE_APPLICATION_ERROR(
-20001
,'function true_point can only work on 2 and 3 dimensional points - dims=' || p_input.get_dims() || ' '
);
END IF;
ELSE
RAISE_APPLICATION_ERROR(
-20001
,'function true_point can only work on point geometries'
);
END IF;
END true_point;
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
FUNCTION downsize_2d(
p_input IN MDSYS.SDO_GEOMETRY
) RETURN MDSYS.SDO_GEOMETRY
-- Originally taken from
-- Ravikanth Kothuri, Albert Godfrind, Euro Beinat
-- Pro Oracle Spatial for Oracle Database 11g
AS
geom_2d MDSYS.SDO_GEOMETRY;
dim_count PLS_INTEGER;
gtype PLS_INTEGER;
n_points PLS_INTEGER;
n_ordinates PLS_INTEGER;
i PLS_INTEGER;
j PLS_INTEGER;
k PLS_INTEGER;
offset PLS_INTEGER;
BEGIN
IF p_input IS NULL
THEN
RETURN NULL;
END IF;
IF LENGTH(p_input.SDO_GTYPE) = 4
THEN
dim_count := p_input.get_dims();
gtype := p_input.get_gtype();
ELSE
RAISE_APPLICATION_ERROR(
-20001
, 'Unable to determine dimensionality from gtype'
);
END IF;
IF dim_count = 2
THEN
RETURN p_input;
END IF;
geom_2d := MDSYS.SDO_GEOMETRY(
2000 + gtype
,p_input.SDO_SRID
,p_input.SDO_POINT
,MDSYS.SDO_ELEM_INFO_ARRAY()
,MDSYS.SDO_ORDINATE_ARRAY()
);
IF geom_2d.SDO_POINT IS NOT NULL
THEN
geom_2d.SDO_POINT.Z := NULL;
geom_2d.SDO_ELEM_INFO := NULL;
geom_2d.SDO_ORDINATES := NULL;
ELSE
n_points := p_input.SDO_ORDINATES.COUNT / dim_count;
n_ordinates := n_points * 2;
geom_2d.SDO_ORDINATES.EXTEND(n_ordinates);
j := p_input.SDO_ORDINATES.FIRST;
k := 1;
FOR i IN 1 .. n_points
LOOP
geom_2d.SDO_ORDINATES(k) := p_input.SDO_ORDINATES(j);
geom_2d.SDO_ORDINATES(k + 1) := p_input.SDO_ORDINATES(j + 1);
j := j + dim_count;
k := k + 2;
END LOOP;
geom_2d.SDO_ELEM_INFO := p_input.SDO_ELEM_INFO;
i := geom_2d.SDO_ELEM_INFO.FIRST;
WHILE i < geom_2d.SDO_ELEM_INFO.LAST
LOOP
offset := geom_2d.SDO_ELEM_INFO(i);
geom_2d.SDO_ELEM_INFO(i) := (offset - 1) / dim_count * 2 + 1;
i := i + 3;
END LOOP;
END IF;
IF geom_2d.SDO_GTYPE = 2001
THEN
RETURN true_point(
p_input => geom_2d
);
ELSE
RETURN geom_2d;
END IF;
END downsize_2d;
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
FUNCTION downsize_2dM(
p_input IN MDSYS.SDO_GEOMETRY
) RETURN MDSYS.SDO_GEOMETRY
AS
geom_2dm MDSYS.SDO_GEOMETRY;
dim_count PLS_INTEGER;
measure_chk PLS_INTEGER;
gtype PLS_INTEGER;
n_points PLS_INTEGER;
n_ordinates PLS_INTEGER;
i PLS_INTEGER;
j PLS_INTEGER;
k PLS_INTEGER;
offset PLS_INTEGER;
BEGIN
IF p_input IS NULL
THEN
RETURN NULL;
END IF;
IF LENGTH (p_input.SDO_GTYPE) = 4
THEN
dim_count := p_input.get_dims();
measure_chk := p_input.get_lrs_dim();
gtype := p_input.get_gtype();
ELSE
RAISE_APPLICATION_ERROR(
-20001
,'unable to determine dimensionality from gtype'
);
END IF;
--------------------------------------------------------------------------
-- Simple 2D input so just throw it back
--------------------------------------------------------------------------
IF dim_count = 2
THEN
RETURN p_input;
--------------------------------------------------------------------------
-- 2D + measure on 3 so just throw it back
--------------------------------------------------------------------------
ELSIF dim_count = 3
AND measure_chk = 3
THEN
RETURN p_input;
--------------------------------------------------------------------------
-- Simple 3D so downsize to 2D
--------------------------------------------------------------------------
ELSIF dim_count = 3
AND measure_chk = 0
THEN
RETURN downsize_2d(p_input);
--------------------------------------------------------------------------
-- 4D so assume measure on the 4
--------------------------------------------------------------------------
ELSIF dim_count = 4
THEN
--THIS IS BECAUSE ArcSDE is DUMB!
measure_chk := 4;
END IF;
IF gtype = 1
THEN
geom_2dm := MDSYS.SDO_GEOMETRY(
3300 + gtype
,p_input.sdo_srid
,MDSYS.SDO_POINT_TYPE(NULL,NULL,NULL)
,NULL
,NULL
);
geom_2dm.SDO_POINT.X := p_input.SDO_ORDINATES(1);
geom_2dm.SDO_POINT.Y := p_input.SDO_ORDINATES(2);
geom_2dm.SDO_POINT.Z := p_input.SDO_ORDINATES(4);
RETURN geom_2dm;
ELSE
geom_2dm := MDSYS.SDO_GEOMETRY(
3300 + gtype
,p_input.sdo_srid
,NULL
,MDSYS.SDO_ELEM_INFO_ARRAY()
,MDSYS.SDO_ORDINATE_ARRAY()
);
END IF;
n_points := p_input.SDO_ORDINATES.COUNT / dim_count;
n_ordinates := n_points * 3;
geom_2dm.SDO_ORDINATES.EXTEND(n_ordinates);
j := p_input.SDO_ORDINATES.FIRST;
k := 1;
FOR i IN 1 .. n_points
LOOP
geom_2dm.SDO_ORDINATES(k) := p_input.SDO_ORDINATES(j);
geom_2dm.SDO_ORDINATES(k + 1) := p_input.SDO_ORDINATES(j + 1);
geom_2dm.SDO_ORDINATES(k + 2) := p_input.SDO_ORDINATES(j + 3);
j := j + dim_count;
k := k + 3;
END LOOP;
geom_2dm.sdo_elem_info := p_input.sdo_elem_info;
i := geom_2dm.SDO_ELEM_INFO.FIRST;
WHILE i < geom_2dm.SDO_ELEM_INFO.LAST
LOOP
offset := geom_2dm.SDO_ELEM_INFO(i);
geom_2dm.SDO_ELEM_INFO(i) := (offset - 1) / dim_count * 2 + 1;
i := i + 3;
END LOOP;
RETURN geom_2dm;
END downsize_2dM;
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
FUNCTION downsize_3d(
p_input IN MDSYS.SDO_GEOMETRY
) RETURN MDSYS.SDO_GEOMETRY
AS
geom_3d MDSYS.SDO_GEOMETRY;
num_lrs NUMBER;
dim_count PLS_INTEGER;
gtype PLS_INTEGER;
n_points PLS_INTEGER;
n_ordinates PLS_INTEGER;
i PLS_INTEGER;
j PLS_INTEGER;
k PLS_INTEGER;
offset PLS_INTEGER;
BEGIN
IF p_input IS NULL
THEN
RETURN NULL;
END IF;
IF LENGTH(p_input.SDO_GTYPE) = 4
THEN
dim_count := p_input.get_dims();
gtype := p_input.get_gtype();
num_lrs := p_input.get_lrs_dim();
ELSE
RAISE_APPLICATION_ERROR(
-20001
,'unable to determine dimensionality from gtype'
);
END IF;
IF dim_count = 3 AND num_lrs = 0
THEN
RETURN p_input;
ELSIF dim_count = 3 AND num_lrs != 0
THEN
RETURN downsize_2d(p_input);
ELSIF dim_count = 4 AND num_lrs = 0
THEN
-- we ASSUME that we remove the 4th dimension
num_lrs := 4;
END IF;
geom_3d := MDSYS.SDO_GEOMETRY(
3000 + gtype
,p_input.SDO_SRID
,p_input.SDO_POINT
,MDSYS.SDO_ELEM_INFO_ARRAY()
,MDSYS.SDO_ORDINATE_ARRAY()
);
IF geom_3d.sdo_point IS NOT NULL
THEN
geom_3d.sdo_elem_info := NULL;
geom_3d.sdo_ordinates := NULL;
ELSE
n_points := p_input.SDO_ORDINATES.COUNT / dim_count;
n_ordinates := n_points * 3;
geom_3d.SDO_ORDINATES.EXTEND(n_ordinates);
j := p_input.SDO_ORDINATES.FIRST;
k := 1;
FOR i IN 1 .. n_points
LOOP
geom_3d.SDO_ORDINATES(k) := p_input.SDO_ORDINATES(j);
geom_3d.SDO_ORDINATES(k + 1) := p_input.SDO_ORDINATES(j + 1);
IF num_lrs = 4
THEN
geom_3d.SDO_ORDINATES(k + 2) := p_input.SDO_ORDINATES(j + 2);
ELSIF num_lrs = 3
THEN
geom_3d.SDO_ORDINATES(k + 2) := p_input.SDO_ORDINATES(j + 3);
END IF;
j := j + dim_count;
k := k + 3;
END LOOP;
geom_3d.sdo_elem_info := p_input.sdo_elem_info;
i := geom_3d.SDO_ELEM_INFO.FIRST;
WHILE i < geom_3d.SDO_ELEM_INFO.LAST
LOOP
offset := geom_3d.SDO_ELEM_INFO(i);
geom_3d.SDO_ELEM_INFO(i) := (offset - 1) / dim_count * 3 + 1;
i := i + 4;
END LOOP;
END IF;
IF geom_3d.SDO_GTYPE = 2001
THEN
RETURN true_point(geom_3d);
ELSE
RETURN geom_3d;
END IF;
END downsize_3d;
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
FUNCTION prune_number(
p_input IN NUMBER
,p_trunc IN NUMBER DEFAULT NULL
) RETURN NUMBER
AS
BEGIN
IF p_trunc IS NULL
THEN
RETURN p_input;
END IF;
IF p_input IS NULL
THEN
RETURN NULL;
END IF;
RETURN TRUNC(p_input,p_trunc);
END prune_number;
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
FUNCTION prune_number_varchar2(
p_input IN NUMBER
,p_trunc IN NUMBER DEFAULT NULL
) RETURN VARCHAR2
AS
BEGIN
RETURN TO_CHAR(
prune_number(
p_input => p_input
,p_trunc => p_trunc
)
,'TM9'
,'NLS_NUMERIC_CHARACTERS = ''.,'''
);
END prune_number_varchar2;
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
FUNCTION prune_number_clob(
p_input IN NUMBER
,p_trunc IN NUMBER DEFAULT NULL
) RETURN CLOB
AS
BEGIN
RETURN TO_CLOB(
prune_number_varchar2(
p_input => p_input
,p_trunc => p_trunc
)
);
END prune_number_clob;
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
FUNCTION indent(
p_level IN NUMBER
,p_amount IN VARCHAR2 DEFAULT ' '
) RETURN VARCHAR2
AS
str_output VARCHAR2(4000 Char) := '';
BEGIN
IF p_level IS NOT NULL
AND p_level > 0
THEN
FOR i IN 1 .. p_level
LOOP
str_output := str_output || p_amount;
END LOOP;
RETURN str_output;
ELSE
RETURN '';