[Qgis-developer] Using api to move features

Marco Gomes marco.gomes at edisoft.pt
Fri Jun 14 08:44:06 PDT 2013


Thank you Jordi for you help.

 

I finally solve my problem and your algorithm work fine (beside a little
slow refreshing....not sure if I could improve the refresh update to be
quicker and not flicking...).

 

My problem, were in the figure id that I am using! In my application i
set the figure id, during it's creation, but Qgis internally, modify it
and remade the numeration starting by 1. Not sure if this is a good
behavior or bug.

 

 

From: Jordi Torres [mailto:jtorresfabra at gmail.com] 
Sent: Friday, June 14, 2013 12:39 PM
To: Marco Gomes
Cc: qgis-developer at lists.osgeo.org
Subject: Re: [Qgis-developer] Using api to move features

 

Hi Marco, 


I'm using  categorized symbology too, the only difference I see is that
I'm not calling 

categorizedSymbolRendererV2->startRender(QgsRenderContext(),
symbolsLayer);

nor 


categorizedSymbolRendererV2->renderFeature(feature, QgsRenderContext(),
symbolsLayer->id().toInt(), false, false);

Maybe you can try a simple example with default simbology and see if it
works ok, and then try to port it to your code. Anyway I'm new to qgis
too, so not an expert here :)

Cheers. 

 

2013/6/14 Marco Gomes <marco.gomes at edisoft.pt>

Thank you Jordi!

 

But i am using a point memory layer as you mention but using special
markers as defined below, and the code that you propose me, do not
worked and my features with the symbols do not move. I need to have any
kind of special consideration when moving feature points with marker
symbols attached?

 

//create Vector layer in memory for adding iamge markers

QgsVectorLayer *symbolsLayer = new QgsVectorLayer("Point",
"temporairy_points", "memory");

 

QgsVectorDataProvider *provider = symbolsLayer->dataProvider();

 

//create new feature

QgsFeature feature = QgsFeature();

 

QgsPoint point =
ui.qgsMapCanvas->getCoordinateTransform()->toMapCoordinates(e->pos().x()
, e->pos().y());

QgsGeometry *geometry = geometry->fromPoint(point);         

feature.setGeometry(geometry);

 

QgsSvgMarkerSymbolLayerV2 svgMarkersymbolLayerV2 = new
QgsSvgMarkerSymbolLayerV2(QCoreApplication::applicationDirPath () +
"/data/symbols/test.svg");

 

QgsSymbolLayerV2List symbolLayerV2List;

symbolLayerV2List.clear();

symbolLayerV2List.append(svgMarkersymbolLayerV2);

 

QgsMarkerSymbolV2 *markerSymbolV2 = new
QgsMarkerSymbolV2(symbolLayerV2List);

 

QgsRendererCategoryV2 *rendererCategoryV2 = new QgsRendererCategoryV2(0,
markerSymbolV2, "0");

 

QgsCategoryList categoryList;

categoryList.clear();

categoryList.append(*rendererCategoryV2);

 

QgsCategorizedSymbolRendererV2 *categorizedSymbolRendererV2 = new
QgsCategorizedSymbolRendererV2("A", categoryList);

 

symbolsLayer->startEditing();

symbolsLayer->setRendererV2(categorizedSymbolRendererV2);               

symbolsLayer->addAttribute(QgsField("A", QVariant::String, "String", 4,
0, ""));

 

QList<QgsField> lstFields;

lstFields.insert(0, QgsField("A", QVariant::String, "String", 4, 0,
""));

provider->addAttributes(lstFields);

 

categorizedSymbolRendererV2->startRender(QgsRenderContext(),
symbolsLayer);

 

feature.clearAttributeMap();

QgsAttributeMap attributeMap;

attributeMap.insert(0, "0");

feature.setAttributeMap(attributeMap);

 

categorizedSymbolRendererV2->renderFeature(feature, QgsRenderContext(),
symbolsLayer->id().toInt(), false, false);

 

QgsFeatureList qgsfeaturesList = QgsFeatureList();

qgsfeaturesList.append(feature);

provider->addFeatures(qgsfeaturesList);

 

symbolsLayer->updateExtents();

 

// Add the Vector Layer to the Layer Registry

QgsMapLayerRegistry::instance()->addMapLayer(symbolsLayer, false);

 

myLayerSet.prepend(QgsMapCanvasLayer(symbolsLayer, true));

 

// Set the Map Canvas Layer Set

ui.qgsMapCanvas->setLayerSet(myLayerSet);

 

 

 

From: qgis-developer-bounces at lists.osgeo.org
[mailto:qgis-developer-bounces at lists.osgeo.org] On Behalf Of Jordi
Torres
Sent: Friday, June 14, 2013 11:54 AM
To: qgis-developer at lists.osgeo.org
Subject: Re: [Qgis-developer] Using api to move features

 

One more thing:


It should be _vectorLayer instead of _contactLayer in the last three
lines.

Cheers.

 

2013/6/14 Jordi Torres <jtorresfabra at gmail.com>

OOPs forgot to send it to the list.

 

Hi Marco, 

This is working for me, don't know if is the best option:

//_vector Layer is a point layer in memory

 _vectorLayer->startEditing();
 QgsGeometryMap m;

//Pass the feature id and the new x,y

 m.insert(feature.id(),*QgsGeometry::fromPoint(QgsPoint(x,y)));
 _vectorLayer->dataProvider()->changeGeometryValues(m);
 _contactLayer->updateExtents();
 _contactLayer->commitChanges();

 _contactLayer.triggerRepaint();

Hope it helps.

 

2013/6/14 Marco Gomes <marco.gomes at edisoft.pt>

I am trying to move features in a Qt timer without success. What it is
missing?

 

Below all my trials to make this work.  

 

 
//pointMarkersLayer->dataProvider()->changeGeometryValues(geomMap);

       //pointMarkersLayer->updateFeature(featureRoute_p);

                    

 

       //pointMarkersLayer->changeGeometry(featureRoute_p.id(),
geometry);

       //pointMarkersLayer->moveVertex(finalPoint.x(), finalPoint.y(),
featureRoute_p.id(), 0);

 

       //pointMarkersLayer->changeGeometry(featureRoute_p.id(),
geometry);

       //pointMarkersLayer->updateFeature(featureRoute_p);

       //categorizedSymbolRendererV2->renderFeature(featureRoute_p,
QgsRenderContext(), pointMarkersLayer->id().toInt(), false, false);

 

       //pointMarkersLayer->startEditing();

       //pointMarkersLayer->select(featureRoute_p.id(), false);

 

       //featureRoute_p.setGeometry(QgsGeometry::fromPoint(finalPoint));

       //pointMarkersLayer->updateFeature(featureRoute_p);

 

       //float dx = finalPoint.x() -
featureRoute_p.geometry()->asPoint().x();

       //float dy = finalPoint.y() -
featureRoute_p.geometry()->asPoint().y();

 

       //pointMarkersLayer->translateFeature(featureRoute_p.id(), dx,
dy);

 

 

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




-- 

Jordi Torres





-- 

Jordi Torres




-- 

Jordi Torres


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




-- 

Jordi Torres



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20130614/d47e3792/attachment-0001.html>


More information about the Qgis-developer mailing list