372 | | |
| 372 | === Plgsql function which returns the riversegments upstream from the riversegment selected === |
| 373 | {{{ |
| 374 | DROP TYPE IF EXISTS gid; |
| 375 | CREATE TYPE gid as (gid int); |
| 376 | DROP FUNCTION IF EXISTS ccm21.upstream_segments(gid_ numeric); |
| 377 | CREATE OR REPLACE FUNCTION ccm21.upstream_segments(gid_ numeric) RETURNS setof int AS $$ |
| 378 | DECLARE |
| 379 | gid_riversegments gid%rowtype; |
| 380 | rec_pfafstette_length RECORD; |
| 381 | pfafstette_chain_length int; |
| 382 | pfafstette_value int8; |
| 383 | BEGIN |
| 384 | -- Valeur de la chaine pfafstette |
| 385 | select into pfafstette_value CAST(round(pfafstette) AS int8) FROM ccm21.riversegments where gid=gid_; |
| 386 | RAISE NOTICE 'pfafstette is %', pfafstette_value; |
| 387 | -- Longueur de la chaine pfafstette |
| 388 | select into rec_pfafstette_length character_length(CAST(round(pfafstette) AS TEXT)) AS length |
| 389 | FROM ccm21.riversegments WHERE gid=gid_; |
| 390 | pfafstette_chain_length=rec_pfafstette_length.length; |
| 391 | RAISE NOTICE 'pfafstette chain length is %', pfafstette_chain_length; |
| 392 | -- loop |
| 393 | for gid_riversegments in select gid from ccm21.riversegments |
| 394 | where wso1_id in (SELECT ccm21.pricatch_from_catch(gid_)) |
| 395 | and CAST( |
| 396 | substring(CAST(round(pfafstette) AS TEXT),1,pfafstette_chain_length) |
| 397 | as numeric)> pfafstette_value loop |
| 398 | gid_riversegments.gid=CAST(gid_riversegments.gid AS int8); |
| 399 | return next gid_riversegments.gid; |
| 400 | end loop; |
| 401 | return; |
| 402 | END; |
| 403 | $$ |
| 404 | LANGUAGE 'plpgsql' ; |
| 405 | COMMENT ON FUNCTION ccm21.upstream_segments(gid_ numeric) IS 'This function uses ccm21.pricatch_from_catch 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'; |
| 406 | }}} |
| 407 | Usage example |
| 408 | {{{ |
| 409 | select ccm21.upstream_segments(234706); |
| 410 | }}} |