分享给有需要的人,代码质量勿喷
/// <summary>
/// 1、定义结构体
/// </summary>
struct LasGridInfo
{
public double MinX;
public double MinY;
public double GSD;
public int SumRow;
public int SumCol;
public int SumGN;
}
/// <summary>
/// 2、划分格网,确定DoPP
/// </summary>
/// <param name="LasFileFullName"></param>
/// <param name="xjLasGridInfo"></param>
/// <returns></returns>
private Hashtable xjDividePointToGrid(string LasFileFullName, ref LasGridInfo xjLasGridInfo)
{
Hashtable xjHtAllGridPoint = new Hashtable();
#region Las文件的基本信息
LASReader xjLASReader = new LASReader(LasFileFullName);
LASHeader xjLASHeader = xjLASReader.GetHeader();
double xjMaxX = xjLASHeader.GetMaxX();
double xjMinX = xjLASHeader.GetMinX();
double xjMaxY = xjLASHeader.GetMaxY();
double xjMinY = xjLASHeader.GetMinY();
xjLasGridInfo.MinX = xjMinX;
xjLasGridInfo.MinY = xjMinY;
double ThrGSD = xjLasGridInfo.GSD;
int xjSumRow = (int)((xjMaxY - xjMinY) / ThrGSD) + 1;//总行数
int xjSumCol = (int)((xjMaxX - xjMinX) / ThrGSD) + 1;//总列数
int xjGridCount = xjSumRow * xjSumCol;
xjLasGridInfo.SumRow = xjSumRow;
xjLasGridInfo.SumCol = xjSumCol;
xjLasGridInfo.SumGN = xjGridCount;
#endregion
int xjRowNumber = 0;
int xjColNumber = 0;
int xjGridNumber = 0;
while (xjLASReader.GetNextPoint())
{
LASPoint xjLASPoint = xjLASReader.GetPoint();
xjRowNumber = (int)((xjLASPoint.Y - xjMinY) / ThrGSD) + 1;
xjColNumber = (int)((xjLASPoint.X - xjMinX) / ThrGSD) + 1;
xjGridNumber = (xjRowNumber - 1) * xjSumCol + xjColNumber;
if (!xjHtAllGridPoint.ContainsKey(xjGridNumber))
{
xjHtAllGridPoint.Add(xjGridNumber, 1);
}
else
{
int xjCount = (int)xjHtAllGridPoint[xjGridNumber];
xjHtAllGridPoint.Remove(xjGridNumber);
xjHtAllGridPoint.Add(xjGridNumber, xjCount + 1);
}
}
return xjHtAllGridPoint;
}
/// <summary>
/// 3、输出特征图像
/// </summary>
/// <param name="HtDoPP"></param>
/// <param name="xjLasGridInfo"></param>
/// <param name="ExportLasFullName"></param>
private void xjOutputTiff(Hashtable HtDoPP, LasGridInfo xjLasGridInfo, string ExportLasFullName)
{
string xjOutRasterPath = System.IO.Path.GetDirectoryName(ExportLasFullName);
IRasterDataset xjOutRasterDataset = CreateRasterDataset(xjOutRasterPath, ExportLasFullName, xjLasGridInfo.MinX,
xjLasGridInfo.MinY, xjLasGridInfo.GSD, HtDoPP, xjLasGridInfo.SumRow, xjLasGridInfo.SumCol);
IRasterLayer xjOutRasterLayer = new RasterLayerClass();
xjOutRasterLayer.CreateFromDataset(xjOutRasterDataset);
ILayer xjOutLayer = xjOutRasterLayer;
xjOutRasterLayer.Name = ExportLasFullName;
}
/// <summary>
/// 3.1 创建栅格数据集
/// </summary>
/// <param name="FileDirectoryName"></param>
/// <param name="FielName"></param>
/// <param name="G_Xmin"></param>
/// <param name="G_Ymin"></param>
/// <param name="G_GridSide"></param>
/// <param name="G_Hashtable"></param>
/// <param name="G_RowSum"></param>
/// <param name="G_ColumnSum"></param>
/// <returns></returns>
private IRasterDataset CreateRasterDataset(string FileDirectoryName, string FielName, double G_Xmin,
double G_Ymin, double G_GridSide, Hashtable G_Hashtable, int G_RowSum, int G_ColumnSum)
{
//原点
IPoint xjOriginPoint = new PointClass();
xjOriginPoint.PutCoords(G_Xmin, G_Ymin);
//创建数据集
IWorkspaceFactory xjWorkspaceFactory = new RasterWorkspaceFactoryClass();
IRasterWorkspace2 xjRasterWorkspace = xjWorkspaceFactory.OpenFromFile(FileDirectoryName, 0) as IRasterWorkspace2;
IRasterDataset xjRasterDataset = xjRasterWorkspace.CreateRasterDataset(FielName, "TIFF", xjOriginPoint, G_ColumnSum,
G_RowSum, G_GridSide, G_GridSide, 1, rstPixelType.PT_DOUBLE, new UnknownCoordinateSystemClass(), true);
IRasterBandCollection xjRasterBandCollection = (IRasterBandCollection)xjRasterDataset;
IRasterBand xjRasterBand = xjRasterBandCollection.Item(0);
IRasterProps xjRasterProps = (IRasterProps)xjRasterBand;
xjRasterProps.NoDataValue = -1.0;
IPnt xjPntPixelBlockOrigin = new DblPntClass();
xjPntPixelBlockOrigin.SetCoords(0, 0);
IPnt xjPntPixelBlockSize = new DblPntClass();
xjPntPixelBlockSize.SetCoords(xjRasterProps.Width, xjRasterProps.Height);//(列数,行数)
IRawPixels xjRawPixels = (IRawPixels)xjRasterBandCollection.Item(0);
IPixelBlock3 xjPixelBlock3 = (IPixelBlock3)xjRawPixels.CreatePixelBlock(xjPntPixelBlockSize);
xjRawPixels.Read(xjPntPixelBlockOrigin, (IPixelBlock)xjPixelBlock3);
System.Array xjPixelData = (System.Array)xjPixelBlock3.get_PixelDataByRef(0);
for (int i = 0; i < xjRasterProps.Width; i++)//列数
{
for (int j = 0; j < xjRasterProps.Height; j++)//行数
{
int GridIndex = (G_RowSum - j - 1) * G_ColumnSum + (i + 1);
if (G_Hashtable.Contains(GridIndex))
{
double GridValueTemp = Convert.ToDouble(G_Hashtable[GridIndex]);
xjPixelData.SetValue(GridValueTemp, i, j);
}
else
{
xjPixelData.SetValue(-1.0, i, j);
}
}
}
xjPixelBlock3.set_PixelData(0, (System.Object)xjPixelData);
byte[] xjNoDataArray = (byte[])xjPixelBlock3.get_NoDataMaskByRef(0);
xjPixelBlock3.set_NoDataMask(0, (System.Object)xjNoDataArray);
System.Object xjCachePointer = xjRawPixels.AcquireCache();
xjRawPixels.Write(xjPntPixelBlockOrigin, (IPixelBlock)xjPixelBlock3);
xjRawPixels.ReturnCache(xjCachePointer);
return xjRasterDataset;
}
VS2010窗体+代码:https://download.csdn.net/download/xinjiang666/11038353
参考文献:史文中,李必军,李清泉.基于投影点密度的车载激光扫描距离图像分割方法[J].测绘学报,2005(02):95-100.