Hi,

Sorry another slight miscommunication I think; I didn't add the PostGIS layer 
via the Python code. I'm saying the project I'm using already contained a 
PostGIS layer. It is the existing layers that are being corrupted by the save 
process not the ones you add.

Everything I 'do' as it were works fine it is the things that break that 
shouldn't be touched that I'm trying to understand.

Have you tried it with a PostGIS layer already in your project before you start?

The method to replicate; if that is even possible as it might be the way I'm 
running it rather than general, is:
1) Create a project with a few layers in QGIS (ensure one is a database layer)
2) Open and write the project file via a standalone script (you can add a layer 
if you like but you don't have to and thus far I've only tried adding 
GeoPackage layers)
3) Re-open the project in QGIS and look to see if all the original layers are 
still working.

I hope that clarifies but I will now have a go at adding a second PostGIS layer 
via the script to see if the existing layer is then okay. If we consider your 
earlier comment about initialising the Oracle driver it seems possible that 
adding a PostGIS layer in the standalone script will require the correct 
drivers to exist so perhaps that will protect the existing layers? I'll give it 
a shot.

Thanks,
Paul

-----Original Message-----
From: Richard Duivenvoorde <rdmaili...@duif.net>
Sent: 25 May 2021 20:32
To: Paul Wittle <paul.wit...@dorsetcouncil.gov.uk>
Cc: qgis-dev <Qgis-developer@lists.osgeo.org>
Subject: Re: [QGIS-Developer] QGIS standalone script examples (and Oracle)


Hi Paul,

Adding the following to my earlier test script resulted in a valid Postgis 
layer in QGIS:

```
uri = QgsDataSourceUri()
uri.setConnection("localhost", "5432", "gis", "user", "password") 
uri.setDataSource("public", "plaatsen", "geom")  # schema, table, geom column 
dblayer = QgsVectorLayer(uri.uri(), "plaatsen", "postgres") # uri, layername, 
provider if dblayer.isValid():
  project_instance.addMapLayer(dblayer,False)
else:
  print("Postgres layer NOT valid !")

group_entry.insertChildNode(2,QgsLayerTreeLayer(dblayer))
```

Note that the 'uri' info you can retrieve from the python console, by loading 
the layer via QGIS, make it active and then in console:

iface.mapCanvas().currentLayer().dataProvider().uri()

which for me resulted in something like:

# dbname='gis' host=localhost port=5432 user='user' password='password' 
sslmode=disable key='id' srid=0 type=Point checkPrimaryKeyUnicity='0' 
table="public"."plaatsen" (geom)

HTH,

Richard Duivenvoorde

