[QGIS Commit] r8299 - branches/ogr-plugin-branch/src/app/dbutilities

svn_qgis at osgeo.org svn_qgis at osgeo.org
Fri Mar 28 17:48:05 EDT 2008


Author: gcontreras
Date: 2008-03-28 17:48:04 -0400 (Fri, 28 Mar 2008)
New Revision: 8299

Modified:
   branches/ogr-plugin-branch/src/app/dbutilities/qgsogrdatabaseconnection.cpp
   branches/ogr-plugin-branch/src/app/dbutilities/qgsogrdatabaseconnection.h
   branches/ogr-plugin-branch/src/app/dbutilities/qgspostgresdatabaseconnection.cpp
   branches/ogr-plugin-branch/src/app/dbutilities/qgspostgresdatabaseconnection.h
Log:
Eliminated some methods that weren?\194?\180t used and qgsogrdatabaseconnection
was modified to use ogr c api

Modified: branches/ogr-plugin-branch/src/app/dbutilities/qgsogrdatabaseconnection.cpp
===================================================================
--- branches/ogr-plugin-branch/src/app/dbutilities/qgsogrdatabaseconnection.cpp	2008-03-28 21:45:43 UTC (rev 8298)
+++ branches/ogr-plugin-branch/src/app/dbutilities/qgsogrdatabaseconnection.cpp	2008-03-28 21:48:04 UTC (rev 8299)
@@ -41,7 +41,8 @@
  * Class destructor
  */
 QgsOgrDatabaseConnection::~QgsOgrDatabaseConnection(){
-
+  OGR_DS_Destroy(poDS);
+  poDS = 0;
 }
 
 
@@ -51,9 +52,7 @@
 bool QgsOgrDatabaseConnection::connect(){
   QString uri;   
   bool result=true;   
-  //QgsURIManager* uriman= new QgsURIManager(mConnectionParameters);   
-  //qDebug("QgsOgrDatabaseConnection::connect: "+ mConnection.name);
-  //qDebug("QgsOgrDatabaseConnection::connect: "+ uriman->getURI());
+  
   if ((this->uri().isEmpty())or(this->uri().isNull()))
    {
      //generateURI According to connectionParameters
@@ -71,13 +70,18 @@
    {
      uri=this->uri();            
    }
-  qDebug("inche uri:"+uri);  
   //clean all previous errors
   CPLErrorReset(); 
   // Register all OGR-drivers
   OGRRegisterAll();
-  OGRDataSource *poDS;
-  poDS = OGRSFDriverRegistrar::Open(uri, FALSE );
+  
+  
+  
+  CPLPushErrorHandler(CPLQuietErrorHandler);
+  poDS = OGROpen(uri, FALSE,&ogrDriver);
+  CPLPopErrorHandler();
+  
+  //poDS = OGRSFDriverRegistrar::Open(uri, FALSE );
 
   if( poDS == NULL ) {
        QString error=QObject::tr( "Open failed.\n" );
@@ -85,7 +89,7 @@
 	   setError(error);
        return (false);
     } 
-  OGRDataSource::DestroyDataSource( poDS );
+  //OGRDataSource::DestroyDataSource( poDS );
   return result;       
 }
 
@@ -104,7 +108,63 @@
  */
 QList<QgsGeometryColumnDescription *> QgsOgrDatabaseConnection::geometryTables(){
 
-	//return  NULL;
+
+  bool searchGeometryColumnsOnly=mConnectionParameters->geometryColumnsOnly;
+  bool searchPublicSchemaOnly=mConnectionParameters->publicOnly;
+  
+  QList<QgsGeometryColumnDescription *> details;  
+  
+   
+  int nLayers = OGR_DS_GetLayerCount(poDS);
+  
+  // create a pointer to the layer
+  OGRLayerH  poLayer;
+  // create a pointer to the feature
+  //OGRFeature *poFeature;
+  // create a pointer to the feature-definition
+  OGRFeatureDefnH poFDefn;
+        
+    
+  QStringList result;
+   
+  
+  for (int j = 0; j < nLayers  ; j++) {
+    poLayer = OGR_DS_GetLayer(poDS,j);  
+ 	OGR_L_ResetReading(poLayer);   
+	//poLayer->ResetReading();
+	poFDefn = OGR_L_GetLayerDefn(poLayer);
+	// show names of the layer
+	qDebug("Name of Layer: " + QString(OGR_FD_GetName(poFDefn)));
+	// show geometry of layer
+	qDebug("Geometry of Layer: " +QString(OGR_FD_GetGeomType(poFDefn)));
+    //result << poFDefn->GetName();		
+	
+  
+    
+    QString tableName = OGR_FD_GetName(poFDefn);
+    QString schemaName = "public";
+    QString type;
+    //QString type = OGR_FD_GetGeomType(poFDefn);
+    switch (OGR_FD_GetGeomType(poFDefn))
+     {
+      case wkbUnknown:type="UNKNOWN";break;
+      case wkbPoint: type="POINT";break;
+      case wkbMultiPoint: type="MULTIPOINT";break;
+      case wkbLineString: type="LINESTRING";break;
+      case wkbMultiLineString: type="MULTILINESTRING";break;
+      case wkbPolygon: type="POLYGON";break;
+      case wkbMultiPolygon:type="MULTIPOLYGON";break;
+      case wkbGeometryCollection:type="GEOMETRYCOLLECTION";break;
+      case wkbNone:type="NONE";break;     
+     }
+    //need to check for geometry column name
+    QString column=""; 
+    details.append(new QgsGeometryColumnDescription(type,schemaName,tableName,column));
+        
+	    
+  }
+
+  return  details;
 }
 
 
@@ -116,7 +176,11 @@
 	return  NULL;
 }
 
