AW: [Qgis-developer] Rendering single layer from mapcanvas

Hugentobler Marco marco.hugentobler at karto.baug.ethz.ch
Mon May 25 07:52:29 EDT 2009


Hi Bonar

QGIS renders the map into one image canvas item (QgsMapCanvasMap). Currently, it is not possible to refresh only one layer in this item. 
However, it is possible to render the content of the three layers that stay constant into a QgsMapCanvasMap (as you do now with all the four layers). For the layer that changes you could create a new canvas item (as a new subclass of QgsMapCanvasItem). In this subclass you need to create a QgsVectorLayer configured with your database instance . Then you could implement the paint method, there query all the features inside the view extent, use the transformation methods of QgsMapCanvasItem  (toCanvasCoordinates()) and finally use QPainter to draw the objects as you want. Important is that the new item has the same size as the mapcanvas and also a transparent background such that you may see the content of the other three background layers.

If then something in the datasource changes, you update your item class and only the item with one layer is repainted. The background with the three other layers stays the same.


Regards,
Marco

 


-----Ursprüngliche Nachricht-----
Von: qgis-developer-bounces at lists.osgeo.org im Auftrag von bonar
Gesendet: Mo 25.05.2009 11:41
An: qgis-developer at lists.osgeo.org
Betreff: [Qgis-developer] Rendering single layer from mapcanvas
 

Hi guys, Tim & Dr.Marco 

I was tried to display real-time data in my mapcanvas. In my mapcanvas, i
have 4 layer, but i juz want to refresh 1 layer only(this layer retrieve
data from PostGis) from the mapcanvas without refreshing all layer in the
mapcanvas. I already searching from this forum and Google it, and the result
is i should use QgsMapCanvasItem that have that capabilities to rendering
single layer without refreshing mapcanvas. But, i don't know how to
implement it in my application. I'm using sample application from Mr.Tim
Sutton, it is a great tutorial for a beginner like me, and i think its
better to include the code in here. This is what i already done: 


//Setting my layer 

mpMapCanvas= new QgsMapCanvas(0, 0); 
  mpMapCanvas->enableAntiAliasing(true); 
  mpMapCanvas->useImageToRender(true); 
  mpMapCanvas->setCanvasColor(QColor(255, 255, 255)); 
  mpMapCanvas->freeze(false); 
  mpMapCanvas->setVisible(true); 

  QString myLayerPathNegeri         = LAYERPATH() + "klkv_state.shp"; 
  QString myLayerBaseNameNegeri     = "klkv_state"; 
  QString myProviderName            = "ogr"; 

  QString myLayerPathDistrict         = LAYERPATH() + "klkv_district.shp"; 
  QString myLayerBaseNameDistrict     = "klkv_district"; 

  QString myLayerPathMukim         = LAYERPATH() + "klkv_mukim.shp"; 
  QString myLayerBaseNameMukim     = "klkv_mukim"; 

QString myLayerPathPoi         = "location_poi"; 
  QString myLayerBaseNamePoi     = "location_poi"; 

QgsVectorLayer * mypLayerNegeri = new QgsVectorLayer(myLayerPathNegeri,
myLayerBaseNameNegeri, myProviderName); 

  QgsVectorLayer * mypLayerDistrict = new
QgsVectorLayer(myLayerPathDistrict, myLayerBaseNameDistrict,
myProviderName); 

  QgsVectorLayer * mypLayerMukim = new QgsVectorLayer(myLayerPathMukim,
myLayerBaseNameMukim, myProviderName); 

  QgsVectorLayer * mypLayerPoi = new
QgsVectorLayer(POSTGIS_LAYER_SETUP_INCIDENT_POI_FUNC(), myLayerBaseNamePoi,
"postgres"); 


QgsSingleSymbolRenderer *mypRendererNegeri = new
QgsSingleSymbolRenderer(mypLayerNegeri->geometryType()); 
  QList<QgsMapCanvasLayer> myLayerSet; 
  mypLayerNegeri->setRenderer(mypRendererNegeri); 

  QgsSingleSymbolRenderer *mypRendererDistrict = new
QgsSingleSymbolRenderer(mypLayerDistrict->geometryType()); 
  mypLayerDistrict->setRenderer(mypRendererDistrict); 

  QgsSingleSymbolRenderer *mypRendererMukim = new
QgsSingleSymbolRenderer(mypLayerMukim->geometryType()); 
  mypLayerMukim->setRenderer(mypRendererMukim); 


QgsSingleSymbolRenderer *mypRendererPoi = new
QgsSingleSymbolRenderer(mypLayerPoi->geometryType()); 
  mypLayerPoi->setRenderer(mypRendererPoi); 


QgsMapLayerRegistry::instance()->addMapLayer(mypLayerNegeri, TRUE); 
  QgsMapLayerRegistry::instance()->addMapLayer(mypLayerDistrict, TRUE); 
  QgsMapLayerRegistry::instance()->addMapLayer(mypLayerMukim, TRUE); 
QgsMapLayerRegistry::instance()->addMapLayer(mypLayerPoi, TRUE); 

myLayerSet.append(QgsMapCanvasLayer(mypLayerPoi)); 
myLayerSet.append(QgsMapCanvasLayer(mypLayerMukim)); 
  myLayerSet.append(QgsMapCanvasLayer(mypLayerDistrict)); 
  myLayerSet.append(QgsMapCanvasLayer(mypLayerNegeri)); 

mpMapCanvas->setLayerSet(myLayerSet); 


//I make a Qtimer to check if any update position for mypLayerPoi from
updatingLocationPoiLayer() function 

QTimer *timerPoiRenderer = new QTimer(this); 
  connect(timerPoiRenderer, SIGNAL(timeout()), this,
SLOT(updatingLocationPoiLayer())); 
  timerPoiRenderer->start(10 * 1000);// 10 seconds 


//this is how i tried to using QgsMapCanvasItem, sorry for the code because
i am beginner for this QGis, so i juz try to play-around with the
QgsMapCanvasItem code . 

void MyMain::updatingLocationPoiLayer() 
{ 

  QGraphicsScene *mScene = new QGraphicsScene(); 
  QgsMapCanvasMap* mMap = new QgsMapCanvasMap(mpMapCanvas); 
  mScene->addItem( mMap ); 
  mScene->update(); 
  QList<QGraphicsItem*> list = mScene->items(); 
  QList<QGraphicsItem*>::iterator it = list.begin(); 

  while ( it != list.end() ){ 
          QgsMapCanvasItem* item = dynamic_cast<QgsMapCanvasItem*>( *it ); 

          if(item){ 
                  item->updatePosition(); 
                  QMessageBox::information( this, tr( "TEST" ), tr( "Update
rendering" ) ); 
          }else{ 
//it will goes here. 
                  QMessageBox::information( this, tr( "TEST" ), tr( "NOT
Update rendering" ) ); 
          } 

          it++; 
  } 
} 

from my reading,what i understand is i should use
QgsMapCanvasItem::updatePosition() to make my mapcanvas look like real-time.
Is it true? And i really hope you guys can teach me about how to implement
it in my code. 


Thanks in advance, 
Bonar
-- 
View this message in context: http://n2.nabble.com/Rendering-single-layer-from-mapcanvas-tp2968794p2968794.html
Sent from the qgis-developer mailing list archive at Nabble.com.

_______________________________________________
Qgis-developer mailing list
Qgis-developer at lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-developer



More information about the Qgis-developer mailing list