GIMMS NDVI产品(1981-2015)hdf转TIFF(基于ArcEngine+C#)

GIMMS NDVI产品(1981-2015)hdf转TIFF(基于ArcEngine+C#)

前言

GIMMS NDVI产品
来源:https://ecocast.arc.nasa.gov/data/pub/gimms/3g.v1/
范围:全球
时间:1981-2015年
分辨率:1/12度(8km),半月合成
下载格式:.hdf(站点抓取下载的,不是nc4格式),下载方式可参考博客 https://blog.csdn.net/weixin_42071896/article/details/80193025

转换后TIFF产品命名格式:yyyymma(b).tif,年月上半期(下半期)。

AE+C# 代码:

    private void button9_Click(object sender, EventArgs e)
    {
        string[] Findfiles;
        Findfiles = Directory.GetFiles(@"G:\GISer\Data\GIMMSNDVI", "*.hdf");
        string name="";
        string outname = "";
        IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactory();
        IWorkspace workspace;
        workspace = workspaceFactory.OpenFromFile(@"G:\GISer\Data\GIMMSNDVI", 0);
        IRasterWorkspace rastWork = (IRasterWorkspace)workspace;
      for (int i = 0; i < Findfiles.Count(); i++)
        {
            name = System.IO.Path.GetFileName(Findfiles[i]);
            IRasterDataset rasterdataset = rastWork.OpenRasterDataset(name);
            IRaster raster = rasterdataset.CreateDefaultRaster();
            
            

            IRasterProps rasterProps = (IRasterProps)raster;
            IPnt pBlockSize = new Pnt();
            pBlockSize.SetCoords(rasterProps.Width, rasterProps.Height);
            IPixelBlock pPixelBlock = raster.CreatePixelBlock(pBlockSize);
            
            IPnt tlp = new Pnt();
            tlp.SetCoords(0, 0);
            IRasterBandCollection pRasterBands = rasterdataset as IRasterBandCollection;
            
            //int NumBand = pRasterBands.Count;

            string year = name.Split('_')[3];
            string mouth = System.IO.Path.GetFileNameWithoutExtension(Findfiles[i]).Split('_')[4];

            IRasterBand pRasterBand;
            IRawPixels pRawRixels;

            if(mouth.Equals("0106"))        //上半年
            {
                for (int m = 0; m < 12; m = m + 2)
                {
                    outname = year +"0"+ (m / 2 + 1).ToString() + "a" + ".tif";
                    pRasterBand = pRasterBands.Item(m);
                    pRawRixels = pRasterBands.Item(m) as IRawPixels;
                    pRawRixels.Read(tlp, pPixelBlock);
                    CreateRasterDataset_2(pPixelBlock, @"G:\GISer\Data\hdf2tiff", outname);
                }
                for (int m = 1; m < 12; m = m + 2)
                {
                    outname = year + "0" + (m / 2 + 1).ToString() + "b" + ".tif";
                    pRasterBand = pRasterBands.Item(m);
                    pRawRixels = pRasterBands.Item(m) as IRawPixels;
                    pRawRixels.Read(tlp, pPixelBlock);
                    CreateRasterDataset_2(pPixelBlock, @"G:\GISer\Data\hdf2tiff", outname);
                }
            }
            else if (mouth.Equals("0712"))         //下半年
            {
                for (int m = 0; m < 12; m = m + 2)
                {
                    if (m / 2 + 7 < 10)
                    {
                        outname = year + "0" + (m / 2 + 7).ToString() + "a" + ".tif";
                        pRasterBand = pRasterBands.Item(m);
                        pRawRixels = pRasterBands.Item(m) as IRawPixels;
                        pRawRixels.Read(tlp, pPixelBlock);
                        CreateRasterDataset_2(pPixelBlock, @"G:\GISer\Data\hdf2tiff", outname);
                    }
                    else
                    {
                        outname = year+ (m / 2 + 7).ToString() + "a" + ".tif";
                        pRasterBand = pRasterBands.Item(m);
                        pRawRixels = pRasterBands.Item(m) as IRawPixels;
                        pRawRixels.Read(tlp, pPixelBlock);
                        CreateRasterDataset_2(pPixelBlock, @"G:\GISer\Data\hdf2tiff", outname);
                    }
                    
                }
                for (int m = 1; m < 12; m = m + 2)
                {
                    if (m / 2 + 7 < 10)
                    {
                        outname = year + "0" + (m / 2 + 7).ToString() + "b" + ".tif";
                        pRasterBand = pRasterBands.Item(m);
                        pRawRixels = pRasterBands.Item(m) as IRawPixels;
                        pRawRixels.Read(tlp, pPixelBlock);
                        CreateRasterDataset_2(pPixelBlock, @"G:\GISer\Data\hdf2tiff", outname);
                    }
                    else
                    {
                        outname = year + (m / 2 + 7).ToString() + "b" + ".tif";
                        pRasterBand = pRasterBands.Item(m);
                        pRawRixels = pRasterBands.Item(m) as IRawPixels;
                        pRawRixels.Read(tlp, pPixelBlock);
                        CreateRasterDataset_2(pPixelBlock, @"G:\GISer\Data\hdf2tiff", outname);
                    }
                }
            }
        }           

    }
    public static void CreateRasterDataset_2(IPixelBlock pixelblock,string path, string fileName)
    {
        try
        {
            IRasterWorkspace2 rasterWorkSpace;
            IWorkspaceFactory workspaceFact = new RasterWorkspaceFactoryClass();
            rasterWorkSpace = workspaceFact.OpenFromFile(path, 0) as IRasterWorkspace2;
           


            ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass();
            ISpatialReference spatialReference = spatialReferenceFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984);//定义空间参考
            IPoint OrigionalPoint = new PointClass();
            OrigionalPoint.PutCoords(-180 + (1.0 / 24), -90 + (1.0 / 24));
            int width = 4320;
            int height = 2160;
            double xcell = 1.0 / 12;
            double ycell = 1.0 / 12;

            IRasterDataset rasterDataset = rasterWorkSpace.CreateRasterDataset(fileName, "TIFF", OrigionalPoint, width, height, xcell, ycell, 1, rstPixelType.PT_DOUBLE, spatialReference, true);

            IRasterBandCollection rasterBands = (IRasterBandCollection)rasterDataset;
            IRasterBand rasterBand; 
            IRasterProps rasterProps; 
            rasterBand = rasterBands.Item(0);
            rasterProps = (IRasterProps)rasterBand; 
            //Set NoData if necessary. For a multiband image, a NoData value needs to be set for each band.
            rasterProps.NoDataValue = -32768; 
            //Create a raster from the dataset.
            IRaster raster = rasterDataset.CreateDefaultRaster();

            System.Array pSafeArray = pixelblock.get_SafeArray(0) as System.Array;            //原像元数据


            IPnt blocksize = new PntClass();
            blocksize.SetCoords(width, height);
            IPixelBlock3 pixelblock11 = raster.CreatePixelBlock(blocksize) as IPixelBlock3;
            System.Array pixels ;
            pixels = (System.Array)pixelblock11.get_PixelData(0);
            for (int y = 0; y < rasterProps.Height; y++)
            {
                for (int x = 0; x < rasterProps.Width; x++)
                {
                    if (Convert.ToInt32(pSafeArray.GetValue(x, y)) ==-32768)
                    {
                        pixels.SetValue(-32768, x, y);
                    }
                    else
                    {                            
                        pixels.SetValue(Convert.ToDouble(Convert.ToInt32(pSafeArray.GetValue(x, y))*1.0/10000),x,y);
                    }
                }
            }
            pixelblock11.set_PixelData(0, (System.Array)pixels);

            IPnt upperLeft = new PntClass();
            upperLeft.SetCoords(0, 0);
            IRasterEdit rasterEdit = (IRasterEdit)raster;
            rasterEdit.Write(upperLeft, (PixelBlock)pixelblock11);
            rasterEdit.Refresh();
            GC.Collect(); 
            System.Runtime.InteropServices.Marshal.ReleaseComObject(rasterEdit);              //Release rasterEdit explicitly. 

        }
        catch (Exception e)
        {

        }
    }
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cshgiser

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值