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