[Qgis-developer] Create memory layer containing only features visible in current canvas extent

Martin Dobias wonder.sk at gmail.com
Thu Jan 12 08:43:47 PST 2017


Hi Tom

Try something like this:

request = QgsFeatureRequest(iface.mapCanvas().extent())
request.setFlags(QgsFeatureRequest.ExactIntersect)
for f in layer.getFeatures(r):
  print f.id()

This feature request will only use features that intersect canvas
view. The extra flag ExactIntersect is there to make sure that true
intersection test is done, rather just doing fast intersection of
geometry's bounding box with the canvas extent recangle.

Cheers
Martin

On Fri, Jan 13, 2017 at 12:07 AM, Tom Chadwin <tom.chadwin at nnpa.org.uk> wrote:
> Hello all
>
> I currently create a memory layer from an existing layer as follows
> ('usedFields' is a subset of the original layer's attributes/fields):
>
> newlayer = QgsVectorLayer(uri, layer.name(), 'memory')
> writer = newlayer.dataProvider()
> outFeat = QgsFeature()
> for feature in layer.getFeatures():
>     if feature.geometry() is not None:
>         outFeat.setGeometry(feature.geometry())
>     attrs = [feature[f] for f in usedFields]
>     if attrs:
>         outFeat.setAttributes(attrs)
>     writer.addFeatures([outFeat])
>
> What is the best way to add only features visible in the current QGIS view
> of the canvas, rather than every feature in the layer?
>
> Thanks
>
> Tom
>
>
>
> -----
> Buy Pie Spy: Adventures in British pastry 2010-11 on Amazon
> --
> View this message in context: http://osgeo-org.1560.x6.nabble.com/Create-memory-layer-containing-only-features-visible-in-current-canvas-extent-tp5303218.html
> Sent from the Quantum GIS - Developer mailing list archive at Nabble.com.
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer


More information about the Qgis-developer mailing list