[gdal-dev] Updating a single PostgreSQL field from ogr/python
Martin Jensen
martin.martinjensen at gmail.com
Tue May 15 05:50:20 EDT 2012
Luca,
thank you so much for your help. For a programming beginner like me, the
obvious is not alway so clear!
Now I see the light :)
I ended up using the ExecuteSQL method because the SetFeature method
deleted the content of the other fields. I'm sure there is an obvious
solution to this also but I didn' t find it :)
I also noticed the CopyLayer function dosen't write the geometry type of
the copied layer. Is this expected behaviour ?
Best regards
Martin
On 14 May 2012 12:07, Luca Sigfrido Percich <sigfrido at tiscali.it> wrote:
>
> Martin,
>
> you're updating existing features so you don't want to use the
> CreateFeature method. If you try to re-insert an existing feature you
> will get the duplicate pk error.
>
> See the docs for the SetFeature method.
>
> Moreover, your loop code is a bit confused; usually you would do:
>
> while True:
> outFeature = dstLayer.GetNextFeature()
> if (outFeature is None):
> break
> outFeature.SetField('gradient',666)
> outLayer.SetFeature(outFeature)
>
> You may also use an SQL UPDATE instruction:
>
> dataSource.ExecuteSQL("update gwr.dstlayer set gradient = 666")
>
> Sig
>
> Il giorno sab, 12/05/2012 alle 19.43 +0200, Martin Jensen ha scritto:
> > Hi list,
> >
> >
> > I was trying to do the following with ogr python bindings
> > 1. Create srclayer from PostGIS based on sql statement
> > 2. Copy srclayer to dstlayer (also PostGIS)
> > 3. Add new field to dstlayer
> > 4. assign value to new field
>
>
> > outFeature = dstlayer.GetNextFeature()
> >
> > ## update the new field
> >
> > for feature in dstlayer:
> >
> > outFeature.SetField('gradient',666) ##set this field only
> >
> > outFeature.SetFID(-1) ## tried to unset ogc_fid so not violating
> > pk column constraint
> >
> > dstlayer.CreateFeature(outFeature)
> >
> > outFeature = dstlayer.GetNextFeature()
> >
> >
> >
> >
> >
> >
> > I get the error:
> >
> >
> > ERROR: duplicate key value violates unique constraint "dstlayer_pk"
> > DETAIL: Key (ogc_fid)=(1) already exists.
> >
> >
> >
> >
> > I have a feeling i'm missing something really simple here.
> >
> >
> > Any suggestions would be much appreciated!
> >
> >
> > Software:
> > GDAL 1.9
> > PG: PostgreSQL version string : 'PostgreSQL 9.1.3 on
> > x86_64-apple-darwin11.3.0, compiled by
> > i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc.
> > build 5658) (LLVM build 2336.9.00), 64-bit'
> > PG: PostGIS version string : '2.0 USE_GEOS=1 USE_PROJ=1 USE_STATS=1'
> >
> >
> > Best regards
> >
> >
> > Martin
> > _______________________________________________
> > gdal-dev mailing list
> > gdal-dev at lists.osgeo.org
> > http://lists.osgeo.org/mailman/listinfo/gdal-dev
>
>
> _____________
> PRIVACY
> Le informazioni contenute in questo messaggio sono riservate e
> confidenziali. Il loro utilizzo e' consentito esclusivamente al
> destinatario del messaggio, per le finalità indicate nel messaggio stesso.
> Qualora Lei non fosse la persona a cui il presente messaggio è destinato,
> La invitiamo ad eliminarlo dal Suo Sistema e a distruggere le varie copie o
> stampe, dandone gentilmente comunicazione all’indirizzo mail del mittente.
> Ogni utilizzo improprio e' contrario ai principi del D.lgs 196/03 e alla
> legislazione europea (Direttiva 2002/58/CE).
>
> PRIVACY
> Le informazioni contenute in questo messaggio sono riservate e
> confidenziali. Il loro utilizzo e' consentito esclusivamente al
> destinatario del messaggio, per le finalità indicate nel messaggio stesso.
> Qualora Lei non fosse la persona a cui il presente messaggio è destinato,
> La invitiamo ad eliminarlo dal Suo Sistema e a distruggere le varie copie o
> stampe, dandone gentilmente comunicazione all’indirizzo mail del mittente.
> Ogni utilizzo improprio e' contrario ai principi del D.lgs 196/03 e alla
> legislazione europea (Direttiva 2002/58/CE).
>
--
Med venlig hilsen
Martin Bjærge Jensen
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/gdal-dev/attachments/20120515/bfa6e0a9/attachment-0001.html
More information about the gdal-dev
mailing list