Changeset: da5e7a1a6b71 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=da5e7a1a6b71
Modified Files:
        sql/backends/monet5/UDF/80_ssdb.sql
        sql/backends/monet5/UDF/ssdb.c
        sql/backends/monet5/UDF/ssdb.h
        sql/backends/monet5/UDF/ssdb.mal
Branch: ssdb
Log Message:

Add grouping function + some changes in density,intersects.


diffs (truncated from 511 to 300 lines):

diff --git a/sql/backends/monet5/UDF/80_ssdb.sql 
b/sql/backends/monet5/UDF/80_ssdb.sql
--- a/sql/backends/monet5/UDF/80_ssdb.sql
+++ b/sql/backends/monet5/UDF/80_ssdb.sql
@@ -35,6 +35,17 @@ All Rights Reserved.
 CREATE PROCEDURE cooking (tin CHAR(128), tout_obs CHAR(128), tout_plg 
CHAR(128), imgsize INT)
 EXTERNAL NAME ssdb.cooking;
 
+-- Cooks observations from the input table
+--   'tin' according to the description in the SS-DB paper.
+-- tin: name of the input table for image pixel values, with columns
+--             (l_obsid int, l_time int, l_centerx int, l_centery int, r_obsid 
int, r_time int, r_centerx int, r_centery int)
+-- tout_grp: name of the output table for groups, with columns
+--             (obsgroupid INT, center_traj_x INT, center_traj_y INT, fromtime 
INT, totime INT)
+-- tout_obs: name of the output table for groups-observations, with columns
+--             (obsgroupid INT, obsid INT)
+CREATE PROCEDURE grouping (tin CHAR(128), tout_grp CHAR(128), tout_obs 
CHAR(128))
+EXTERNAL NAME ssdb.grouping;
+
 -- Determines for each polygon in the input table 'tin' if it intersects with
 --   the bounding box by [(xstart, ystart), (xstart+xlen, ystart+ylen)
 -- The ID of the intersecting polygons are stored in the output table 'tout'
diff --git a/sql/backends/monet5/UDF/ssdb.c b/sql/backends/monet5/UDF/ssdb.c
--- a/sql/backends/monet5/UDF/ssdb.c
+++ b/sql/backends/monet5/UDF/ssdb.c
@@ -386,15 +386,35 @@ SSDBintersects(Client cntxt, MalBlkPtr m
        tout_obsid_t = (int*)Tloc(tout_obsid, BUNfirst(tout_obsid));
 
 
-       /* First pass over all input pixels, to group the pixels into
-        * observations and assign them a unique obsid */
        for (idx = 0; idx < nr_points; idx++) 
        {
                int x,y,x_next,y_next, obs,obs_next;
                float D,l,m;
                obs = tin_obsid_t[idx];
                obs_next = tin_obsid_t[idx+1];
-               if (obs==obs_next && cur_obs!=obs)
+               
+               /*check for single-point polygons*/
+               if(obs!=tin_obsid_t[idx-1] && obs!=obs_next)
+               {
+                       x = tin_x_t[idx];
+                       y = tin_y_t[idx];
+                       for(j=0;j<4;j++)
+                       {
+
+                               if(cur_obs!=obs)
+                               {
+                                       if ((x==linex[j])|| (y==liney[j]))
+                                       {
+                                               cur_obs=obs;
+                                               tout_obsid_t[inter_obsid] = 
cur_obs;
+                                               inter_obsid++;
+                                       }
+                               }
+                       }
+
+               }
+               /*check for polygons that intersect with slab*/
+               else if (obs==obs_next && cur_obs!=obs)
                {
                        x = tin_x_t[idx];
                        y = tin_y_t[idx];
@@ -1134,6 +1154,7 @@ SSDBdensity(Client cntxt, MalBlkPtr mb, 
        int v2 = *(int *) getArgReference(stk, pci, pci->retc + 5);
        int tilesize = *(int *) getArgReference(stk, pci, pci->retc + 6);
        int threshold = *(int *) getArgReference(stk, pci, pci->retc + 7);
+       
 
        BAT *tin_x = NULL, *tin_y = NULL;
        BAT *tout_x = NULL, *tout_y = NULL, *tout_cnt = NULL;
@@ -1144,7 +1165,7 @@ SSDBdensity(Client cntxt, MalBlkPtr mb, 
        int l=0,k=0,elements=0;
 
        int **tile_weights = NULL;
-       
+
        if (msg)
                return msg;
        if (pci->argc - pci->retc != 8)
@@ -1188,22 +1209,21 @@ SSDBdensity(Client cntxt, MalBlkPtr mb, 
        nr_points = BATcount(tin_x);
        for(idx=0; idx<nr_points; idx++)
        {
-               for(l=tin_y_t[idx]-y2-3;l<=(tin_y_t[idx]-y2);l++)
+               for(l=tin_y_t[idx]-y2-tilesize;l<=(tin_y_t[idx]-y2);l++)
                {
                        if(l>=0 && l<=(v2-tilesize))
                        {
-                               
for(k=tin_x_t[idx]-x2-3;k<=(tin_x_t[idx]-x2);k++)
+                               
for(k=tin_x_t[idx]-x2-tilesize;k<=(tin_x_t[idx]-x2);k++)
                                {
                                        if(k>=0 && k<=(u2-tilesize))
                                        {
-                                               tile_weights[l][k]++;
+                                               tile_weights[k][l]++;
                                        }
                                }
                        }
                }
                
        }
-
        /* Create BATs for all columns of the output table 'tout_obs' */
        tout_x = BATnew(TYPE_void, TYPE_int, u2*v2);
        tout_y = BATnew(TYPE_void, TYPE_int, u2*v2);
@@ -1217,11 +1237,11 @@ SSDBdensity(Client cntxt, MalBlkPtr mb, 
        {
                for(k=0;k<(u2-(tilesize-1));k++)
                {
-                       if (tile_weights[l][k] > threshold)
+                       if (tile_weights[k][l] > threshold)
                        {
                                tout_x_t[elements]=k+x2;
                                tout_y_t[elements]=l+y2;
-                               tout_cnt_t[elements]=tile_weights[l][k];
+                               tout_cnt_t[elements]=tile_weights[k][l];
                                elements++;
                        }
                }
@@ -1285,3 +1305,352 @@ CLEANUP_RETURN:
        return MAL_SUCCEED;
 }
 
+str
+SSDBgrouping(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
+{      
+/* grouping(tin:str, tout_grp:str, tout_obs:str):void */
+       mvc *sql = NULL;
+       sql_table *tin = NULL, *tout_grp = NULL, *tout_obs = NULL;
+       str msg = getSQLContext(cntxt, mb, &sql, NULL);
+       str tin_name = *(str *) getArgReference(stk, pci, pci->retc + 0);
+       str tout_grp_name = *(str *) getArgReference(stk, pci, pci->retc + 1);
+       str tout_obs_name = *(str *) getArgReference(stk, pci, pci->retc + 2);
+
+       BAT *tin_l_obsid = NULL, *tin_l_time = NULL, *tin_l_centerx = NULL, 
*tin_l_centery = NULL, *tin_r_obsid = NULL, *tin_r_time = NULL, *tin_r_centerx 
= NULL, *tin_r_centery = NULL;
+       BAT *tout_grp_obsgroupid = NULL, *tout_grp_center_traj_x = NULL, 
*tout_grp_center_traj_y = NULL, *tout_grp_fromtime = NULL, *tout_grp_totime = 
NULL;
+       BAT *tout_obs_obsgroupid = NULL, *tout_obs_obsid = NULL;
+
+       BUN idx=0, nr_points=0;
+       int nr_obs = 0,l,obsid,groups=0,found=0,j,tuples_grp=0;
+       int *tin_l_obsid_t = NULL, *tin_l_time_t = NULL, *tin_l_centerx_t = 
NULL, *tin_l_centery_t = NULL, *tin_r_obsid_t = NULL, *tin_r_time_t = NULL, 
*tin_r_centerx_t = NULL, *tin_r_centery_t = NULL;
+       int *tout_grp_obsgroupid_t = NULL, *tout_grp_center_traj_x_t = NULL, 
*tout_grp_center_traj_y_t = NULL, *tout_grp_fromtime_t = NULL, 
*tout_grp_totime_t = NULL;
+       int *tout_obs_obsgroupid_t = NULL, *tout_obs_obsid_t = NULL;
+
+       obs_group **groupFrom;
+       int **groupTo;
+       int *current_groupids;
+
+
+       if (msg)
+               return msg;
+       if (pci->argc - pci->retc != 3)
+               return sql_message("GROUPING(): 3 parameters expected, got %d", 
pci->argc - pci->retc);
+       if (!tin_name)
+               return sql_message("GROUPING(): missing name of the input 
table");
+       if (!tout_grp_name)
+               return sql_message("GROUPING(): missing name of the first 
output table");
+       if (!tout_obs_name)
+               return sql_message("GROUPING(): missing name of the second 
output table");
+
+       if(!(tin = _bind_table(sql, "tmp", tin_name)))
+               return sql_message("42S02!GROUPING(): no such table '%s'", 
tin_name);
+       if(!(tout_grp = _bind_table(sql, NULL, tout_grp_name)))
+               return sql_message("42S02!GROUPING(): no such table '%s'", 
tout_grp_name);
+       if(!(tout_obs = _bind_table(sql, NULL, tout_obs_name)))
+               return sql_message("42S02!GROUPING(): no such table '%s'", 
tout_obs_name);
+
+
+       groupFrom = GDKmalloc(nr_obs * sizeof(obs_group*));
+       if (!groupFrom)
+               return sql_message("GROUPING(): GDKzalloc 'groupFrom' failed");
+       for (l=0;l<nr_obs;l++) {
+               groupFrom[l] = GDKmalloc(sizeof(obs_group));
+               if (!groupFrom[l]) {
+                       msg = sql_message("GROUPING(): GDKmalloc 
'groupFrom[%d]' failed", l);
+                       goto CLEANUP_RETURN;
+               }
+               groupFrom[l]->obsid = GDKzalloc(nr_obs*sizeof(int));
+               if (!(groupFrom[l]->obsid)) {
+                       msg = sql_message("GROUPING(): GDKzalloc 
'groupFrom[%d]->obsid' failed", l);
+                       goto CLEANUP_RETURN;
+               }
+               groupFrom[l]->fromtime=0;
+               groupFrom[l]->totime=0;
+               groupFrom[l]->totalx=0;
+               groupFrom[l]->totaly=0;
+               groupFrom[l]->cnt=0;
+       }
+
+       groupTo = GDKmalloc(nr_obs*sizeof(int*));
+       if (!groupTo)
+               return sql_message("GROUPING(): GDKmalloc 'groupTo' failed");
+       for (l=0;l<nr_obs;l++) {
+               groupTo[l] = GDKzalloc(nr_obs*sizeof(int));
+               if (!groupTo[l]) {
+                       msg = sql_message("GROUPING(): GDKzalloc 'groupTo[%d]' 
failed", l);
+                       goto CLEANUP_RETURN;
+               }
+       }
+
+       current_groupids = GDKzalloc(nr_obs*sizeof(int));
+       if (!current_groupids)
+               return sql_message("GROUPING(): GDKzalloc 'current_groupids' 
failed");
+
+
+       /* get the BATs of all columns of the input table 'tin' */
+       /* RD_INS means we want the insert BATs, since all input tables are
+        *   declared as TEMP */
+       if(!(tin_l_obsid = _bind_bat(sql, tin, "l_obsid", TYPE_int, RD_INS))) {
+               msg = sql_message("42S22!GROUPING(): no such column '%s.%s'", 
tin_name, "tin_l_obsid");
+               goto CLEANUP_RETURN;
+       }
+       if(!(tin_l_time = _bind_bat(sql, tin, "l_time", TYPE_int, RD_INS))) {
+               msg = sql_message("42S22!GROUPING(): no such column '%s.%s'", 
tin_name, "tin_l_time");
+               goto CLEANUP_RETURN;
+       }
+       if(!(tin_l_centerx = _bind_bat(sql, tin, "l_centerx", TYPE_int, 
RD_INS))) {
+               msg = sql_message("42S22!GROUPING(): no such column '%s.%s'", 
tin_name, "tin_l_centerx");
+               goto CLEANUP_RETURN;
+       }
+       if(!(tin_l_centery= _bind_bat(sql, tin, "l_centery", TYPE_int, 
RD_INS))) {
+               msg = sql_message("42S22!GROUPING(): no such column '%s.%s'", 
tin_name, "tin_l_centery");
+               goto CLEANUP_RETURN;
+       }
+       if(!(tin_r_obsid = _bind_bat(sql, tin, "r_obsid", TYPE_int, RD_INS))) {
+               msg = sql_message("42S22!GROUPING(): no such column '%s.%s'", 
tin_name, "tin_r_obsid");
+               goto CLEANUP_RETURN;
+       }
+       if(!(tin_r_time = _bind_bat(sql, tin, "r_time", TYPE_int, RD_INS))) {
+               msg = sql_message("42S22!GROUPING(): no such column '%s.%s'", 
tin_name, "tin_r_time");
+               goto CLEANUP_RETURN;
+       }
+       if(!(tin_r_centerx = _bind_bat(sql, tin, "r_centerx", TYPE_int, 
RD_INS))) {
+               msg = sql_message("42S22!GROUPING(): no such column '%s.%s'", 
tin_name, "tin_r_centerx");
+               goto CLEANUP_RETURN;
+       }
+       if(!(tin_r_centery= _bind_bat(sql, tin, "r_centery", TYPE_int, 
RD_INS))) {
+               msg = sql_message("42S22!GROUPING(): no such column '%s.%s'", 
tin_name, "tin_r_centery");
+               goto CLEANUP_RETURN;
+       }
+
+       tin_l_obsid_t = (int*)Tloc(tin_l_obsid, BUNfirst(tin_l_obsid));
+       tin_l_time_t = (int*)Tloc(tin_l_time, BUNfirst(tin_l_time));
+       tin_l_centerx_t = (int*)Tloc(tin_l_centerx, BUNfirst(tin_l_centerx));
+       tin_l_centery_t = (int*)Tloc(tin_l_centery, BUNfirst(tin_l_centery));
+       tin_r_obsid_t = (int*)Tloc(tin_r_obsid, BUNfirst(tin_r_obsid));
+       tin_r_time_t = (int*)Tloc(tin_r_time, BUNfirst(tin_r_time));
+       tin_r_centerx_t = (int*)Tloc(tin_r_centerx, BUNfirst(tin_r_centerx));
+       tin_r_centery_t = (int*)Tloc(tin_r_centery, BUNfirst(tin_r_centery));
+
+       /*main algorithm*/
+       nr_points = BATcount(tin_l_obsid);
+       nr_obs = tin_l_obsid_t[nr_points-1]+1;
+       for(idx=0; idx<nr_points; idx++)
+       {
+               obsid=tin_r_obsid_t[idx];
+               for(l=0;l<nr_obs;l++)
+               {
+                       if(groupTo[obsid][l]!=0)
+                       {
+                               found=1;
+                               current_groupids[l]=1;
+                       }
+               }
+               if (found==0)
+               {
+                       groupTo[obsid][groups]=1;
+                       current_groupids[groups]=1;
+                       groupFrom[groups]->obsid[obsid]=1;
+                       groupFrom[groups]->totalx = groupFrom[groups]->totalx + 
tin_r_centerx_t[idx];
+                       groupFrom[groups]->totaly = groupFrom[groups]->totaly + 
tin_r_centery_t[idx];
+                       groupFrom[groups]->cnt = groupFrom[groups]->cnt + 1;
+                       if(tin_r_time_t[idx] < (groupFrom[groups]->fromtime))
+                               groupFrom[groups]->fromtime = tin_r_time_t[idx];
+                       else if(tin_r_time_t[idx] > (groupFrom[groups]->totime))
+                               groupFrom[groups]->totime = tin_r_time_t[idx];
+                       groups++;
+               }
+               if(((idx<(nr_points-1)) && 
(tin_l_obsid_t[idx]!=tin_l_obsid_t[idx+1])) || (idx==nr_points-1))
+               {
+                       for(l=0;l<nr_obs;l++)
+                       {
+                               if(current_groupids[l]!=0)
+                               {
+                                       groupTo[tin_l_obsid_t[idx]][l]=1;
+                                       
if(groupFrom[l]->obsid[tin_l_obsid_t[idx]]==0)
+                                       {
+                                               
groupFrom[l]->obsid[tin_l_obsid_t[idx]]=1;
+                                               groupFrom[l]->totalx = 
groupFrom[l]->totalx + tin_l_centerx_t[idx];
+                                               groupFrom[l]->totaly = 
groupFrom[l]->totaly + tin_l_centery_t[idx];
+                                               groupFrom[l]->cnt = 
groupFrom[l]->cnt + 1;
+                                               if(tin_l_time_t[idx] < 
(groupFrom[l]->fromtime))
+                                                       groupFrom[l]->fromtime 
= tin_l_time_t[idx];
+                                               else if(tin_l_time_t[idx] > 
(groupFrom[l]->totime))
+                                                       groupFrom[l]->totime = 
tin_l_time_t[idx];
+
+                                       }
+                                       current_groupids[l]=0;
+                               }
_______________________________________________
checkin-list mailing list
[email protected]
http://mail.monetdb.org/mailman/listinfo/checkin-list

Reply via email to