Advertisement
Guest User

Point clustering

a guest
Feb 9th, 2013
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Table: public.strength3
  2.  
  3. -- DROP TABLE public.strength3;
  4.  
  5. CREATE TABLE public.strength3
  6. (
  7.   gpstime timestamp with time zone,
  8.   latitude double precision,
  9.   longitude double precision,
  10.   strength integer,
  11.   uniqueid integer,
  12.   clusterid integer,
  13.   buffered geometry
  14. )
  15. WITH (
  16.   OIDS=FALSE
  17. );
  18. ALTER TABLE public.strength3
  19.   OWNER TO oru;
  20.  
  21. -- Index: public.strength3_buffered_idx
  22.  
  23. -- DROP INDEX public.strength3_buffered_idx;
  24.  
  25. CREATE INDEX strength3_buffered_idx
  26.   ON public.strength3
  27.   USING gist
  28.   (buffered);
  29.  
  30. -- Index: public.strength3_clusterid_idx
  31.  
  32. -- DROP INDEX public.strength3_clusterid_idx;
  33.  
  34. CREATE INDEX strength3_clusterid_idx
  35.   ON public.strength3
  36.   USING btree
  37.   (clusterid);
  38.  
  39. -- Index: public.strength3_current_idx
  40.  
  41. -- DROP INDEX public.strength3_current_idx;
  42.  
  43. CREATE INDEX strength3_current_idx
  44.   ON public.strength3
  45.   USING gist
  46.   (current);
  47.  
  48. -- Index: public.strength3_uniqueid_idx
  49.  
  50. -- DROP INDEX public.strength3_uniqueid_idx;
  51.  
  52. CREATE INDEX strength3_uniqueid_idx
  53.   ON public.strength3
  54.   USING btree
  55.   (uniqueid, buffered);
  56.  
  57.  
  58. -- Table: public.strengthpoly
  59.  
  60. -- DROP TABLE public.strengthpoly;
  61.  
  62. CREATE TABLE public.strengthpoly
  63. (
  64.   strength integer,
  65.   clusterid integer,
  66.   geom geometry
  67. )
  68. WITH (
  69.   OIDS=FALSE
  70. );
  71. ALTER TABLE public.strengthpoly
  72.   OWNER TO oru;
  73. -- Sequence: public.cities1000_id_seq
  74.  
  75. -- DROP SEQUENCE public.cities1000_id_seq;
  76.  
  77. CREATE SEQUENCE public.cities1000_id_seq
  78.   INCREMENT 1
  79.   MINVALUE 1
  80.   MAXVALUE 9223372036854775807
  81.   START 1
  82.   CACHE 1;
  83. ALTER TABLE public.cities1000_id_seq
  84.   OWNER TO postgres;
  85.  
  86. -- Function: public.cluster_points_buffer(integer, text)
  87.  
  88. -- DROP FUNCTION public.cluster_points_buffer(integer, text);
  89.  
  90. CREATE OR REPLACE FUNCTION public.cluster_points_buffer(srcuniqueid integer, tablename text)
  91.   RETURNS void AS
  92. $BODY$
  93. DECLARE
  94.     sql text;
  95.     clusterId_temp int:= null;
  96.     rec_dest record;
  97.     count int;
  98. BEGIN
  99.     count :=0;
  100.     FOR rec_dest IN SELEct s2.uniqueid, s2.clusterid
  101.         from strength3 s1, strength3 s2
  102.         where s1.uniqueid =srcuniqueid
  103.         and st_intersects(s1.buffered, s2.buffered)
  104.         LOOP
  105.         count := count+1;
  106.         if 0 = count%1000 then
  107. raise notice 'Processing iteration % at %.',count, clock_timestamp();
  108.         end if;
  109.         IF ( clusterid_temp is null and rec_dest.clusterId IS NULL ) THEN
  110.             clusterId_temp = nextval('seq_pts_clusterId');
  111.             sql := 'UPDATE ' || tablename || ' SET clusterId = '
  112.             || clusterId_temp || ' WHERE uniqueid = ' || srcUniqueid; --CURRENT OF curs_src;
  113. --raise notice '(%)sql1 %',count,sql;
  114.             execute sql;
  115.             sql := 'UPDATE ' || tablename || ' SET clusterId = '
  116.             || clusterId_temp || ' WHERE uniqueid = ' ||rec_dest.uniqueid; --CURRENT OF curs_dest;
  117. --raise notice '(%)sql2 %',count,sql;
  118.             execute sql;
  119. --raise notice 'updating both src (%) and dest (%) with clusterid %', srcuniqueid, rec_dest.uniqueid, clusterid_temp;
  120.         ELSIF (clusterid_temp is null and rec_dest.clusterId IS NOT NULL) THEN
  121.             clusterId_temp = rec_dest.clusterId;
  122. --raise notice 'updating src (%) with clusterid %', uniqueid, clusterid_temp;
  123.             sql := 'UPDATE ' || tablename || ' SET clusterId = '
  124.             || clusterId_temp || ' WHERE uniqueid = ' || srcUniqueId; --CURRENT OF curs_src;
  125. --raise notice '(%)sql3 %',count,sql;
  126.             execute sql;
  127.         ELSIF (clusterId_temp IS NOT NULL AND rec_dest.clusterId IS NULL) THEN
  128. --raise notice 'updating dest (%) with clusterid %', rec_dest.id, clusterid_temp;
  129.             sql := 'UPDATE ' || tablename || ' SET clusterId = '
  130.             || clusterId_temp || ' WHERE uniqueid = ' || rec_dest.uniqueid; --CURRENT OF curs_dest;
  131. --raise notice '(%)sql4 %',count,sql;
  132.             execute sql;
  133.         ELSIF (clusterId_temp != rec_dest.clusterId) THEN -- implicit: clusterId_src IS NOT NULL AND rec_dest.clusterId IS NOT NULL
  134.             sql := 'UPDATE ' || tablename || ' SET clusterId = '
  135.             || rec_dest.clusterid || ' WHERE clusterId = ' || clusterId_temp;
  136. --raise notice '(%) % - sql5 %',count,rec_dest.uniqueid, sql;
  137.             clusterId_temp := rec_dest.clusterId;
  138.             EXECUTe sql;
  139.         END IF;
  140.     end loop;
  141. END;
  142. $BODY$
  143.   LANGUAGE plpgsql VOLATILE
  144.   COST 100;
  145. ALTER FUNCTION public.cluster_points_buffer(integer, text)
  146.   OWNER TO postgres;
  147.  
  148.  
  149. Python script:
  150. import pg
  151. import datetime
  152. con = pg.connect(dbname='signalstrength')
  153. while True:
  154.         idList = con.query("select uniqueid from strength3 where latitude > 0 and clusterid is null limit 250;").getresult()
  155.         if len(idList) == 0:
  156.                 break
  157.         for row in idList:
  158.                 s = "select * from cluster_points_buffer(%s, 'strength3')" % row[0]
  159.                 now = datetime.datetime.now()
  160.                 print '({0}) {1}'.format(now.strftime("%H:%M:%S") , s)
  161.                 id = con.query( s )
  162.         print 'Going to vacuum at %s' % now.strftime("%H:%M:%S %d-%m-%Y")
  163.         con.query( "vacuum strength3" )
  164. print "Closing db and ending"
  165. con.close()
  166.  
  167. --sql
  168. insert into strengthpoly                      
  169. select 3, clusterid, st_astext((st_union(buffered))) as area                                                    
  170. from strength3                                                                                                  
  171. group by clusterid
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement