OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
gdal_das.cc
Go to the documentation of this file.
1 // This file is part of the GDAL OPeNDAP Adapter
2 
3 // Copyright (c) 2004 OPeNDAP, Inc.
4 // Author: Frank Warmerdam <warmerdam@pobox.com>
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 //
20 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
21 
22 
23 #include "config.h"
24 
25 #include <iostream>
26 #include <string>
27 
28 #include <gdal.h>
29 #include <cpl_string.h>
30 
31 #include <DAS.h>
32 
33 using namespace libdap;
34 
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 );
38 
39 /************************************************************************/
40 /* read_variables() */
41 /************************************************************************/
42 
43 void gdal_read_dataset_attributes( DAS &das, const string &filename)
44 {
45 /* -------------------------------------------------------------------- */
46 /* Open the dataset. */
47 /* -------------------------------------------------------------------- */
48  GDALDatasetH hDS;
49 
50  GDALAllRegister();
51 
52  hDS = GDALOpen( filename.c_str(), GA_ReadOnly );
53 
54  if( hDS == NULL )
55  throw Error(string(CPLGetLastErrorMsg()));
56 
57 /* -------------------------------------------------------------------- */
58 /* Create the dataset container. */
59 /* -------------------------------------------------------------------- */
60  AttrTable *attr_table;
61  char **md;
62 
63  attr_table = das.add_table( string("GLOBAL"), new AttrTable );
64 
65 /* -------------------------------------------------------------------- */
66 /* Geotransform */
67 /* -------------------------------------------------------------------- */
68  double adfGeoTransform[6];
69 
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) )
77  {
78  char szGeoTransform[200];
79  double dfMaxX, dfMinX, dfMaxY, dfMinY;
80  int nXSize = GDALGetRasterXSize( hDS );
81  int nYSize = GDALGetRasterYSize( hDS );
82 
83  dfMaxX = MAX(
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));
89 
90  dfMinX = MIN(
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));
96 
97  dfMaxY = MAX(
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));
103 
104  dfMinY = MIN(
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));
110 
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 ) );
123 
124  snprintf( szGeoTransform, 200, "%.16g %.16g %.16g %.16g %.16g %.16g",
125  adfGeoTransform[0],
126  adfGeoTransform[1],
127  adfGeoTransform[2],
128  adfGeoTransform[3],
129  adfGeoTransform[4],
130  adfGeoTransform[5] );
131 
132  attach_str_attr_item( attr_table, "GeoTransform", szGeoTransform );
133 
134 
135  }
136 
137 /* -------------------------------------------------------------------- */
138 /* Metadata. */
139 /* -------------------------------------------------------------------- */
140  md = GDALGetMetadata( hDS, NULL );
141  if( md != NULL )
142  translate_metadata( md, attr_table );
143 
144 /* -------------------------------------------------------------------- */
145 /* SRS */
146 /* -------------------------------------------------------------------- */
147  const char *pszWKT = GDALGetProjectionRef( hDS );
148 
149  if( pszWKT != NULL && strlen(pszWKT) > 0 )
150  attach_str_attr_item( attr_table, "spatial_ref", pszWKT );
151 
152 /* ==================================================================== */
153 /* Generation info for bands. */
154 /* ==================================================================== */
155  for( int iBand = 0; iBand < GDALGetRasterCount( hDS ); iBand++ )
156  {
157  GDALRasterBandH hBand = GDALGetRasterBand( hDS, iBand+1 );
158  char szName[128];
159  AttrTable *band_attr;
160 
161 /* -------------------------------------------------------------------- */
162 /* Create container named after the band. */
163 /* -------------------------------------------------------------------- */
164  snprintf( szName, 128, "band_%d", iBand+1 );
165  band_attr = das.add_table( string(szName), new AttrTable );
166 
167 /* -------------------------------------------------------------------- */
168 /* Offset. */
169 /* -------------------------------------------------------------------- */
170  int bSuccess;
171  double dfValue;
172  char szValue[128];
173 
174  dfValue = GDALGetRasterOffset( hBand, &bSuccess );
175  if( bSuccess )
176  {
177  snprintf( szValue, 128, "%.16g", dfValue );
178  band_attr->append_attr( "add_offset", "Float64", szValue );
179  }
180 
181 /* -------------------------------------------------------------------- */
182 /* Scale */
183 /* -------------------------------------------------------------------- */
184  dfValue = GDALGetRasterScale( hBand, &bSuccess );
185  if( bSuccess )
186  {
187  snprintf( szValue, 128, "%.16g", dfValue );
188  band_attr->append_attr( "scale_factor", "Float64", szValue );
189  }
190 
191 /* -------------------------------------------------------------------- */
192 /* nodata/missing_value */
193 /* -------------------------------------------------------------------- */
194  dfValue = GDALGetRasterNoDataValue( hBand, &bSuccess );
195  if( bSuccess )
196  {
197  snprintf( szValue, 128, "%.16g", dfValue );
198  band_attr->append_attr( "missing_value", "Float64", szValue );
199  }
200 
201 /* -------------------------------------------------------------------- */
202 /* Description. */
203 /* -------------------------------------------------------------------- */
204  if( GDALGetDescription( hBand ) != NULL
205  && strlen(GDALGetDescription( hBand )) > 0 )
206  {
207  attach_str_attr_item( band_attr,
208  "Description",
209  GDALGetDescription( hBand ) );
210  }
211 
212 /* -------------------------------------------------------------------- */
213 /* PhotometricInterpretation. */
214 /* -------------------------------------------------------------------- */
215  if( GDALGetRasterColorInterpretation( hBand ) != GCI_Undefined )
216  {
217  attach_str_attr_item(
218  band_attr, "PhotometricInterpretation",
219  GDALGetColorInterpretationName(
220  GDALGetRasterColorInterpretation(hBand) ) );
221  }
222 
223 /* -------------------------------------------------------------------- */
224 /* Band Metadata. */
225 /* -------------------------------------------------------------------- */
226  md = GDALGetMetadata( hBand, NULL );
227  if( md != NULL )
228  translate_metadata( md, band_attr );
229 
230 /* -------------------------------------------------------------------- */
231 /* Colormap. */
232 /* -------------------------------------------------------------------- */
233  GDALColorTableH hCT;
234 
235  hCT = GDALGetRasterColorTable( hBand );
236  if( hCT != NULL )
237  {
238  AttrTable *ct_attr;
239  int iColor, nColorCount = GDALGetColorEntryCount( hCT );
240 
241  ct_attr = band_attr->append_container( string( "Colormap" ) );
242 
243  for( iColor = 0; iColor < nColorCount; iColor++ )
244  {
245  GDALColorEntry sRGB;
246  AttrTable *color_attr;
247 
248  color_attr = ct_attr->append_container(
249  string( CPLSPrintf( "color_%d", iColor ) ) );
250 
251  GDALGetColorEntryAsRGB( hCT, iColor, &sRGB );
252 
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 ) );
261  }
262  }
263  }
264 
265 /* -------------------------------------------------------------------- */
266 /* Close the dataset. */
267 /* -------------------------------------------------------------------- */
268  GDALClose(hDS);
269 
270 }
271 
272 /************************************************************************/
273 /* translate_metadata() */
274 /* */
275 /* Turn a list of metadata name/value pairs into DAS into and */
276 /* attach it to the passed container. */
277 /************************************************************************/
278 
279 static void translate_metadata( char **md, AttrTable *parent_table )
280 
281 {
282  AttrTable *md_table;
283  int i;
284 
285  md_table = parent_table->append_container( string("Metadata") );
286 
287  for( i = 0; md != NULL && md[i] != NULL; i++ )
288  {
289  const char *pszValue;
290  char *pszKey = NULL;
291 
292  pszValue = CPLParseNameValue( md[i], &pszKey );
293 
294  attach_str_attr_item( md_table, pszKey, pszValue );
295 
296  CPLFree( pszKey );
297  }
298 }
299 
300 /************************************************************************/
301 /* attach_str_attr_item() */
302 /* */
303 /* Add a string attribute item to target container with */
304 /* appropriate quoting and escaping. */
305 /************************************************************************/
306 
307 static void attach_str_attr_item( AttrTable *parent_table,
308  const char *pszKey, const char *pszValue )
309 
310 {
311  //string oQuotedValue;
312  char *pszEscapedText = CPLEscapeString( pszValue, -1,
313  CPLES_BackslashQuotable );
314 #if 0
315  oQuotedValue = "\"";
316  oQuotedValue += pszEscapedText;
317  oQuotedValue += "\"";
318 #endif
319 
320  parent_table->append_attr( pszKey, "String", pszEscapedText /*oQuotedValue*/ );
321 
322  CPLFree( pszEscapedText );
323 }
324 
325 // $Log: gdal_das.cc,v $
326 // Revision 1.1 2004/10/19 20:38:28 warmerda
327 // New
328 //
329 // Revision 1.2 2004/10/15 18:06:45 warmerda
330 // Strip the extension off the filename.
331 //
332 // Revision 1.1 2004/10/04 14:29:29 warmerda
333 // New
334 //
void gdal_read_dataset_attributes(DAS &das, const string &filename)
Definition: gdal_das.cc:43
static class NCMLUtil overview
#define NULL
Definition: wcsUtil.h:65