[Gdal-dev] [C# bindings] TIF Point or Area

Chapman, Martin MChapman at sanz.com
Sun Jan 28 14:03:03 EST 2007


Jean,

 

Here is a function I use to get a bunch of metadata from any image.  If
the image has the value AREA_OR_POINT in a tiff file then it will be in
the list output by the first for loop in the function below and the
value will be "Point" or "Area".  To do it in c# just use the __unsafe
{} block in your code and use the c++ code below.  The classes that
start with IVP... and CVP... are my own classes.  Let me know if you
have questions.

 

Best regards,

 

Martin chapman

 

vector< string>* CVPGdalRasterSet::GetMetaData()

{

      try

      {

            if (m_pMetaData) return (vector< string>*) m_pMetaData;

            m_pMetaData = new vector< string>;

 

            if (!((CVPGdalConnection*)m_spConnection.p)->m_pDataSource)
return m_pMetaData;

 

            GDALDataset* pDataSource =
((CVPGdalConnection*)m_spConnection.p)->m_pDataSource;

            char** ppszMetadata = pDataSource->GetMetadata();

            if (ppszMetadata)

            {

                  for (int i = 0; ppszMetadata[i] != NULL; i++)

                  {

                        string sMetaItem = (string) (char*)
ppszMetadata[i];

 

                        if (sMetaItem.size() == 0)

                              continue;

 

                        string sLastChar =
sMetaItem.substr(sMetaItem.size() - 1, 1);

                        if (sLastChar == "=")

                              sMetaItem += "Undefined";

 

                        m_pMetaData->push_back(sMetaItem);

                  }

            }

 

            for (int iBand = 0; iBand < pDataSource->GetRasterCount();
iBand++)

            {

                  double dfMin, dfMax, dfNoData;

                  int bGotMin, bGotMax, bGotNodata;

                  int nBlockXSize, nBlockYSize;

 

                  GDALRasterBand* pBand =
pDataSource->GetRasterBand(iBand+1);

 

                  /*float afSample[10000];

                  int nCount = GDALGetRandomRasterSample(pBand, 10000,
afSample);

                  stringstream sSamples;

                  sSamples << "BAND " << iBand + 1 << " SAMPLES=" <<
nCount;

                  m_pMetaData->push_back(sSamples.str());*/

                

                  pBand->GetBlockSize(&nBlockXSize, &nBlockYSize);

                  stringstream sBlock;

                  sBlock << "BAND " << iBand + 1 << " BLOCK=" <<
nBlockXSize << "x" << nBlockYSize;

                  m_pMetaData->push_back(sBlock.str());

 

                  string sDataTypeName = (string) (char*)
GDALGetDataTypeName(pBand->GetRasterDataType());

                  stringstream sDataTypeNameStream;

                  sDataTypeNameStream << "BAND " << iBand + 1 << " DATA
TYPE=" << sDataTypeName.c_str();

                  m_pMetaData->push_back(sDataTypeNameStream.str());

 

                  string sColorInterpret = (string) (char*)
GDALGetColorInterpretationName(pBand->GetColorInterpretation());

                  stringstream sColorInterpretStream;

                  sColorInterpretStream << "BAND " << iBand + 1 << "
COLOR INTERPRETATION=" << sColorInterpret.c_str();

                  m_pMetaData->push_back(sColorInterpretStream.str());

 

                  string sDescription = (char*) pBand->GetDescription();

                  if (sDescription.size() > 0)

                  {

                        if (sDescription == "") sDescription = " ";

                        stringstream sStream;

                        sStream << "BAND " << iBand + 1 << "
DESCRIPTION=" << (char*) sDescription.c_str();

                        m_pMetaData->push_back(sStream.str());

                  }

 

                  dfMin = pBand->GetMinimum(&bGotMin);

                  dfMax = pBand->GetMaximum(&bGotMax);

                  stringstream sStreamMin;

                  sStreamMin << "BAND " << iBand + 1 << " MINIMUM=" <<
dfMin;

                  m_pMetaData->push_back(sStreamMin.str());

 

                  stringstream sStreamMax;

                  sStreamMax << "BAND " << iBand + 1 << " MAXIMUM=" <<
dfMax;

                  m_pMetaData->push_back(sStreamMax.str());

 

                  dfNoData = pBand->GetNoDataValue(&bGotNodata);

                  stringstream sStreamNoData;

                  sStreamNoData << "BAND " << iBand + 1 << " NO DATA
VALUE=" << dfNoData;

                  m_pMetaData->push_back(sStreamNoData.str());

            }

 

            return (vector< string>*) m_pMetaData;

      }

      catch(exception e)      

            {throw e;}

      catch(char* e)    

            {throw e;}

      catch(...)  

            {throw "An unexpected error occurred in
CVPGdalRasterSet::GetMetaData().";}

}

 

 

Martin Chapman

SANZ Inc.

Software Developer

http://www.earthwhere.com

W - 303.495.6326

C  - 303.898.0397

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/gdal-dev/attachments/20070128/9475e02e/attachment.html


More information about the Gdal-dev mailing list