+QString QgsOgrDatabaseConnection::tableGeometryFromData(QString schema, QString tableName, QString column){
+  return NULL;
+}
 
+
 /**
  * Disconnects from OGR database
  */

Modified: branches/ogr-plugin-branch/src/app/dbutilities/qgsogrdatabaseconnection.h
===================================================================
--- branches/ogr-plugin-branch/src/app/dbutilities/qgsogrdatabaseconnection.h	2008-03-28 21:45:43 UTC (rev 8298)
+++ branches/ogr-plugin-branch/src/app/dbutilities/qgsogrdatabaseconnection.h	2008-03-28 21:48:04 UTC (rev 8299)
@@ -19,9 +19,15 @@
 #include "qgsdatabaseconnection.h"
 #include "qgsconnectionparameters.h"
 
-#include "cpl_port.h"
+//#include "cpl_port.h"
+//#include <ogr_api.h>
+//#include <ogrsf_frmts.h>
+
+#define CPL_SUPRESS_CPLUSPLUS
+
 #include <ogr_api.h>
-#include <ogrsf_frmts.h>
+#include <ogr_srs_api.h>
+#include <cpl_error.h>
 
 /**
  * Class to connect to databases using OGR
@@ -31,17 +37,19 @@
 
 public:
 	QgsOgrDatabaseConnection();
-
 	QgsOgrDatabaseConnection(QgsConnectionParameters* connectionParameters);
 	~QgsOgrDatabaseConnection();
 	bool connect();
-	QString baseKey();
 	QList<QgsGeometryColumnDescription *> geometryTables();
 	QString tableGeometry(QString tableName);
-
+	QString tableGeometryFromData(QString schema, QString tableName, QString column);
 	void disconnect();
 	void isConnected();
-	
-
+	QString baseKey();
+private:
+    //ogr datasource    
+    OGRDataSourceH poDS;
+    //ogr driver
+    OGRSFDriverH ogrDriver;    	   
 };
-#endif // !defined(EA_55811BBB_229C_4411_8F41_521DE4B175F7__INCLUDED_)
+#endif  

Modified: branches/ogr-plugin-branch/src/app/dbutilities/qgspostgresdatabaseconnection.cpp
===================================================================
--- branches/ogr-plugin-branch/src/app/dbutilities/qgspostgresdatabaseconnection.cpp	2008-03-28 21:45:43 UTC (rev 8298)
+++ branches/ogr-plugin-branch/src/app/dbutilities/qgspostgresdatabaseconnection.cpp	2008-03-28 21:48:04 UTC (rev 8299)
@@ -35,7 +35,6 @@
 
 
 QgsPostgresDatabaseConnection::~QgsPostgresDatabaseConnection(){
-  // free pg connection resources
   PQfinish(pd);                                                                
 }
 
@@ -53,14 +52,13 @@
     " port=" + mConnectionParameters->port +
     " user=" + mConnectionParameters->user + 
     " password='" + pass + "'";
-  pd = PQconnectdb(connInfo.toLocal8Bit().data());
-  //  std::cout << pd->ErrorMessage();
+  pd = PQconnectdb(connInfo.toLocal8Bit().data());  
   if (PQstatus(pd) == CONNECTION_OK)
     {
-      // Database successfully opened; we can now issue SQL commands.
       PQsetClientEncoding(pd, "UNICODE");
       result=true;
-  } else
+    }
+  else
     {
       mError=QObject::tr("Connection failed - Check settings and try again.\n\nExtended error information:\n");
       qDebug (QString(PQerrorMessage(pd)));
@@ -68,20 +66,17 @@
       qDebug (mError);
       result=false;
     }
-  
   return result;     
 }
 
 
 QString QgsPostgresDatabaseConnection::fullDescription(QString schema, QString table, QString column){
-
-	return  NULL;
+  return  NULL;
 }
 
 
 QString QgsPostgresDatabaseConnection::baseKey(){
-
-	return  NULL;
+  return  NULL;
 }
 
 
@@ -89,61 +84,42 @@
 {
   bool ok = false;
 
-
   QString sql = "select * from geometry_columns";
-  // where f_table_schema ='" + settings.readEntry(key + "/database") + "'";
   sql += " order by f_table_schema,f_table_name";
-  //qDebug("Fetching tables using: " + sql);
   PGresult *result = PQexec(pg, sql.toLocal8Bit().data());
   if (result)
   {
     QString msg;
     msg=msg+"Fetched "+(QString)PQntuples(result)+" tables from database";
-    //QTextOStream(&msg) << "Fetched " << PQntuples(result) << " tables from database";
-    //qDebug(msg);
     for (int idx = 0; idx < PQntuples(result); idx++)
     {
-      // Be a bit paranoid and check that the table actually
-      // exists. This is not done as a subquery in the query above
-      // because I can't get it to work correctly when there are tables
-      // with capital letters in the name.
-
       // Take care to deal with tables with the same name but in different schema.
       QString tableName = PQgetvalue(result, idx, PQfnumber(result, "f_table_name"));
       QString schemaName = PQgetvalue(result, idx, PQfnumber(result, "f_table_schema"));
       sql = "select oid from pg_class where relname = '" + tableName + "'";
       if (schemaName.length() > 0)
-	sql +=" and relnamespace = (select oid from pg_namespace where nspname = '" +
-	  schemaName + "')";
-
+	     sql +=" and relnamespace = (select oid from pg_namespace where nspname = '" +
+	     schemaName + "')";
       PGresult* exists = PQexec(pg, sql.toLocal8Bit().data());
       if (PQntuples(exists) == 1)
       {
         QString column = PQgetvalue(result, idx, PQfnumber(result, "f_geometry_column"));
         QString type = PQgetvalue(result, idx, PQfnumber(result, "type"));
-        
-	QString as = "";
-	if(type=="GEOMETRY" && !searchGeometryColumnsOnly) {
-	  //fredaddSearchGeometryColumn(schemaName, tableName,  column);
-	  as=type="WAITING";
-	}
-
-	//freddetails.push_back(geomPair(fullDescription(schemaName, tableName, column, as), type));
+	    QString as = "";
+	    if(type=="GEOMETRY" && !searchGeometryColumnsOnly){
+	     as=type="WAITING";
+        }
       }
       PQclear(exists);
     }
     ok = true;
   }
   PQclear(result);
-
-
   if (searchGeometryColumnsOnly)
     return ok;
 
   // Now have a look for geometry columns that aren't in the
-  // geometry_columns table. This code is specific to postgresql,
-  // but an equivalent query should be possible in other
-  // databases.
+  // geometry_columns table.
   sql = "select pg_class.relname, pg_namespace.nspname, pg_attribute.attname, "
     "pg_class.relkind from "
     "pg_attribute, pg_class, pg_type, pg_namespace where pg_type.typname = 'geometry' and "
@@ -161,20 +137,12 @@
 
   for (int i = 0; i < PQntuples(result); i++)
   {
-    // Have the column name, schema name and the table name. The concept of a
-    // catalog doesn't exist in postgresql so we ignore that, but we
-    // do need to get the geometry type.
-
     // Make the assumption that the geometry type for the first
     // row is the same as for all other rows. 
-
     QString table  = PQgetvalue(result, i, 0); // relname
     QString schema = PQgetvalue(result, i, 1); // nspname
     QString column = PQgetvalue(result, i, 2); // attname
     QString relkind = PQgetvalue(result, i, 3); // relation kind
-
-    //addSearchGeometryColumn(schema, table, column);
-    //details.push_back(GeometryPair(fullDescription(schema, table, column, "WAITING"), "WAITING"));
   }
   ok = true;
 
@@ -185,17 +153,13 @@
 }
 
 QList<QgsGeometryColumnDescription *> QgsPostgresDatabaseConnection::geometryTables(){
-  return(geometryTables(mConnectionParameters->geometryColumnsOnly,mConnectionParameters->publicOnly));                                  
-}                                    
-
-
-QList<QgsGeometryColumnDescription *> QgsPostgresDatabaseConnection::geometryTables(bool searchGeometryColumnsOnly, bool searchPublicSchemaOnly){
-  qDebug("uno");                                  
-  QList<QgsGeometryColumnDescription *> details;  
   
-  //*********************************************************
-  //bool ok = false;
+  bool searchGeometryColumnsOnly=mConnectionParameters->geometryColumnsOnly;
+  bool searchPublicSchemaOnly=mConnectionParameters->publicOnly;
   
+  QList<QgsGeometryColumnDescription *> details;  
+  
+   
   QString sql = "select * from geometry_columns";
   sql += " order by f_table_schema,f_table_name";
 
@@ -225,17 +189,12 @@
 	    QString as = "";
 	    if(type=="GEOMETRY" && !searchGeometryColumnsOnly) 
 	     {
-           //agregar a las busquedas                 
-	       //addSearchGeometryColumn(schemaName, tableName,  column);
-	       as=type="WAITING";
+           as=type="WAITING";
 	     }
-        //geometryColumn.setType(type);
-        //geometryColumn.setSchema(schemaName);
-        //geometryColumn.setTable(tableName);
-        //geometryColumn.setColumn(column);
+       
         details.append(new QgsGeometryColumnDescription(type,schemaName,tableName,column));
         
-	    //mTableModel.addTableEntry(type, schemaName, tableName, column, "");
+	    
       }
       PQclear(exists);
     }
@@ -284,27 +243,12 @@
     QString column = PQgetvalue(result, i, 2); // attname
     QString relkind = PQgetvalue(result, i, 3); // relation kind
 
-    //addSearchGeometryColumn(schema, table, column);
-    //details.push_back(geomPair(fullDescription(schema, table, column, "WAITING"), "WAITING"));
-    //mTableModel.addTableEntry("Waiting", schema, table, column, "");
+    
     details.append(new QgsGeometryColumnDescription("Waiting",schema,table,column));
   }
-  //ok = true;
-
+  
   PQclear(result);
-  //return ok;*/
-  //*********************************************************
-          
- /* if(geometryColumnInformation(pd,details, searchGeometryColumnsOnly, searchPublicSchemaOnly))	
-      {
-       //check the thread
-      }
-  else
-    {
-      qDebug("Unable to get list of spatially enabled tables from the database");
-      qDebug(PQerrorMessage(pd));
-    }*/                  
-  //qDebug("dos");  
+ 
   return  details;
 }
 
