Rendering single layer from mapcanvas

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Rendering single layer from mapcanvas

bonar
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