[Qgis-developer] Re: QGIS API: change layer field length

Martin Dobias wonder.sk at gmail.com
Mon Jul 18 08:27:17 EDT 2011


Hi Romain

On Sun, Jul 17, 2011 at 11:23 PM, romain <romain.riviere.974 at gmail.com> wrote:
> Sorry, I think i haven't explained well what I want.
>
> I know how to get my fields' lengths from SpatiaLite.
>
> Here is what I've already done:
> 1/ create QgsvectorLayer from spatiaLite table : vlayer
> 2/ strore field's length in a python list: lengths
>
> One of my text fields (FIELD1) have 300 characters....
>
>
> What my problem is when i want to save the layer as mapinfo file with :
>
> QgsVectorFileWritter.writteAsFromat(vlayer,........,mapinfo)
>
> -> The mapinfo File is created but all the fields are truncated at 254
> char.... So, I lost informations on FIELD1...

There is one more way how to use QgsVectorFileWriter:
1. construct QgsVectorFileWriter object
2. call addFeature() for each feature you want add
3. delete the writer object (in order to save changes)

This is essentially what writeAsFormat() function does. But using the
above approach has more flexibility: in the constructor you pass a map
of fields - so instead of using input layer's field definitions you
could change them and set higher (or lower) length, rename some fields
etc.


> I've tried to set fields lengths for my layer, in order to avoid truncature,
> but, it doesn't change anything:
>
> fields=vlayer.dataProvider().fields()
> for field in fields:
>      fields[field].setLength(301)

This will not work because you are changing the length only of the
local object: this change will not get back to the provider.


> The only thing I need is to pass an option to Qgsvectorfilewritter to change
> fields maximum characters...
> Does anybody know how to do this directly with OGR ? (within python, of
> course)

Another problem could be that the truncation is a limitation of the
format or OGR driver...

Regards
Martin


More information about the Qgis-developer mailing list