GDAL create kml

新增kml 点
 public void WriteKmlPiont()
{ string driverName = "KML"; //MapInfo File OSGeo.GDAL.Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
// 为了使属性表字段支持中文,请添加下面这句SHAPE_ENCODING //UTF-8 to ISO-8859-1.
OSGeo.GDAL.Gdal.SetConfigOption("SHAPE_ENCODING", "");
//注册
Ogr.RegisterAll(); OSGeo.OGR.Driver ogrDriver = Ogr.GetDriverByName(driverName); DataSource ogrDS = ogrDriver.CreateDataSource("C:\\test.kml", null); // string fileName = DateTime.Now.ToString("yyyyMMddHHmmss"); Layer ogrlayer = ogrDS.CreateLayer("kml", null, wkbGeometryType.wkbPoint, null); FieldDefn fdefn = new FieldDefn("Name", FieldType.OFTString);
fdefn.SetWidth();
ogrlayer.CreateField(fdefn,); fdefn = new FieldDefn("IntField", FieldType.OFTInteger);
ogrlayer.CreateField(fdefn, ); fdefn = new FieldDefn("DbleField", FieldType.OFTReal);
ogrlayer.CreateField(fdefn, ); fdefn = new FieldDefn("DateField", FieldType.OFTDate);
ogrlayer.CreateField(fdefn, ); Feature feature = new Feature(ogrlayer.GetLayerDefn());
feature.SetField("Name", "新增kml");
feature.SetField("IntField", (int));
feature.SetField("DbleField", (double)12.345);
feature.SetField("DateField", , , , , , , ); Geometry geom = Geometry.CreateFromWkt("POINT(115.91466833333 28.668223333)");
feature.SetGeometry(geom);
ogrlayer.CreateFeature(feature); ogrlayer.Dispose();
ogrDS.Dispose();
}
新增kml 线
   public void WriteKmlLine()
{ string driverName = "KML"; //MapInfo File OSGeo.GDAL.Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
// 为了使属性表字段支持中文,请添加下面这句SHAPE_ENCODING //UTF-8 to ISO-8859-1.
OSGeo.GDAL.Gdal.SetConfigOption("SHAPE_ENCODING", "");
//注册
Ogr.RegisterAll(); OSGeo.OGR.Driver ogrDriver = Ogr.GetDriverByName(driverName); DataSource ogrDS = ogrDriver.CreateDataSource("C:\\wkbLineString.kml", null); // string fileName = DateTime.Now.ToString("yyyyMMddHHmmss"); Layer ogrlayer = ogrDS.CreateLayer("kml", null, wkbGeometryType.wkbLineString, null); FieldDefn fdefn = new FieldDefn("Name", FieldType.OFTString);
fdefn.SetWidth();
ogrlayer.CreateField(fdefn, ); fdefn = new FieldDefn("IntField", FieldType.OFTInteger);
ogrlayer.CreateField(fdefn, ); fdefn = new FieldDefn("DbleField", FieldType.OFTReal);
ogrlayer.CreateField(fdefn, ); fdefn = new FieldDefn("DateField", FieldType.OFTDate);
ogrlayer.CreateField(fdefn, ); Feature feature = new Feature(ogrlayer.GetLayerDefn());
feature.SetField("Name", "新增kml");
feature.SetField("IntField", (int));
feature.SetField("DbleField", (double)12.345);
feature.SetField("DateField", , , , , , , ); Geometry geom = new Geometry(wkbGeometryType.wkbLineString); //Geometry.CreateFromWkt("LINESTRING(115.91466833333 28.668223333)"); //需要增加两个点,两点成一线,开始测试的时候就经常报错。、
geom.AddPoint_2D(115.91466833333, 28.668223333); geom.AddPoint_2D(115.92466833333, 28.668223333); feature.SetGeometry(geom);
ogrlayer.CreateFeature(feature); ogrlayer.Dispose();
ogrDS.Dispose();
}
上一篇:我的STL学习之路


下一篇:CRT(C Runtime Library)—— C/C++运行时库