@@ -350,104 +294,3 @@
 
 
 
-//******************************************
-
-/*bool QgsPostgresDatabaseConnection::geometryTableInformation(PGconn *pg, bool searchGeometryColumnsOnly, bool searchPublicOnly)
-{
-  bool ok = false;
-  
-  QString sql = "select * from geometry_columns";
-  sql += " order by f_table_schema,f_table_name";
-
-  PGresult *result = PQexec(pg, sql.toLocal8Bit().data());
-  if (result)
-  {
-    for (int idx = 0; idx < PQntuples(result); idx++)
-    {
-      // Be a bit paranoid and check that the table actually
-      // exists. This is not done as a subquery in the query above
-      // because I can't get it to work correctly when there are tables
-      // with capital letters in the name.
-
-      // Take care to deal with tables with the same name but in different schema.
-      QString tableName = PQgetvalue(result, idx, PQfnumber(result, "f_table_name"));
-      QString schemaName = PQgetvalue(result, idx, PQfnumber(result, "f_table_schema"));
-      sql = "select oid from pg_class where relname = '" + tableName + "'";
-      if (schemaName.length() > 0)
-	sql +=" and relnamespace = (select oid from pg_namespace where nspname = '" +
-	  schemaName + "')";
-
-      PGresult* exists = PQexec(pg, sql.toLocal8Bit().data());
-      if (PQntuples(exists) == 1)
-      {
-        QString column = PQgetvalue(result, idx, PQfnumber(result, "f_geometry_column"));
-        QString type = PQgetvalue(result, idx, PQfnumber(result, "type"));
-        
-	QString as = "";
-	if(type=="GEOMETRY" && !searchGeometryColumnsOnly) 
-	  {
-	    addSearchGeometryColumn(schemaName, tableName,  column);
-	    as=type="WAITING";
-	  }
-
-	mTableModel.addTableEntry(type, schemaName, tableName, column, "");
-      }
-      PQclear(exists);
-    }
-    ok = true;
-  }
-  PQclear(result);
-
-  //search for geometry columns in tables that are not in the geometry_columns metatable
-  
-
-  QApplication::restoreOverrideCursor();
-  if (searchGeometryColumnsOnly)
-    {
-      return ok;
-    }
-
-   // Now have a look for geometry columns that aren't in the
-  // geometry_columns table. This code is specific to postgresql,
-  // but an equivalent query should be possible in other
-  // databases.
-  sql = "select pg_class.relname, pg_namespace.nspname, pg_attribute.attname, "
-    "pg_class.relkind from "
-    "pg_attribute, pg_class, pg_type, pg_namespace where pg_type.typname = 'geometry' and "
-    "pg_attribute.atttypid = pg_type.oid and pg_attribute.attrelid = pg_class.oid ";
-
-  if (searchPublicOnly)
-    sql += "and pg_namespace.nspname = 'public' ";
-
-  sql += "and cast(pg_class.relname as character varying) not in "
-    "(select f_table_name from geometry_columns) "
-    "and pg_namespace.oid = pg_class.relnamespace "
-    "and pg_class.relkind in ('v', 'r')"; // only from views and relations (tables)
-  
-  result = PQexec(pg, sql.toLocal8Bit().data());
-
-  for (int i = 0; i < PQntuples(result); i++)
-  {
-    // Have the column name, schema name and the table name. The concept of a
-    // catalog doesn't exist in postgresql so we ignore that, but we
-    // do need to get the geometry type.
-
-    // Make the assumption that the geometry type for the first
-    // row is the same as for all other rows. 
-
-    QString table  = PQgetvalue(result, i, 0); // relname
-    QString schema = PQgetvalue(result, i, 1); // nspname
-    QString column = PQgetvalue(result, i, 2); // attname
-    QString relkind = PQgetvalue(result, i, 3); // relation kind
-
-    addSearchGeometryColumn(schema, table, column);
-    //details.push_back(geomPair(fullDescription(schema, table, column, "WAITING"), "WAITING"));
-    mTableModel.addTableEntry("Waiting", schema, table, column, "");
-  }
-  ok = true;
-
-  PQclear(result);
-  return ok;
-}
-
-*/

