~ubuntu-branches/ubuntu/quantal/qgis/quantal

« back to all changes in this revision

Viewing changes to providers/postgres/qgspostgresextentthread.cpp

  • Committer: Bazaar Package Importer
  • Author(s): William Grant
  • Date: 2007-05-06 13:42:32 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20070506134232-pyli6t388w5asd8x
Tags: 0.8.0-3ubuntu1
* Merge from Debian unstable. Remaining Ubuntu changes:
  - debian/rules, debian/qgis.install, debian/qgis.dirs debian/qgis.desktop:
    Add and install .desktop.
* debian/qgis.desktop: Remove Applications category; it's not real.
* Modify Maintainer value to match Debian-Maintainer-Field Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
      qgspostgresextentthread.cpp  -  Multithreaded PostgreSQL layer extents
3
 
                                      retrieval
4
 
                             -------------------
5
 
    begin                : Feb 1, 2005
6
 
    copyright            : (C) 2005 by Brendan Morley
7
 
    email                : morb at ozemail dot com dot au
8
 
 ***************************************************************************/
9
 
 
10
 
/***************************************************************************
11
 
 *                                                                         *
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.                                   *
16
 
 *                                                                         *
17
 
 ***************************************************************************/
18
 
/* $Id: qgspostgresextentthread.cpp,v 1.1 2005/03/10 05:38:27 gsherman Exp $ */
19
 
 
20
 
#include <fstream>
21
 
#include <qevent.h>
22
 
#include <qapplication.h>
23
 
 
24
 
#include "../../src/qgis.h"
25
 
#include "../../src/qgsrect.h"
26
 
#include "../../src/qgsmapcanvas.h"
27
 
 
28
 
 
29
 
#include "qgspostgresextentthread.h"
30
 
 
31
 
 
32
 
/*
33
 
QgsPostgresExtentThread::QgsPostgresExtentThread()
34
 
 : QgsPostgresProvider(), QThread()
35
 
{
36
 
 
37
 
 
38
 
 
39
 
}
40
 
 
41
 
 
42
 
QgsPostgresExtentThread::~QgsPostgresExtentThread()
43
 
{
44
 
}
45
 
*/
46
 
 
47
 
void QgsPostgresExtentThread::setConnInfo( QString s )
48
 
{
49
 
  connInfo = s;
50
 
}
51
 
 
52
 
void QgsPostgresExtentThread::setTableName( QString s )
53
 
{
54
 
  tableName = s;
55
 
}
56
 
 
57
 
void QgsPostgresExtentThread::setSqlWhereClause( QString s )
58
 
{
59
 
  sqlWhereClause = s;
60
 
}
61
 
 
62
 
void QgsPostgresExtentThread::setGeometryColumn( QString s )
63
 
{
64
 
  geometryColumn = s;
65
 
}
66
 
 
67
 
//void QgsPostgresExtentThread::setCallback( QgsPostgresProvider* o )
68
 
//{
69
 
//  callbackObject = o;
70
 
//}
71
 
 
72
 
 
73
 
void QgsPostgresExtentThread::run()
74
 
{
75
 
//  // placeholders for now.
76
 
//  QString connInfo;
77
 
  
78
 
  std::cout << "QgsPostgresExtentThread: Started running." << std::endl;
79
 
 
80
 
  // Open another connection to the database
81
 
  PGconn *connection = PQconnectdb((const char *) connInfo);
82
 
 
83
 
  // get the extents
84
 
 
85
 
  QString sql = "select extent(" + geometryColumn + ") from " + tableName;
86
 
  if(sqlWhereClause.length() > 0)
87
 
  {
88
 
    sql += " where " + sqlWhereClause;
89
 
  }
90
 
 
91
 
#if WASTE_TIME
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;
95
 
#endif
96
 
 
97
 
#ifdef QGISDEBUG 
98
 
  qDebug("+++++++++QgsPostgresExtentThread::run -  Getting extents using schema.table: " + sql);
99
 
#endif
100
 
 
101
 
 
102
 
  std::cout << "QgsPostgresExtentThread: About to issue query." << std::endl;
103
 
 
104
 
  PGresult *result = PQexec(connection, (const char *) sql);
105
 
  
106
 
  std::cout << "QgsPostgresExtentThread: Query completed." << std::endl;
107
 
 
108
 
  
109
 
    
110
 
  std::string box3d = PQgetvalue(result, 0, 0);
111
 
  std::string s;
112
 
 
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);
117
 
 
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);
121
 
 
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);
126
 
 
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);
130
 
 
131
 
  layerExtent = new QgsRect(minx, miny, maxx, maxy);
132
 
  
133
 
/*
134
 
  layerExtent.setXmax(maxx);
135
 
  layerExtent.setXmin(minx);
136
 
  layerExtent.setYmax(maxy);
137
 
  layerExtent.setYmin(miny);
138
 
*/
139
 
  
140
 
 
141
 
#ifdef QGISDEBUG
142
 
  std::cout << "QgsPostgresExtentThread: Set extents to: " 
143
 
        << layerExtent->xMin() << ", " << layerExtent->yMin() <<
144
 
    " " << layerExtent->xMax() << ", " << layerExtent->yMax() << std::endl;
145
 
#endif
146
 
 
147
 
  // clear query result
148
 
  PQclear(result);
149
 
 
150
 
 
151
 
  // Send some events (instead of a signal) as it is thread-safe
152
 
  
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)
156
 
 
157
 
  std::cout << "QgsPostgresExtentThread: About to create and dispatch event " << QGis::ProviderExtentCalcEvent << " to callback" << std::endl;
158
 
  
159
 
  QCustomEvent * e1 = new QCustomEvent ( QGis::ProviderExtentCalcEvent );
160
 
  e1->setData(layerExtent);
161
 
  QApplication::postEvent( (QObject *)callbackObject, e1);
162
 
  
163
 
//  QApplication::postEvent(qApp->mainWidget(), e1);
164
 
  
165
 
  std::cout << "QgsPostgresExtentThread: Posted event " << QGis::ProviderExtentCalcEvent << " to callback" << std::endl;
166
 
 
167
 
  
168
 
  std::cout << "QgsPostgresExtentThread: About to finish connection." << std::endl;
169
 
 
170
 
  // ending the thread, clean up
171
 
  PQfinish(connection);
172
 
  
173
 
  std::cout << "QgsPostgresExtentThread: About to complete running." << std::endl;
174
 
  
175
 
    
176
 
}
177