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

Chris Crook ccrook at linz.govt.nz
Sun Jan 9 14:51:19 EST 2011

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 


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.  


-----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.


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