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; }
void CDlg_GISDataExchange::ExportToGISFile(LPCTSTR lpszCSVFileName,LPCTSTR lpszShapeFileName, CString GISTypeString ) { #ifndef _WIN64 m_MessageList.ResetContent (); CWaitCursor wait; CCSVParser parser; int i= 0; // open csv file if (parser.OpenCSVFile(lpszCSVFileName)) { CString message_str; OGRSFDriver *poDriver; OGRRegisterAll(); poDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(GISTypeString ); if( poDriver == NULL ) { message_str.Format ( "%s driver not available.", GISTypeString ); m_MessageList.AddString (message_str); return; } OGRDataSource *poDS; poDS = poDriver->CreateDataSource(lpszShapeFileName, NULL ); if( poDS == NULL ) { message_str.Format ( "Creation of GIS output file %s failed.\nPlease do not overwrite the exiting file and please select a new file name.", lpszShapeFileName ); m_MessageList.AddString (message_str); return; } ///// export to link layer // link layer OGRLayer *poLayer; poLayer = poDS->CreateLayer( "link", NULL, wkbLineString, NULL ); if( poLayer == NULL ) { m_MessageList.AddString ("link Layer creation failed"); return; } vector<string> HeaderVector = parser.GetHeaderVector(); std::vector <CString> LongFieldVector; for(unsigned int i = 0; i < HeaderVector.size(); i++) { if(HeaderVector[i].find ("geometry") != string::npos|| HeaderVector[i].find ("name") != string::npos || HeaderVector[i].find ("code") != string::npos) { OGRFieldDefn oField (HeaderVector[i].c_str (), OFTString); CString str; if( poLayer->CreateField( &oField ) != OGRERR_NONE ) { str.Format("Creating field %s failed", oField.GetNameRef()); m_MessageList.AddString (str); return; } }else { CString field_string = HeaderVector[i].c_str (); OGRFieldDefn oField (field_string, OFTReal); CString str; if( poLayer->CreateField( &oField ) != OGRERR_NONE ) { str.Format("Creating field %s failed", oField.GetNameRef()); m_MessageList.AddString (str); return; } } if(HeaderVector[i].size()>=11) { LongFieldVector.push_back (HeaderVector[i].c_str ()); } } message_str.Format ("%d fields have been created.",HeaderVector.size()); m_MessageList.AddString (message_str); if(LongFieldVector.size() >=1) { message_str.Format("Warning: Arc GIS file only supports field names with not more than 10 characters.\nThe following fields have long field names. "); m_MessageList.AddString (message_str); for(unsigned l = 0; l< LongFieldVector.size(); l++) { message_str.Format ("%s",LongFieldVector[l]); m_MessageList.AddString (message_str); } } int count = 0 ; while(parser.ReadRecord()) { //create feature OGRFeature *poFeature; poFeature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() ); //step 1: write all fields except geometry for(unsigned int i = 0; i < HeaderVector.size(); i++) { if(HeaderVector[i]!="geometry") { if(HeaderVector[i].find ("name") != string::npos || HeaderVector[i].find ("code") != string::npos) { std::string str_value; parser.GetValueByFieldName(HeaderVector[i],str_value); // TRACE("field: %s, value = %s\n",HeaderVector[i].c_str (),str_value.c_str ()); poFeature->SetField(i,str_value.c_str ()); }else { double value = 0; parser.GetValueByFieldName(HeaderVector[i],value); // TRACE("field: %s, value = %f\n",HeaderVector[i].c_str (),value); CString field_name = HeaderVector[i].c_str (); poFeature->SetField(i,value); } } } string geo_string; std::vector<CCoordinate> CoordinateVector; if(parser.GetValueByFieldName("geometry",geo_string)) { // overwrite when the field "geometry" exists CGeometry geometry(geo_string); CoordinateVector = geometry.GetCoordinateList(); if( m_GIS_data_type == GIS_Point_Type && CoordinateVector.size ()==1) { OGRPoint pt; pt.setX( CoordinateVector[0].X ); pt.setY( CoordinateVector[0].Y); poFeature->SetGeometry( &pt ); } if( m_GIS_data_type == GIS_Line_Type) { OGRLineString line; for(unsigned int si = 0; si< CoordinateVector.size(); si++) { line.addPoint (CoordinateVector[si].X , CoordinateVector[si].Y); } poFeature->SetGeometry( &line ); } if( m_GIS_data_type == GIS_Polygon_Type) { OGRPolygon polygon; OGRLinearRing ring; for(unsigned int si = 0; si< CoordinateVector.size(); si++) { ring.addPoint (CoordinateVector[si].X , CoordinateVector[si].Y,1); } polygon.addRing(&ring); poFeature->SetGeometry( &polygon ); } } else { // no geometry field /// create geometry data from m_GIS_data_type == GIS_Point_Type if( m_GIS_data_type == GIS_Point_Type ) { double x, y; if(parser.GetValueByFieldName("x",x) && parser.GetValueByFieldName("y",y) ) { OGRPoint pt; pt.setX( CoordinateVector[0].X ); pt.setY( CoordinateVector[0].Y); poFeature->SetGeometry( &pt ); }else { AfxMessageBox("Pleaes prepare fields x and y in the csv file in order to create a node GIS layer.", MB_ICONINFORMATION); return; } } ///create geometry if( m_GIS_data_type == GIS_Line_Type) { int number_of_shape_points = 0; if(parser.GetValueByFieldName("number_of_shape_points", number_of_shape_points)) { if(number_of_shape_points>=2) { OGRLineString line; for(int s= 1; s<= number_of_shape_points; s++) { CString str_x, str_y; str_x.Format ("x%d",s); str_y.Format ("y%d",s); double x = 0; double y = 0; string string_x, string_y; string_x = m_pDoc->CString2StdString (str_x); string_y = m_pDoc->CString2StdString (str_y); if(parser.GetValueByFieldName(string_x, x) && parser.GetValueByFieldName(string_y, y)) { line.addPoint(x,y); }else { AfxMessageBox("Pleaes prepare fields x1,y1,x2,y2,...,xn,yn in the csv file in order to create a link GIS layer.", MB_ICONINFORMATION); return; } } poFeature->SetGeometry( &line ); } }else { AfxMessageBox("Pleaes prepare fields number_of_shape_points, x1,y1,x2,y2,...,xn,yn in the csv file in order to create a link GIS layer.", MB_ICONINFORMATION); return; } } // if( m_GIS_data_type == GIS_Polygon_Type) { OGRPolygon polygon; OGRLinearRing ring; int number_of_shape_points = 0; if(parser.GetValueByFieldName("number_of_shape_points", number_of_shape_points)) { if(number_of_shape_points>=2) { OGRLineString line; for(int s= 0; s< number_of_shape_points; s++) { CString str_x, str_y; str_x.Format ("x%d",str_x); str_y.Format ("y%d",str_y); double x = 0; double y = 0; string string_x, string_y; string_x = m_pDoc->CString2StdString (str_x); string_y = m_pDoc->CString2StdString (str_y); if(parser.GetValueByFieldName(string_x, x) && parser.GetValueByFieldName(string_y, y)) { ring.addPoint (x,y,1); }else { AfxMessageBox("Pleaes prepare fields x1,y1,x2,y2,...,xn,yn in the csv file in order to create a zone GIS layer.", MB_ICONINFORMATION); return; } } polygon.addRing(&ring); poFeature->SetGeometry( &polygon ); } } } } if( poLayer->CreateFeature( poFeature ) != OGRERR_NONE ) { AfxMessageBox("Failed to create line feature in shapefile.\n"); return; } OGRFeature::DestroyFeature( poFeature ); count++; } message_str.Format ("%d records have been created.",count); m_MessageList.AddString (message_str); OGRDataSource::DestroyDataSource( poDS ); CString ShapeFile = lpszShapeFileName; CString ShapeFileFolder = ShapeFile.Left(ShapeFile.ReverseFind('\\') + 1); ShellExecute( NULL, "explore", ShapeFileFolder, NULL, NULL, SW_SHOWNORMAL ); } #endif }
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; }
bool CTLiteDoc::ReadSensorData(LPCTSTR lpszFileName, int simulation_start_time_in_min) { CCSVParser parser; int error_count = 0; int data_count = 0; if (parser.OpenCSVFile(lpszFileName)) { std::list<DTALink*>::iterator iLink; for (iLink = m_LinkSet.begin(); iLink != m_LinkSet.end(); iLink++) { (*iLink)->m_bSensorData = false; } int sensor_count = 0; CString error_message; CString prev_error_message; while(parser.ReadRecord()) { DTA_sensor sensor; if(!parser.GetValueByFieldName("from_node_id",sensor.FromNodeNumber )) continue; if(!parser.GetValueByFieldName("to_node_id",sensor.ToNodeNumber )) continue; if(!parser.GetValueByFieldName("sensor_type",sensor.SensorType)) continue; parser.GetValueByFieldName("x_coord",sensor.pt.x ); parser.GetValueByFieldName("y_coord",sensor.pt.y ); DTALink* pLink; /* int link_id = 0; parser.GetValueByFieldName("link_id",link_id ); if(link_id >0) pLink = m_LinkNoMap[link_id -1]; else */ pLink = FindLinkWithNodeNumbers(sensor.FromNodeNumber , sensor.ToNodeNumber,lpszFileName ); if(pLink!=NULL) { sensor.LinkID = pLink->m_LinkNo ; if(m_SensorMap.find(sensor.SensorID) == m_SensorMap.end()) { m_SensorMap[sensor.SensorID] = sensor; } pLink->m_bSensorData = true; float start_time_in_min =0; float end_time_in_min = 0; parser.GetValueByFieldName("start_time_in_min",start_time_in_min ); parser.GetValueByFieldName("end_time_in_min",end_time_in_min ); int volume_count= 0; pLink->m_SensorTypeString = sensor.SensorType; if(sensor.SensorType.find("link_count")!= string::npos) { if(parser.GetValueByFieldName ("count",volume_count )==false) { AfxMessageBox("Field count is missing from file input_sensor.csv. Please check."); return false; } }else if(sensor.SensorType.find("lane_count")!= string::npos) { if(parser.GetValueByFieldName ("count",volume_count ) == false) { AfxMessageBox("Field count is missing from file input_sensor.csv. Please check."); return false; } volume_count = volume_count* pLink->m_NumberOfLanes; } data_count++; if(start_time_in_min <0 && error_message.GetLength () < 1000) { CString msg; msg.Format ("Sensor %d-> %d has an error of start_time_in_min <0.\n",sensor.FromNodeNumber , sensor.ToNodeNumber); if(prev_error_message!=msg) { error_message+=msg; prev_error_message= msg; } } if(end_time_in_min < start_time_in_min+1 && error_message.GetLength () < 1000) { CString msg; msg.Format ("Sensor %d-> %d has an error of end_time_in_min <= start_time_in_min: %d < %d.\n",sensor.FromNodeNumber , sensor.ToNodeNumber, end_time_in_min, start_time_in_min); if(prev_error_message!=msg) { error_message+=msg; prev_error_message= msg; } } for(int t = start_time_in_min; t< end_time_in_min; t++) { if(pLink->m_FromNodeNumber == 54656 && pLink->m_ToNodeNumber == 56154 && t>=720) { TRACE(""); } int time = t - simulation_start_time_in_min; // allow shift of start time if(time>=0 && (unsigned int)time < pLink->m_LinkMOEAry.size()) { // if(!sensor.SensorType.empty () && sensor.SensorType.find("count")!= string::npos) { pLink->m_LinkMOEAry[ time].SensorLinkCount = volume_count/(max(1.0,end_time_in_min-start_time_in_min)); // convert to per hour link flow } } } }else { if(error_message.GetLength () < 1000) { CString msg; msg.Format ("Link %d -> %d in input_sensor.csv does not exist in input_link.csv.\n", sensor.FromNodeNumber , sensor.ToNodeNumber); if(prev_error_message!=msg) { error_message+=msg; prev_error_message= msg; } error_count++; continue; } } } if(error_message.GetLength ()>=1) { AfxMessageBox(error_message); } if(m_SensorMap.size()>0) { m_SensorLocationLoadingStatus.Format("%d sensors and %d sensor records are loaded from file %s.",m_SensorMap.size(),data_count,lpszFileName); return true; } else return false; // no sensors have been specified } return false; }