您好我想在 C# 中使用 Gdal 库将 shapefile (shp) 转换为 kml。我写了一个代码,但输出不是 kml 格式。
这是我的代码:
using OSGeo.OGR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OSGeo.OSR;
using OSGeo.GDAL;
namespace ConsoleApp1 {
class Program {
static void Main(string[] args) {
GdalConfiguration.ConfigureGdal();
GdalConfiguration.ConfigureOgr();
convert();
}
public static void convert() {
string shapeFilePath = @ "C:\riv1.shp";
Ogr.RegisterAll();
var drv = Ogr.GetDriverByName("ESRI Shapefile");
var ds = drv.Open(shapeFilePath, 0);
OSGeo.OGR.Layer layer = ds.GetLayerByIndex(0);
OSGeo.OGR.Feature f;
layer.ResetReading();
System.Text.StringBuilder sb = new System.Text.StringBuilder();
while ((f = layer.GetNextFeature()) != null) {
var geom = f.GetGeometryRef();
if (geom != null) {
var geometryKml = geom.ExportToKML("");
sb.AppendLine(geometryKml);
}
}
var kmlStr = sb.ToString();
System.IO.File.WriteAllText("c:/riv1.kml", kmlStr);
}
}
}
FWTools Shell 的这种转换工作正常,但我需要在我的代码中进行。如果你知道我想念什么,请帮助我。