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

Chaitanya kumar CH chaitanya.ch at gmail.com
Sat Jul 24 10:31:57 EDT 2010


Esben,

I am not sure why the value is not quoted in the error report. The
PostgreSQL driver quotes the string values.
When using CreateFeature() you need to make sure to set the feature's FID to
OGRNullFID using SetFID(OGRNullFID). In your error report it shows that
ogc_fid is set to 0. Perhaps there is already a feature with that value?

On Fri, Jul 23, 2010 at 7:05 PM, Esben Taudorf <eta at le34.dk> wrote:

>  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.
>
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/gdal-dev
>



-- 
Best regards,
Chaitanya kumar CH.
/tʃaɪθənjə/ /kʊmɑr/
+91-9494447584
17.2416N 80.1426E
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/gdal-dev/attachments/20100724/8e17a3ac/attachment.html


More information about the gdal-dev mailing list