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

 

新增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();
      }
View Code
相關文章
相關標籤/搜索