pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

yancho private pastebin - collaborative debugging tool What's a private pastebin?


Posted by Yancho on Thu 24 Apr 08:49 (modification of post by view diff)
report spam | download | new post

  1. CREATE OR REPLACE FUNCTION give_nearest_aid_station(accidentloc TEXT, type_of_station TEXT)
  2.   RETURNS SETOF geoms AS
  3. $_$
  4.  
  5.         DECLARE
  6.                         var_target TEXT;
  7.                         var_target_geom GEOMETRY;
  8.                         var_target_gid INTEGER;
  9.  
  10.                         var_source_geom GEOMETRY;
  11.                        
  12.                         var_select TEXT;
  13.                         this_station FLOAT;
  14.                         this_route_dist FLOAT;
  15.                        
  16.                         rec_nearest_station RECORD;
  17.  
  18.                         distances RECORD;
  19.                        
  20.                         var_id INTEGER;
  21.                         var_row_count INTEGER;
  22.                         var_num_geoms INTEGER;
  23.                         var_i INTEGER;
  24.                      
  25.                         var_new_line GEOMETRY;
  26.                         var_first_linestring GEOMETRY;
  27.                         final_geom GEOMETRY;
  28.  
  29.                         var_gid_where_source GEOMETRY;
  30.  
  31.                         var_dist FLOAT;
  32.                         var_small_dist FLOAT;
  33.                         var_near_linestring INTEGER;
  34.                         var_j INTEGER;
  35.  
  36.                         var_params_check_alt TEXT;
  37.                         var_text_shootingstar TEXT;
  38.                         var_params_shootingstar TEXT;
  39.  
  40.                         rec_shooting_star RECORD;
  41.                         rec_path_result GEOMS;
  42.  
  43.                         var_function_result_query TEXT;
  44.                         var_text_select TEXT;
  45.  
  46.         BEGIN
  47.                 var_target := 'POINT(' || accidentloc || ')';
  48.                 var_target_geom := pointfromtext ( var_target );
  49.  
  50.          
  51.                 SELECT INTO var_target_gid gid
  52.                 FROM streets
  53.                         WHERE
  54.                           (
  55.                             (the_geom && expand ( var_target_geom , 100 )) AND
  56.                             distance ( the_geom, var_target_geom ) < 100
  57.                           )
  58.                 ORDER BY distance ( the_geom, var_target_geom )
  59.                 LIMIT 1;
  60.                
  61.                 raise notice ' station is : % , geom : %', type_of_station, astext(var_target_geom);
  62.  
  63.                -- Making an Expansion of 20 KMs
  64.                
  65.                var_select := 'SELECT the_geom , gid FROM  ' || type_of_station || '  WHERE
  66.                                the_geom @ Expand( ' || quote_literal(var_target_geom) || ' ::geometry , 20000::float)';
  67.                
  68.                RAISE NOTICE 'The QUERY is %', var_select;
  69.  
  70.                SELECT 'inf'::FLOAT AS dist, NULL::TEXT AS gid INTO rec_nearest_station;
  71.  
  72.                FOR distances IN
  73.                    EXECUTE var_select
  74.                            LOOP
  75.                                
  76.                                SELECT INTO this_station
  77.                                       MAX ( LENGTH(the_geom) )
  78.                                       FROM shootingstar_sp_where_new
  79.                                           (
  80.                                             'streets',
  81.                                             distances.gid,
  82.                                             var_target_gid,
  83.                                             5000,
  84.                                             'length',
  85.                                             TRUE,
  86.                                             TRUE,
  87.                                             'WHERE accident_access = true'
  88.                                           );
  89.                              
  90.                               IF this_station < rec_nearest_station.dist THEN
  91.                              
  92.                                   rec_nearest_station.dist :=  this_station;
  93.                                   rec_nearest_station.gid :=  distances.gid;
  94.                                  
  95.                               END IF;
  96.                            END LOOP;
  97.                            
  98.                RAISE NOTICE 'Nearest Hospital has a GID of : % and is % meters far from the accident',
  99.                             rec_nearest_station.gid , rec_nearest_station.dist;
  100.                            
  101.  
  102.                var_text_shootingstar :=
  103.                             $qt$
  104.                               SELECT gid, the_geom, id
  105.                                   FROM shootingstar_sp_where_new
  106.                                   (
  107.                                     'streets',
  108.                             $qt$ || rec_nearest_station.gid || $qt$,
  109.                             $qt$ || var_target_gid || $qt$,
  110.                                     5000,
  111.                                     'length',
  112.                                     TRUE,
  113.                                     TRUE,
  114.                                     'WHERE accident_access = true'
  115.                                   )
  116.                             $qt$;
  117.  
  118.  
  119.                         RAISE NOTICE 'SQL Query : %',var_text_shootingstar;
  120.  
  121.                         var_id := 1;
  122.  
  123.                         SELECT INTO var_source_geom the_geom FROM hospitals WHERE gid = rec_nearest_station.gid;
  124.  
  125.                         FOR rec_shooting_star IN
  126.                                     EXECUTE var_text_shootingstar
  127.                      
  128.                                             LOOP
  129.                                                 IF var_id = 1 THEN
  130.                            
  131.                                                    SELECT INTO final_geom the_geom FROM STREETS
  132.                                                         WHERE
  133.                                                                 touches(the_geom, rec_shooting_star.the_geom) AND
  134.                                                                 distance ( the_geom, var_source_geom ) < 100
  135.                                                         ORDER BY
  136.                                                                 distance ( the_geom, var_source_geom )
  137.                                                         LIMIT 1;
  138.      
  139.                                                    RAISE NOTICE '------------------------- Starting Part Changing ------------------';
  140.                                                    RAISE NOTICE 'Geometry type working on is : %, and as text : %',geometrytype(rec_shooting_star.the_geom), astext(rec_shooting_star.the_geom);
  141.                                  
  142.                                                   var_num_geoms := NumGeometries(rec_shooting_star.the_geom);
  143.                                                   RAISE NOTICE 'Total Number of Geometries : %',var_num_geoms;
  144.                          
  145.                                                   var_dist := -1000.00;
  146.                                                   var_small_dist := 40000.00;
  147.                                
  148.                                                   FOR var_j IN 1 .. var_num_geoms LOOP
  149.                                
  150.                                                           var_dist := line_locate_point(geometryN(final_geom, var_j), var_source_geom);
  151.                                                           IF var_dist <= var_small_dist THEN
  152.                          
  153.                                                                                                        
  154.                                                                var_small_dist := var_dist;
  155.                                                                var_near_linestring := var_j;
  156.                                                                
  157.                                                                  RAISE NOTICE 'Found nearer LINESTRING ==========, Dist : %, And Geom num : %', var_small_dist, var_near_linestring;
  158.                                                    
  159.                                                           END IF;
  160.                                      
  161.                                                   END LOOP;
  162.                                  
  163.                                
  164.      
  165.      
  166.                                                   var_new_line := line_substring (
  167.                                                                                      (geometryN (final_geom, var_near_linestring)),
  168.                                                                                      line_locate_point ( (geometryN (final_geom, var_near_linestring)), var_source_geom ),
  169.                                                                                      1
  170.                                                                                      );
  171.                                                      
  172.                                                    RAISE NOTICE 'Point to end of Target is : %', astext(var_new_line);
  173.      
  174.                                                    final_geom := GEOMUNION (var_new_line , rec_shooting_star.the_geom) ;
  175.                            
  176.                                                 END IF;
  177.                              
  178.                                                 IF var_id = 2 THEN
  179.                                                         --RETURN NEXT rec_path_result;
  180.                                                         rec_path_result.id       := rec_shooting_star.id;
  181.                                                         --RAISE NOTICE 'Working on ID : % <><> And stored as %',rec_shooting_star.id, rec_path_result.id;
  182.                                                         rec_path_result.gid      := rec_shooting_star.gid;
  183.  
  184.                                                         var_gid_where_source := GEOMUNION ( final_geom , var_new_line );
  185.                
  186.                                                         rec_path_result.the_geom := GEOMUNION ( var_gid_where_source, rec_shooting_star.the_geom );
  187.                
  188.                                                         --RETURN NEXT rec_path_result;
  189.  
  190.                                                 ELSE
  191.                              
  192.                                                         RAISE NOTICE 'Returning result ID : %',rec_path_result.id;
  193.                                                         IF var_id >= 2 THEN RETURN NEXT rec_path_result; END IF;
  194.                                                         rec_path_result.id       := rec_shooting_star.id;
  195.                                                         RAISE NOTICE 'Working on ID : % <><> And stored as %',rec_shooting_star.id, rec_path_result.id;
  196.                                                         rec_path_result.gid      := rec_shooting_star.gid;
  197.                                                         rec_path_result.the_geom := rec_shooting_star.the_geom;
  198.                                                
  199.                                                 END IF;
  200.  
  201.                                                 var_id := var_id + 1;
  202.  
  203.                                              END LOOP;
  204.  
  205.   RAISE NOTICE 'Modifying the last result, with ID : % and having Gid : %', rec_shooting_star.id, rec_shooting_star.gid ;
  206.   RAISE NOTICE 'Geometry type working on is : %, and as text : %',geometrytype(rec_shooting_star.the_geom), astext(rec_shooting_star.the_geom);
  207.  
  208.   var_num_geoms := NumGeometries(rec_shooting_star.the_geom);
  209.   RAISE NOTICE 'Total Number of Geometries : %',var_num_geoms;
  210.  
  211.   var_dist := -1000.00;
  212.   var_small_dist := 40000.00;
  213.  
  214.   FOR var_j IN 1 .. var_num_geoms LOOP
  215.  
  216.       var_dist := line_locate_point(geometryN(rec_shooting_star.the_geom, var_j), var_target_geom);
  217.  
  218.         IF var_dist <= var_small_dist THEN
  219.                
  220.            var_small_dist := var_dist;
  221.            var_near_linestring := var_j;
  222.                
  223.             RAISE NOTICE 'Found nearer LINESTRING ==========, Dist : %, And Geom num : %', var_small_dist, var_near_linestring;
  224.  
  225.         END IF;
  226.  
  227.   END LOOP;
  228.  
  229.   RAISE NOTICE 'Nearest Linestring is : % Point is : %', var_near_linestring, astext(var_target_geom);
  230.  
  231.   var_new_line := line_substring(
  232.                                (geometryN (rec_shooting_star.the_geom, var_near_linestring)),
  233.                                0,
  234.                                var_small_dist
  235.                                );
  236.  
  237.   rec_path_result.gid      := rec_shooting_star.gid;
  238.  
  239.   RAISE NOTICE 'New line is : %, Text as : %', var_new_line, astext(var_new_line);     
  240.  
  241.   final_geom := Multi ( var_new_line  );
  242.   RAISE NOTICE 'The last line is : %',astext(final_geom);              
  243.  
  244.   rec_path_result.the_geom := final_geom;
  245.  
  246.   RETURN NEXT rec_path_result;
  247.        
  248.  
  249.   RETURN;
  250.        
  251.  
  252. END;
  253. $_$
  254.  
  255. LANGUAGE 'plpgsql' VOLATILE;

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me