//! Get Ascii file headers information void clsRasterData::getASCHeaders(string databasePath,map<string,float>* headers) { DBManager dbman; string sql; slTable* tbl; // open the database dbman.Open(databasePath + File_ParameterDB); // if there is not an error if(dbman.IsError()) throw ModelException("clsRasterData","getASCHeaders","Can't open paramter database!"); // constrcut the SQL statement for the query sql = "SELECT Parameter,Value FROM Header"; // run the query tbl = dbman.Load(sql); if (tbl->nRows == 0) throw ModelException("ModuleParamter","getParameterFromDatabase","Can't find ASC Headers in paramter database!"); headers->clear(); //headers = new map<string,float>(); for(int i=1;i<=tbl->nRows;i++) { (*headers)[tbl->FieldValue(i, 1)] = float(atof(tbl->FieldValue(i, 2).c_str())); } delete tbl; tbl = NULL; dbman.Close(); }
//! if the given table exists bool DBManager::IsTableExist(string databasePath,string tableName) { utils util; if(!(util.FileExists(databasePath))) return false; DBManager dbman; bool exist = false; dbman.Open(databasePath); if (!dbman.IsError()) { string strSQL = "pragma table_info("+tableName+")"; //string strSQL = "SELECT * FROM sqlite_master WHERE type = 'table' and name = '"+tableName+"'"; slTable* tbl = dbman.Load(strSQL); if(tbl->nRows > 0) exist = true; delete tbl; tbl = NULL; } dbman.Close(); return exist; }
//! Constructor for Sqlite //! \deprecated For now, this constructor is deprecated! clsSpecificOutput::clsSpecificOutput(string projectPath,string databasePath,clsRasterData* templateRasterData,string outputID) { m_outputID = outputID; string path = databasePath + File_HydroClimateDB; string tableName = TableNameFromOutputID(outputID); if(tableName.length() == 0) throw ModelException( "clsSpecificOutput","clsSpecificOutput", "The output id "+ outputID + " can't output specific cells."); if(!DBManager::IsTableExist(path,tableName)) throw ModelException( "clsSpecificOutput","clsSpecificOutput", "The database " + path + " dose not exist or the table "+tableName+" does not exist in this database."); if(templateRasterData == NULL) throw ModelException( "clsSpecificOutput","clsSpecificOutput", "The templateRasterData is null."); m_templateRasterData = templateRasterData; utils util; DBManager db; DBManager dbman; try { // open the hydroclimate database dbman.Open(path); // if there is no error if (!dbman.IsError()) { // create the query for the data table // Use the start date and end date to limit the time series data string strSQL = "SELECT TIME, Longitude, Latitude, ID, MEASURED FROM " + tableName + " order by TIME"; // run the query slTable* tbl = dbman.Load(strSQL); // if the query is successful if (tbl != NULL) { // read in the data for (int idx=1; idx<=tbl->nRows; idx++) { time_t time= util.ConvertToTime(tbl->FieldValue(idx,0), "%4d-%2d-%2d", false); float nrow = (float)(atof(tbl->FieldValue(idx,1).c_str())); float ncol = (float)(atof(tbl->FieldValue(idx,2).c_str())); int position = templateRasterData->getPosition(nrow,ncol); if(position > -1) { m_times.push_back(time); m_positions.push_back(position); m_values.push_back(-99.0f); m_slope.push_back(-99.0f); m_curvature.push_back(-99.0f); m_landuse.push_back(-99.0f); m_ids.push_back(tbl->FieldValue(idx,3)); //the measurement is added for convenient comparison m_measurement.push_back((float)(atof(tbl->FieldValue(idx,4).c_str()))); } } setSlope(projectPath); setCurvature(projectPath); setLanduse(projectPath); delete tbl; } tbl = NULL; dbman.Close(); } } catch (...) { dbman.Close(); throw; } }
void clsSiteData::readSiteBasicInfo(string tableName) { string path = m_databasePath + File_HydroClimateDB; if (!DBManager::IsTableExist(path, "stations")) throw ModelException("clsSiteData", "readSiteBasicInfo", "The database " + path + " dose not exist or the table stations does not exist in this database."); if (!DBManager::IsTableExist(path, tableName)) throw ModelException("clsSiteData", "readSiteBasicInfo", "The database " + path + " dose not exist or the table " + tableName + " does not exist in this database."); //read data DBManager dbman; try { // open the hydrclimate database dbman.Open(path); // if there is no error if (!dbman.IsError()) { // create the SQL query for the stations table //ID, NAME, XPR, YPR, LAT, LONG, ELEVATION, TYPE, UNITS, AREA, STARTDATE, ENDDATE, INTERVAL, TABLENAME string strSQL = "SELECT ID, NAME, XPR, YPR, LAT, LONG, ELEVATION, AREA, TYPE FROM stations WHERE TABLENAME='" + tableName + "'"; // run the query slTable *tbl = dbman.Load(strSQL); // if the query is successful if (tbl != NULL) { // if there is at least one record if (tbl->nRows > 0) { utils util; // remember row 0 contains the field names not values m_ID = atoi(tbl->FieldValue(1, 0).c_str()); m_Name = tbl->FieldValue(1, 1).c_str(); m_XPR = (float) atof(tbl->FieldValue(1, 2).c_str()); m_YPR = (float) atof(tbl->FieldValue(1, 3).c_str()); m_Latitude = (float) atof(tbl->FieldValue(1, 4).c_str()); m_Longitude = (float) atof(tbl->FieldValue(1, 5).c_str()); m_Elevation = (float) atof(tbl->FieldValue(1, 6).c_str()); m_Area = (float) atof(tbl->FieldValue(1, 7).c_str()); string type = tbl->FieldValue(1, 8); if (clsHydroClimateData::IsHydroClimateDataType(&type)) { m_timeSerieseData[type] = new clsHydroClimateData(m_databasePath, tableName); } else { throw ModelException("clsSiteData", "readSiteBasicInfo", "The data type for 'station:" + m_Name + ",tableName:" + tableName + "' is not correct."); } } else throw ModelException("clsSiteData", "readSiteBasicInfo", "There is no row whose table name is " + tableName); delete tbl; } tbl = NULL; } dbman.Close(); } catch (...) { dbman.Close(); throw; } }