database.cc 8.24 KB
Newer Older
jben's avatar
jben committed
1 2 3 4 5
#include "database.h"
#include<iostream>

node database::get_node(const unsigned long long & ull_id)
{
jben's avatar
jben committed
6
    pqxx::result r=txn.prepared("get_node")(ull_id).exec();
jben's avatar
jben committed
7 8 9

    if(r.size()==0)
    {
jben's avatar
jben committed
10 11
        fprintf(stdout,"\n# fatal error: missing node %llu\n",ull_id);
        fflush(stdout);
jben's avatar
jben committed
12
        write_state(21);
jben's avatar
jben committed
13 14 15 16 17 18 19 20 21 22 23 24
        abort();
    }


    double d_lon    = r[0][0].as<double>();
    double d_lat    = r[0][1].as<double>();
    double d_height = r[0][2].as<double>();

    node n_result(ull_id,d_lon,d_lat,d_height);
    return(n_result);
}

jben's avatar
jben committed
25
unsigned int database::get_state()
jben's avatar
jben committed
26 27 28 29 30 31 32 33 34 35
{
    char z_req[1024];

    snprintf(z_req,1024,"SELECT state FROM rv_jobs WHERE jid=%u;",jid);

    pqxx::result res=txn.exec(z_req);

    return(res[0][0].as<unsigned int>());
}

jben's avatar
jben committed
36

jben's avatar
jben committed
37
void database::write_state(unsigned int state)
jben's avatar
jben committed
38 39 40 41 42 43 44 45
{
    pqxx::work txnw(dbw);

    char z_req[1024];
    snprintf(z_req,1024,"UPDATE rv_jobs SET state=%u WHERE jid=%u;",state,jid);
    txnw.exec(z_req);
    txnw.commit();
}
jben's avatar
jben committed
46

jben's avatar
jben committed
47 48 49 50 51 52 53 54 55 56 57
void database::write_pid()
{
    pqxx::work txnw(dbw);

    char z_req[1024];
    snprintf(z_req,1024,"UPDATE rv_jobs SET pid=%u WHERE jid=%u;",getpid(),jid);
    txnw.exec(z_req);
    txnw.commit();
}

void database::write_status(double status)
jben's avatar
jben committed
58
{
jben's avatar
jben committed
59 60 61 62 63 64 65 66
    pqxx::work txnw(dbw);

    char z_req[1024];
    snprintf(z_req,1024,"UPDATE rv_jobs SET status=%f WHERE jid=%u;",status,jid);
    txnw.exec(z_req);
    txnw.commit();
}

jben's avatar
jben committed
67
void database::write_results(std::list<instant> lI, double puissance)
jben's avatar
jben committed
68 69 70 71 72 73 74 75
{
    pqxx::work txnw(dbw);

    unsigned int rank=0;

    char z_req[1024];
    for(std::list<instant>::iterator it=lI.begin(); it!=lI.end(); it++)
    {
76
        snprintf(z_req,1024,"INSERT INTO rv_results (jid,rank,height,speed,dist,cum_elev,energy,time,node_id,way_id,geom) VALUES (%u,%u,%f,%f,%f,%f,%f,%f,%u,%u,'SRID=4326;POINT(%.8f %.8f)'::geometry);",
jben's avatar
jben committed
77 78 79 80 81 82 83 84
                jid,
                rank++,
                it->d_height,
                it->d_V,
                it->d_dist,
                it->d_deniv_pos,
                it->d_ET,
                it->d_ET/puissance,
85 86
                it->ull_id,
                it->ull_way_from_id,
jben's avatar
jben committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
                it->d_lon,
                it->d_lat);
        txnw.exec(z_req);
    }

    instant If = *(lI.rbegin());

    snprintf(z_req,1024,"UPDATE rv_jobs SET (cum_elev,energy,dist,mean_speed,time)=(%f,%f,%f,%f,%f) WHERE jid=%u;",
            If.d_deniv_pos,
            If.d_ET,
            If.d_dist,
            If.d_dist*puissance/If.d_ET,
            If.d_ET/puissance,
            jid);

    txnw.exec(z_req);

    txnw.commit();
}



