Skip to content

Commit

Permalink
initial point and line drawing
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk@32 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
gsherman committed Jul 19, 2002
1 parent 022574f commit d028a06
Show file tree
Hide file tree
Showing 12 changed files with 217 additions and 30 deletions.
8 changes: 8 additions & 0 deletions src/qgisapp.cpp
Expand Up @@ -31,9 +31,11 @@
#include <libpq++.h>
#include <iostream>
#include <iomanip>
#include "qgsrect.h"
#include "qgsmapcanvas.h"
#include "qgsdbsourceselect.h"
#include "qgsdatabaselayer.h"

#include "qgisapp.h"
#include "xpm/qgis.xpm"

Expand Down Expand Up @@ -95,6 +97,7 @@ QgisApp::addLayer ()
mapCanvas->addLayer (lyr);
// no drawing done -- need to pass the layer collection
// to the rendering engine (yet to be written)

++it;
}

Expand Down Expand Up @@ -247,3 +250,8 @@ QgisApp::drawPoint (double x, double y)
paint.drawPoint (x, y);
paint.end ();
}

void QgisApp::drawLayers(){
cout << "In QgisApp::drawLayers()" << endl;
mapCanvas->render();
}
2 changes: 2 additions & 0 deletions src/qgisapp.h
Expand Up @@ -45,6 +45,8 @@ class QgisApp : public QgisAppBase {
void readWKB(const char *, QStringList tables);
//! Draw a point on the map canvas
void drawPoint(double x, double y);
//! draw layers
void drawLayers();
private:
//! Map canvase
QgsMapCanvas *mapCanvas;
Expand Down
26 changes: 24 additions & 2 deletions src/qgisappbase.ui
Expand Up @@ -95,6 +95,7 @@
<string>Toolbar_2</string>
</property>
<action name="actionAddLayer"/>
<action name="drawAction"/>
</toolbar>
</toolbars>
<actions>
Expand Down Expand Up @@ -173,6 +174,17 @@
<string>Add Layer</string>
</property>
</action>
<action>
<property name="name">
<cstring>drawAction</cstring>
</property>
<property name="iconSet">
<iconset>image5</iconset>
</property>
<property name="text">
<string>Draw</string>
</property>
</action>
</actions>
<images>
<image name="image0">
Expand All @@ -190,6 +202,9 @@
<image name="image4">
<data format="XPM.GZ" length="661">789cd3d7528808f055d0d2e72a2e492cc94c5648ce482c52d04a29cdcdad8c8eb5ade6523232520022630543251d2e253d856405bffcbc54105b19c8563600020b03103711c44d0303b04aac005546593911b74ce2d09151c60089046512a100641a14109419789f522203f6082e19385ba9d69a0b008e2682ef</data>
</image>
<image name="image5">
<data format="XPM.GZ" length="1226">789cc5d3c10ac2300c00d07bbf222cb722dd9c1741fc04c5a3201e4615f430059d0711ffdd2ada364d8a3010c30e813cb2a4b4a586e56206ba54e7aee9f616ecae3981de5cdaf6ba5a4f6faa18d5e0be1a86c54015062ccc8f87ed33479763e5625cbd4a5fa287411f598328a1d8c485388f0ce96f244347202818b60e336917d130c24d98826f9f3f9f8c61bb894622c9398be48f465e8c190191bb81463c23660ced85c4840ac629355177afd064cda7292686c67bfe74e614f91fe6df6998fc47efbdbfb94fd403df21d37e</data>
</image>
</images>
<connections>
<connection>
Expand Down Expand Up @@ -222,17 +237,24 @@
<receiver>QgisAppBase</receiver>
<slot>zoomIn()</slot>
</connection>
<connection>
<sender>drawAction</sender>
<signal>activated()</signal>
<receiver>QgisAppBase</receiver>
<slot>drawLayers()</slot>
</connection>
</connections>
<includes>
<include location="local" impldecl="in implementation">qgisappbase.ui.h</include>
</includes>
<slots>
<slot>addLayer()</slot>
<slot>fileExit()</slot>
<slot>fileOpen()</slot>
<slot>addLayer()</slot>
<slot>init()</slot>
<slot>drawLayers()</slot>
<slot>zoomIn()</slot>
<slot>zoomOut()</slot>
<slot>init()</slot>
</slots>
<layoutdefaults spacing="6" margin="11"/>
</UI>
6 changes: 6 additions & 0 deletions src/qgisappbase.ui.h
Expand Up @@ -47,3 +47,9 @@ void QgisAppBase::init()
PopupMenu_2->setFont( menubar_font );
*/
}


