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

Chris Crook ccrook at linz.govt.nz
Mon Jan 17 16:00:05 EST 2011


I've added submitted a patch to implement this change at http://trac.osgeo.org/qgis/ticket/3414.

Changes made are:

* QgsCoordinateReferenceSystem? string constructor has been generalised to allow string formats
   epsg:1234
   postgis:1234
   internal:1234
   proj4:proj_4_string
   wkt:wkt_string
   wkt_string
This is implemented by a createFromString function, similar to the createFromId function

* Changed QgsMemoryProvider URI format to:
   memory?geometry=Point&index=yes&crs=epsg:1234&field=id:integer&field=name:string(25)

The URI can contain the following items:
   geometry=Point|LineString|Polygon|MultiPoint|MultiLineString|MultiPolygon
   index=yes  - creates a spatial index
   crs=...    - defines the CRS in one of the formats above
   field=definition - defines an attribute field.

The field definition is a field name, optionally followed by :type(length,precision), where type is one of integer, int, real, double, and string (int/integer are synonyms, as are real/double).  Eg
   field=magnitude:real(16,2)
   field=owner:string(64)
   field=owner

This uses Qurl for encoding and decoding the strings.  To provide backwards compatibility, if there is no geometry query string field, then the path is taken as the geometry type.  For example
   point
   point?crs=epsg:1234

* Added an option crs=xxxx to the QgsDelimitedTextProvider URI, so that plugin code can specify the CRS when creating a delimited text layer.  The crs can be any of the formats listed above. Also updated the provider to use Qurl for encoding and decoding the URI, to ensure consistent handling of special characters.

* Made a couple of small fixes to the QgsMemoryProviderPlugin UI component to ensure that it correctly remembers the last choice of delimiter type (and to encode the data source URI using Qurl)


One thing I haven't done is to modify the handling of datasourceUri in QgsVectorLayer to ensure that it always uses the datasourceUri from the provider, rather than storing its own version when the layer is created (or the provider is reset).  I'm not sure how safe this would be - that is whether all the data providers correctly handle the datasourceUri() function.  I think this would be the right thing to do though - I think that the provider should be responsible for its own URI.  Any comments on the wisdom of changing this would be welcome!

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