码迷,mamicode.com
首页 > 其他好文 > 详细

GDAL create kml

时间:2014-07-29 20:47:32      阅读:373      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   art   div   

新增kml 点
bubuko.com,布布扣
 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();
      }
View Code

 

新增kml 线
bubuko.com,布布扣
   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();
      }
View Code

 

GDAL create kml,布布扣,bubuko.com

GDAL create kml

标签:style   blog   http   color   os   io   art   div   

原文地址:http://www.cnblogs.com/chenxiao/p/3875880.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!