<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif;font-size:small">Hi Gerard,</div><div class="gmail_default" style="font-family:georgia,serif;font-size:small">Your first example is trying to make a new layer (table), and since trackslayer exists, and you selected overwrite is NO, it raises an error because you're trying to replace the table. If you are adding features to an existing table, your second method is the way to do it. You only need CreateLayer for new tables.</div><div class="gmail_default" style="font-family:georgia,serif;font-size:small"><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Nov 25, 2019 at 3:00 PM Gerard <<a href="mailto:gerardg@gmail.com">gerardg@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_default" style="font-size:small">Thanks!</div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small"><font face="monospace">def Open(*args, **kwargs): <br> """Open(char const * utf8_path, int update=0) -> DataSource""" <br> return _ogr.Open(*args, **kwargs) </font><br></div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small">This still causes an error, even though OVERWRITE is set to NO.</div><div class="gmail_default" style="font-size:small"><font face="monospace">ds = ogr.Open("PG:dbname='db' user='user'", update=1)</font><br></div><div class="gmail_default" style="font-size:small"><font face="monospace"># .....</font></div><div class="gmail_default" style="font-size:small"><font face="monospace">trackslayer = ds.CreateLayer(table, srs, ogr.wkbMultiLineString, ['OVERWRITE=NO'] )</font><br></div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small">However, this seems to work fine:</div><div class="gmail_default" style="font-size:small"><font face="monospace">trackslayer = ds.GetLayerByName('tracks')<br></font></div><div class="gmail_default" style="font-size:small"><font face="monospace">tracksdef = trackslayer.GetLayerDefn()<br>outFeature = ogr.Feature(tracksdef)<br></font></div><div class="gmail_default" style="font-size:small"><font face="monospace">outFeature.SetField("name", name)<br></font></div><div class="gmail_default" style="font-size:small"><font face="monospace"># .....</font></div><div class="gmail_default" style="font-size:small"><font face="monospace">trackslayer.CreateFeature(outFeature)<br></font></div><div class="gmail_default" style="font-size:small"><br></div><div><div dir="ltr"><div dir="ltr">Gerard</div></div></div><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Nov 25, 2019 at 11:24 AM Thomas Juntunen <<a href="mailto:junt0015@umn.edu" target="_blank">junt0015@umn.edu</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif;font-size:small">Gerard,</div><div class="gmail_default" style="font-family:georgia,serif;font-size:small">You need to open your data source in update mode.<br>
<font face="monospace">ds = ogr.Open("PG:dbname='db' user='user'", update=1)</font>
</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Nov 25, 2019 at 10:58 AM Gerard <<a href="mailto:gerardg@gmail.com" target="_blank">gerardg@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_default" style="font-size:small">Is it possible to add features to an existing PostGIS table using Python OGR/GDAL? I've tried these two approaches:</div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small"><font face="monospace"># Approach 1. Results in:</font></div><font face="monospace"># ERROR 1: Layer tracks already exists, CreateLayer failed.<br># Use the layer creation option OVERWRITE=YES to replace it.<br></font><div class="gmail_default" style="font-size:small"><font face="monospace">ds = ogr.Open("PG:dbname='db' user='user'")<br>srs = osr.SpatialReference()<br>srs.ImportFromEPSG(4326)</font></div><div class="gmail_default" style="font-size:small"><font face="monospace">layer = ds.CreateLayer('tracks', srs, ogr.wkbMultiLineString, ['OVERWRITE=NO'] )<br><br># Approach 2. Results in:<br># ERROR 1: Invalid index : -1<br># ERROR 6: CreateFeature : unsupported operation on a read-only datasource.<br>ds = ogr.Open("PG:dbname='db' user='user'")</font><br><font face="monospace">layer = ds.GetLayerByName('tracks')</font><br></div><div class="gmail_default" style="font-size:small"><font face="monospace"># set fields, etc.</font></div><div class="gmail_default" style="font-size:small"><font face="monospace">layer.CreateFeature(outFeature)</font><br></div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small">Think I could accomplish it via SQL statements and ds.ExecuteSQL(), and I've found examples using psycopg2 (or SQLAlchemy) instead. Just wondering if it's directly possible using Python OGR/GDAL.</div><div class="gmail_default" style="font-size:small"><br></div><div><div dir="ltr"><div dir="ltr">Thanks,<br>Gerard</div></div></div></div>
_______________________________________________<br>
gdal-dev mailing list<br>
<a href="mailto:gdal-dev@lists.osgeo.org" target="_blank">gdal-dev@lists.osgeo.org</a><br>
<a href="https://lists.osgeo.org/mailman/listinfo/gdal-dev" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/gdal-dev</a></blockquote></div><br clear="all"><br>-- <br><div dir="ltr"><i><font face="georgia, serif" color="#999999">The right coordinate system can turn an impossible problem into two really hard problems. -- Charlie Pellerin</font></i><div><i><font color="#999999"><br></font></i><div><span style="border-collapse:collapse;font-family:arial,sans-serif;font-size:13px"><font color="#999999">Thomas Juntunen</font></span><div><span style="border-collapse:collapse;font-family:arial,sans-serif;font-size:13px"><font color="#999999">GIS Specialist</font></span></div><div><span style="border-collapse:collapse;font-family:arial,sans-serif;font-size:13px"><font color="#999999">Polar Geospatial Center<br>R280 Learning and Environmental Science<br>1954 Buford Ave<br>University of Minnesota<br>St. Paul, MN 55108<br><a>612-626-0505</a></font></span></div></div></div></div>
</blockquote></div>
</blockquote></div><br clear="all"><br>-- <br><div dir="ltr" class="gmail_signature"><i><font face="georgia, serif" color="#999999">The right coordinate system can turn an impossible problem into two really hard problems. -- Charlie Pellerin</font></i><div><i><font color="#999999"><br></font></i><div><span style="border-collapse:collapse;font-family:arial,sans-serif;font-size:13px"><font color="#999999">Thomas Juntunen</font></span><div><span style="border-collapse:collapse;font-family:arial,sans-serif;font-size:13px"><font color="#999999">GIS Specialist</font></span></div><div><span style="border-collapse:collapse;font-family:arial,sans-serif;font-size:13px"><font color="#999999">Polar Geospatial Center<br>R280 Learning and Environmental Science<br>1954 Buford Ave<br>University of Minnesota<br>St. Paul, MN 55108<br><a>612-626-0505</a></font></span></div></div></div></div>