On 5/25/21 4:04 PM, Paul Wittle wrote:
> Hi,
>
> I've now done a test using a PostGIS table instead of Oracle and I get a 
> similar result.
>
> It doesn't ask for security credentials or bring up a handle bad layers 
> dialog but the PostGIS table which is working before you run the script 
> appears as an entry in the layers list but doesn't draw.
>
> The
> <layer-tree-layer legend_exp="" source="dbname='TEST' host=***
> port=5432 sslmode=allow key='tid' srid=27700 type=Polygon
> checkPrimaryKeyUnicity='0'
> table=&quot;public&quot;.&quot;TEST_PARISHES&quot; (geom)"
> expanded="1" legend_split_behavior="0" name="TEST_PARISHES"
> checked="Qt::Checked"
> id="TEST_PARISHES_0724930d_e27b_4279_ad0d_e0f6f7bf5973"
> patch_size="-1,-1" providerKey="postgres">
>
> Converts to:
> <layer-tree-layer legend_split_behavior="0" patch_size="-1,-1"
> name="TEST_PARISHES" legend_exp=""
> id="TEST_PARISHES_0724930d_e27b_4279_ad0d_e0f6f7bf5973" expanded="1"
> checked="Qt::Checked">
>
> I think the way I'm calling the standalone script must just not work...but 
> I'm not sure why.
>
> Thanks again for all your help on this.
> Paul
>
> -----Original Message-----
> From: Paul Wittle
> Sent: 25 May 2021 08:41
> To: rdmaili...@duif.net
> Cc: qgis-dev <Qgis-developer@lists.osgeo.org>
> Subject: RE: [QGIS-Developer] QGIS standalone script examples (and
> Oracle)
>
> Ah, I see your point.
>
> I will setup a project with a PostGIS table and a couple of WMS layers and 
> see how that fairs.
>
> I terms of the error I get... When I open the project all the layers are 
> there with datasource entries but the Layer name column is blank as is the 
> provider column.
>
> So and entry reads:
>
> Layer name = null       Type = vector   provider = null Auth config = Edit    
>   Datasource = datasource (no errors there)
>
> I therefore assume it is deleting something in the canvas settings that is 
> needed for QGIS to realise the layers are Oracle. This is also proved by the 
> log windows where I get the same number of errors in a PostGIS tab even 
> though I have no PostGIS layers in the project. It appears that QGIS believes 
> them to be PostGIS layers.
>
> I'll have a go at your suggestions now.
>
> Paul
>
> -----Original Message-----
> From: Richard Duivenvoorde <rdmaili...@duif.net>
> Sent: 24 May 2021 16:59
> To: Paul Wittle <paul.wit...@dorsetcouncil.gov.uk>
> Cc: qgis-dev <Qgis-developer@lists.osgeo.org>
> Subject: Re: [QGIS-Developer] QGIS standalone script examples (and
> Oracle)
>
> On 5/24/21 2:54 PM, Paul Wittle wrote:
>
>> Unfortunately I can't simply exclude Oracle as that is currently our spatial 
>> database provider and whilst we are migrating to PostgresSQL that will be a 
>> while so I do need a solution that leaves those layers alone really.
>
> Sorry, I did not mean to exclude Oracle from the final scripts, just to check 
> if another (inbuild db) provider would work instead (as it does not need 
> separate dll's).
>
>> The batch commands I'm referring to include o4w_env.bat, qt5_env.bat and 
>> py3_env.bat which I believe are default with the install so hopefully you 
>> can see them.
>
> For what I know, both Windows install types (Standalone and osgeo4w) set 
> indeed the right paths using those bat files...
>
> Checking IF the oracle provider is working, you should be able to check with 
> a simple read of an Oracle table and counting the features or so (in pyqgis I 
> mean)?
>
> Anybody has a trick how to find out what exactly is wrong, when a layer is 
> 'invalid'?
>
> Regards,
>
> Richard Duivenvoorde
> This e-mail and any files transmitted with it are intended solely for
> the use of the individual or entity to whom they are addressed. It may
> contain unclassified but sensitive or protectively marked material and
> should be handled accordingly. Unless you are the named addressee (or
> authorised to receive it for the addressee) you may not copy or use
> it, or disclose it to anyone else. If you have received this
> transmission in error please notify the sender immediately. All
> traffic may be subject to recording and/or monitoring in accordance
> with relevant legislation. Any views expressed in this message are
> those of the individual sender, except where the sender specifies and
> with authority, states them to be the views of Dorset Council. Dorset
> Council does not accept service of documents by fax or other
> electronic means. Virus checking: Whilst all reasonable steps have
> been taken to ensure that this electronic communication and its
> attachments whether encoded, encrypted or otherwise supplied are free
> from computer viruses, Dorset Council accepts no liability in respect
> of any loss, cost, damage or expense suffered as a result of accessing
> this message or any of its attachments. For information on how Dorset
> Council processes your information, please see
> www.dorsetcouncil.gov.uk/data-protection
>

This e-mail and any files transmitted with it are intended solely for the use 
of the individual or entity to whom they are addressed. It may contain 
unclassified but sensitive or protectively marked material and should be 
handled accordingly. Unless you are the named addressee (or authorised to 
receive it for the addressee) you may not copy or use it, or disclose it to 
anyone else. If you have received this transmission in error please notify the 
sender immediately. All traffic may be subject to recording and/or monitoring 
in accordance with relevant legislation. Any views expressed in this message 
are those of the individual sender, except where the sender specifies and with 
authority, states them to be the views of Dorset Council. Dorset Council does 
not accept service of documents by fax or other electronic means. Virus 
checking: Whilst all reasonable steps have been taken to ensure that this 
electronic communication and its attachments whether encoded, encrypted or 
otherwise supp
 lied are free from computer viruses, Dorset Council accepts no liability in 
respect of any loss, cost, damage or expense suffered as a result of accessing 
this message or any of its attachments. For information on how Dorset Council 
processes your information, please see www.dorsetcouncil.gov.uk/data-protection
_______________________________________________
QGIS-Developer mailing list
QGIS-Developer@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer

Reply via email to