jben's avatar
jben committed
109
bool database::find_nodes(std::list<unsigned long long> & lull_id)
jben's avatar
jben committed
110 111 112 113 114 115 116 117 118 119 120 121
{
    char z_req[1024];
    snprintf(z_req,1024,"SELECT ST_X(geom),ST_Y(geom) FROM rv_waypoints WHERE jid=%u ORDER BY rank;",jid);

    pqxx::result res = txn.exec(z_req);

    std::list< std::pair<double,double> > l_points;

    for(pqxx::result::iterator it=res.begin(); it!=res.end(); it++)
    {
        l_points.push_back(std::pair<double,double>(it[0].as<double>(),it[1].as<double>()));
    }
jben's avatar
jben committed
122 123 124 125 126 127 128 129
    
    // On trouve la cc
    unsigned long long ull_begin, ull_end;
    unsigned long long ull_begin_cc, ull_end_cc;
    double dist_begin, dist_end;
    unsigned long long ull_cc;

    if(!find_one_node(*(l_points.begin()),false,0,ull_begin,ull_begin_cc,dist_begin))
jben's avatar
jben committed
130
        return false;
jben's avatar
jben committed
131
    if(!find_one_node(*(l_points.rbegin()),false,0,ull_end,ull_end_cc,dist_end))
jben's avatar
jben committed
132
        return false;
jben's avatar
jben committed
133

jben's avatar
jben committed
134
    if(ull_begin_cc==ull_end_cc)
jben's avatar
jben committed
135
    {
jben's avatar
jben committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
        ull_cc=ull_begin_cc;
    }
    else
    {
        unsigned long long ull_begin_oth, ull_end_oth;
        double dist_begin_oth, dist_end_oth;
        unsigned long long ull_cc_poubelle;
        bool b1, b2;

        b1=find_one_node(*(l_points.begin()),true,ull_end_cc,ull_begin_oth,ull_cc_poubelle,dist_begin_oth);
        b2=find_one_node(*(l_points.rbegin()),true,ull_begin_cc,ull_end_oth,ull_cc_poubelle,dist_end_oth);

        if(b1)
        {
            if(b2)
            {
                if(dist_begin+dist_end_oth < dist_begin_oth+dist_end)
                {
                    ull_cc = ull_begin_cc;
                    ull_end = ull_end_oth;
                }
                else
                {
                    ull_cc = ull_end_cc;
                    ull_begin = ull_begin_oth;
                }
            }
            else
            {
                ull_cc = ull_end_cc;
                ull_begin = ull_begin_oth;
            }
        }
        else
        {
            if(b2)
            {
                ull_cc = ull_begin_cc;
                ull_end = ull_end_oth;
            }
            else
            {
jben's avatar
jben committed
178
                return false;
jben's avatar
jben committed
179 180 181
            }
        }
    }
jben's avatar
jben committed
182

jben's avatar
jben committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196
    std::list< std::pair<double,double> >::iterator it=l_points.begin();
    while(it!=l_points.end())
    {
        unsigned long long id;

        std::pair<double,double> & P = *it;

        bool ok=false;

        if(it==l_points.begin())
        {
            id=ull_begin;
            ok=true;
        }
jben's avatar
jben committed
197
        
jben's avatar
jben committed
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
        it++;


        if(it==l_points.end())
        {
            id=ull_end;
            ok=true;
        }

        if(!ok)
        {
            double dist_poubelle;
            unsigned long long ull_cc_poubelle;

            if(!find_one_node(P,true,ull_cc,id,ull_cc_poubelle,dist_poubelle))
jben's avatar
jben committed
213
                return(false);
jben's avatar
jben committed
214
        }
jben's avatar
jben committed
215

jben's avatar
jben committed
216 217 218
        lull_id.push_back(id);
    }

jben's avatar
jben committed
219
    return(true);
jben's avatar
jben committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
}




