0

所以我正在尝试使用 GDAL nuget 包将 kml 文件转换为 mapinfo 选项卡层。我的代码可以使用 gdal 将 kml 转换为 geojson,但是每当我尝试转换为 MapInfo Tab 时,它都会失败并出现错误: CreateFeature() failed: invalid feature id 1 on the CopyLayer line of code。

这是我正在使用的当前代码:

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>());
            //            }
            //        }
            //    }
            //}
        }
    }
}

选项卡输出正在输出文件夹中创建 .dat、.map、.tab 和 .id 文件,尽管它们不起作用。

这方面的文档似乎有限(或者我还没有找到)。有人对我所缺少的有任何建议吗?

谢谢,

4

0 回答 0