Hi list,<div><br></div><div>I was trying to do the following with ogr python bindings</div><div><ol><li>Create srclayer from PostGIS based on sql statement</li><li>Copy srclayer to dstlayer (also PostGIS)</li><li>Add new field to dstlayer</li>
<li>assign value to new field</li></ol><div>My code is this:</div></div><div>
<p class="p1">### Open Database</p>
<p class="p2">Driver = ogr.GetDriverByName(<span class="s1">'PostgreSQL'</span>)</p><p class="p2"><span class="s2">db = </span>'pg:host=<span class="s3">localhost</span> <span class="s3">dbname</span>=<span class="s3">test</span> user=<span class="s3">postgres</span> password=secret'<span class="s2"> </span></p>
<p class="p2">dataSource = Driver.Open(db,<span class="s4">1</span> )</p>
<p class="p4">## crate source and destination layer</p>
<p class="p3"><span class="s2">srclayer = dataSource.ExecuteSQL(</span>"SELECT <span class="s3">gid</span>,<span class="s3">aar</span>,ejd_areal,<span class="s3">pris</span>, <span class="s3">geom</span> FROM salg WHERE <span class="s3">aar</span>=2010 AND <span class="s3">pris</span> >0 AND ejd_areal >0 limit 15"<span class="s2">)</span></p>
<p class="p2">dstlayer = dataSource.CopyLayer(srclayer,<span class="s1">'gwr.dstlayer'</span>, options = [<span class="s1">'OVERWRITE=YES'</span>] )</p>
<p class="p4">## add field to copied layer</p>
<p class="p4">fieldDefn = ogr.FieldDefn(<span class="s1">'gradient'</span>, ogr.OFTReal)</p>
<p class="p2">dstlayer.CreateField(fieldDefn)</p>
<p class="p4">#outFeature = ogr.Feature(dstlayer.GetLayerDefn())</p>
<p class="p2">outFeature = dstlayer.GetNextFeature()</p>
<p class="p4">## update the new field </p>
<p class="p2"><span class="s5">for</span> feature <span class="s5">in</span> dstlayer:</p>
<p class="p2"> outFeature.SetField(<span class="s1">'gradient'</span>,<span class="s4">666</span>) ##set this field only</p>
<p class="p2"> outFeature.SetFID(-<span class="s4">1</span>) ## tried to unset ogc_fid so not violating pk column constraint</p>
<p class="p2"> dstlayer.CreateFeature(outFeature)</p>
<p class="p2"> outFeature = dstlayer.GetNextFeature()</p></div><div><div><br></div><div><br></div><div>I get the error:</div><div><br></div><div><div>ERROR: duplicate key value violates unique constraint "dstlayer_pk"</div>
<div>DETAIL: Key (ogc_fid)=(1) already exists.</div></div><div><br></div><div><br></div><div>I have a feeling i'm missing something really simple here.</div><div><br></div><div>Any suggestions would be much appreciated!</div>
<div><br></div><div>Software:</div><div><div>GDAL 1.9</div><div>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'</div>
<div>PG: PostGIS version string : '2.0 USE_GEOS=1 USE_PROJ=1 USE_STATS=1'</div></div><div><br></div><div>Best regards</div><div><br></div><div>Martin </div>
</div>