Re: [Qgis-developer] Create memory layer containing only features visible in current canvas extent
Hi, Seem a good candidate for PyQGIS Cookbook ... Regards, Harrissou 2017-01-12 17:43 GMT+01:00 Martin Dobias: > 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 > 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@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@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@lists.osgeo.org List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer
Re: [Qgis-developer] Create memory layer containing only features visible in current canvas extent
Many thanks, Matthias and Martin - I got it working exactly as you suggested. Is this the mechanism which QGIS itself uses to determine which features are in the viewport? I'm running into some performance issue which I thought this would help, but they are not solved yet, so I might soon have a more general question about identifying bottlenecks in PyQGIS code. 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-tp5303218p5303381.html Sent from the Quantum GIS - Developer mailing list archive at Nabble.com. ___ Qgis-developer mailing list Qgis-developer@lists.osgeo.org List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer
Re: [Qgis-developer] Create memory layer containing only features visible in current canvas extent
This looks ideal. Thanks, both. Plus I could make ExactIntersect an option in the GUI, as sometimes dotted could be more critical than accuracy. Thanks again 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-tp5303218p5303246.html Sent from the Quantum GIS - Developer mailing list archive at Nabble.com. ___ Qgis-developer mailing list Qgis-developer@lists.osgeo.org List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer
Re: [Qgis-developer] Create memory layer containing only features visible in current canvas extent
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 Chadwinwrote: > 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@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@lists.osgeo.org List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer
Re: [Qgis-developer] Create memory layer containing only features visible in current canvas extent
http://www.qgis.org/api/classQgsFeatureRequest.html#ac7c734b9cab6939a706ea53040afe6b3 that might help On January 12, 2017 5:20:07 PM GMT+01:00, Tom Chadwinwrote: >I've found this method: > >viewportPolygon = >QgsGeometry().fromWkt(iface.mapCanvas().extent().asWktPolygon()) >layer = iface.activeLayer() > >resultlayer = QgsVectorLayer("Polygon", "result", "memory") >resultlayer.dataProvider().addAttributes(list(layer.dataProvider().fields())) > >clippedFeatures = [] >for feature in layer.dataProvider().getFeatures(): >clippedGeometry = feature.geometry().intersection(viewportPolygon) > > >From http://gis.stackexchange.com/a/74471/36921 > >However, this loops through all features. I'm concerned that this will >struggle with large layers. Is there a more efficient method? > > > >- >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-tp5303218p5303223.html >Sent from the Quantum GIS - Developer mailing list archive at >Nabble.com. >___ >Qgis-developer mailing list >Qgis-developer@lists.osgeo.org >List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer >Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer -- Sent from my Android device with K-9 Mail. Please excuse my brevity.___ Qgis-developer mailing list Qgis-developer@lists.osgeo.org List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer
Re: [Qgis-developer] Create memory layer containing only features visible in current canvas extent
Il 12/01/2017 17:20, Tom Chadwin ha scritto: > However, this loops through all features. I'm concerned that this will > struggle with large layers. Is there a more efficient method? I guess that's unsuitable for layers of millions of records. all the best. -- Paolo Cavallini - www.faunalia.eu QGIS & PostGIS courses: http://www.faunalia.eu/training.html https://www.google.com/trends/explore?date=all=IT=qgis,arcgis ___ Qgis-developer mailing list Qgis-developer@lists.osgeo.org List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer
Re: [Qgis-developer] Create memory layer containing only features visible in current canvas extent
I've found this method: viewportPolygon = QgsGeometry().fromWkt(iface.mapCanvas().extent().asWktPolygon()) layer = iface.activeLayer() resultlayer = QgsVectorLayer("Polygon", "result", "memory") resultlayer.dataProvider().addAttributes(list(layer.dataProvider().fields())) clippedFeatures = [] for feature in layer.dataProvider().getFeatures(): clippedGeometry = feature.geometry().intersection(viewportPolygon) From http://gis.stackexchange.com/a/74471/36921 However, this loops through all features. I'm concerned that this will struggle with large layers. Is there a more efficient method? - 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-tp5303218p5303223.html Sent from the Quantum GIS - Developer mailing list archive at Nabble.com. ___ Qgis-developer mailing list Qgis-developer@lists.osgeo.org List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer
[Qgis-developer] Create memory layer containing only features visible in current canvas extent
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@lists.osgeo.org List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer