[QGIS-Developer] How to split QGISMapLayer attribute "source" into parts (python)

Nyall Dawson nyall.dawson at gmail.com
Thu Apr 25 15:56:31 PDT 2019


On Thu, 25 Apr 2019 at 21:19, Bo Victor Thomsen
<bo.victor.thomsen at gmail.com> wrote:
>
> Hi all -
>
> I have a python plugin where I iterate through all vectorlayers and get the source for each layer using
>
> mySource = myVectorLayer.source()
>
> The result could be something like this: (one line)
>
> restrictToRequestBBOX='1' srsname='EPSG:25832' typename='dai:theme-pg-natura2000_omraader_habitatnatur' url='http://wfs2-miljoegis.mim.dk/dai/ows?version=1.1.0' table="" sql=
>
>
> The question:  - How can i split this string into parts ? (In this case: restrictToRequestBBOX, srsname, typename, url, sql)

Use QgsDataSourceUri -- that's what it's designed for.

so something like

    uri = QgsDataSourceUri(layer.source())
    table = uri.table()
    user = uri.username()
    restrict_bbox = uri.param("restrictToRequestBBOX")

etc

For non-db layers (eg disk based OGR/GDAL layers, you should use
QgsProviderRegistry.instance().decodeUri(layer.source())

Nyall






>
> The obvious method is to use ex.:   myList = mySource.split(' ')
>
> But that depends on that no part id or value contains a "blank"
>
> Is there a more secure method to do the spiltting?
>
> --
> Med venlig hilsen / Kind regards
>
> Bo Victor Thomsen
>
> _______________________________________________
> 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


More information about the QGIS-Developer mailing list