1
/***************************************************************************
2
qgspostgresextentthread.cpp - Multithreaded PostgreSQL layer extents
6
copyright : (C) 2005 by Brendan Morley
7
email : morb at ozemail dot com dot au
8
***************************************************************************/
10
/***************************************************************************
12
* This program is free software; you can redistribute it and/or modify *
13
* it under the terms of the GNU General Public License as published by *
14
* the Free Software Foundation; either version 2 of the License, or *
15
* (at your option) any later version. *
17
***************************************************************************/
18
/* $Id: qgspostgresextentthread.cpp,v 1.1 2005/03/10 05:38:27 gsherman Exp $ */
22
#include <qapplication.h>
24
#include "../../src/qgis.h"
25
#include "../../src/qgsrect.h"
26
#include "../../src/qgsmapcanvas.h"
29
#include "qgspostgresextentthread.h"
33
QgsPostgresExtentThread::QgsPostgresExtentThread()
34
: QgsPostgresProvider(), QThread()
42
QgsPostgresExtentThread::~QgsPostgresExtentThread()
47
void QgsPostgresExtentThread::setConnInfo( QString s )
52
void QgsPostgresExtentThread::setTableName( QString s )
57
void QgsPostgresExtentThread::setSqlWhereClause( QString s )
62
void QgsPostgresExtentThread::setGeometryColumn( QString s )
67
//void QgsPostgresExtentThread::setCallback( QgsPostgresProvider* o )
69
// callbackObject = o;
73
void QgsPostgresExtentThread::run()
75
// // placeholders for now.
78
std::cout << "QgsPostgresExtentThread: Started running." << std::endl;
80
// Open another connection to the database
81
PGconn *connection = PQconnectdb((const char *) connInfo);
85
QString sql = "select extent(" + geometryColumn + ") from " + tableName;
86
if(sqlWhereClause.length() > 0)
88
sql += " where " + sqlWhereClause;
92
sql = "select xmax(extent(" + geometryColumn + ")) as xmax,"
93
"xmin(extent(" + geometryColumn + ")) as xmin,"
94
"ymax(extent(" + geometryColumn + ")) as ymax," "ymin(extent(" + geometryColumn + ")) as ymin" " from " + tableName;
98
qDebug("+++++++++QgsPostgresExtentThread::run - Getting extents using schema.table: " + sql);
102
std::cout << "QgsPostgresExtentThread: About to issue query." << std::endl;
104
PGresult *result = PQexec(connection, (const char *) sql);
106
std::cout << "QgsPostgresExtentThread: Query completed." << std::endl;
110
std::string box3d = PQgetvalue(result, 0, 0);
113
box3d = box3d.substr(box3d.find_first_of("(")+1);
114
box3d = box3d.substr(box3d.find_first_not_of(" "));
115
s = box3d.substr(0, box3d.find_first_of(" "));
116
double minx = strtod(s.c_str(), NULL);
118
box3d = box3d.substr(box3d.find_first_of(" ")+1);
119
s = box3d.substr(0, box3d.find_first_of(" "));
120
double miny = strtod(s.c_str(), NULL);
122
box3d = box3d.substr(box3d.find_first_of(",")+1);
123
box3d = box3d.substr(box3d.find_first_not_of(" "));
124
s = box3d.substr(0, box3d.find_first_of(" "));
125
double maxx = strtod(s.c_str(), NULL);
127
box3d = box3d.substr(box3d.find_first_of(" ")+1);
128
s = box3d.substr(0, box3d.find_first_of(" "));
129
double maxy = strtod(s.c_str(), NULL);
131
layerExtent = new QgsRect(minx, miny, maxx, maxy);
134
layerExtent.setXmax(maxx);
135
layerExtent.setXmin(minx);
136
layerExtent.setYmax(maxy);
137
layerExtent.setYmin(miny);
142
std::cout << "QgsPostgresExtentThread: Set extents to: "
143
<< layerExtent->xMin() << ", " << layerExtent->yMin() <<
144
" " << layerExtent->xMax() << ", " << layerExtent->yMax() << std::endl;
147
// clear query result
151
// Send some events (instead of a signal) as it is thread-safe
153
// First we tell the object that invoked us that we have some new extents for her
154
// Second we tell the application that the extents have changed, so that it
155
// can go on and do any visual housekeeping (e.g. update the overview window)
157
std::cout << "QgsPostgresExtentThread: About to create and dispatch event " << QGis::ProviderExtentCalcEvent << " to callback" << std::endl;
159
QCustomEvent * e1 = new QCustomEvent ( QGis::ProviderExtentCalcEvent );
160
e1->setData(layerExtent);
161
QApplication::postEvent( (QObject *)callbackObject, e1);
163
// QApplication::postEvent(qApp->mainWidget(), e1);
165
std::cout << "QgsPostgresExtentThread: Posted event " << QGis::ProviderExtentCalcEvent << " to callback" << std::endl;
168
std::cout << "QgsPostgresExtentThread: About to finish connection." << std::endl;
170
// ending the thread, clean up
171
PQfinish(connection);
173
std::cout << "QgsPostgresExtentThread: About to complete running." << std::endl;