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 <[email protected]> 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
> [email protected]
> List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer
_______________________________________________
Qgis-developer mailing list
[email protected]
List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer

Reply via email to