| 633 | |
| 634 | {{{#!sql |
| 635 | -- Function: ccm21.upstream_segments2(numeric) |
| 636 | |
| 637 | -- DROP FUNCTION ccm21.upstream_segments2(numeric); |
| 638 | |
| 639 | CREATE OR REPLACE FUNCTION ccm21.upstream_segments2(wso1_id_ numeric) |
| 640 | RETURNS SETOF integer AS |
| 641 | $BODY$ |
| 642 | DECLARE |
| 643 | wso1_id_riversegments wso1_id%ROWTYPE; |
| 644 | rec_pfafstette_length RECORD; |
| 645 | rec_riversegments ccm21.riversegments%ROWTYPE; |
| 646 | -- pfafstette_chain_length int; |
| 647 | pfafstette_max_chain_length int; |
| 648 | pfafstette_value int8; |
| 649 | BEGIN |
| 650 | -- filling a new table with the results from a catchment |
| 651 | |
| 652 | DROP TABLE IF EXISTS ccm21.upstream_riversegments; |
| 653 | CREATE TABLE ccm21.upstream_riversegments AS SELECT * FROM ccm21.riversegments |
| 654 | where wso1_id in (SELECT ccm21.mycatchment2(wso1_id_)); |
| 655 | ALTER TABLE ccm21.upstream_riversegments ADD CONSTRAINT pk_upstream_riversegments PRIMARY KEY (wso1_id); |
| 656 | |
| 657 | -- pfafstette chain length |
| 658 | --select into pfafstette_chain_length character_length(CAST(round(pfafstette) AS TEXT) |
| 659 | -- FROM ccm21.upstream_riversegments WHERE wso1_id=wso1_id_; |
| 660 | -- RAISE NOTICE 'pfafstette chain length is %', pfafstette_chain_length; |
| 661 | -- pfafstette max chain length in the selected basin |
| 662 | select into pfafstette_max_chain_length max(character_length(CAST(round(pfafstette) AS TEXT))) |
| 663 | FROM ccm21.upstream_riversegments; |
| 664 | -- pfafstette value |
| 665 | -- the chain is lengthened to the pfafstette max chain length |
| 666 | Update ccm21.upstream_riversegments set pfafstette = |
| 667 | floor(pfafstette)*power(10,(pfafstette_max_chain_length-character_length(CAST(floor(pfafstette) AS TEXT) ))); |
| 668 | select into pfafstette_value CAST(round(pfafstette) AS int8) FROM ccm21.upstream_riversegments where wso1_id=wso1_id_; |
| 669 | RAISE NOTICE 'pfafstette is %', pfafstette_value; |
| 670 | |
| 671 | DELETE FROM ccm21.upstream_riversegments WHERE pfafstette < pfafstette_value; |
| 672 | -- extracting the riversegments corresponding to the basin |
| 673 | |
| 674 | for wso1_id_riversegments in select wso1_id from ccm21.upstream_riversegments order by wso1_id loop |
| 675 | wso1_id_riversegments.wso1_id=CAST(wso1_id_riversegments.wso1_id AS int8); |
| 676 | return next wso1_id_riversegments.wso1_id; |
| 677 | end loop; |
| 678 | return; |
| 679 | END; |
| 680 | $BODY$ |
| 681 | LANGUAGE plpgsql VOLATILE |
| 682 | COST 100 |
| 683 | ROWS 1000; |
| 684 | ALTER FUNCTION ccm21.upstream_segments2(numeric) |
| 685 | OWNER TO postgres; |
| 686 | COMMENT ON FUNCTION ccm21.upstream_segments2(numeric) IS 'This function uses ccm21.mycatchment2 to get all the segments from a catchment and searches all segments with a pfafstetter higher than the segment in the bassin. The pfafstette are trucated to the level of the segment used as input in the function'; |
| 687 | }}} |