[Qgis-developer] Cleanly create memory layer in Python?

Chris Crook ccrook at linz.govt.nz
Sun Jan 9 19:17:40 EST 2011


Looks like one of my ideas below won't work.  I was hoping that I could over-ride the QgsDataProvider.dataSourceUri() function to update the Uri as the provider was modified (such as adding an index).  

However I think that this function is only referenced when the provider is set up (ie QgsVectorLayer.setDataProvider() and QgsVectorLayer.setSubString()), which means any other provider initiated changes don't get reflected back to the QgsVectorLayer :-(

For some providers (osm, postgres) the dataSourceUri() function is used to reset the data source when the provider is first assigned to the layer, but not thereafter.

I'm wondering why the QgsDataProvider.dataSourceUri() function isn't used whenever the Uri is needed - as it seems to me it belongs very much to the provider and not to the layer?

Would it make sense to replace references to QgsMapLayer.mDataSource with calls to QgsMapLayer.dataSourceUri(), and to have that use the provider dataSourceUri() function unless the provider was not defined.

Cheers
Chris

-----Original Message-----
From: Chris Crook 
Sent: Monday, 10 January 2011 8:51 a.m.
To: 'Martin Dobias'
Cc: qgis-developer at lists.osgeo.org
Subject: RE: [Qgis-developer] Cleanly create memory layer in Python?

Hi Martin and list readers

I'm experimenting with using a Url format very similar to what Martin suggested.  I'm thinking that this could include the Crs, the list of fields, and the creation of a spatial index. I'm thinking that a Url of the form:

memory?geometry=point&crs=1234&index=yes&field=integer:id&field=string:name ...

The crs can be a simple srid, epsg:id, or (possibly) a proj4 text string.

The path component of this is redundant of course - it could be anything.  To retain backward compatibility though if there is no geometry query field, then the query path will be used.  So the format could be 

point?crs=1234&index=yes...

Including the index in the URI is useful for me (for the MemoryLayerSaver plugin), as it restores that when the layer is recreated when a project is reopened.

I'm not so sure about whether to include the field definitions.  And if I do, I'm not sure about the best way to encode them, and whether to include length and precision fields.  It would probably be more conventional to use something like name:type:length:precision for each field?  Any thoughts.

Finally, I'd like to override the dataSourceUri() function to return a definition reflecting the current state of the provider (ie index, fields, etc).  This is potentially breaking to plugin code if it uses this to determine the geometry type, not that there is any reason to do so.  

Cheers
Chris

-----Original Message-----
From: Martin Dobias [mailto:wonder.sk at gmail.com]
Sent: Saturday, 8 January 2011 7:06 a.m.
To: Chris Crook
Cc: qgis-developer at lists.osgeo.org
Subject: Re: [Qgis-developer] Cleanly create memory layer in Python?

On Thu, Jan 6, 2011 at 9:16 PM, Chris Crook <ccrook at linz.govt.nz> wrote:
> Hi All
>
> I'm wanting to create a memory provider in Python without invoking the CRS selection dialog (even if the QGIS settings are set to "Prompt for CRS when a new layer is created".
>
> At the moment my code looks like:
>
>        settings = QSettings()
>        prjSetting = settings.value("/Projections/defaultBehaviour")
>        settings.setValue("/Projections/defaultBehaviour",
> QVariant(""))
>        layer = QgsVectorLayer("point",name,"memory")
>        if prjSetting:
>            
> settings.setValue("/Projections/defaultBehaviour",prjSetting)
>
> This does work, but it seems a bit cumbersome (at first glance it doesn't even look like creating a vector layer!), so I'm wondering if there is a cleaner approach..

Hi Chris

I agree this is cumbersome. A possible solution would be to extend the URI for memory provider - currently it accepts only a string with geometry type. New syntax could be introduced that would explicitly set layer CRS, e.g. "geometry=point&crs=4326". Any thoughts and patches are welcome.

Regards
Martin
______________________________________________________________________________________________________

This message contains information, which is confidential and may be subject to legal privilege. 
If you are not the intended recipient, you must not peruse, use, disseminate, distribute or copy this message.
If you have received this message in error, please notify us immediately (Phone 0800 665 463 or info at linz.govt.nz) and destroy the original message.
LINZ accepts no responsibility for changes to this email, or for any attachments, after its transmission from LINZ.

Thank you.
______________________________________________________________________________________________________


More information about the Qgis-developer mailing list