Gdal reads data from subdatasets such as HDF and netcdf (multiple datasets)

Source: Internet
Author: User

 

Because the structure of the satellite data (HDF data) is different from that of geotif, you must pay special attention to it when reading the data. Geotif data is generally a file that contains data in multiple bands. While while the modemis, a file contains multiple subdatasets. Gdal. Each subdataset contains multiple band data. In addition, the default compiled gdal does not include support for the modem_data. You need to download the source code for hdf4 and hdf5 separately, and then modify make. the opt file, then compile gdal, and then read and write the data in the MNS. If it is too troublesome, download a compiled project from someone else. There are many open-source projects based on gdal on the Internet. You can search for them.
In the following example, the key gdal reads the data formats of multiple datasets, such as HDF data, netcdf data, and ntif data;

//////////////////////////////////////// ////////////////////////////////////
# Include <stdio. h>
# Include <vector>
# Include <string>
Using namespace STD;

# Include <math. h>
# Include gdal. H"
# Include gdal_priv.h"
# Include ogr_srs_api.h"
# Include cpl_string.h"
# Include cpl_conv.h"
# Pragma comment (Lib, ".. // lib // gdal_ I .lib ")

Int main (INT argc, char * argv [])
{

Gdalallregister ();
Vector <gdaldataset *> datasets;

Char * hdffilename = "D: // fsbdata // v2krns101_20060411_ndvi1_se-Asia // 0001 // 0001_ndv.hdf ";
Gdaldataset * tmpdataset = (gdaldataset *) gdalopen (hdffilename, ga_readonly );

If (tmpdataset = NULL) return 0;
Gdaldriver * driver = tmpdataset-> getdriver ();
String papszmetadata = gdalgetdrivershortname (gdaldriverh) tmpdataset );
Int Index = papszmetadata. find_first_of ("HDF ");
If (index <0)
Index = papszmetadata. find_first_of ("HDF ");
If (INDEX)
{
Char ** subdatasets = gdalgetmetadata (gdaldataseth) tmpdataset, "subdatasets ");
If (cslcount (subdatasets)> 0)
{
Printf ("subdatasets:/N ");
For (INT I = 0; subdatasets [I]! = NULL; I ++)
{
If (I % 2 = 0)
{
String tmpstr = string (subdatasets [I]);
Tmpstr = tmpstr. substr (tmpstr. find_first_of ("=") + 1 );
Const char * tmpfilename = tmpstr. c_str ();
Gdaldataset * tmpdt = (gdaldataset *) gdalopen (tmpfilename, ga_readonly );
If (tmpdt)
{
Datasets. push_back (tmpdt );
}
}
}
}
Else
{
Int bandcount = tmpdataset-> getrastercount ();
If (bandcount> 0)
{
Gdalrasterband * poband = tmpdataset-> getrasterband (1 );
}
Printf ("It contains % d bands! /N ", bandcount );
}
}
If (! Datasets. Empty ())
{
Int COUNT = datasets. Size ();
Int bandcount = 0;
For (INT I = 0; I <count; I ++)
{
Bandcount + = datasets [I]-> getrastercount ();
If (datasets [I]-> getrastercount ()> 0)
{
Gdalrasterband * poband = datasets [I]-> getrasterband (1 );
}
}
Printf ("It contains % d bands! /N ", bandcount );
For (I = 0; I <count; I ++)
{
Delete datasets [I];
Datasets [I] = NULL;
}
Datasets. Reserve (0 );
}

Delete tmpdataset;
Delete driver;
Printf ("It's OK! Please enter any key! /N ");
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.