node * ReadNetwork(char * inputPath){ FILE * fp; char line[MAX_LINE_SIZE]; int tail, head, role; node * network; fp = fopen(inputPath, "r"); if(fp == NULL){ printf("Error opening file: %s\n", strerror(errno)); return NULL; } numberOfNodes = FindNumberOfNodes(inputPath); ///////// Para debug!!! if(INPUT_LIMIT < numberOfNodes) numberOfNodes = INPUT_LIMIT; ///////// Para debug!!! network = CreateNetwork(numberOfNodes); while(fgets(line, MAX_LINE_SIZE, fp) != NULL){ if(sscanf(line, "%d %d %d", &tail, &head, &role) == 3){ ///////// Para debug!!! if(tail<INPUT_LIMIT && head<INPUT_LIMIT) ///////// Para debug!!! AddEdge(network, tail, head, role); } else printf("Line with invalid or few arguments\n"); } GetProgressThresholds(); fclose(fp); return network; };
int main(int argc, char * argv[]){ if(argc < 3){ fprintf(stderr,"Usage: %s <Network File> <Prefix File>\n",argv[0]); exit(-1); } srand(time(NULL)); CreateNetwork(argv[1]); ReadPrefixFile(argv[2]); RouteDetectGlobal(); AnalyzePartitions(); TestAllAgg(); PrintPrefixes("prefixes_with_agg.txt"); DestroyNetwork(); return 0; }
void MyFrame::BuildNetwork(wxString & table, wxString & from, wxString & to, wxString & geometry, wxString & name, bool cost_length, wxString & cost, bool bidirectional, bool one_way, wxString & one_way_from_to, wxString & one_way_to_from, bool aStarSupported) { // // trying to build a Network // int ret; sqlite3_stmt *stmt; Network *p_graph = NULL; wxString sql; char xsql[2048]; char **results; int n_rows; int n_columns; int i; char *errMsg = NULL; char *col_name; int type; bool ok_from_column = false; bool ok_to_column = false; bool ok_cost_column = false; bool ok_geom_column = false; bool ok_name_column = false; bool ok_oneway_tofrom = false; bool ok_oneway_fromto = false; bool from_null = false; bool from_int = false; bool from_double = false; bool from_text = false; bool from_blob = false; bool to_null = false; bool to_int = false; bool to_double = false; bool to_text = false; bool to_blob = false; bool cost_null = false; bool cost_text = false; bool cost_blob = false; bool tofrom_null = false; bool tofrom_double = false; bool tofrom_text = false; bool tofrom_blob = false; bool fromto_null = false; bool fromto_double = false; bool fromto_text = false; bool fromto_blob = false; bool geom_null = false; bool geom_not_linestring = false; int col_n; int fromto_n = 0; int tofrom_n = 0; sqlite3_int64 rowid; sqlite3_int64 id_from = -1; sqlite3_int64 id_to = -1; char code_from[1024]; char code_to[1024]; double node_from_x; double node_from_y; double node_to_x; double node_to_y; double cost_val; int fromto; int tofrom; wxString endMsg; wxString msg; bool wr; bool aStarLength; double a_star_length; double a_star_coeff; double min_a_star_coeff = DBL_MAX; char xname[1024]; ::wxBeginBusyCursor(); // checking for table existence sql = wxT("SELECT tbl_name FROM sqlite_master WHERE Lower(tbl_name) = Lower('"); strcpy(xname, table.ToUTF8()); CleanSqlString(xname); sql += wxString::FromUTF8(xname); sql += wxT("') AND type = 'table'"); strcpy(xsql, sql.ToUTF8()); ret = sqlite3_get_table(SqliteHandle, xsql, &results, &n_rows, &n_columns, &errMsg); if (ret != SQLITE_OK) { // some error occurred wxMessageBox(wxT("SQLite SQL error: ") + wxString::FromUTF8(errMsg), wxT("spatialite_gui"), wxOK | wxICON_ERROR, this); sqlite3_free(errMsg); goto abort; } if (n_rows == 0) { // required table does not exists wxMessageBox(wxT("ERROR: table ") + table + wxT(" does not exists"), wxT("spatialite_gui"), wxOK | wxICON_ERROR, this); goto abort; } else sqlite3_free_table(results); // checking for columns existence sql = wxT("PRAGMA table_info("); strcpy(xname, table.ToUTF8()); DoubleQuotedSql(xname); sql += wxString::FromUTF8(xname); sql += wxT(")"); strcpy(xsql, sql.ToUTF8()); ret = sqlite3_get_table(SqliteHandle, xsql, &results, &n_rows, &n_columns, &errMsg); if (ret != SQLITE_OK) { // some error occurred wxMessageBox(wxT("SQLite SQL error: ") + wxString::FromUTF8(errMsg), wxT("spatialite_gui"), wxOK | wxICON_ERROR, this); sqlite3_free(errMsg); goto abort; } if (n_rows > 1) { for (i = 1; i <= n_rows; i++) { char xcol[256]; col_name = results[(i * n_columns) + 1]; strcpy(xcol, from.ToUTF8()); if (strcasecmp(xcol, col_name) == 0) ok_from_column = true; strcpy(xcol, to.ToUTF8()); if (strcasecmp(xcol, col_name) == 0) ok_to_column = true; if (cost_length == false) { strcpy(xcol, cost.ToUTF8()); if (strcasecmp(xcol, col_name) == 0) ok_cost_column = true; } strcpy(xcol, geometry.ToUTF8()); if (strcasecmp(xcol, col_name) == 0) ok_geom_column = true; if (name.Len() > 0) { strcpy(xcol, name.ToUTF8()); if (strcasecmp(xcol, col_name) == 0) ok_name_column = true; } if (one_way == true) { strcpy(xcol, one_way_from_to.ToUTF8()); if (strcasecmp(xcol, col_name) == 0) ok_oneway_tofrom = true; } if (one_way == true) { strcpy(xcol, one_way_to_from.ToUTF8()); if (strcasecmp(xcol, col_name) == 0) ok_oneway_fromto = true; } } sqlite3_free_table(results); } if (ok_from_column == true && ok_to_column == true && ok_geom_column == true) ; else goto abort; if (name.Len() > 0 && ok_name_column == false) goto abort; if (cost_length == false && ok_cost_column == false) goto abort; if (one_way == true && ok_oneway_tofrom == false) goto abort; if (one_way == true && ok_oneway_fromto == false) goto abort; // checking column types p_graph = new Network(); strcpy(xname, from.ToUTF8()); DoubleQuotedSql(xname); sql = wxT("SELECT ") + wxString::FromUTF8(xname); strcpy(xname, to.ToUTF8()); DoubleQuotedSql(xname); sql += wxT(", ") + wxString::FromUTF8(xname); strcpy(xname, geometry.ToUTF8()); DoubleQuotedSql(xname); sql += wxT(", GeometryType(") + wxString::FromUTF8(xname) + wxT(")"); col_n = 3; if (cost_length == false) { strcpy(xname, cost.ToUTF8()); DoubleQuotedSql(xname); sql += wxT(", ") + wxString::FromUTF8(xname); col_n++; } if (one_way == true) { strcpy(xname, one_way_to_from.ToUTF8()); DoubleQuotedSql(xname); sql += wxT(", ") + wxString::FromUTF8(xname); tofrom_n = col_n; col_n++; strcpy(xname, one_way_from_to.ToUTF8()); DoubleQuotedSql(xname); sql += wxT(", ") + wxString::FromUTF8(xname); fromto_n = col_n; col_n++; } strcpy(xname, table.ToUTF8()); DoubleQuotedSql(xname); sql += wxT(" FROM ") + wxString::FromUTF8(xname); strcpy(xsql, sql.ToUTF8()); ret = sqlite3_prepare_v2(SqliteHandle, xsql, strlen(xsql), &stmt, NULL); if (ret != SQLITE_OK) { wxString err = wxString::FromUTF8(sqlite3_errmsg(SqliteHandle)); wxMessageBox(wxT("SQL error: ") + err, wxT("spatialite_gui"), wxOK | wxICON_ERROR, this); goto abort; } n_columns = sqlite3_column_count(stmt); while (1) { ret = sqlite3_step(stmt); if (ret == SQLITE_DONE) break; if (ret == SQLITE_ROW) { // the NodeFrom type type = sqlite3_column_type(stmt, 0); if (type == SQLITE_NULL) from_null = true; if (type == SQLITE_INTEGER) { from_int = true; id_from = sqlite3_column_int(stmt, 0); p_graph->InsertNode(id_from); } if (type == SQLITE_FLOAT) from_double = true; if (type == SQLITE_TEXT) { from_text = true; strcpy(code_from, (char *) sqlite3_column_text(stmt, 0)); p_graph->InsertNode(code_from); } if (type == SQLITE_BLOB) from_blob = true; // the NodeTo type type = sqlite3_column_type(stmt, 1); if (type == SQLITE_NULL) to_null = true; if (type == SQLITE_INTEGER) { to_int = true; id_to = sqlite3_column_int(stmt, 1); p_graph->InsertNode(id_to); } if (type == SQLITE_FLOAT) to_double = true; if (type == SQLITE_TEXT) { to_text = true; strcpy(code_to, (char *) sqlite3_column_text(stmt, 1)); p_graph->InsertNode(code_to); } if (type == SQLITE_BLOB) to_blob = true; // the Geometry type type = sqlite3_column_type(stmt, 2); if (type == SQLITE_NULL) geom_null = true; else if (strcmp("LINESTRING", (char *) sqlite3_column_text(stmt, 2)) != 0) geom_not_linestring = true; col_n = 3; if (cost_length == false) { // the Cost type type = sqlite3_column_type(stmt, col_n); col_n++; if (type == SQLITE_NULL) cost_null = true; if (type == SQLITE_TEXT) cost_text = true; if (type == SQLITE_BLOB) cost_blob = true; } if (one_way == true) { // the FromTo type type = sqlite3_column_type(stmt, col_n); col_n++; if (type == SQLITE_NULL) fromto_null = true; if (type == SQLITE_FLOAT) fromto_double = true; if (type == SQLITE_TEXT) fromto_text = true; if (type == SQLITE_BLOB) fromto_blob = true; // the ToFrom type type = sqlite3_column_type(stmt, col_n); col_n++; if (type == SQLITE_NULL) tofrom_null = true; if (type == SQLITE_FLOAT) tofrom_double = true; if (type == SQLITE_TEXT) tofrom_text = true; if (type == SQLITE_BLOB) tofrom_blob = true; } } else { wxString err = wxString::FromUTF8(sqlite3_errmsg(SqliteHandle)); wxMessageBox(wxT("sqlite3_step error: ") + err, wxT("spatialite_gui"), wxOK | wxICON_ERROR, this); sqlite3_finalize(stmt); goto abort; } } sqlite3_finalize(stmt); ret = 1; if (from_null == true) ret = 0; if (from_blob == true) ret = 0; if (from_double == true) ret = 0; if (to_null == true) ret = 0; if (to_blob == true) ret = 0; if (to_double == true) ret = 0; if (geom_null == true) ret = 0; if (geom_not_linestring == true) ret = 0; if (cost_length == false) { if (cost_null == true) ret = 0; if (cost_blob == true) ret = 0; if (cost_text == true) ret = 0; } if (one_way == true) { if (fromto_null == true) ret = 0; if (fromto_blob == true) ret = 0; if (fromto_text == true) ret = 0; if (fromto_double == true) ret = 0; if (tofrom_null == true) ret = 0; if (tofrom_blob == true) ret = 0; if (tofrom_text == true) ret = 0; if (tofrom_double == true) ret = 0; } if (!ret) goto abort; if (from_int == true && to_int == true) { // each node is identified by an INTEGER id p_graph->SetNodeCode(false); } else if (from_text == true && to_text == true) { // each node is identified by a TEXT code p_graph->SetNodeCode(true); } else goto abort; p_graph->InitNodes(); // checking topologic consistency strcpy(xname, from.ToUTF8()); DoubleQuotedSql(xname); sql = wxT("SELECT ROWID, ") + wxString::FromUTF8(xname); strcpy(xname, to.ToUTF8()); DoubleQuotedSql(xname); sql += wxT(", ") + wxString::FromUTF8(xname) + wxT(", "); strcpy(xname, geometry.ToUTF8()); DoubleQuotedSql(xname); sql += wxT("X(StartPoint(") + wxString::FromUTF8(xname) + wxT(")), Y(StartPoint("); sql += wxString::FromUTF8(xname) + wxT(")), "); sql += wxT("X(EndPoint(") + wxString::FromUTF8(xname) + wxT(")), Y(EndPoint("); sql += wxString::FromUTF8(xname) + wxT("))"); if (aStarSupported == true) { // supporting A* algorithm if (cost_length == false) { strcpy(xname, cost.ToUTF8()); DoubleQuotedSql(xname); sql += wxT(", ") + wxString::FromUTF8(xname); strcpy(xname, geometry.ToUTF8()); DoubleQuotedSql(xname); sql += wxT(", GLength(") + wxString::FromUTF8(xname) + wxT(")"); col_n = 9; aStarLength = true; } else { strcpy(xname, geometry.ToUTF8()); DoubleQuotedSql(xname); sql += wxT(", GLength(") + wxString::FromUTF8(xname) + wxT(")"); col_n = 8; aStarLength = false; min_a_star_coeff = 1.0; } } else { // A* algorithm unsupported if (cost_length == false) { strcpy(xname, cost.ToUTF8()); DoubleQuotedSql(xname); sql += wxT(", ") + wxString::FromUTF8(xname); } else { strcpy(xname, geometry.ToUTF8()); DoubleQuotedSql(xname); sql += wxT(", GLength(") + wxString::FromUTF8(xname) + wxT(")"); } col_n = 8; aStarLength = false; } if (one_way == true) { strcpy(xname, one_way_to_from.ToUTF8()); DoubleQuotedSql(xname); sql += wxT(", ") + wxString::FromUTF8(xname); tofrom_n = col_n; col_n++; strcpy(xname, one_way_from_to.ToUTF8()); DoubleQuotedSql(xname); sql += wxT(", ") + wxString::FromUTF8(xname); fromto_n = col_n; col_n++; } strcpy(xname, table.ToUTF8()); DoubleQuotedSql(xname); sql += wxT(" FROM ") + wxString::FromUTF8(xname); strcpy(xsql, sql.ToUTF8()); ret = sqlite3_prepare_v2(SqliteHandle, xsql, strlen(xsql), &stmt, NULL); if (ret != SQLITE_OK) { wxString err = wxString::FromUTF8(sqlite3_errmsg(SqliteHandle)); wxMessageBox(wxT("SQL error: ") + err, wxT("spatialite_gui"), wxOK | wxICON_ERROR, this); goto abort; } n_columns = sqlite3_column_count(stmt); while (1) { ret = sqlite3_step(stmt); if (ret == SQLITE_DONE) break; if (ret == SQLITE_ROW) { fromto = true; tofrom = true; if (p_graph->IsNodeCode() == true) { id_from = -1; id_to = -1; } else { *code_from = '\0'; *code_to = '\0'; } // fetching the ROWID rowid = sqlite3_column_int64(stmt, 0); // fetching the NodeFrom value if (p_graph->IsNodeCode() == true) strcpy(code_from, (char *) sqlite3_column_text(stmt, 1)); else id_from = sqlite3_column_int64(stmt, 1); // fetching the NodeTo value if (p_graph->IsNodeCode() == true) strcpy(code_to, (char *) sqlite3_column_text(stmt, 2)); else id_to = sqlite3_column_int64(stmt, 2); // fetching the NodeFromX value node_from_x = sqlite3_column_double(stmt, 3); // fetching the NodeFromY value node_from_y = sqlite3_column_double(stmt, 4); // fetching the NodeFromX value node_to_x = sqlite3_column_double(stmt, 5); // fetching the NodeFromY value node_to_y = sqlite3_column_double(stmt, 6); // fetching the Cost value cost_val = sqlite3_column_double(stmt, 7); if (one_way == true) { // fetching the OneWay-FromTo value fromto = sqlite3_column_int(stmt, fromto_n); // fetching the OneWay-ToFrom value tofrom = sqlite3_column_int(stmt, tofrom_n); } if (cost_val <= 0.0) p_graph->SetError(); if (aStarLength == true) { // supporting A* - fetching the arc length a_star_length = sqlite3_column_double(stmt, 8); a_star_coeff = cost_val / a_star_length; if (a_star_coeff < min_a_star_coeff) min_a_star_coeff = a_star_coeff; } if (one_way == true) { // fetching the OneWay-FromTo value fromto = sqlite3_column_int(stmt, fromto_n); // fetching the OneWay-ToFrom value tofrom = sqlite3_column_int(stmt, tofrom_n); } if (bidirectional == true) { if (fromto) { if (p_graph->IsNodeCode() == true) p_graph->AddArc(rowid, code_from, code_to, node_from_x, node_from_y, node_to_x, node_to_y, cost_val); else p_graph->AddArc(rowid, id_from, id_to, node_from_x, node_from_y, node_to_x, node_to_y, cost_val); } if (tofrom) { if (p_graph->IsNodeCode() == true) p_graph->AddArc(rowid, code_to, code_from, node_to_x, node_to_y, node_from_x, node_from_y, cost_val); else p_graph->AddArc(rowid, id_to, id_from, node_to_x, node_to_y, node_from_x, node_from_y, cost_val); } } else { if (p_graph->IsNodeCode() == true) p_graph->AddArc(rowid, code_from, code_to, node_from_x, node_from_y, node_to_x, node_to_y, cost_val); else p_graph->AddArc(rowid, id_from, id_to, node_from_x, node_from_y, node_to_x, node_to_y, cost_val); } if (p_graph->IsError() == true) { sqlite3_finalize(stmt); goto abort; } } else { wxString err = wxString::FromUTF8(sqlite3_errmsg(SqliteHandle)); wxMessageBox(wxT("sqlite3_step error: ") + err, wxT("spatialite_gui"), wxOK | wxICON_ERROR, this); sqlite3_finalize(stmt); goto abort; } } sqlite3_finalize(stmt); ::wxEndBusyCursor(); wr = CreateNetwork(p_graph, table, from, to, geometry, name, aStarSupported, min_a_star_coeff); if (wr == true) { endMsg = wxT("OK: VirtualNetwork table '") + table + wxT("_net' successfully created"); wxMessageBox(endMsg, wxT("spatialite_gui"), wxOK | wxICON_INFORMATION, this); } else { endMsg = wxT("DB ERROR: VirtualNetwork table '") + table + wxT("_net' was not created"); wxMessageBox(endMsg, wxT("spatialite_gui"), wxOK | wxICON_ERROR, this); } if (p_graph) delete p_graph; InitTableTree(); return; abort: ::wxEndBusyCursor(); msg = wxT ("It's impossible to build a Network using the given configuration;\nsome fatal error occurred\n\n"); msg += wxT("please note: using the 'spatialite_network' command-line tool\n"); msg += wxT ("you can obtain a full detailed report explaining causes for this failure"); wxMessageBox(msg, wxT("spatialite_gui"), wxOK | wxICON_ERROR, this); if (p_graph) delete p_graph; }