29 #include <cpl_string.h>
35 static void translate_metadata(
char **md, AttrTable *parent_table );
36 static void attach_str_attr_item( AttrTable *parent_table,
37 const char *pszKey,
const char *pszValue );
52 hDS = GDALOpen( filename.c_str(), GA_ReadOnly );
55 throw Error(
string(CPLGetLastErrorMsg()));
60 AttrTable *attr_table;
63 attr_table = das.add_table(
string(
"GLOBAL"),
new AttrTable );
68 double adfGeoTransform[6];
70 if( GDALGetGeoTransform( hDS, adfGeoTransform ) == CE_None
71 && (adfGeoTransform[0] != 0.0
72 || adfGeoTransform[1] != 1.0
73 || adfGeoTransform[2] != 0.0
74 || adfGeoTransform[3] != 0.0
75 || adfGeoTransform[4] != 0.0
76 || fabs(adfGeoTransform[5]) != 1.0) )
78 char szGeoTransform[200];
79 double dfMaxX, dfMinX, dfMaxY, dfMinY;
80 int nXSize = GDALGetRasterXSize( hDS );
81 int nYSize = GDALGetRasterYSize( hDS );
84 MAX(adfGeoTransform[0],
85 adfGeoTransform[0] + adfGeoTransform[1] * nXSize),
86 MAX(adfGeoTransform[0] + adfGeoTransform[2] * nYSize,
87 adfGeoTransform[0] + adfGeoTransform[2] * nYSize
88 + adfGeoTransform[1] * nXSize));
91 MIN(adfGeoTransform[0],
92 adfGeoTransform[0] + adfGeoTransform[1] * nXSize),
93 MIN(adfGeoTransform[0] + adfGeoTransform[2] * nYSize,
94 adfGeoTransform[0] + adfGeoTransform[2] * nYSize
95 + adfGeoTransform[1] * nXSize));
98 MAX(adfGeoTransform[3],
99 adfGeoTransform[3] + adfGeoTransform[4] * nXSize),
100 MAX(adfGeoTransform[3] + adfGeoTransform[5] * nYSize,
101 adfGeoTransform[3] + adfGeoTransform[5] * nYSize
102 + adfGeoTransform[4] * nXSize));
105 MIN(adfGeoTransform[3],
106 adfGeoTransform[3] + adfGeoTransform[4] * nXSize),
107 MIN(adfGeoTransform[3] + adfGeoTransform[5] * nYSize,
108 adfGeoTransform[3] + adfGeoTransform[5] * nYSize
109 + adfGeoTransform[4] * nXSize));
111 attr_table->append_attr(
112 "Northernmost_Northing",
"Float64",
113 CPLSPrintf(
"%.16g", dfMaxY ) );
114 attr_table->append_attr(
115 "Southernmost_Northing",
"Float64",
116 CPLSPrintf(
"%.16g", dfMinY ) );
117 attr_table->append_attr(
118 "Easternmost_Easting",
"Float64",
119 CPLSPrintf(
"%.16g", dfMaxX ) );
120 attr_table->append_attr(
121 "Westernmost_Northing",
"Float64",
122 CPLSPrintf(
"%.16g", dfMinX ) );
124 snprintf( szGeoTransform, 200,
"%.16g %.16g %.16g %.16g %.16g %.16g",
130 adfGeoTransform[5] );
132 attach_str_attr_item( attr_table,
"GeoTransform", szGeoTransform );
140 md = GDALGetMetadata( hDS,
NULL );
142 translate_metadata( md, attr_table );
147 const char *pszWKT = GDALGetProjectionRef( hDS );
149 if( pszWKT !=
NULL && strlen(pszWKT) > 0 )
150 attach_str_attr_item( attr_table,
"spatial_ref", pszWKT );
155 for(
int iBand = 0; iBand < GDALGetRasterCount( hDS ); iBand++ )
157 GDALRasterBandH hBand = GDALGetRasterBand( hDS, iBand+1 );
159 AttrTable *band_attr;
164 snprintf( szName, 128,
"band_%d", iBand+1 );
165 band_attr = das.add_table(
string(szName),
new AttrTable );
174 dfValue = GDALGetRasterOffset( hBand, &bSuccess );
177 snprintf( szValue, 128,
"%.16g", dfValue );
178 band_attr->append_attr(
"add_offset",
"Float64", szValue );
184 dfValue = GDALGetRasterScale( hBand, &bSuccess );
187 snprintf( szValue, 128,
"%.16g", dfValue );
188 band_attr->append_attr(
"scale_factor",
"Float64", szValue );
194 dfValue = GDALGetRasterNoDataValue( hBand, &bSuccess );
197 snprintf( szValue, 128,
"%.16g", dfValue );
198 band_attr->append_attr(
"missing_value",
"Float64", szValue );
204 if( GDALGetDescription( hBand ) !=
NULL
205 && strlen(GDALGetDescription( hBand )) > 0 )
207 attach_str_attr_item( band_attr,
209 GDALGetDescription( hBand ) );
215 if( GDALGetRasterColorInterpretation( hBand ) != GCI_Undefined )
217 attach_str_attr_item(
218 band_attr,
"PhotometricInterpretation",
219 GDALGetColorInterpretationName(
220 GDALGetRasterColorInterpretation(hBand) ) );
226 md = GDALGetMetadata( hBand,
NULL );
228 translate_metadata( md, band_attr );
235 hCT = GDALGetRasterColorTable( hBand );
239 int iColor, nColorCount = GDALGetColorEntryCount( hCT );
241 ct_attr = band_attr->append_container(
string(
"Colormap" ) );
243 for( iColor = 0; iColor < nColorCount; iColor++ )
246 AttrTable *color_attr;
248 color_attr = ct_attr->append_container(
249 string( CPLSPrintf(
"color_%d", iColor ) ) );
251 GDALGetColorEntryAsRGB( hCT, iColor, &sRGB );
253 color_attr->append_attr(
"red",
"byte",
254 CPLSPrintf(
"%d", sRGB.c1 ) );
255 color_attr->append_attr(
"green",
"byte",
256 CPLSPrintf(
"%d", sRGB.c2 ) );
257 color_attr->append_attr(
"blue",
"byte",
258 CPLSPrintf(
"%d", sRGB.c3 ) );
259 color_attr->append_attr(
"alpha",
"byte",
260 CPLSPrintf(
"%d", sRGB.c4 ) );
279 static void translate_metadata(
char **md, AttrTable *parent_table )
285 md_table = parent_table->append_container(
string(
"Metadata") );
287 for( i = 0; md !=
NULL && md[i] !=
NULL; i++ )
289 const char *pszValue;
292 pszValue = CPLParseNameValue( md[i], &pszKey );
294 attach_str_attr_item( md_table, pszKey, pszValue );
307 static void attach_str_attr_item( AttrTable *parent_table,
308 const char *pszKey,
const char *pszValue )
312 char *pszEscapedText = CPLEscapeString( pszValue, -1,
313 CPLES_BackslashQuotable );
316 oQuotedValue += pszEscapedText;
317 oQuotedValue +=
"\"";
320 parent_table->append_attr( pszKey,
"String", pszEscapedText );
322 CPLFree( pszEscapedText );
void gdal_read_dataset_attributes(DAS &das, const string &filename)
static class NCMLUtil overview