private static string CONNECTION_STRING = "PG:host=localhost dbname=london user=postgres password=1234";
layer.GlobalCallback = form;
bool forUpdate = true;
if (!layer.OpenFromDatabase(CONNECTION_STRING, "buildings", forUpdate))
{
Debug.Print("Failed to open layer: " + layer.get_ErrorMsg(layer.LastErrorCode));
return false;
}
Debug.Print(
"Driver supports editing: " + layer.TestCapability(
tkOgrLayerCapability.olcRandomWrite));
now check if we can actually do it, as there can be other limitations
{
Debug.Print("Can't edit a layer: " + layer.get_ErrorMsg(layer.LastErrorCode));
layer.Close();
return false;
}
var sf = layer.GetBuffer();
if (sf != null)
{
bool editValue = true;
bool addShape = true;
bool editShape = true;
bool removeShape = true;
if (editValue)
{
int shapeIndex = 0;
int fieldIndex = 2;
object val = sf.get_CellValue(fieldIndex, shapeIndex);
sf.EditCellValue(fieldIndex, shapeIndex, "test_writing");
sf.ShapeModified[shapeIndex] = true;
}
if (addShape)
{
int shapeIndex = sf.NumShapes;
var shp = sf.get_Shape(0);
shp = shp.Buffer(1, 50);
bool result = sf.EditInsertShape(shp, ref shapeIndex);
Debug.Print("Shape was inserted: " + result);
}
if (editShape)
{
var shp = sf.get_Shape(sf.NumShapes - 1);
for (int i = 0; i < shp.NumPoints; i++)
{
double x = 0.0, y = 0.0;
if (shp.get_XY(i, ref x, ref y))
shp.put_XY(i, x + 0.01, y + 0.01);
}
}
if (removeShape)
{
bool result = sf.EditDeleteShape(sf.NumShapes - 1);
Debug.Print("Shape was deleted: " + result);
}
int count;
var saveResults = layer.SaveChanges(out count);
Debug.Print("Save result: " + saveResults.ToString());
Debug.Print("Number of shapes saved: " + count);
for (int i = 0; i < layer.UpdateSourceErrorCount; i++)
{
Debug.Print(string.Format("Error for shape id {0}: {1}",
layer.UpdateSourceErrorShapeIndex[i], layer.UpdateSourceErrorMsg[i]));
}
return true;
}
layer.Close();
return false;
tkOgrLayerCapability
List of capabilities that might or might not be supported by particular OGR driver.
Definition: Enumerations.cs:2245
tkOgrSaveType
Defines possible subjects of OgrLayer.SaveChanges method.
Definition: Enumerations.cs:2224
Represents a single layer in GDAL/OGR datasource or result of SQL query against such datasource.
Definition: OgrLayer.cs:245