<div dir="ltr"><div><div>Hi,<br></div>Seem a good candidate for PyQGIS Cookbook ...<br><br></div>Regards,<br>Harrissou<br></div><div class="gmail_extra"><br><div class="gmail_quote">2017-01-12 17:43 GMT+01:00 Martin Dobias <span dir="ltr"><<a href="mailto:wonder.sk@gmail.com" target="_blank">wonder.sk@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Tom<br>
<br>
Try something like this:<br>
<br>
request = QgsFeatureRequest(iface.<wbr>mapCanvas().extent())<br>
request.setFlags(<wbr>QgsFeatureRequest.<wbr>ExactIntersect)<br>
for f in layer.getFeatures(r):<br>
  print <a href="http://f.id" rel="noreferrer" target="_blank">f.id</a>()<br>
<br>
This feature request will only use features that intersect canvas<br>
view. The extra flag ExactIntersect is there to make sure that true<br>
intersection test is done, rather just doing fast intersection of<br>
geometry's bounding box with the canvas extent recangle.<br>
<br>
Cheers<br>
<span class="HOEnZb"><font color="#888888">Martin<br>
</font></span><div class="HOEnZb"><div class="h5"><br>
On Fri, Jan 13, 2017 at 12:07 AM, Tom Chadwin <<a href="mailto:tom.chadwin@nnpa.org.uk">tom.chadwin@nnpa.org.uk</a>> wrote:<br>
> Hello all<br>
><br>
> I currently create a memory layer from an existing layer as follows<br>
> ('usedFields' is a subset of the original layer's attributes/fields):<br>
><br>
> newlayer = QgsVectorLayer(uri, <a href="http://layer.name" rel="noreferrer" target="_blank">layer.name</a>(), 'memory')<br>
> writer = newlayer.dataProvider()<br>
> outFeat = QgsFeature()<br>
> for feature in layer.getFeatures():<br>
>     if feature.geometry() is not None:<br>
>         outFeat.setGeometry(feature.<wbr>geometry())<br>
>     attrs = [feature[f] for f in usedFields]<br>
>     if attrs:<br>
>         outFeat.setAttributes(attrs)<br>
>     writer.addFeatures([outFeat])<br>
><br>
> What is the best way to add only features visible in the current QGIS view<br>
> of the canvas, rather than every feature in the layer?<br>
><br>
> Thanks<br>
><br>
> Tom<br>
><br>
><br>
><br>
> -----<br>
> Buy Pie Spy: Adventures in British pastry 2010-11 on Amazon<br>
> --<br>
> View this message in context: <a href="http://osgeo-org.1560.x6.nabble.com/Create-memory-layer-containing-only-features-visible-in-current-canvas-extent-tp5303218.html" rel="noreferrer" target="_blank">http://osgeo-org.1560.x6.<wbr>nabble.com/Create-memory-<wbr>layer-containing-only-<wbr>features-visible-in-current-<wbr>canvas-extent-tp5303218.html</a><br>
> Sent from the Quantum GIS - Developer mailing list archive at Nabble.com.<br>
> ______________________________<wbr>_________________<br>
> Qgis-developer mailing list<br>
> <a href="mailto:Qgis-developer@lists.osgeo.org">Qgis-developer@lists.osgeo.org</a><br>
> List info: <a href="http://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer" target="_blank">http://lists.osgeo.org/<wbr>mailman/listinfo/qgis-<wbr>developer</a><br>
> Unsubscribe: <a href="http://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer" target="_blank">http://lists.osgeo.org/<wbr>mailman/listinfo/qgis-<wbr>developer</a><br>
______________________________<wbr>_________________<br>
Qgis-developer mailing list<br>
<a href="mailto:Qgis-developer@lists.osgeo.org">Qgis-developer@lists.osgeo.org</a><br>
List info: <a href="http://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer" target="_blank">http://lists.osgeo.org/<wbr>mailman/listinfo/qgis-<wbr>developer</a><br>
Unsubscribe: <a href="http://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer" target="_blank">http://lists.osgeo.org/<wbr>mailman/listinfo/qgis-<wbr>developer</a></div></div></blockquote></div><br></div>