[QGIS-Developer] Probably a stupid python question regarding QgsVectorlayer

Prem Kumar prem.netgis at gmail.com
Thu Apr 29 06:37:48 PDT 2021


Hi mate,

If you still need further details of database uri, you may refer to the
skeleton code below.

obj_vl = QgsVectorLayer(in_fc,"new lyr","ogr")
datapr=obj_vl.dataProvider()
lyrUri=None
if isinstance(obj_vl, QgsVectorLayer):
    lyrUri = QgsDataSourceUri(obj_vl.dataProvider().dataSourceUri())
    if lyrUri.database() == "shapefileName" or \

os.path.splitext(os.path.basename(lyrUri.uri().split('|')[0]))[0] ==
"shapefileName":
        pass

connParams = dict()
if obj_vl.providerType() == 'postgres':
    connParams['host'] = lyrUri.host()
    connParams['port'] = lyrUri.port()
    connParams['user'] = lyrUri.username()
    connParams['password'] = lyrUri.password()
elif obj_vl.providerType() == 'spatialite':
    connParams['dbPath'] = lyrUri.database()
elif obj_vl.providerType() == 'ogr':
    connParams['dbPath'] = lyrUri.database()

-Prem

On Thu, Apr 29, 2021 at 7:01 PM Bo Victor Thomsen <
bo.victor.thomsen at gmail.com> wrote:

> Thanks Alessandro :-)
>
> (I need some new reading glasses, or maybe a new brain. When you read the
> *Properties* -> *Information* tab for the layer, the needed information
> is shown in the tab with the prefix *Source: *! It shouldn't be that hard
> for me to guess!!)
>
> Med venlig hilsen / Kind regards
>
> Bo Victor Thomsen
>
> Den 29-04-2021 kl. 14:32 skrev Alessandro Pasotti:
>
> Hi Bo,
>
> iface.activeLayer().source() should give you what you want.
>
>
> On Thu, Apr 29, 2021 at 2:21 PM Bo Victor Thomsen <
> bo.victor.thomsen at gmail.com> wrote:
>
>> Hi list -
>>
>> I have a (probably totally obvious, stupid) Pyhton question:
>>
>> I need the datasource URI string of a maplayer chosen by the user.
>>
>> I have found  the QgsMapLayer chosen by the user. And the QgsVectorLayer
>> associated with the Maplayer.
>>
>> How do I find the datasource string associated with the QgsVectorLayer?
>>
>> There is oodles of QGIS information about generating a vector layer from
>> a specific datasource. I need the opposite to generate a virtual layer
>> in Python using a join between the chosen layer with a another
>> predefined layer (which I have the datasource string for).
>>
>> --
>>
>> 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
>>
>
>
> --
> Alessandro Pasotti
> QCooperative:  www.qcooperative.net
> ItOpen:   www.itopen.it
>
> _______________________________________________
> 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/20210429/61de5778/attachment.html>


More information about the QGIS-Developer mailing list