Пример #1
0
        public bool TransToGrid(double[] padfX, double[] padfY, double[] padfZ, double[] dEnvelopes, string outFilename, double dPixCellSize = 0.0005, double dEnvelope_offsetX = 0, double dEnvelope_offsetY = 0, DataType typeData = DataType.GDT_Float64)
        {
            if (padfX.Length != padfY.Length && padfX.Length != padfZ.Length)
            {
                return(false);
            }

            //计算图像大小-按数据边界(dEnvelopes:MinX,MaxX,MinY,MaxY)
            int nNumPoints = padfX.Length;
            int nXSize     = (int)((dEnvelopes[1] - dEnvelopes[0] + 2 * dEnvelope_offsetX) / dPixCellSize);
            int nYSize     = (int)((dEnvelopes[3] - dEnvelopes[2] + 2 * dEnvelope_offsetY) / dPixCellSize);


            // 创建输出数据集,格式为GeoTiff格式
            OSGeo.GDAL.Driver poDriver  = Gdal.GetDriverByName(_DstDriverName);
            Dataset           poDataset = poDriver.Create(outFilename, nXSize, nYSize, 1, typeData, null);

            if (_SpatialWKT != "")
            {
                poDataset.SetProjection(_SpatialWKT);
            }


            // 离散点内插方法,参数生成
            string poOptions = _GdalAlg.InitOptions();

            double[] pData = new double[nXSize * nYSize];
            Gdal.GridCreate(poOptions, nNumPoints, padfX, padfY, padfZ,
                            dEnvelopes[0], dEnvelopes[1], dEnvelopes[2], dEnvelopes[3], nXSize, nYSize,
                            typeData, pData, nXSize * nYSize * 8, null, null);

            // 设置六参数
            double[] adfGeoTransform = new double[] { dEnvelopes[0] - dEnvelope_offsetX, dPixCellSize, 0, dEnvelopes[3] + dEnvelope_offsetY, 0, -dPixCellSize };
            poDataset.SetGeoTransform(adfGeoTransform);

            // 写入影像
            Band poBand = poDataset.GetRasterBand(1);

            poBand.WriteRaster(0, 0, nXSize, nYSize, pData, nXSize, nYSize, 0, 0);
            poBand.SetNoDataValue(_NoDataValue);

            poBand.FlushCache();
            poDataset.FlushCache();

            // 释放资源 关闭图像
            poBand.Dispose();
            poDriver.Dispose();
            poDataset.Dispose();
            return(true);
        }