void QgisAppBase::drawLayers()
{

}
77 changes: 72 additions & 5 deletions src/qgsdatabaselayer.cpp
@@ -1,4 +1,5 @@
#include <qstring.h>
#include <qpainter.h>
#include "qgsrect.h"
#include <libpq++.h>
#include <qmessagebox.h>
Expand Down Expand Up @@ -59,7 +60,7 @@ QgsDatabaseLayer::~QgsDatabaseLayer(){
QgsRect QgsDatabaseLayer::calculateExtent(){
return layerExtent;
}
void QgsDatabaseLayer::draw(QPainter *p, QgsRect *viewExtent){
void QgsDatabaseLayer::draw(QPainter *p, QgsRect *viewExtent, int yTransform){
// painter is active (begin has been called
/* Steps to draw the layer
1. get the features in the view extent by SQL query
Expand All @@ -68,16 +69,82 @@ void QgsDatabaseLayer::draw(QPainter *p, QgsRect *viewExtent){
4. draw
*/
PgCursor pgs(dataSource, "drawCursor");
QString sql = "select asbinary(" + geometryColumn + ",'" + endianString() +
"') as features from " + tableName + " where " + geometryColumn +
" && GeometryFromText('BOX3D(" + viewExtent->xMin() + " " + viewExtent->yMin()
+ "," + viewExtent->xMax() + " " + viewExtent->yMax() + ")'::box3d,-1)";
QString sql = "select asbinary(" + geometryColumn + ",'" + endianString();
sql += "') as features from " + tableName;
sql += " where " + geometryColumn;
sql += " && GeometryFromText('BOX3D(" + viewExtent->stringRep();
sql += ")'::box3d,-1)";
qWarning(sql);
pgs.Declare((const char *)sql, true);
int res = pgs.Fetch();
cout << "Number of matching records: " << pgs.Tuples() << endl;
for (int idx = 0; idx < pgs.Tuples (); idx++)
{
// allocate memory for the item
char *feature = new char[pgs.GetLength (idx, 0) + 1];
memset (feature, '\0', pgs.GetLength (idx, 0) + 1);
memcpy (feature, pgs.GetValue (idx, 0), pgs.GetLength (idx, 0));
wkbType = (int)feature[1];
cout << "Feature type: " << wkbType << endl;
// read each feature based on its type
double *x;
double *y;
int *nPoints;
int numPoints;
int numLineStrings;
int idx,jdx;
char *ptr;
char lsb;
int ttype;
switch(wkbType){
case WKBPoint:
x = (double *) (feature + 5);
y = (double *) (feature + 5 + sizeof (double));
p->drawRect ((int) *x, yTransform - (int) *y, 15000,
15000);
break;
case WKBLineString:
// get number of points in the line
numPoints = (int)(feature + 1 + sizeof(int));
ptr = feature + 1 + 2 * sizeof(int);
for(idx = 0; idx < numPoints; idx++){
x = (double *) ptr;
ptr += sizeof(double);
y = (double *) ptr;
ptr += sizeof(double);
if(idx == 0)
p->moveTo((int) *x, yTransform - (int) *y);
else
p->lineTo((int) *x, yTransform - (int) *y);

}
break;
case WKBMultiLineString:
numLineStrings = (int)(feature[5]);
ptr = feature+9;
for(jdx = 0; jdx < numLineStrings; jdx++){
// each of these is a wbklinestring so must handle as such
lsb = *ptr;
ptr += 5; // skip type since we know its 2
nPoints = (int *)ptr;
ptr += sizeof(int);
for(idx = 0; idx < *nPoints; idx++){
x = (double *) ptr;
ptr += sizeof(double);
y = (double *) ptr;
ptr += sizeof(double);
if(idx == 0)
p->moveTo((int) *x, yTransform - (int) *y);
else
p->lineTo((int) *x, yTransform - (int) *y);

}
}
break;
case WKBPolygon:
break;
}

}


Expand Down
2 changes: 1 addition & 1 deletion src/qgsdatabaselayer.h
Expand Up @@ -38,7 +38,7 @@ class QgsDatabaseLayer : public QgsMapLayer {
QgsDatabaseLayer(const char *conninfo=0, QString table=QString::null);
//! Destructor
~QgsDatabaseLayer();
void draw(QPainter *p, QgsRect *viewExtent=0);
virtual void draw(QPainter *, QgsRect *, int );
private:
//! Calculates extent of the layer using SQL and PostGIS functions
QgsRect calculateExtent();
Expand Down
46 changes: 44 additions & 2 deletions src/qgsmapcanvas.cpp
Expand Up @@ -15,19 +15,61 @@
* *
***************************************************************************/
#include <qstring.h>
#include <qpainter.h>
#include <qrect.h>
#include "qgsrect.h"
#include "qgsmaplayer.h"
#include "qgsdatabaselayer.h"
#include "qgsmapcanvas.h"

QgsMapCanvas::QgsMapCanvas(QWidget *parent, const char *name ) : QWidget(parent,name) {
mapWindow = new QRect();
}
QgsMapCanvas::~QgsMapCanvas(){
delete mapWindow;
}
void QgsMapCanvas::addLayer(QgsMapLayer *lyr){
layers[lyr->name()] = *lyr;
layers[lyr->name()] = lyr;
// update extent if warranted
if(layers.size() == 1){
fullExtent = lyr->extent();
}
// set zpos to something...
//lyr->zpos = 0;
}
void QgsMapCanvas::render(){
QPainter *paint = new QPainter();
paint->begin(this);
currentExtent = fullExtent;
mapWindow->setLeft(currentExtent.xMin());
mapWindow->setBottom(currentExtent.yMin());
// determine the dominate direction for the mapcanvas
if (width () > height ())
{
mapWindow->setWidth(currentExtent.width());
mapWindow->setHeight(currentExtent.width());
}
else
{
mapWindow->setWidth(currentExtent.height());
mapWindow->setHeight(currentExtent.height());
}
paint->setWindow(*mapWindow);
QRect v = paint->viewport ();
int d = QMIN (v.width (), v.height ());
int dm = QMAX(v.width(), v.height());
paint->setViewport (v.left () + (v.width () - d) / 2,
v.top () + (v.height () - d) / 2, d, d);

// render all layers in the stack, starting at the base

map<QString,QgsMapLayer *>::iterator mi = layers.begin();
int yTransform = mapWindow->bottom() - abs(mapWindow->height() - currentExtent.height())/2;
while(mi != layers.end()){
QgsMapLayer *ml = (*mi).second;
// QgsDatabaseLayer *dbl = (QgsDatabaseLayer *)&ml;
ml->draw(paint, &currentExtent, yTransform);
mi++;
// mi.draw(p, &fullExtent);
}
paint->end();
}
40 changes: 23 additions & 17 deletions src/qgsmapcanvas.h
Expand Up @@ -4,7 +4,7 @@
begin : Sun Jun 30 2002
copyright : (C) 2002 by Gary E.Sherman
email : sherman@mrcc.com
***************************************************************************/
***************************************************************************/

/***************************************************************************
* *
Expand All @@ -19,31 +19,37 @@
#define QGSMAPCANVAS_H
#include <map>
#include <qwidget.h>

#include "qgsrect.h"
class QRect;
class QgsMapLayer;

/*! \class QgsMapCanvas
* \brief Map canvas class for displaying all GIS data types.
*/

class QgsMapCanvas : public QWidget {
Q_OBJECT
Q_OBJECT
public:
//! Constructor
QgsMapCanvas(QWidget *parent=0, const char *name=0);
//! Destructor
~QgsMapCanvas();
/*! Adds a layer to the map canvas.
* @param lyr Pointer to a layer derived from QgsMapLayer
*/
void addLayer(QgsMapLayer *lyr);
/*! Draw the map using the symbology set for each layer
*/
void render();
private:
//! Constructor
QgsMapCanvas(QWidget *parent=0, const char *name=0);
//! Destructor
~QgsMapCanvas();
/*! Adds a layer to the map canvas.
* @param lyr Pointer to a layer derived from QgsMapLayer
*/
void addLayer(QgsMapLayer *lyr);
/*! Draw the map using the symbology set for each layer
*/
void render();
private:

//! map containing the layers by name
map<QString,QgsMapLayer> layers;
//! map containing the layers by name
map<QString,QgsMapLayer *>layers;
//! Full extent of the map canvas
QgsRect fullExtent;
//! Current extent
QgsRect currentExtent;
QRect *mapWindow;
};

#endif
10 changes: 9 additions & 1 deletion src/qgsmaplayer.cpp
Expand Up @@ -14,6 +14,7 @@
* (at your option) any later version. *
* *
***************************************************************************/
#include <iostream>
#include "qgsrect.h"
#include "qgsmaplayer.h"

Expand All @@ -37,7 +38,14 @@ void QgsMapLayer::setlayerName( const QString& _newVal){
const QString QgsMapLayer::name(){
return layerName;
}
const QgsRect QgsMapLayer::extent(){
return layerExtent;
}
QgsRect QgsMapLayer::calculateExtent(){

}
void QgsMapLayer::draw(QPainter *, QgsRect *){
void QgsMapLayer::draw(QPainter *p, QgsRect *viewExtent, int yTransform){
cout << "In QgsMapLayer::draw" << endl;
}


2 changes: 1 addition & 1 deletion src/qgsmaplayer.h
Expand Up @@ -55,7 +55,7 @@ class QgsMapLayer : public QgsDataSource {
* based on the layer type
*/
virtual QgsRect calculateExtent();
virtual void draw(QPainter *, QgsRect * = 0);
virtual void draw(QPainter *, QgsRect *, int);
/*! Return the extent of the layer as a QRect
*/
const QgsRect extent();
Expand Down

0 comments on commit d028a06

Please sign in to comment.