source: ether_eccad/trunk/API_EXTRACT/src/eccad.c

Last change on this file was 70, checked in by cbipsl, 14 years ago

maj eccad V3.2

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 26.1 KB
RevLine 
[68]1#include "eccad.h"
2
3EccadDB connectEccadDB(const char* server, const char* dbName,
4             const char* user, const char* pass, const char* port)
5{
6  EccadDB conn = PQsetdbLogin(server, port, NULL, NULL, dbName, user, pass);
7  if(!checkConnectionStatus(conn))
8  {
9    if (PQstatus(conn) == CONNECTION_BAD)
10    {
[70]11//      fprintf(stderr, "Connection to database '%s' failed.\n", dbName);
12//      fprintf(stderr, "%s", PQerrorMessage(conn));
[68]13      PQfinish(conn);
14    }
15    return NULL;
16  }
17  return conn;
18}
19
20void closeEccadDB(EccadDB conn)
21{
22   PQfinish(conn);
23}
24
[70]25gdImagePtr grid2img(EccadDB db, ptrGrid g, ptrParam p, int idMap, int idClassdis)
[68]26{
27  gdImagePtr im=NULL;
28  register int i,j;
29  LIST map;
[70]30
31  map= mapOfParamById(db, idMap, idClassdis, p->mini, p->maxi);
32//  fprintf(stderr,"start colorAllocate with %d colors\n", map->count);
33
[68]34  if(map)
35  {
[70]36        if (im= gdImageCreate(g->width, g->height))
37        {
38//              fprintf(stderr," width %d height %d\n",g->width, g->height );
39                colorAllocate(im, map);
40//              fprintf(stderr,"im->sy=%d\n",im->sy);
41//              fprintf(stderr,"im->sx=%d\n",im->sx);
42//              for (j=0 ;  j < im->sy; j++)
43//                      for (i=0  ; i < im->sx ; i++)
44        for (j=0 ;  j < gdImageSY(im); j++)
45            for (i=0  ; i < gdImageSX(im) ; i++)
46                        {
47//                              int value= val2Color(g->datas[j * im->sx +i],  map);
48                                int value= val2Color(g->datas[j * g->width +i],  map);
49//if (i>=363 && i<=365 && j>=81 && j<=83)       fprintf(stderr,"i=%d, j=%d, color Paris=%d, val=%ld\n",i,j,value,g->datas[j * g->width +i]);
50                                gdImageSetPixel(im, i, j, value);
51                        }
52                }
[68]53  }
54  return im;
55}
56
[70]57// insertion d'une classmap dans une image gd
[68]58// aucune couleur ne doit etre alloue dans l'image d'origine
59void colorAllocate(gdImagePtr im, LIST map)
60{
61   ptrInterval v;
62   int i, j=0;
63   loop_through_list(map, v, ptrInterval)
64   {
65     i=gdImageColorAllocate(im, v->color->red ,v->color->green, v->color->blue);
66     //fprintf(stderr, "color Allocatealloue a l-ndex %d :%d \n", j, i);
67     j++;
68   }
69}
70
71char * oid2filename(EccadDB db, int oidGrille, int width, int height,
[70]72                    char * idParam, int idMap, int idClassdis, char *resultFile)
[68]73{
74   char *fileName=NULL;
75   int grilleId;
76   char query[512];
77
78   PQexec(db,("BEGIN"));
79   if( (grilleId = lo_open(db, oidGrille, INV_READ)) != -1)
80   {
81     ptrGrid g= allocGrid( width, height);
82     if(g)
83     {
84       unsigned long K;
85       unsigned long size;
86
87       size=  (long)g->width * (long)g->height * sizeof(int);
88//fprintf(stderr, "MAXINT %ld 2*MAXINT %ld\n", MAXINT, (unsigned)MAXINT + MAXINT -1);
89       if( (K= lo_read(db, grilleId, (char*)g->datas, size)) == size)
90       {
91         PGresult *res;
92         sprintf(query, 
93            "SELECT min_param,max_param FROM parametre WHERE id_param='%s'",
94            idParam);
95         res=PQexec(db,query);
96         if(checkSQLErrors(res)) 
97         {
98           double mini= atof( PQgetvalue(res, 0, 0));
99           double maxi= atof( PQgetvalue(res, 0, 1));
100           LIST map;
101
[70]102           if(  map = mapOfParamById(db, idMap, idClassdis, mini, maxi))
[68]103           {
104             gdImagePtr im;
105             if (im= gdImageCreate(width, height))
106             {
107               register int i,j;
108               FILE *out;
[70]109//fprintf(stderr," oidGrille %d mini %f maxi %f\n", oidGrille, mini,maxi);
[68]110               colorAllocate(im, map);
111               for (j=0 ;  j < im->sy; j++)
112                 for (i=0  ; i < im->sx ; i++)
113                 {
114                   int value= val2Color(g->datas[j * im->sx +i], map);
115// if( g->datas[j * im->sx +i] !=0) printf( " %d\n", g->datas[j * im->sx +i]);
116                   gdImageSetPixel(im, i, j, value);
117                 }
118//               sprintf(query, "/tmp/%d.%d.png", oidGrille, idMap);
119               if( out= fopen(resultFile, "w"))
120               {
121                 gdImagePng(im, out);
122                 fclose(out);
123                 fileName=resultFile;
124               }
125               else fprintf(stderr,"Erreur open File %s\n", fileName);
126               gdImageDestroy (im);
127             }
128             else fprintf(stderr,"Erreur gdImageCreate\n");
129             freeMap(map);
130           }
131           else fprintf(stderr,"Erreur Check Map idMap %d\n", idMap);
132         }
133         else fprintf(stderr,"Erreur Check min max from idParam %s\n", idParam);
134         PQclear(res);
135       }
136       else fprintf(stderr," Erreur Read OID : %ld sur %d octets\n", K, size);
[70]137//fprintf (stderr, " now free grid\n");
[68]138       freeGrid(g);
139     }
140     lo_close(db, grilleId);
141   }
142   else fprintf(stderr," Erreur Open OID : %d\n", oidGrille);
143   PQexec(db,("END"));
144   return fileName;
145}
146
[70]147char * getRaster(const char *dbHost, const char * dbName, const char *dbUser, int idGrille, int idMap, int idClassdis)
[68]148{
149    char *resultFile;
150    struct stat sts;
151    char query[256];
152
[70]153    //sprintf(query,"/tmp/%d.%d.%d.png", idGrille, idMap, idClassdis);
[68]154    sprintf(query,"/tmp/%d.%d.png", idGrille, idMap);
155    resultFile= strdup(query);
156
157    if ( stat (query, &sts) != 0) 
158    {
159      char *fileName=NULL;
160      EccadDB db; 
161
162      if((db= PQsetdbLogin(dbHost,"5432", NULL, NULL, dbName, dbUser, NULL)) &&
[70]163                (PQstatus(db) != CONNECTION_BAD) )
[68]164      {
165        PGresult *res;
[70]166        sprintf(query, "SELECT valeurs_grille, id_param, ncol_grille, nlign_grille FROM grille WHERE id_grille = %d;",idGrille);
[68]167        res=PQexec(db,query);
168        if(checkSQLErrors(res) && PQntuples(res))
169        {
170          int oid=   atoi(PQgetvalue(res, 0, 0));
171          int width=  atoi(PQgetvalue(res, 0, 2));
172          int height= atoi(PQgetvalue(res, 0, 3));
[70]173          fileName= oid2filename(db, oid, width, height, PQgetvalue(res, 0, 1), idMap, idClassdis, resultFile);
[68]174        }
175        else  fprintf(stderr, "No found id-grille %d\n", idGrille); 
176        PQclear(res);
177        PQfinish(db);
178      }
[70]179      else  fprintf(stderr, "Erreur connect to db %s on host %s\n", dbName, dbHost);
[68]180      resultFile= fileName;
181   }
182   return resultFile;
183}
184//      ============================ Fonctions ==================================
185
186
187int checkConnectionStatus(EccadDB conn)
188{
189  char* erreur;
190       
191  if (conn==NULL || PQstatus(conn) == CONNECTION_BAD)
192  {
[70]193     return 0==1;
[68]194  }
195  else{
196    // la connection est ok
197    // mise en place de la date en UTC/GMT
198    // db->clock_consumed=clock();
199      PGresult* res=PQexec(conn, "set time zone 'UTC';");
200      if (!(res) || 
201          ((PQresultStatus(res) != PGRES_TUPLES_OK ) &&
202                  (PQresultStatus(res) != PGRES_COMMAND_OK) ) ){
203                PQclear(res);
204        return 0==1;
205      }
206      res= PQexec(conn, "set enable_seqscan='false';");
207      if (!(res) ||
208          ((PQresultStatus(res) != PGRES_TUPLES_OK ) &&
209          (PQresultStatus(res) != PGRES_COMMAND_OK) ) )
210      {
211        PQclear(res);
212        return 0==1;
213      }
214      res= PQexec(conn, "set datestyle to 'ISO, YMD';");
215      if (!(res) ||
216          ((PQresultStatus(res) != PGRES_TUPLES_OK ) &&
217          (PQresultStatus(res) != PGRES_COMMAND_OK) ) )
218      {
219        PQclear(res);
220        return 0==1;
221      }
222      res= PQexec(conn, "set autocommit to 'on';");
223      if (!(res) ||
224          ((PQresultStatus(res) != PGRES_TUPLES_OK ) &&
225          (PQresultStatus(res) != PGRES_COMMAND_OK) ) )
226      {
227        PQclear(res);
228        return 0==1;
229      }
230    }
231    return 0==0;
232}
233
234int checkSQLErrors(PGresult* res){
235        if ( !(res) ||
236          ((PQresultStatus(res) != PGRES_TUPLES_OK ) &&
237          (PQresultStatus(res) != PGRES_COMMAND_OK) ) )
238      {
239                return 0==1;
240        }
241        return 0==0;
242}
243
244LIST initColors(EccadDB conn)
245{
246    LIST colors= make_list();
247    PGresult *res;
248    res=PQexec(conn,"SELECT * FROM color;");
249    if(checkSQLErrors(res)) 
250    {
251      register int i;
252      for(i=0; i< PQntuples(res); i++)
253      {
254         ptrRGB c= alloueColor( 
255             atoi(PQgetvalue(res, i, 0)),
256             atoi(PQgetvalue(res, i, 1)),
257             atoi(PQgetvalue(res, i, 2)),
258             atoi(PQgetvalue(res, i, 3)));
259        add_to_tail(colors, c);
260      }
261    }
262    PQclear(res);
263
264    return colors;
265}
266LIST initParams(EccadDB conn)
267{
268    LIST params= make_list();
269    PGresult *res;
270    res=PQexec(conn,"SELECT * FROM parametre;");
271    if(checkSQLErrors(res)) 
272    {
273      register int i;
274      for(i=0; i< PQntuples(res); i++)
275      {
[70]276//               fprintf(stderr, " PQgetvalue*--> maxPar %s   %s", PQgetvalue(res, i, 6), PQgetvalue(res, i, 3));
[68]277         //fprintf(stderr, " PQgetvalue-> maxPar %f", (double)strtod(PQgetvalue(res, i, 6),NULL));
278       
279         ptrParam p= allocParam( 
280             atoi(PQgetvalue(res, i, 0)),    //id
[70]281             atoi(PQgetvalue(res, i, 2)),    //id unite
[68]282             PQgetvalue(res, i, 3),         // full name
[70]283             atof(PQgetvalue(res, i, 6)),    // minPar
284             atof(PQgetvalue(res, i, 7)),NULL);   // maxPar
[68]285        add_to_tail(params, p);
286      }
287
288    }
289    PQclear(res);
290    return params;
291}
292
[70]293ptrParam getParamFromId(EccadDB db, int idParam)
294{
295        PGresult *res;
296        char query[256];
297        sprintf(query, "SELECT id_param, id_unite, fullname_param, min_param, max_param, shortname_unite FROM parametre inner join unite using (id_unite) WHERE id_param = %d;", idParam);
298        res=PQexec(db,query);
299        ptrParam p = NULL;
300
301        if(checkSQLErrors(res) && PQntuples(res) )
302        {
303                p= allocParam(
304            atoi(PQgetvalue(res, 0, 0)),    //id
305            atoi(PQgetvalue(res, 0, 1)),    //id unite
306            PQgetvalue(res, 0, 2),          // full name
307            atof(PQgetvalue(res, 0, 3)),    // minParam
308            atof(PQgetvalue(res, 0, 4)),    // maxParam
309            PQgetvalue(res, 0, 5));         // shortname unite
310        }
311        PQclear(res);
312        return p;
313}
314
315ptrParam getParamFromName(EccadDB db, char* nomParam)
316{
317        PGresult *res;
318        char query[256];
319        sprintf(query, "SELECT id_param, id_unite, fullname_param, min_param, max_param, shortname_unite FROM parametre inner join unite using (id_unite) WHERE fullname_param = '%s';", nomParam);
320        res=PQexec(db,query);
321        ptrParam p = NULL;
322
323        if(checkSQLErrors(res) && PQntuples(res) )
324        {
325                p= allocParam(
326            atoi(PQgetvalue(res, 0, 0)),    //id
327            atoi(PQgetvalue(res, 0, 1)),    //id unite
328            PQgetvalue(res, 0, 2),          // full name
329            atof(PQgetvalue(res, 0, 3)),    // minPar
330            atof(PQgetvalue(res, 0, 4)),    // maxPar
331                PQgetvalue(res, 0, 5));                 // shortname unite
332        }
333        PQclear(res);
334        return p;
335}
336
[68]337// generation de la table de couleur d'une map de la base
338//        idMap : id de la base
339//        mini/maxi : valeurs min max du parametre de la base
[70]340//            ces deux valeurs sont nécessaire car les intervalles de couleurs
[68]341//              sont codes en unsigned maxhort / au extremes du param
342
[70]343LIST mapOfParamById(EccadDB conn, int idMap, int idClassdis, double mini, double  maxi)
[68]344{
345   LIST map=NULL;
346   static LIST colors=NULL;
347   PGresult *res, *resc;
348   char query[256];
349   
350   int isDynamic = 0;
[70]351   
352   /*****************/
353   //mini=-1;
354   //maxi=100;
355   //int pas=10;
356   /****************/
[68]357
358// mapping des couleurs de la base
359   if(!colors) colors=initColors(conn);
360
[70]361//fprintf(stderr,  "Generation interval for idMap %d With  min %f max %f\n",
362//    idMap,  mini,  maxi);
[68]363
[70]364//verification de la classmap dynamique
[68]365sprintf(query, 
[70]366       "SELECT scl_name FROM scale_value WHERE scl_id=%d;", idClassdis);
367res=PQexec(conn,query);
[68]368 if(checkSQLErrors(res) && PQntuples(res)) 
369   {
[70]370           isDynamic = (!strcmp(PQgetvalue(res, 0, 0),"linear") || !strcmp(PQgetvalue(res, 0, 0),"logarithm") || !strcmp(PQgetvalue(res, 0, 0),"exponential"));
[68]371   }
[70]372
373// recherche des interval de la classmap
374   sprintf(query,"SELECT * FROM class_distribution as c JOIN class_index USING (num_order) WHERE scl_id=%d and clm_id=%d order by c.num_order;",idClassdis, idMap);
[68]375   res=PQexec(conn,query);
[70]376   if(checkSQLErrors(res) && PQntuples(res))
[68]377   {
378// mise en place des intervalles base dans la liste map
379      ptrInterval interv;
380      register int i;
381      map = make_list();
[70]382//      fprintf(stderr,  "--------*******--------min %ld max %ld\n",
383//      v2int(mini,mini,maxi), v2int(maxi,mini,maxi));
384      unsigned int cmin,cmax;
385      double borneMin, borneMax;
[68]386      for(i=0; i< PQntuples(res); i++)
387      {
[70]388
389        //conversion en unsigned int des intervalles
390 
391        int id=atoi(PQgetvalue(res, i, 5));//idColor
392        borneMin = atof(PQgetvalue(res, i, 3));
393        borneMax = atof(PQgetvalue(res, i, 4));
394        if (borneMin < 0) borneMin = 0;
395        if (isDynamic)  cmin= v2int(borneMin*maxi,mini, maxi);
396        else  cmin= v2int(borneMin,mini, maxi);
397        if (isDynamic)  cmax= v2int(borneMax*maxi,mini, maxi);
398        else cmax= v2int(borneMax,mini, maxi);
399//fprintf(stderr,  "--------*i=%d, min=%f, max=%f, f1=%.1f, f2=%.1f, cmin=%ld, cmax=%ld\n",i,mini,maxi,borneMin,borneMax,cmin,cmax);
400        /*
401        int id= atoi(PQgetvalue(res, i, 1));   //idColor
402        if (isDynamic)  cmin= v2int(atof(PQgetvalue(res, i, 3))* maxi,mini, maxi);
403        else  cmin= v2int(atof(PQgetvalue(res, i, 3)),mini, maxi);
404        if (isDynamic)  cmax= v2int(atof(PQgetvalue(res, i, 2))* maxi,mini, maxi);
405        else cmax= v2int(atof(PQgetvalue(res, i, 2)),mini, maxi);
406        */
407
408
409        //affectation des couleurs aux intervalles
410        ptrRGB c;
411        if( c= (ptrRGB)search_list(colors, &id, (PFI)inColorId) )
412        {
413                        interv = alloueInterval( c,cmin, cmax);
414//                      fprintf(stderr,  "add interval: cmin %s  %ld cmax %s  %ld id -> col %d \n",
415//                      PQgetvalue(res, i, 3), cmin, PQgetvalue(res, i, 4), cmax, c->id);
416
417                        add_to_tail( map , interv);
[68]418         }
419         else
420         {
421           fprintf(stderr,  "No found color for id %d\n", id);
422           exit(1);
423         }
424      }
425   }
[70]426   else fprintf(stderr,  "No found color for idMap  %d\n", idMap);
[68]427
428   PQclear(res);
429
430   return map;
431}
432
[70]433//generation d'une table de couleur en fonction d'un parametre //
434/*LIST mapOfParam(EccadDB conn, ptrParam p)
[68]435{
436   LIST map=NULL;
437   PGresult *res;
438   char query[256];
439   
440   sprintf(query,"%s \'%s\';",
[70]441                "SELECT cm_id, scl_default_id FROM param_color NATURAL JOIN parametre WHERE fullName_param = ",p->name);
[68]442   res=PQexec(conn,query);
443   if(checkSQLErrors(res))
[70]444      map=  mapOfParamById(conn, atoi(PQgetvalue(res, 0, 0)), atoi(PQgetvalue(res, 0, 1)), p->mini, p->maxi);
[68]445   PQclear(res);
446   return map;
[70]447}*/
[68]448
449// w, h : size of origin grid isur zone globale implicite
450// z :  zone a extraire en inputs zone extraite en output
451static ptrGrid loadDatasFromBd(EccadDB db,int oidGrille,int w,int h,ptrZone z)
452{
453// la grille origine sans data alloue
454   Grid g;
455   g.width=w;
456   g.height=h;
457// la grille de sortie
458   ptrGrid grid=NULL;
459   {
460     int id;
461     PQexec(db,("BEGIN"));
462     if( (id = lo_open(db, oidGrille, INV_READ)) != -1)
463     {
464       if ((z != NULL) && memcmp(z, &globalZone, sizeof(Zone)) )
465       {
466         int iDeb ,iFin, jDeb, jFin;
467         double  paslon, paslat;
[70]468         float x, y, latMin, lonMin, latMax, lonMax;
[68]469         unsigned long K;
470// le pas dorigine       
471         paslat= pasLat(&globalZone, &g);
472         paslon= pasLon(&globalZone, &g);
473          // la zone qui doit etre extraite
[70]474//         fprintf(stderr, "Extrait Origin Lat %f->%f  pasLat %.2f lon %f->%f pasLon %.2f\n",
475//                       z->latMin,z->latMax,paslat,z->lonMin,z->lonMax,paslon);
476// la zone à extraire
477//               z->lonMax = z->lonMax - paslon/2.;
478//               z->lonMin = z->lonMin + paslon/2.;
479//               z->latMax = z->latMax - paslat/2.;
480//               z->latMin = z->latMin + paslat/2.;
481                 if (z->latMin == z->latMax && z->lonMin == z->lonMax) {
482                         if (z->latMin == 0 && z->lonMin == 0) {
483                                 lonMax = paslon/2.;
484                                 lonMin = paslon/2.;
485                                 latMax = paslon/2.;
486                                 latMin = paslon/2.;
487                         } else {
488                                 lonMax = z->lonMax;
489                                 lonMin = z->lonMin;
490                                 latMax = z->latMax;
491                                 latMin = z->latMin;
492                         }
493                 } else {
494                         lonMax = z->lonMax - paslon/2.;
495                         lonMin = z->lonMin + paslon/2.;
496                         latMax = z->latMax - paslat/2.;
497                         latMin = z->latMin + paslat/2.;
498                 }
499                 jDeb= lat2j(latMin, &g, &globalZone);
500         jFin= lat2j(latMax, &g, &globalZone);
501         iDeb= lon2i(lonMin, &g, &globalZone);
502         iFin= lon2i(lonMax, &g, &globalZone);
503//fprintf(stderr,"************ 1) zone:lat=%f->%f, lon=%f->%f, lat2j=%d->%d, lon2i=%d->%d, w=%d, h=%d, paslat=%f, paslon=%f\n",
504//              latMin,latMax,lonMin,lonMax,jDeb,jFin,iDeb,iFin,w,h,paslat,paslon);
[68]505         //la borne en width et height ne peut etre atteinte
[70]506//         if( iDeb == g.width) iDeb--;
507//         if( jDeb == g.height) jDeb--;
508//         if( iFin == g.width) iFin--;
509//         if( jFin == g.height) jFin--;
[68]510         
511         // recalcul de la zone extraite
[70]512//         lonMin = i2lon(iDeb,  &g, &globalZone);
513//         latMin = j2lat(jDeb,  &g, &globalZone);
514//         lonMax = i2lon(iFin, &g, &globalZone) ;
515//         latMax = j2lat(jFin, &g, &globalZone)
[68]516// ajout des limites de domaines
517// car les positions sont aux centres pour les macro I2 et j2
[70]518//               z->lonMax = lonMax + paslon/2.;
519//         z->lonMin = lonMin - paslon/2.;
520//         z->latMax = latMax - paslat/2.;
521//         z->latMin = latMin + paslat/2.;
522//fprintf(stderr,"************ 2) zone:lat=%f->%f, lon=%f->%f\n",z->latMin,z->latMax,z->lonMin,z->lonMax);
[68]523         
524         // extraction dans une nouvelle grille
[70]525         grid= allocGrid(iFin -iDeb +1, jFin -jDeb +1);
526//              grid= allocGrid((z->lonMax-z->lonMin)*paslon, (z->latMax-z->latMin)*paslon);
[68]527         if(iFin < iDeb)
528         {
529            freeGrid(grid);
530            grid=NULL;
[70]531//            fprintf(stderr,"  Not now ok on partial grid crossing in longitude \n iDeb %d Ifin %d jDeb %d jFin %d\n", iDeb ,iFin, jDeb, jFin);
[68]532         }
533         else
534         {
535           int i,j, pos, skip, width;
536           unsigned char *data=(unsigned char *)(grid->datas);
537           pos= (g.width*jDeb + iDeb) * sizeof(unsigned int);
538           lo_lseek(db, id, pos, SEEK_SET);
539           width= (iFin -iDeb +1) * sizeof(unsigned int);
540           skip= g.width * sizeof(unsigned int) - width ;
[70]541//           fprintf(stderr,"*** id=%d, g.w=%d, w=%d, pos=%d, skip=%d\n", id, g.width, width, pos, skip);
[68]542           for(i=jDeb; i<=jFin; i++, data += width)
543           {
[70]544                   K= lo_read(db, id, (unsigned char*)data, width);
545//fprintf(stderr,"*** i=%d, K=%d\n", i, K);
546             if(K != (unsigned long)width)
[68]547             {
548               fprintf(stderr, "Err readLine ligne %d read %ld sur %d demandes\n", i, K, width); 
[70]549               fprintf(stderr, "EXIT trap:\n iDeb %d iFin %d jDeb %d jFin %d\n pos %d width %d skip %d\n",
550                                 iDeb , iFin , jDeb , jFin , pos , width , skip);
[68]551               exit (1);
552             }
553             lo_lseek(db, id, skip, SEEK_CUR);
554           }
555         }
556       }
557       else
558       {
559         unsigned long  K, size= g.width * g.height * sizeof(int);
560         grid= allocGrid(g.width , g.height);
[70]561         if( (K= lo_read(db, id, (char*)grid->datas, size)) != size)
[68]562            fprintf(stderr," Erreur Read OID : %ld sur %d octets\n", K, size);
[70]563//         else
564//               fprintf(stderr,"  Full Read OID : %ld sur %d octets\n", K, size);
[68]565       }
566       lo_close(db,id);
567     }
568     else fprintf(stderr," Erreur Open OID : %d\n", oidGrille);
569     PQexec(db,("END"));
570   }
[70]571//   fprintf(stderr,"Open OID \n");
[68]572   return grid;
573}
574
575ptrGridGL extractGridFromDb(EccadDB db, int idGrille, ptrZone z)
576{
577   static LIST params=NULL;
578   ptrGridGL gl=NULL;
579   PGresult *res;
580   char query[256];
581
582   sprintf(query,
583     "SELECT valeurs_grille, id_param, ncol_grille, nlign_grille, date_grille FROM grille WHERE id_grille = %d;", idGrille);
584   res=PQexec(db,query);
585   if(checkSQLErrors(res) && PQntuples(res))
586   {
587      int oid=     atoi(PQgetvalue(res, 0, 0));
588      int idParam= atoi(PQgetvalue(res, 0, 1));
589      int w=  atoi(PQgetvalue(res, 0, 2));
590      int h=  atoi(PQgetvalue(res, 0, 3));
[70]591//      if(!params) params=initParams(db);
592      ptrParam p;
593//      if( p= (ptrParam)search_list(params, &idParam, (PFI)inParamId) )
594      if( p = (ptrParam)getParamFromId(db,idParam) )
[68]595      {
596        ptrGrid g;
597        Zone zoneExtr;
598        g= loadDatasFromBd( db, oid, w, h, z);
599        strcpy(g->date, PQgetvalue(res, 0, 4) );
600        gl=  allocGridGL(g, z);
601        gl->param=p;
602      }
603      else fprintf(stderr, " No found para for id %d\n", idParam);
604
605    }
606    PQclear(res);
607    return gl; 
608}
609
[70]610LIST getGrids(EccadDB db, int idProduct, int idSecteur, int idRcp, ptrParam p, ptrZone z,
611                      const char* dateBegin, const char* dateEnd, const char* alt)
[68]612{
613   int i;
614   LIST grids=NULL;
[70]615   char query[2000];
616   strcpy(query, "SELECT valeurs_grille, ncol_grille, nlign_grille, date_grille, deltadate_grille");
617   strcat(query, ", (cast(date_grille+deltadate_grille as date)-1) as dateFinal FROM grille");
618   if (strcmp(alt,"0")) {
619           strcat(query, " LEFT JOIN code ON id_grille = num_code");
620   }
621   sprintf(&query[strlen(query)], " WHERE id_param = %d",  p->id);
622   sprintf(&query[strlen(query)], " AND id_produit = %d",  idProduct);
623   sprintf(&query[strlen(query)], " AND date_grille BETWEEN '%s' AND '%s'", dateBegin, dateEnd);
624   if (idSecteur != 0) {
625           sprintf(&query[strlen(query)], " AND id_secteur = %d",  idSecteur);
626   } else {
627           strcat(query, " AND (id_secteur is null OR id_secteur");
628           strcat(query, " IN (select id_secteur from secteur where fullname_secteur <> 'Aviation'))");
629   }
630   if (idRcp != 0) {
631           sprintf(&query[strlen(query)], " AND id_rcp = %d",  idRcp);
632   } else {
633           strcat(query," AND id_rcp is null");
634   }
635   if (strcmp(alt,"0")) {
636           sprintf(&query[strlen(query)], " AND legend_code = '%s'",  alt);
637   }
638   strcat(query," order by date_grille");
639fprintf(stderr, "********* requete= %s\n", query);
640        PGresult *res=PQexec(db,query);
641//fprintf(stderr, "found  %d grilles\n", PQntuples(res));
642//fprintf(stderr, "param=%d, prod=%d, dateBeg=%s, dateEnd=%s\n", p->id, idProduct, dateBegin,dateEnd);
[68]643   if(checkSQLErrors(res) && PQntuples(res) ) 
644   {
[70]645         // origin size
[68]646     int width= atoi(PQgetvalue(res,0,1));
647     int height= atoi(PQgetvalue(res,0,2));
[70]648//fprintf(stderr, "w=%d, h=%d\n", width,height);
649     ptrZone zTemp = (ptrZone)malloc(sizeof(Zone));
650         memcpy(zTemp, z, sizeof(Zone));
651
[68]652     for(i=0; i< PQntuples(res); i++)
653     {
[70]654        memcpy(z, zTemp, sizeof(Zone));
[68]655       int oid= atoi(PQgetvalue(res,i,0) );
[70]656//fprintf(stderr, "Load oid %d from %dx%d grille date %s\n", oid,  width, height, PQgetvalue(res, i, 3) );
[68]657
[70]658//fprintf(stderr,"Avant--------- : Extrait Reel Lat min %f max %f   lon min %f max %f \n",z->latMin,z->latMax,z->lonMin,z->lonMax);
[68]659
660
661       ptrGrid g= loadDatasFromBd( db, oid, width, height, z);
662       
[70]663//fprintf(stderr,"AprÚs--------- : Extrait Reel Lat min %f max %f   lon min %f max %f \n",z->latMin,z->latMax,z->lonMin,z->lonMax);
664//fprintf(stderr,"date grille %s\n",g->date);
[68]665       if(g)
666       {
667         char *date= PQgetvalue(res, i, 3);
668         sprintf(g->date, "%.4s%.2s%.2s", date, &date[5], &date[8]);
[70]669         char *delta= PQgetvalue(res, i, 4);
670         sprintf(g->delta, "%s", delta);
671         char *dateEndPeriod= PQgetvalue(res, i, 5);
672         sprintf(g->dateEndPeriod, "%.4s%.2s%.2s", dateEndPeriod, &dateEndPeriod[5], &dateEndPeriod[8]);
673//         sprintf(g->date, "%s", date);
674//fprintf(stderr,"date grille=%s, dateFinPeriode=%s, delta=%s, i=%d, nbtuples=%d\n",g->date,g->dateEndPeriod,g->delta,i,PQntuples(res));
675         if(!grids) grids = make_list();
676                         add_to_tail(grids, g);
[68]677       }
678       else fprintf(stderr, "loadGrid error\n");
679     }
680   }
681   PQclear(res);
682   return grids;
683}
684
685
686
687ptrGrid getGrid(EccadDB db, int idGrille, ptrZone z)
688{
[70]689//      fprintf(stderr, "--getGrid \n");fflush(stderr);
[68]690        PGresult *res;
691        char query[256];
692        sprintf(query,
693      "SELECT valeurs_grille, ncol_grille, nlign_grille, date_grille FROM grille WHERE id_grille = %d;", idGrille);
[70]694// fprintf(stderr, "-->requete getGrid -> %s\n",query );
[68]695   res=PQexec(db,query);
696   //fprintf(stderr, "found  %d grilles SQLErrors %d\n", PQntuples(res), checkSQLErrors(res));
697   ptrGrid g = NULL;
698   
699   if(checkSQLErrors(res) && PQntuples(res) ) 
700   {
701        // origin size
702     int width= atoi(PQgetvalue(res,0,1));
703     int height= atoi(PQgetvalue(res,0,2));
704     
705     int oid= atoi(PQgetvalue(res,0,0) );
706//fprintf(stderr,"Avant--------- : Extrait Reel Lat min %f max %f   lon min %f max %f \n",z->latMin,z->latMax,z->lonMin,z->lonMax);
[70]707
[68]708     g= loadDatasFromBd( db, oid, width, height, z);
709     
710//fprintf(stderr,"Apres--------- : Extrait Reel Lat min %f max %f   lon min %f max %f \n",z->latMin,z->latMax,z->lonMin,z->lonMax);
711     
712     
713     if(g)
714     {
715        char *date= PQgetvalue(res, 0, 3);
716        sprintf(g->date, "%.4s%.2s%.2s", date, &date[5], &date[8]);
717       
[70]718//                              fprintf(stderr, "date grille %s \n", date);
[68]719     }   
720   }
[70]721   PQclear(res);
[68]722   return g;
723}
724
[70]725ptrGrid getGridFromProduit(EccadDB db, char* nomProd, ptrZone z)
726{
727        PGresult *res;
728        char query[256];
729        sprintf(query,
730                "SELECT valeurs_grille, ncol_grille, nlign_grille, date_grille FROM grille WHERE id_produit in (select id_produit from produit where title_produit = '%s');", nomProd);
731        res=PQexec(db,query);
732        ptrGrid g = NULL;
733        if(checkSQLErrors(res) && PQntuples(res) )
734        {
735                // origin size
736                int width= atoi(PQgetvalue(res,0,1));
737                int height= atoi(PQgetvalue(res,0,2));
738                int oid= atoi(PQgetvalue(res,0,0) );
739                g= loadDatasFromBd( db, oid, width, height, z);
[68]740
[70]741                if(g)
742                {
743                        char *date= PQgetvalue(res, 0, 3);
744                        sprintf(g->date, "%.4s%.2s%.2s", date, &date[5], &date[8]);
745                }
746        }
747        PQclear(res);
748        return g;
749}
750
[68]751double* getMaxParam(int idGrid, EccadDB conn)
752{
753        double* maxParam = NULL;
754        PGresult *res;
755        char query[256];
756    sprintf(query,"SELECT id_param FROM grille WHERE id_grille=%d;", idGrid);
757    res=PQexec(conn,query);
758   
759    if(checkSQLErrors(res) && PQntuples(res) ) 
760    {
761        int idParam= atoi(PQgetvalue(res,0,0));
762       
[70]763//      ptrParam p;
764//      LIST params=initParams(conn);
765//      if( p= (ptrParam)search_list(params, (NODE)&idParam, (PFI)inParamId) )
766//                      maxParam = &p->maxi;
767
768        ptrParam p = (ptrParam)getParamFromId(conn,idParam);
769        maxParam = &p->maxi;
[68]770    }
771        return maxParam;
772}
773
774double* getMinParam(int idGrid, EccadDB conn)
775{
776        double* minParam = NULL;
777        PGresult *res;
778        char query[256];
779    sprintf(query,"SELECT id_param FROM grille WHERE id_grille=%d;", idGrid);
780    res=PQexec(conn,query);
781   
782    if(checkSQLErrors(res) && PQntuples(res) ) 
783    {
784        int idParam= atoi(PQgetvalue(res,0,0));
785       
[70]786//      ptrParam p;
787//      LIST params=initParams(conn);
788//      if( p= (ptrParam)search_list(params, (NODE)&idParam, (PFI)inParamId) )
789//                      minParam = &p->mini;
790
791        ptrParam p = (ptrParam)getParamFromId(conn,idParam);
792                minParam = &p->mini;
793
[68]794    }
795   
796        return minParam;
797}
798
[70]799long datation (long j, long m, long a){
800        long n = a*365 + (m-1)*31 + j ;
801        if (m <= 2) a-- ;
802        n = n + a/4 - a/100 + a/400 ;
803        if (m > 2) n = n - (int)((m-1)*0.4+2.7) ;
804        return n ;
805}
Note: See TracBrowser for help on using the repository browser.