前言
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)
{
}
}