首页 文章

使用gdal从C#中的现有位图创建地理基准

提问于
浏览
6

我必须在c#项目中使用gdal . 我要做的是将一个简单的位图“转换”为GeoTiff . 我在gdal网站上阅读了一些文档,但是我没有成功完成它 . 事实上,我的位图已成功导出到地理位置,但如果我使用GIS软件(例如QuantumGIS)打开geotiff,则GeoTiff会在y轴上反转:

enter image description here

而原始位图看起来像这样:

enter image description here

这就是我所做的:

首先我将一个临时文件写入磁盘(即位图),我创建了一个包含位图的数据集,这要归功于gdal函数(Gdal.Open(path)),我创建了一个新的数据集(使用GTiff驱动程序)位图数据集,我设置地理转换,并将geotiff写入磁盘:

String wktProj = null;
  String tmpPath = @"C:\tmp.bmp";
  Bitmap tmpBitmap = bmp.Clone(new Rectangle(0, 0, bmp.Width, bmp.Height), pixFormat);
  tmpBitmap.Save(tmpPath, ImageFormat.Bmp);

  String[] options = null;
  Gdal.AllRegister();
  OSGeo.GDAL.Driver srcDrv = Gdal.GetDriverByName("GTiff");
  Dataset srcDs = Gdal.Open(tmpPath, Access.GA_ReadOnly);
  Dataset dstDs = srcDrv.CreateCopy(path, srcDs, 0, options, null, null);

  //Set the map projection
  Osr.GetWellKnownGeogCSAsWKT("WGS84", out wktProj);
  dstDs.SetProjection(wktProj);

  //Set the map georeferencing
  double mapWidth = Math.Abs(latLongMap.listBounds.topRight.x - latLongMap.listBounds.bottomLeft.x);
  double mapHeight = Math.Abs(latLongMap.listBounds.topRight.y - latLongMap.listBounds.bottomLeft.y);
  double[] geoTransfo = new double[] { -5.14, mapWidth / bmp.Width, 0, 48.75, 0, mapHeight / bmp.Height };
  dstDs.SetGeoTransform(geoTransfo);

  dstDs.FlushCache();
  dstDs.Dispose();
  srcDs.Dispose();
  srcDrv.Dispose();
  tmpBitmap.Dispose();

  File.Delete(tmpPath);

关于我做错了什么的任何想法?

Edit 我不知道它是否重要但像素位图是8bppIndexed .

1 回答

  • 5

    为了解决这个问题,我替换了这一行:

    double[] geoTransfo = new double[] { -5.14, mapWidth / bmp.Width, 0, 48.75, 0, mapHeight / bmp.Height };
    

    通过这个:

    double[] geoTransfo = new double[] { -5.14, mapWidth / bmp.Width, 0, 48.75, 0, (mapHeight / bmp.Height)*(-1) };
    

    它看起来像素大小(高度)必须是负数 .

相关问题