首页 > 代码库 > GDAL create kml
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(32); ogrlayer.CreateField(fdefn,1); fdefn = new FieldDefn("IntField", FieldType.OFTInteger); ogrlayer.CreateField(fdefn, 1); fdefn = new FieldDefn("DbleField", FieldType.OFTReal); ogrlayer.CreateField(fdefn, 1); fdefn = new FieldDefn("DateField", FieldType.OFTDate); ogrlayer.CreateField(fdefn, 1); Feature feature = new Feature(ogrlayer.GetLayerDefn()); feature.SetField("Name", "新增kml"); feature.SetField("IntField", (int)123); feature.SetField("DbleField", (double)12.345); feature.SetField("DateField", 2007, 3, 15, 18, 24, 30, 0); 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(32); ogrlayer.CreateField(fdefn, 1); fdefn = new FieldDefn("IntField", FieldType.OFTInteger); ogrlayer.CreateField(fdefn, 1); fdefn = new FieldDefn("DbleField", FieldType.OFTReal); ogrlayer.CreateField(fdefn, 1); fdefn = new FieldDefn("DateField", FieldType.OFTDate); ogrlayer.CreateField(fdefn, 1); Feature feature = new Feature(ogrlayer.GetLayerDefn()); feature.SetField("Name", "新增kml"); feature.SetField("IntField", (int)123); feature.SetField("DbleField", (double)12.345); feature.SetField("DateField", 2007, 3, 15, 18, 24, 30, 0); 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(); }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。