Modified: branches/ogr-plugin-branch/src/app/dbutilities/qgspostgresdatabaseconnection.h
===================================================================
--- branches/ogr-plugin-branch/src/app/dbutilities/qgspostgresdatabaseconnection.h	2008-03-28 21:45:43 UTC (rev 8298)
+++ branches/ogr-plugin-branch/src/app/dbutilities/qgspostgresdatabaseconnection.h	2008-03-28 21:48:04 UTC (rev 8299)
@@ -41,27 +41,16 @@
 {
 
 public:
-	typedef std::pair<QString, QString> geomPair;
-
-	typedef std::list<geomPair > geomCol;
-
-
 	QgsPostgresDatabaseConnection();
-
 	QgsPostgresDatabaseConnection(QgsConnectionParameters* conn);
 	~QgsPostgresDatabaseConnection();
 	bool connect();
 	QString baseKey();
 	QList<QgsGeometryColumnDescription *> geometryTables();
-	QList<QgsGeometryColumnDescription *> geometryTables(bool searchGeometryColumnsOnly, bool searchPublicSchemaOnly);
 	QString tableGeometry(QString tableName);
 	QString tableGeometryFromData(QString schema, QString tableName, QString column);
-
 private:
-	QgsGeomColumnTypeThread* mColumnTypeThread;
-
 	QString fullDescription(QString schema, QString table, QString column);
-//	bool geometryTableInformation(PGconn* pg, GeometryColumns& details, bool searchGeometryColumnsOnly, bool searchPublicOnly);
 	bool geometryColumnInformation(PGconn *pg,GeometryColumns& details, bool searchGeometryColumnsOnly, bool searchPublicOnly);
 	
 	PGconn *pd;



More information about the QGIS-commit mailing list