BOOL CSensorNetworkDataSettingDlg::ReadNetworkDataCSVFile(const char* ElementType, std::vector<std::string>& name_vector,std::vector<std::vector<std::string>>& value_vector) { std::string fileName = m_pDoc->m_ProjectDirectory + ElementType + ".csv"; std::vector<std::string> value; CCSVParser csvParser; csvParser.OpenCSVFile(fileName); name_vector = csvParser.GetHeaderList(); while(csvParser.ReadRecord()) { value_vector.push_back(csvParser.GetLineRecord()); } csvParser.CloseCSVFile(); return TRUE; }
bool CTLiteDoc::ReadGPSCSVFile(LPCTSTR lpszFileName) { // save the original code //CCSVParser parser; //if (parser.OpenCSVFile(lpszFileName)) //{ // m_VehicleSet.clear(); // string trajecory_id = "-1"; // string prev_trajecory_id = "0"; // float x1, y1, x2, y2; // float SegmentTravelTime; // DTAVehicle* pVehicle = 0; // while(parser.ReadRecord()) // { // if(parser.GetValueByFieldName("trajecory_id",trajecory_id) == false) // { // break; // } // GDPoint pt1; // GDPoint pt2; // parser.GetValueByFieldName("x1",pt1.x); // parser.GetValueByFieldName("y1",pt1.y); // parser.GetValueByFieldName("x2",pt2.x); // parser.GetValueByFieldName("y2",pt2.y); // if(prev_trajecory_id.compare(trajecory_id)!=0) // { // // we have a new vehicle // pVehicle = new DTAVehicle; // pVehicle->m_VehicleID = m_VehicleSet.size(); // we skip vehicle id // pVehicle-> m_VehicleKey = trajecory_id; // // m_VehicleSet.push_back (pVehicle); // m_VehicleIDMap[pVehicle->m_VehicleID] = pVehicle; // // } // pVehicle->m_GPSLocationVector.push_back(pt1); // pVehicle->m_GPSLocationVector.push_back(pt2); // prev_trajecory_id = trajecory_id; // } //} // return true; // save the original code CCSVParser parser; if (parser.OpenCSVFile(lpszFileName)) { m_VehicleSet.clear(); float TrajecoryTravelTime; DTAVehicle* pVehicle = 0; std::vector<string> SeperatedStrings; while(parser.ReadRecord()) { SeperatedStrings=parser.GetLineRecord(); TrajecoryTravelTime=::atof(SeperatedStrings[4].c_str()); pVehicle = new DTAVehicle; m_VehicleSet.push_back(pVehicle); m_VehicleIDMap[pVehicle->m_VehicleID] = pVehicle; for(int i=6;i<(SeperatedStrings.size()-6);i=i+6)// read the data every 6 numbers { if((SeperatedStrings[i]!="")&&(SeperatedStrings[i+1]!="")&&(SeperatedStrings[i+2]!="")&&(SeperatedStrings[i+3]!="")) { GDPoint pt1; GDPoint pt2; pt1.x=::atof(SeperatedStrings[i].c_str()); pt1.y=::atof(SeperatedStrings[i+1].c_str()); pt2.x=::atof(SeperatedStrings[i+2].c_str()); pt2.y=::atof(SeperatedStrings[i+3].c_str()); pVehicle->m_GPSLocationVector.push_back(pt1); pVehicle->m_GPSLocationVector.push_back(pt2); } } } parser.CloseCSVFile(); return true; } return true; }
bool PT_Network::ReadGTFFiles(GDRect network_rect) // Google Transit files { // // step 1: read route files string str0 = m_ProjectDirectory +"routes.txt"; //string str = "h:/routes.csv"; CT2CA pszConvertedAnsiString (str0.c_str()); // construct a std::string using the LPCSTR input std::string strStd (pszConvertedAnsiString); CCSVParser parser; if (parser.OpenCSVFile(strStd)) { // AfxMessageBox("Start reading Google Transit Feed files...", MB_ICONINFORMATION); int count =0; PT_Route route; while(parser.ReadRecord()) { if(parser.GetValueByFieldName("route_id",route.route_id ) == false) break; if(parser.GetValueByFieldName("route_long_name",route.route_long_name) == false) route.route_long_name=""; if(parser.GetValueByFieldName("route_short_name",route.route_short_name) == false) route.route_short_name=""; if(parser.GetValueByFieldName("route_url",route.route_url) == false) route.route_url=""; if(parser.GetValueByFieldName("route_type",route.route_type) == false) route.route_type=""; // make sure there is not duplicated key // If the requested key is not found, find() returns the end iterator for //the container, so: if(m_PT_RouteMap.find(route.route_id) == m_PT_RouteMap.end() ) { m_PT_RouteMap[route.route_id] = route; } else { AfxMessageBox("Duplicated Route ID!"); } count++; } parser.CloseCSVFile (); CMainFrame* pMainFrame = (CMainFrame*) AfxGetMainWnd(); pMainFrame->m_bShowLayerMap[layer_transit] = true; }else { return false; } CString missing_stops_message; // step 1: read stop information string str2 = m_ProjectDirectory +"stops.txt"; CT2CA pszConvertedAnsiString2 (str2.c_str()); // construct a std::string using the LPCSTR input std::string strStd2 (pszConvertedAnsiString2); if (parser.OpenCSVFile(strStd2)) { int count =0; while(parser.ReadRecord()) { PT_Stop stop; if(parser.GetValueByFieldName("stop_id",stop.stop_id ) == false) break; bool NonnegativeFlag = false; if(parser.GetValueByFieldName("stop_lat",stop.m_ShapePoint.y, NonnegativeFlag) == false) break; if(parser.GetValueByFieldName("stop_lon",stop.m_ShapePoint.x , NonnegativeFlag) == false) break; if(parser.GetValueByFieldName("direction",stop.direction ) == false) stop.direction=""; if(parser.GetValueByFieldName("location_type",stop.location_type) == false) stop.location_type=0; if(parser.GetValueByFieldName("position",stop.position ) == false) stop.position=""; if(parser.GetValueByFieldName("stop_code",stop.stop_code ) == false) stop.stop_code=0; parser.GetValueByFieldName("stop_desc",stop.stop_desc); if(parser.GetValueByFieldName("stop_name",stop.stop_name) == false) stop.stop_name=""; if(parser.GetValueByFieldName("zone_id",stop.zone_id) == false) stop.zone_id=0; if(m_PT_StopMap.find(stop.stop_id) == m_PT_StopMap.end()) { m_PT_StopMap[stop.stop_id] = stop; }else { CString msg; msg.Format("Duplicated Stop ID %d", stop.stop_id); AfxMessageBox(msg); } count++; } parser.CloseCSVFile (); } //read trip file string str3 = m_ProjectDirectory +"trips.txt"; CT2CA pszConvertedAnsiString3 (str3.c_str()); // construct a std::string using the LPCSTR input std::string strStd3 (pszConvertedAnsiString3); if (parser.OpenCSVFile(strStd3)) { int count =0; while(parser.ReadRecord()) { PT_Trip trip; if(parser.GetValueByFieldName("trip_id",trip.trip_id) == false) break; if(parser.GetValueByFieldName("route_id",trip.route_id) == false) break; if(parser.GetValueByFieldName("service_id",trip.service_id) == false) break; if(parser.GetValueByFieldName("block_id",trip.block_id) == false) trip.block_id=0; if(parser.GetValueByFieldName("direction_id",trip.direction_id) == false) trip.direction_id=0; if(parser.GetValueByFieldName("shape_id",trip.shape_id) == false) trip.shape_id=0; if(parser.GetValueByFieldName("trip_type",trip.trip_type) == false) trip.trip_type=0; if(m_PT_TripMap.find(trip.trip_id) == m_PT_TripMap.end()) { m_PT_TripMap[trip.trip_id] = trip; }else { AfxMessageBox("Duplicated Trip ID!"); } count++; } parser.CloseCSVFile (); } // read stop_times.txt int stop_times_count =0; int max_stop_times_record = 1000; string str4 = m_ProjectDirectory +"stop_times.txt"; CT2CA pszConvertedAnsiString4 (str4.c_str()); // construct a std::string using the LPCSTR input std::string strStd4 (pszConvertedAnsiString4); if (parser.OpenCSVFile(strStd4)) { while(parser.ReadRecord()) { PT_StopTime TransitStopTime; if(parser.GetValueByFieldName("stop_id",TransitStopTime.stop_id) == false) break; string time_string; char char_array[20]; if(parser.GetValueByFieldName("arrival_time",time_string) == false) break; int hour,min,second; sprintf(char_array,"%s",time_string.c_str ()); sscanf(char_array,"%d:%d:%d", &hour,&min,&second); TransitStopTime.arrival_time = hour*60+min; if(parser.GetValueByFieldName("departure_time",time_string) == false) break; sprintf(char_array,"%s",time_string.c_str ()); sscanf(char_array,"%d:%d:%d", &hour,&min,&second); TransitStopTime.departure_time = hour*60+min; if(parser.GetValueByFieldName("trip_id",TransitStopTime.trip_id) == false) break; if(parser.GetValueByFieldName("stop_sequence",TransitStopTime.stop_sequence) == false) break; /* if(parser.GetValueByFieldName("drop_off_type",TransitStopTime.drop_off_type) == false) TransitStopTime.drop_off_type=0; if(parser.GetValueByFieldName("pickup_type",TransitStopTime.pickup_type) == false) TransitStopTime.pickup_type=0; if(parser.GetValueByFieldName("shape_dist_traveled",TransitStopTime.shape_dist_traveled) == false) TransitStopTime.shape_dist_traveled=0; if(parser.GetValueByFieldName("stop_headsign",TransitStopTime.stop_headsign) == false) TransitStopTime.stop_headsign=0; if(parser.GetValueByFieldName("timepoint",TransitStopTime.timepoint) == false) TransitStopTime.timepoint=0;*/ if(m_PT_StopMap.find(TransitStopTime.stop_id)!= m_PT_StopMap.end()) { TransitStopTime.pt.x = m_PT_StopMap[TransitStopTime.stop_id].m_ShapePoint .x ; TransitStopTime.pt.y = m_PT_StopMap[TransitStopTime.stop_id].m_ShapePoint .y ; }else { if(missing_stops_message.GetLength ()<1000) { CString msg; msg.Format ("Mising stop %d\n",TransitStopTime.stop_id); missing_stops_message+=msg; } } // m_PT_StopTimeVector.push_back(TransitStopTime) ; stop_times_count++; //if(stop_times_count >=max_stop_times_record) // for testing purposes // break; if(m_PT_TripMap.find(TransitStopTime.trip_id) != m_PT_TripMap.end()) { m_PT_TripMap[TransitStopTime.trip_id].m_PT_StopTimeVector .push_back(TransitStopTime); } } parser.CloseCSVFile (); } CString message; int trip_size = m_PT_TripMap.size(); int stop_size = m_PT_StopMap.size(); int stop_time_size = stop_times_count; message.Format("%d transit trips, %d stops and %d stop time records are loaded.", trip_size, stop_size, stop_time_size); AfxMessageBox(message, MB_ICONINFORMATION); if(missing_stops_message.GetLength ()>0) { AfxMessageBox(missing_stops_message); } /* //read transfer file string str6 = m_ProjectDirectory +"transfers.txt"; CT2CA pszConvertedAnsiString6 (str6.c_str()); // construct a std::string using the LPCSTR input std::string strStd6 (pszConvertedAnsiString6); if (parser.OpenCSVFile(strStd6)) { int count =0; while(parser.ReadRecord()) { PT_transfers transfers; if(parser.GetValueByFieldName("from_stop_id",transfers.from_stop_id) == false) break; if(parser.GetValueByFieldName("to_stop_id",transfers.to_stop_id) == false) break; if(parser.GetValueByFieldName("transfer_type",transfers.transfer_type) == false) break; m_PT_transfers.push_back(transfers) ; count++; } parser.CloseCSVFile (); } */ // map matching return true; }