private static void ImportShapefile(IOgrDatasource ds, string filename, string name) { // Check file: if (!File.Exists(filename)) { Assert.Fail(filename + " does not exists."); } // Open shapefile: var sf = new Shapefile(); if (!sf.Open(filename)) { Assert.Fail("Failed to open shapefile: " + sf.ErrorMsg[sf.LastErrorCode]); } // Import: if (!ds.ImportShapefile(sf, name, "OVERWRITE=YES", tkShapeValidationMode.NoValidation)) { Assert.Fail("Failed to import shapefile"); } Debug.Print($"ds.Error: {ds.ErrorMsg[ds.LastErrorCode]} Gdal error: {ds.GdalLastErrorMsg}"); // Check: Debug.Print("Layer was imported: " + name); var layer = ds.GetLayerByName(name); Assert.IsNotNull(layer, "layer is null"); Debug.Print("Imported features count: " + layer.FeatureCount); Assert.AreEqual(sf.NumShapes, layer.FeatureCount, $"The amount of imported shapes is incorrect. GDAL Error: {layer.GdalLastErrorMsg} Error: {layer.ErrorMsg[layer.LastErrorCode]}"); // Export again, using GetBuffer: var sfFromBuffer = layer.GetBuffer(); Debug.Print("Number shapes from GetBuffer: " + sfFromBuffer.NumShapes); Assert.AreEqual(sf.NumShapes, sfFromBuffer.NumShapes, "The amount of exported shapes is incorrect."); // Save shapefile: var tmpFilename = Path.ChangeExtension(Path.Combine(Path.GetTempPath(), Path.GetTempFileName()), ".shp"); DeleteShapefile(tmpFilename); if (!sfFromBuffer.SaveAs(tmpFilename)) { Assert.Fail("Failed to save shapefile: " + sfFromBuffer.ErrorMsg[sfFromBuffer.LastErrorCode]); } if (!ds.ImportShapefile(sfFromBuffer, "sfFromBuffer", "OVERWRITE=YES", tkShapeValidationMode.NoValidation)) { Assert.Fail("Failed to import buffered shapefile"); } layer.Close(); }
private static void TestSQLiteLayers(IOgrDatasource ogrDatasource) { // Get layers: var lastLayername = string.Empty; Debug.WriteLine("Number of layers: " + ogrDatasource.LayerCount); for (var i = 0; i < ogrDatasource.LayerCount; i++) { var layer = ogrDatasource.GetLayer(i, true); Assert.IsNotNull(layer, "Layer is null"); lastLayername = layer.Name; Debug.WriteLine("Layer name: " + layer.Name); Debug.WriteLine("Driver Name: " + layer.DriverName); Assert.AreEqual("SQLite", layer.DriverName, "Wrong driver name"); var capability = layer.TestCapability(tkOgrLayerCapability.olcRandomRead); Debug.WriteLine("olcRandomRead: " + capability); Assert.IsTrue(capability, "Cannot random read"); capability = layer.TestCapability(tkOgrLayerCapability.olcRandomWrite); Debug.WriteLine("olcRandomWrite: " + capability); Assert.IsTrue(capability, "Cannot random write"); capability = layer.TestCapability(tkOgrLayerCapability.olcSequentialWrite); Debug.WriteLine("olcSequentialWrite: " + capability); Assert.IsTrue(capability, "Cannot sequential write"); } Debug.WriteLine("Last layer name: " + lastLayername); if (!string.IsNullOrEmpty(lastLayername)) { var layer = ogrDatasource.GetLayerByName(lastLayername, true); var layerCapability = layer.TestCapability(tkOgrLayerCapability.olcRandomRead); Debug.WriteLine("olcRandomRead: " + layerCapability); Assert.IsTrue(layerCapability, "Cannot random read"); layerCapability = layer.TestCapability(tkOgrLayerCapability.olcRandomWrite); Debug.WriteLine("olcRandomWrite: " + layerCapability); Assert.IsTrue(layerCapability, "Cannot random write"); layerCapability = layer.TestCapability(tkOgrLayerCapability.olcSequentialWrite); Debug.WriteLine("olcSequentialWrite: " + layerCapability); Assert.IsTrue(layerCapability, "Cannot sequential write"); } }