0

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,

David
  • 3,653
  • 2
  • 24
  • 26
  • 1
    Have you stepped through the code in debugging and checked that the tabDataSource is initialized? Does the CopyLayer method have a return type (e.g. status or something like that)? – T_Bacon Feb 14 '20 at 17:38
  • @T_Bacon yes I've stepped through the code. There is no return from CopyLayer as it is crashing on that line (with the error mentioned above). The tabDatasource is intialized and I check to make sure it isn't null before continuing. If you look at the commented out block of code it's almost exactly the same except it exports to geojson and that works without issue. – David Feb 14 '20 at 18:57

0 Answers0