Hi Gerard,
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.


On Mon, Nov 25, 2019 at 3:00 PM Gerard <[email protected]> wrote:

> Thanks!
>
> def Open(*args, **kwargs):
>       """Open(char const * utf8_path, int update=0) -> DataSource"""
>       return _ogr.Open(*args, **kwargs)
>
> This still causes an error, even though OVERWRITE is set to NO.
> ds = ogr.Open("PG:dbname='db' user='user'", update=1)
> # .....
> trackslayer = ds.CreateLayer(table, srs, ogr.wkbMultiLineString,
> ['OVERWRITE=NO'] )
>
> However, this seems to work fine:
> trackslayer = ds.GetLayerByName('tracks')
> tracksdef = trackslayer.GetLayerDefn()
> outFeature = ogr.Feature(tracksdef)
> outFeature.SetField("name", name)
> # .....
> trackslayer.CreateFeature(outFeature)
>
> Gerard
>
>
> On Mon, Nov 25, 2019 at 11:24 AM Thomas Juntunen <[email protected]> wrote:
>
>> Gerard,
>> You need to open your data source in update mode.
>> ds = ogr.Open("PG:dbname='db' user='user'", update=1)
>>
>> On Mon, Nov 25, 2019 at 10:58 AM Gerard <[email protected]> wrote:
>>
>>> Is it possible to add features to an existing PostGIS table using Python
>>> OGR/GDAL? I've tried these two approaches:
>>>
>>> # Approach 1. Results in:
>>> # ERROR 1: Layer tracks already exists, CreateLayer failed.
>>> # Use the layer creation option OVERWRITE=YES to replace it.
>>> ds = ogr.Open("PG:dbname='db' user='user'")
>>> srs = osr.SpatialReference()
>>> srs.ImportFromEPSG(4326)
>>> layer = ds.CreateLayer('tracks', srs, ogr.wkbMultiLineString,
>>> ['OVERWRITE=NO'] )
>>>
>>> # Approach 2. Results in:
>>> # ERROR 1: Invalid index : -1
>>> # ERROR 6: CreateFeature : unsupported operation on a read-only
>>> datasource.
>>> ds = ogr.Open("PG:dbname='db' user='user'")
>>> layer = ds.GetLayerByName('tracks')
>>> # set fields, etc.
>>> layer.CreateFeature(outFeature)
>>>
>>> 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.
>>>
>>> Thanks,
>>> Gerard
>>> _______________________________________________
>>> gdal-dev mailing list
>>> [email protected]
>>> https://lists.osgeo.org/mailman/listinfo/gdal-dev
>>
>>
>>
>> --
>> *The right coordinate system can turn an impossible problem into two
>> really hard problems. -- Charlie Pellerin*
>>
>> Thomas Juntunen
>> GIS Specialist
>> Polar Geospatial Center
>> R280 Learning and Environmental Science
>> 1954 Buford Ave
>> University of Minnesota
>> St. Paul, MN 55108
>> 612-626-0505
>>
>

-- 
*The right coordinate system can turn an impossible problem into two really
hard problems. -- Charlie Pellerin*

Thomas Juntunen
GIS Specialist
Polar Geospatial Center
R280 Learning and Environmental Science
1954 Buford Ave
University of Minnesota
St. Paul, MN 55108
612-626-0505
_______________________________________________
gdal-dev mailing list
[email protected]
https://lists.osgeo.org/mailman/listinfo/gdal-dev

Reply via email to