export to MapInfo tab using GDAL and C#

778 Views Asked by At

so I'm trying to convert a kml file to a mapinfo tab layer using the GDAL nuget package. I've got code working that converts the kml to a geojson using gdal but whenever I try converting to MapInfo Tab it fails with the error: CreateFeature() failed: invalid feature id 1 on the CopyLayer line of code.

Here is the current code I'm working with:

var sourcePath = @"C:\Temp\gdal\sample.kml";
var destPath = @"C:\Temp\gdal\tab\";
var destPathJ = @"C:\Temp\gdal\tab\sample.geojson";

var files = Directory.GetFiles(Path.GetDirectoryName(destPath));

foreach(var file in files) {
    File.Delete(file);
}

using (var kmlDriver = Ogr.GetDriverByName("KML")) {
    using (var kmlDatasource = Ogr.Open(sourcePath, 0)) {
        if (kmlDatasource != null) {
            Debug.WriteLine($"layers {kmlDatasource.GetLayerCount()}");

            var kmlLayer = kmlDatasource.GetLayerByIndex(0);

            Debug.WriteLine($"features {kmlLayer.GetFeatureCount(0)}");

            using (var tabDriver = Ogr.GetDriverByName("MapInfo File")) {
                if (tabDriver != null) {
                    using (var tabDatasource = tabDriver.CreateDataSource(destPath, new string[] { "FORMAT=TAB", "SPACIAL_INDEX_MODE=OPTIMIZED", "BLOCKSIZE=512" })) {
                        if (tabDatasource != null) {    
                            tabDatasource.CopyLayer(kmlLayer, "test", new string[] { "DESCRIPTION=test", "BOUNDS=-180,-90,180,90" });
                        }
                    }
                }
            }

            //using (var jsonDriver = Ogr.GetDriverByName("GeoJSON")) {
            //    if (jsonDriver != null) {
            //        using (var jsonDatasource = jsonDriver.CreateDataSource(destPathJ, Array.Empty<string>())) {
            //            if (jsonDatasource != null) {
            //                jsonDatasource.CopyLayer(kmlLayer, kmlLayer.GetName(), Array.Empty<string>());
            //            }
            //        }
            //    }
            //}
        }
    }
}

The tab output is creating the .dat, .map, .tab and .id files in the output folder though they don't work.

Documentation for this seems limited (or I just haven't found it yet). Does anyone have any suggestions on what I'm missing?

Thank you,

0

There are 0 best solutions below