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

DelazJ delazj at gmail.com
Fri Jan 13 06:56:04 PST 2017


Hi,
Seem a good candidate for PyQGIS Cookbook ...

Regards,
Harrissou

2017-01-12 17:43 GMT+01:00 Martin Dobias <wonder.sk at gmail.com>:

> 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
> _______________________________________________
> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20170113/9204bd50/attachment-0001.html>


More information about the Qgis-developer mailing list