[gdal-dev] Append a shapefile to a postgis table using the GDAL/OGR CSharp interface

Esben Taudorf eta at le34.dk
Fri Jul 23 09:35:30 EDT 2010


Hi everybody

 

I just started using the GDAL/OGR CSharp interface in Visual Studio and
it works great. 

I am trying to append a shapefile to a postgis table. This I can do with
the following lines in ogr2ogr:

 

Fist import the shapefile to postgis a table

ogr2ogr -f "PostgreSQL" PG:"dbname='postgis' host='localhost'
port='5432' user='...' password='...'" -lco PRECISION=NO -lco
GEOMETRY_NAME=geom -a_srs EPSG:25832 "c:\temp\testfile.shp"

 

Then append the same shapefile (this is a test) to the same postgis
table like this

ogr2ogr -append -update -f "PostgreSQL" PG:"dbname='postgis'
host='localhost' port='5432' user='...' password='...'"
"c:\temp\testfile.shp"

 

 

But how do I do this in the GDAL/OGR CSharp interface or which classes
and functions do I use?

 

So far what I have done is:

 

Open a layer from the shapefile:

string fileShp = @"c:\temp\testfile.shp";

DataSource InputDataSource = Ogr.Open(fileShp, 0);

Layer inputLayer = InputDataSource.GetLayerByIndex(0);

FeatureDefn def = inputLayer.GetLayerDefn();

 

Open a datasource to postgis:

OutputDriver = Ogr.GetDriverByName("PostgreSQL");

DataSource OutputDataSource = OutputDriver.Open("PG:dbname='postgis'
host='localhost' port='5432' user='...' password='...'", 1);

 

 

Copy the layer to the postgis table:

Layer outputLayer = OutputDataSource.CopyLayer(inputLayer,
def.GetName(), new string[] { "GEOMETRY_NAME=geom", "PRECISION=NO" });
//How to add -a_srs EPSG:25832?

 

 

This works to import the shapefile to a postgis table. But what should I
do when to append the same shapefile to the postgis a table. So far I
have done this:

 

Open a layer from the shapefile:

string fileShp = @"c:\temp\testfile.shp";

DataSource InputDataSource = Ogr.Open(fileShp, 0);

Layer inputLayer = InputDataSource.GetLayerByIndex(0);

FeatureDefn def = inputLayer.GetLayerDefn();

 

Open a datasource to postgis:

OutputDriver = Ogr.GetDriverByName("PostgreSQL");

DataSource OutputDataSource = OutputDriver.Open("PG:dbname='postgis'
host='localhost' port='5432' user='...' password='...'", 1);

 

 

Layer outputLayer = OutputDataSource.GetLayerByName(def.GetName());

Feature feat;

while ((feat = inputLayer.GetNextFeature()) != null)

                             {

 
outputLayer.CreateFeature(feat);

                             }

 

The CreateFeature functions fails with a syntax error in the INSERT
command. Below is a short version of the command with the syntax error: 

 

Command: INSERT INTO "testfile" ("geom" , "ogc_fid" , "name") VALUES
(GeomFromEWKT('SRID=-1;POINT (701678 6152444)'::TEXT) , 0 , Test)

 

The field "name" is a character varying in postgis a the command needs
single quotes around 'Test'.

 

How do I solve this syntax error?

 

I am also wondering how to assign an output SRS when creating the
postgis table like the ogr2ogr flag -a_srs EPSG:25832?

 

Any help or comment would be appreciated.

 

 

Regards Esben.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/gdal-dev/attachments/20100723/86c5ea47/attachment.html


More information about the gdal-dev mailing list