[Qgis-developer] Using api to move features

Jordi Torres jtorresfabra at gmail.com
Fri Jun 14 04:39:07 PDT 2013


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/197b3f7c/attachment-0001.html>


More information about the Qgis-developer mailing list