[QGIS-Developer] Common PyQGIS functions for QGIS 3

Thomas Baumann rdbath.regiodata at gmail.com
Wed Feb 20 06:50:40 PST 2019


Hi Nyall,

thanks for your feedback.
You are right that some of my examples were not ideal to explain what I
meant, so I try to explain it a bit better this time ;-)

Some of the following pyqgis-examples may be outdated as things can be done
easier in qgis3 now but they should just be an example
for some functions that were written by users to make life easier during
the development of QGIS plugins.


Example1: QgsVectorlayer:

Am Mi., 20. Feb. 2019 um 10:29 Uhr schrieb Nyall Dawson <
nyall.dawson at gmail.com>:

>
> This is a single line via the existing API :
>
>     vl = QgsVectorLayer( '/home/me/my.shp' , 'my_layer' )
>
> I'm curious to hear how this could be improved?
>

I wrote "load a vectorlayer" but meant "create a vectorlayer"

Something like:

https://gist.github.com/rdbath/bbba7640cf98ab09dc9bb6018011f05e#file-pyqgis_common_functions_examples-py-L24


Example2: For loading layer I meant something like

https://gist.github.com/rdbath/bbba7640cf98ab09dc9bb6018011f05e#file-pyqgis_common_functions_examples-py-L7
:


def load_layer(filename, name = None):
'''
Tries to load a layer from the given file
:param filename: the path to the file to load.
:param name: the name to use for adding the layer to the current project.
If not passed or None, it will use the filename basename
'''
name = name or os.path.splitext(os.path.basename(filename))[0]
qgslayer = QgsVectorLayer(filename, name, 'ogr')
if not qgslayer.isValid():
qgslayer = QgsRasterLayer(filename, name)
if not qgslayer.isValid():
raise RuntimeError('Could not load layer: ' + unicode(filename))

return qgslayer

-----

Example3: Iterate through layers:


> # all layers
> layers = [l.layer() for l in
> QgsProject.instance().layerTreeRoot().findLayers()]
>


> # visible layers
> visible_layers = [l.layer() for l in
> QgsProject.instance().layerTreeRoot().findLayers() if l.isVisible()]
>
> Ok, not super-obvious in this case, but still quite concise ;)
>

For the example of  visible_layers = [l.layer() for l in
QgsProject.instance().layerTreeRoot().findLayers() if l.isVisible()]  :
I would probably have needed some minutes to search in one of my plugins if
I have used such a code snippet before or google gis.stackexchange ;-)
Probably I also wouldn't have written such a fancy nice one-liner to get
these layers.

So if there would be a pyqgis-commons library where I would know that I
just have to call a function "get_visible_layers" instead of  using
[l.layer() for l in
QgsProject.instance().layerTreeRoot().findLayers() if l.isVisible()]  I
probably could use this in my next plugin without having to search/google
how to do this.


Example4: show messages / log messages:

>
> > - show a message with different levels (info, warning, critical)
>
> iface.messageBar().pushWarning('blah','some message')
> iface.messageBar().pushInfo('blah','some message')
> iface.messageBar().pushCritical('blah','some message')
>
> > - log messages
> QgsMessageLog.logMessage('my plugin','something', Qgis.Critical)



I had something in mind like the MessageManager of Germán Carrillo:

https://github.com/gacarrillor/AutoFields/blob/master/MessageManager.py

I like the MessageManager because I can easily switch between the
message-modes 'production' or 'debug.

One other example would be the function "display_warning_message_bar" from
the inasafe-utilities where I can show a short message and also provide a
button to show more details about a warning:
https://github.com/inasafe/inasafe/blob/bd00bfeac510722b427544b186bfa10861749e51/safe/utilities/qgis_utilities.py#L132



 Example5: Settings:

>
> > - reading setting, writing settings
>
> The QgsSettings class should make this straightforward
>

Here I hat something in mind like the qgis-settingsmanager of opengis (
https://github.com/opengisch/qgissettingmanager ).
I know it is not only a collection of python-/pyqt-functions but a whole
python module but I still it is an example of how functionalities are
provided to make the development of plugins easier.


So it is always about providing some helper functions that I could also
write by myself but which will save time during development and can help me
to do the things in a consistent way in all of my plugins.



> I'm not saying we can't improve the PyQGIS experience, but I don't
> personally see these as good candidates.
>
> I think we have a LOT of work to do with things like:
> - throwing proper exceptions instead of crashing/returning "None"
> values/returning "invalid" objects (e.g. QgsGeometry.fromWkt should
> ideally raise an exception for invalid wkt instead of returning a null
> geometry)
> - providing Python-esque things like __repr__ methods, __len__, []
> operators (which behave in a python style, e.g. with -1 returning the
> last item), iterators, etc
> - providing more "context managers" (e.g. with .... : #something )
>

I don't have such a deep insight in the QGIS-codebase but I guess this
would probably have to be done in the C++-part of QGIS, wouldn't it?


 (Definitely don't take this as a "don't proceed"... there is a VERY

> STRONG need to improve the PyQGIS API/Documentation/examples, and this
> would be very valuable work!)
>
>
I agree. Even if the PyQGIS-Documentation is now much better than in the
past for me often some short examples like the ones from Thomas Gratier are
really helpful to know how to proceed:
https://webgeodatavore.github.io/pyqgis-samples/

So I see three ideas here:

- provide a collection of common PyQGIS-functions
- provide nice PyQGIS-examples
- improve the PyQGIS experience in generall (your examples like throwing
proper exception)


regards,
Thomas



Nyall
>
> >
> >
> >
> > Wouldn't it be possible to provide such a collection not only from
> privat persons/projects but from the QGIS-project itself so users could add
> common functions?
> > I think the chances would be higher that such a "official" collection
> would be used in the long run and constantly extended.
> >
> > Apart from the fact that developers could save time while writing their
> plugins this could perhaps also help to improve the overall quality of the
> qgis-plugins as there would probably be several persons who could/would
> countercheck these common functions to make sure they are well written.
> >
> > regards,
> > Thomas
> >
> > PS: I asked something similar also on gis.stackexchange recently:
> >
> >
> https://gis.stackexchange.com/questions/311755/looking-for-common-pyqgis-functions-for-qgis-3
> >
> >
> >
> >
> > _______________________________________________
> > QGIS-Developer mailing list
> > QGIS-Developer at lists.osgeo.org
> > List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> > Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20190220/44c76d4d/attachment-0001.html>


More information about the QGIS-Developer mailing list