source: ether_eccad/trunk/ECCAD_INSERT/src/Produit.cpp @ 70

Last change on this file since 70 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: 12.5 KB
Line 
1#include <stdio.h>
2#include <string.h>
3#include <dirent.h>
4#include <sys/types.h>
5#include <sys/stat.h>
6#include <iterator>
7#include <time.h>
8#include <iostream>
9#include <cmath>
10using namespace std;
11
12#include "Produit.h"
13
14static char tampon [2048];
15
16Produit::Produit()
17{
18}
19
20Produit::~Produit()
21{
22}
23
24//Mise ᅵ jour d'un produit
25void Produit::majProduit()
26{
27}
28
29//Insertion d'un nouveau produit
30void Produit::nvProduit()
31{
32}
33
34//Fonction de calcul des surfaces de pixels
35long double Produit::createAreaVincenty(Point point)
36{
37//        Point* pBas = new Point(point.lat, point.lon + pas/2);
38                Point* pBas = new Point(point.lat+pas/2, point.lon);
39//        Point* pHaut = new Point(point.lat+pas, point.lon + pas/2);
40                Point* pHaut = new Point(point.lat-pas/2, point.lon);
41//        Point* pGauche = new Point(point.lat+pas/2, point.lon);
42                Point* pGauche = new Point(point.lat, point.lon-pas/2);
43//        Point* pDroit = new Point(point.lat+pas/2, point.lon+pas);
44                Point* pDroit = new Point(point.lat , point.lon+pas/2);
45//        double distancePhautPbas = distance(pHaut, pBas)/1000;
46        double distancePhautPbas = distance(pBas, pHaut)/1000;
47        double distancePgauchePdroit = distance(pGauche, pDroit)/1000;
48
49        delete pBas;
50        delete pHaut;
51        delete pGauche;
52        delete pDroit;
53        return ( (distancePhautPbas *  distancePgauchePdroit) );
54
55       
56}
57
58long double Produit::distance(Point* p1, Point* p2)
59{
60         ////cout<<"lon p1 "<<p1->lon<<" lat p2 "<<p2->lon<<endl;
61        p1->lonRad = p1->lon  * ((2*3.14159265)/360) ;
62        p2->lonRad = p2->lon * ((2*3.14159265)/360) ;
63        p1->latRad = p1->lat * ((2*3.14159265)/360) ;
64        p2->latRad = p2->lat * ((2*3.14159265)/360) ;
65
66        ////cout<<"lonrad p2 "<<p2->lonRad<<" lonrad p1 "<<p1->lonRad<<endl;
67        long double L = p2->lonRad - p1->lonRad;
68        ////cout<<"L "<<L<<endl;
69        long double U1 = atan((1 - f) * tan(p1->latRad));
70        long double U2 = atan((1 - f) * tan(p2->latRad));
71
72        long double sinU1 = sin(U1);
73        long double cosU1 = cos(U1);
74
75        long double sinU2 = sin(U2);
76        long double cosU2 = cos(U2);
77
78        long double sinLambda = 0, cosLambda = 0, sinSigma = 0, cosSigma = 0, sigma = 0, sinAlpha = 0, cosSqAlpha = 0, cos2SigmaM = 0;
79        long double lambda = L, lambdaP = 2* 3.14159265;
80        long double iterLimit = 20;
81
82        while( fabs(lambda-lambdaP) > 1e-12 && --iterLimit>0 )
83        {
84                sinLambda = sin(lambda);
85                cosLambda = cos(lambda);
86                sinSigma = sqrt((cosU2*sinLambda) * (cosU2*sinLambda) + (cosU1*sinU2-sinU1*cosU2*cosLambda) * (cosU1*sinU2-sinU1*cosU2*cosLambda));
87                if (sinSigma==0) return 0;  // co-incident points
88                cosSigma = sinU1*sinU2 + cosU1*cosU2*cosLambda;
89                sigma = atan2(sinSigma, cosSigma);
90                sinAlpha = cosU1 * cosU2 * sinLambda / sinSigma;
91                cosSqAlpha = 1 - sinAlpha*sinAlpha;
92 cos2SigmaM = cosSigma - 2*sinU1*sinU2/cosSqAlpha;
93                if (isnan(cos2SigmaM)) cos2SigmaM = 0;  // equatorial line: cosSqAlpha=0 (6)
94                long double C = f/16*cosSqAlpha*(4+f*(4-3*cosSqAlpha));
95                lambdaP = lambda;
96                lambda = L + (1-C) * f * sinAlpha * (sigma + C*sinSigma*(cos2SigmaM+C*cosSigma*(-1+2*cos2SigmaM*cos2SigmaM)));
97        }
98
99        if (iterLimit == 0) return NAN;  // formula failed to converge
100
101        long double uSq = cosSqAlpha * (a*a - b*b) / (b*b);
102        long double A = 1 + uSq/16384*(4096+uSq*(-768+uSq*(320-175*uSq)));
103        long double B = uSq/1024 * (256+uSq*(-128+uSq*(74-47*uSq)));
104        long double deltaSigma = B*sinSigma*(cos2SigmaM+B/4*(cosSigma*(-1+2*cos2SigmaM*cos2SigmaM)-B/6*cos2SigmaM*(-3+4*sinSigma*sinSigma)*(-3+4*cos2SigmaM*cos2SigmaM)));
105        long double s = b*A*(sigma-deltaSigma);
106        ////cout<<"s  "<<s<<endl;
107        return s;
108
109       
110       
111}
112
113long double Produit::distanceHaversine(Point* p1, Point* p2)
114{
115       
116        long double a = pow( sin((p2->lat-p1->lat)/2) ,2)+ cos(p2->lat) * pow( sin((p2->lon-p1->lon)/2) ,2);
117        long double c = 2*atan2(sqrt(a), sqrt(1-a));
118        return R*c;
119}
120
121long double Produit::createAreaHaversine(Point point)
122{
123//        Point* pBas = new Point(point.lat, point.lon + pas/2);
124        Point* pBas = new Point(point.lat+pas/2, point.lon);
125//        Point* pHaut = new Point(point.lat+pas, point.lon + pas/2);
126        Point* pHaut = new Point(point.lat-pas/2, point.lon);
127//        Point* pGauche = new Point(point.lat+pas/2, point.lon);
128        Point* pGauche = new Point(point.lat, point.lon-pas/2);
129//        Point* pDroit = new Point(point.lat+pas/2, point.lon+pas);
130        Point* pDroit = new Point(point.lat , point.lon+pas/2);
131
132//      double distancePhautPbas = distanceHaversine(pHaut, pBas)/1000;
133        double distancePhautPbas = distanceHaversine(pBas, pHaut)/1000;
134        double distancePgauchePdroit = distanceHaversine(pGauche, pDroit)/1000;
135       
136        delete pBas; 
137        delete pHaut; 
138        delete pGauche; 
139        delete pDroit; 
140        return ( distancePhautPbas *  distancePgauchePdroit );
141}
142
143
144
145char* Produit::getExtensionFileRead(char* fichier) 
146{
147       
148        string fichierStr = fichier;
149       
150        string nomFich = SplitFilename(fichier);
151        size_t found = (nomFich).find_last_of(".") ;
152       
153        //if ((nomFich).substr(found+1, nomFich.length() ) != NULL )   
154                fichierStr=(nomFich).substr(found+1, nomFich.length());
155        //else
156                //fichierStr="";
157               
158                return (char*)fichierStr.c_str();
159        /*
160        char* fich = fichier;
161        while (strchr(fich, '/') != NULL)
162        {
163                 fich = strchr(fich, '/')+1;
164        }       
165        if ( strchr(fich, '.') != NULL )
166        fich = strchr(fich, '.')+1;
167        else fich = "";
168       
169       
170        return fich;*/
171}
172
173
174
175
176//------------------------------------Fonctions de lecture des donnï¿œes brutes-------------------------------------------//
177
178//Scan d'un repertoire : determiner les sous repertoires et les fichiers
179
180Repertoire Produit::scanRep(Repertoire rep)
181{
182        struct dirent *item;
183        static struct stat stat_item;
184        DIR *dirp;
185        char *addr;
186        //cout<<"-----------------------------------------ScanRep------------------------------------"<<endl;   
187        if ((dirp = opendir(rep.chemin)) != NULL)
188        {
189                strcpy(tampon, rep.chemin);
190                strcat(tampon, "/");
191                addr= &tampon[strlen(tampon)];
192
193                while ((item=readdir(dirp)) != NULL)
194                {
195                        strcpy(addr, item->d_name);
196                        if (!stat(tampon, &stat_item))
197                        {
198                                if (S_ISDIR(stat_item.st_mode))
199                                {
200                                        if (strcmp(item->d_name, ".") && strcmp(item->d_name, ".."))
201                                        {
202                                                Repertoire child;
203                                                child.allouRep(stat_item,item->d_name, tampon, rep.level);
204                                                rep.children.push_back(child);
205                                        }
206                                }
207                                else
208                                        {
209                                                Feuille fich = Feuille(stat_item, item->d_name,tampon, rep.level);
210                                                rep.fichier.push_back(fich);
211                                        }
212                        }
213                }
214                //cout<<"-----------------------------------------end ScanRep------------------------------------"<<endl;
215               
216        }
217        else
218        {
219                 //logger->addReadersMessagesErrors("Impossible d'ouvrir le perertoire : "+rep.chemin);
220                 cout << " --->   Impossible d'ouvrir le repertoire : " << rep.chemin << endl; 
221        }
222       
223        return(rep);
224       
225}
226
227//iterer sur toute l'arborescence
228void Produit::scanParcour(Repertoire rep)
229{
230        rep = scanRep(rep);
231        parcourRep(rep);
232}
233
234//--------------------------------------------------------------------------------------------------------------------//
235
236void Produit::parcourRep(Repertoire rep)
237{       
238        if (!rep.children.empty())
239        {
240                list<Repertoire>::iterator it;
241                for (it = rep.children.begin(); it != rep.children.end(); it++){
242                        scanParcour((*it));
243                }
244        }
245       
246        if (!rep.fichier.empty())
247        {
248                base.readBD(logger);
249//              cout << "    traitement des fichiers du repertoire : "<< rep.nom << endl;
250                       
251                bool verifExt;
252                       
253                list<Feuille>::iterator itt;
254                for (itt = rep.fichier.begin(); itt != rep.fichier.end(); itt++)
255                {
256//                      cout << ".......... Fichier à traiter:"<<  (*itt).nom <<endl;
257                        if(!strcmp("NULL", extension))
258                                verifExt=true;
259                        else
260                                verifExt=!strcmp(getExtensionFileRead((*itt).chemin),extension);
261//                      cout << ".......... verifExt="<<verifExt <<endl;
262                        if ( ( verifExt && base.fichier.verifDate(ctime(&((*itt).stt.st_ctime)), base.produit.getTuple(NomProduit)->id, (*itt).nom) ) || ( !strcmp("", extension) && base.fichier.verifDate(ctime(&((*itt).stt.st_ctime)), base.produit.getTuple(NomProduit)->id, (*itt).nom) ) )
263                        {
264                                //cout<<"-----------verifDate------------"<<  base.fichier.verifDate(ctime(&((*itt).stt.st_ctime)), base.produit.getTuple(NomProduit)->id, (*itt).nom) <<(*itt).chemin <<endl;
265                                //cout<< !strcmp(strchr((*itt).chemin , '.')+1, extension)<<"//////8888//////////"<<endl;
266//                              cout << ".......... Traitement du fichier "<<(*itt).chemin<<"    "<<  (*itt).nom <<endl;
267                                //cout << ".......... Traitement du fichier : "<<"    "<<  (*itt).nom <<endl;
268                                readFile((*itt));
269                                //cout << ".......... fin Traitement du fichier : "<<endl;
270                        }
271                }
272                //cout<< " taille rep="<<rep.fichier.size()<<endl;
273        }
274               
275        if (rep.children.empty() && rep.fichier.empty())
276        {
277                //logger->addReadersMessagesErrors("Le repertoire : "+rep.chemin+"est vide");
278                //printf("--- Le repertoire : %s est vide \n", rep.chemin);
279                cout<<"--- Le repertoire : "<<rep.chemin<<" est vide"<<endl;
280        }
281}
282
283void Produit::deleteProduct(string idProduct)
284{
285        PGconn* conn = base.connectBD(logger);
286        string sql = "delete from grille where id_produit ="+idProduct+";";
287        ////cout << sql << endl;
288       
289        PGresult* res = PQexec(conn, sql.c_str());
290        if ( PQresultStatus(res) != PGRES_TUPLES_OK) 
291        {
292                logger->traitementBaseMessages(" DELETE GRILLE " , res);
293                ////cout<<"delete"<<endl;
294        }
295       
296        sql = "delete from fichier where id_produit ="+idProduct+";";
297        ////cout << sql << endl;
298       
299        res = PQexec(conn, sql.c_str());
300        if ( PQresultStatus(res) != PGRES_TUPLES_OK) 
301        {
302                logger->traitementBaseMessages(" DELETE FICHIER " , res);
303                ////cout<<"delete fichier"<<endl;
304        }
305        PQfinish(conn); 
306}
307
308bool Produit::chercheFichierRep(Rowfichier fichier, Repertoire rep)
309{
310        rep = scanRep(rep);
311        ////cout<<"repchercheFichierRep  "<<rep.nom<<endl;
312        bool trouve = false;
313        if (!rep.children.empty())
314        {
315                ////cout<<"children"<<endl;
316                list<Repertoire>::iterator itRep;
317                for (itRep = rep.children.begin(); itRep != rep.children.end(); itRep++)
318                {
319                        Repertoire reper = scanRep((*itRep));
320                        trouve = chercheFichierRep(fichier, reper);
321                }
322        }
323        if (trouve) return trouve;
324        else
325        {
326                list<Feuille>::iterator itt;
327                ////cout<<"taille "<<rep.fichier.size()<<endl;
328                ////cout<<"nom rep "<<rep.chemin<<endl;
329                //if (rep.fichier.empty()) return trouve;
330                for (itt = rep.fichier.begin(); itt != rep.fichier.end(); itt++)
331                {
332                        /*if (fichier.idProduit == base.produit.getTuple(NomProduit)->id)
333                        {*/
334                        ////cout <<(*itt).nom<<"      "<<fichier.name<<endl;
335                        if(  ( fichier.idProduit == base.produit.getTuple(NomProduit)->id )  && ( !strcmp((*itt).nom, (fichier.name).c_str()) ) ) 
336                        {
337                                ////cout <<(*itt).nom<<"   **   "<<fichier.name<<endl;
338                                trouve = true;
339                                return trouve;
340                                break;
341                        }
342                        //}
343                }
344        }
345        return trouve;
346       
347}
348
349
350
351bool Produit::fichierAbst(Tabfichier tabFichier, Repertoire rep)
352{
353
354        //cout << "fonction  fichierAbst " <<rep.nom <<endl;
355        bool absent; 
356        //cout<<"tabFichier"<<tabFichier.tuples.size()<<endl;
357        //cout<<"rep"<<rep.fichier.size()<<endl;
358        if ( !tabFichier.tuples.empty() )
359        { 
360                absent = false;
361                rep = scanRep(rep);
362               
363                list<Rowfichier>::iterator it;
364               
365                for (it = tabFichier.tuples.begin() ; it != tabFichier.tuples.end(); it++){
366                        if ((*it).idProduit == base.produit.getTuple(NomProduit)->id){
367                                if(!chercheFichierRep((*it), rep)){
368                                        absent = true;
369                                        break;
370                                }
371                        }
372                }
373        }
374        else absent=false;
375       
376        return absent;
377       
378}
379
380void Produit::readFile(Feuille f)
381{
382}
383
384void Produit::verifMinMax(float min, float max)
385{
386        Base baseVerif;
387        baseVerif.readBD(logger);
388       
389        float minBase = atof(  (baseVerif.parametre.getTuple(NomParametre)->minCalculate).c_str()  );
390        float maxBase = atof(  (baseVerif.parametre.getTuple(NomParametre)->maxCalculate).c_str()  );
391//      cout<<"minbase="<<minBase<<", min="<<min<<", maxbase="<<maxBase<<", max="<<max <<endl;
392
393        if (min < minBase)
394        {
395                PGconn* conn = base.connectBD(logger);
396                string sql = "update parametre set min_param_calculate ="+ ftoa(min) +" where id_param="+ base.parametre.getTuple(NomParametre)->id +";";
397                ////cout<<"sql "<<sql<<endl;
398                PGresult* res = PQexec(conn, sql.c_str());
399                PQfinish(conn);
400               
401                /*if ( PQresultStatus(res) != PGRES_TUPLES_OK) 
402                {
403                        logger->traitementBaseMessages(" UPDATE PARAM MIN " , res);
404                }*/
405        }
406       
407        if (max > maxBase)
408        {
409                PGconn* conn = base.connectBD(logger);
410                string sql = "update parametre set max_param_calculate ="+ ftoa(max) +" where id_param="+ base.parametre.getTuple(NomParametre)->id +";";
411                ////cout<<"sql "<<sql<<endl;
412                ////cout<<"maxBase "<<maxBase<<endl;
413                PGresult* res = PQexec(conn, sql.c_str());
414                PQfinish(conn);
415               
416                /*if ( PQresultStatus(res) != PGRES_TUPLES_OK) 
417                {
418                        logger->traitementBaseMessages(" UPDATE PARAM MAX " , res);
419                }*/
420        }
421}
Note: See TracBrowser for help on using the repository browser.