bool database::find_one_node(std::pair<double,double> P, bool respect_cc, unsigned long long ull_cc, unsigned long long & ull_result_id, unsigned long long & ull_result_cc, double & dist)
{

    double diam = 0.013;
    double & d_lon = P.first;
    double & d_lat = P.second;

    
    char z_req[1024];

    if(respect_cc)
        snprintf(z_req,1024,"SELECT id,cc,ST_Distance(geom,'POINT(%.8f %.8f)'::geography) AS d FROM rv_nodes WHERE geom && ST_Expand('SRID=4326;POINT(%.8f %.8f)'::geometry,%f) AND cc=%llu ORDER BY d LIMIT 1;",d_lon,d_lat,d_lon,d_lat,diam,ull_cc);
    else
        snprintf(z_req,1024,"SELECT id,cc,ST_Distance(geom,'POINT(%.8f %.8f)'::geography) AS d FROM rv_nodes WHERE geom && ST_Expand('SRID=4326;POINT(%.8f %.8f)'::geometry,%f) ORDER BY d LIMIT 1;",d_lon,d_lat,d_lon,d_lat,diam);

    pqxx::result res = txn.exec(z_req);

    if(res.size()>0)
    {
        ull_result_id = res[0][0].as<unsigned long long>();
        ull_result_cc = res[0][1].as<unsigned long long>();
        dist = res[0][2].as<double>();
        return true;
    }
        
    return false;
        
jben's avatar
jben committed
252 253
}

jben's avatar
jben committed
254
model database::get_model()
jben's avatar
jben committed
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
{
    char z_req[1024];
    
    snprintf(z_req,1024,"SELECT criterion,speedref,mass,cr,scx,rho,winddir,windspeed FROM rv_jobs WHERE jid=%u LIMIT 1;",jid);

    pqxx::result res = txn.exec(z_req);

    if(res.size()==0)
    {
        fprintf(stderr,"jid %u not found\n",jid);
        abort();
    }

    char c_model_type=(res[0][0].c_str())[0];
    double d_VplatKMH=res[0][1].as<double>();
    double d_masse=res[0][2].as<double>();
    double d_Cr=res[0][3].as<double>();
    double d_SCx=res[0][4].as<double>();
    double d_rho_air=res[0][5].as<double>();
    double d_vent_from_deg=res[0][6].as<double>();
    double d_vent_VKMH=res[0][7].as<double>();

    return model(c_model_type,d_VplatKMH,d_masse,d_Cr,d_SCx,d_rho_air,d_vent_from_deg,d_vent_VKMH);
}
        



jben's avatar
jben committed
283

jben's avatar
jben committed
284
std::pair<unsigned long long, unsigned int> database::get_edge(const unsigned long long & ull_from, const unsigned long long & ull_to)
jben's avatar
jben committed
285 286
{
    char z_req[1024];
jben's avatar
jben committed
287
    snprintf(z_req,1024,"SELECT way_id,version FROM rv_edges INNER JOIN ways ON ways.id=rv_edges.way_id WHERE from_id=%lld AND to_id=%lld;",ull_from,ull_to);
jben's avatar
jben committed
288 289 290 291

    pqxx::result r=txn.exec(z_req);
    if(r.size()==0)
    {
jben's avatar
jben committed
292 293
        fprintf(stdout,"\n# fatal error: missing edge (%llu,%llu)\n",ull_from,ull_to);
        fflush(stdout);
jben's avatar
jben committed
294
        write_state(22);
jben's avatar
jben committed
295
        abort();
jben's avatar
jben committed
296

jben's avatar
jben committed
297 298 299 300
    }


    unsigned long long ull_id_way    = r[0][0].as<unsigned long long>();
jben's avatar
jben committed
301
    unsigned int ui_rev    = r[0][1].as<unsigned int>();
jben's avatar
jben committed
302

jben's avatar
jben committed
303
    return(std::pair<unsigned long long, unsigned int>(ull_id_way,ui_rev));
jben's avatar
jben committed
304 305 306 307
}

std::list<unsigned long long> database::get_adjoining(const unsigned long long & ull_id)
{
jben's avatar
jben committed
308 309
    pqxx::result r=txn.prepared("get_adj")(ull_id).exec();
    
jben's avatar
jben committed
310 311 312 313 314 315 316 317 318 319 320 321 322
    std::list<unsigned long long> lull_adj;

    for(pqxx::result::iterator it=r.begin();it!=r.end();it++)
    {
        lull_adj.push_back(it[0].as<unsigned long long>());
    }
    
    return(lull_adj);
}