using OSGeo.GDAL;
|
using OSGeo.OGR;
|
using OSGeo.OSR;
|
using System;
|
using System.Collections.Generic;
|
using System.IO;
|
|
namespace SimuTools.Tools
|
{
|
public class GdalHelper
|
{
|
#region 成员变量+构造函数
|
private static bool isInited;
|
|
private static GdalHelper instance;
|
|
private static readonly object obj = new object();
|
|
public static SpatialReference sr4326 { set; get; }
|
|
public static SpatialReference sr4490 { set; get; }
|
|
public static readonly String CGCS2000 = "CGCS2000";
|
|
/// <summary>
|
/// 构造函数
|
/// </summary>
|
private GdalHelper()
|
{
|
lock (obj)
|
{
|
if (!isInited)
|
{
|
RegisterGDal();
|
|
sr4326 = new SpatialReference(null);
|
sr4326.ImportFromEPSG(4326);
|
|
sr4490 = new SpatialReference(null);
|
sr4490.ImportFromEPSG(4490);
|
|
isInited = true;
|
}
|
}
|
}
|
#endregion
|
|
/// <summary>
|
/// 实例
|
/// </summary>
|
public static GdalHelper Instance
|
{
|
get
|
{
|
lock (obj)
|
{
|
if (null == instance)
|
{
|
instance = new GdalHelper();
|
}
|
|
return instance;
|
}
|
}
|
}
|
|
/// <summary>
|
/// 注册GDAL
|
/// </summary>
|
public void RegisterGDal()
|
{
|
string gdalData = Path.Combine(Handle.BaseDir, "gdal-data");
|
Environment.SetEnvironmentVariable("GDAL_DATA", gdalData);
|
|
string proj7 = Path.Combine(Handle.BaseDir, "proj7\\share");
|
Environment.SetEnvironmentVariable("PROJ_LIB", proj7);
|
|
Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES"); // NO,YES
|
Gdal.SetConfigOption("SHAPE_ENCODING", ""); // 空,gb2312,CP936
|
|
Ogr.RegisterAll();
|
Gdal.AllRegister();
|
}
|
|
/// <summary>
|
/// 获取Dataset的最小点
|
/// </summary>
|
public static Geometry GetMinPoint(Dataset ds)
|
{
|
double[] transform = new double[6];
|
ds.GetGeoTransform(transform);
|
|
double xMin = transform[0];
|
double yMin = transform[3] - ds.RasterYSize * transform[1];
|
|
Geometry point = new Geometry(wkbGeometryType.wkbPoint);
|
point.AddPoint(xMin, yMin, 0);
|
|
return Transform(ds, point);
|
}
|
|
/// <summary>
|
/// 获取Dataset的最大点
|
/// </summary>
|
public static Geometry GetMaxPoint(Dataset ds)
|
{
|
/*
|
* transform[0] 左上角x坐标
|
* transform[1] 东西方向分辨率
|
* transform[2] 旋转角度, 0表示图像 "北方朝上"
|
*
|
* transform[3] 左上角y坐标
|
* transform[4] 旋转角度, 0表示图像 "北方朝上"
|
* transform[5] 南北方向分辨率
|
*/
|
double[] transform = new double[6];
|
ds.GetGeoTransform(transform);
|
|
double xMax = transform[0] + (ds.RasterYSize * transform[1]);
|
double yMax = transform[3];
|
|
Geometry point = new Geometry(wkbGeometryType.wkbPoint);
|
point.AddPoint(xMax, yMax, 0);
|
|
return Transform(ds, point);
|
}
|
|
/// <summary>
|
/// 坐标转换
|
/// </summary>
|
public static Geometry Transform(Dataset ds, Geometry point)
|
{
|
point.AssignSpatialReference(ds.GetSpatialRef());
|
if (ds.GetSpatialRef().IsGeographic() > 0)
|
{
|
return point;
|
}
|
|
String srsName = ds.GetSpatialRef().GetName();
|
//if (srsName.Contains(CGCS2000))
|
//{
|
// point.TransformTo(sr4490);
|
//}
|
//else
|
//{
|
point.TransformTo(sr4326);
|
//}
|
point.SwapXY();
|
|
return point;
|
}
|
|
/// <summary>
|
/// 创建金字塔
|
/// </summary>
|
public static void CreatePyramid(String file)
|
{
|
Dataset ds = null;
|
try
|
{
|
if (!File.Exists(file)) return;
|
|
ds = Gdal.Open(file, Access.GA_ReadOnly);
|
if (null == ds) return;
|
|
Band band = ds.GetRasterBand(1);
|
if (0 == band.GetOverviewCount())
|
{
|
ds.BuildOverviews("nearest", new int[] { 2, 4, 6, 8, 16 });
|
}
|
}
|
catch (Exception ex)
|
{
|
LogOut.Error(ex.Message);
|
}
|
finally
|
{
|
if (null != ds) ds.Dispose();
|
}
|
}
|
|
/// <summary>
|
/// 创建PNG
|
/// </summary>
|
public void CreatePng(byte[] buffer, string filePath, int width, int height, int bands = 3)
|
{
|
// 创建内存驱动
|
OSGeo.GDAL.Driver memDriver = Gdal.GetDriverByName("MEM");
|
// 创建内存数据集
|
Dataset ds = memDriver.Create("mem::", width, height, bands, DataType.GDT_Byte, null);
|
|
// 设置图像信息
|
for (int i = 1; i <= bands; i++)
|
{
|
Band band = ds.GetRasterBand(i);
|
band.SetRasterColorInterpretation((ColorInterp)i);
|
}
|
|
// 写入内存图像
|
for (int i = 1; i <= bands; i++)
|
{
|
Band band = ds.GetRasterBand(i);
|
band.WriteRaster(0, 0, width, height, buffer, width, height, 0, 0);
|
}
|
|
// 创建PNG驱动
|
OSGeo.GDAL.Driver pngDriver = Gdal.GetDriverByName("PNG");
|
|
// 保存为PNG文件
|
//ds.SetProjection(Gdal.GetProjectionRef(""));
|
//ds.SetGeoTransform(Gdal.GetGeoTransform(""));
|
pngDriver.CreateCopy(filePath, ds, 0, null, null, null);
|
pngDriver.Dispose();
|
|
// 关闭数据集
|
ds.Dispose();
|
memDriver.Dispose();
|
}
|
}
|
}
|