gdal_rat.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /******************************************************************************
  2. * $Id: gdal_rat.h 29243 2015-05-24 15:53:26Z rouault $
  3. *
  4. * Project: GDAL Core
  5. * Purpose: GDALRasterAttributeTable class declarations.
  6. * Author: Frank Warmerdam, warmerdam@pobox.com
  7. *
  8. ******************************************************************************
  9. * Copyright (c) 2005, Frank Warmerdam <warmerdam@pobox.com>
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a
  12. * copy of this software and associated documentation files (the "Software"),
  13. * to deal in the Software without restriction, including without limitation
  14. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  15. * and/or sell copies of the Software, and to permit persons to whom the
  16. * Software is furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included
  19. * in all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  22. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  24. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  26. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  27. * DEALINGS IN THE SOFTWARE.
  28. ****************************************************************************/
  29. #ifndef GDAL_RAT_H_INCLUDED
  30. #define GDAL_RAT_H_INCLUDED
  31. #include "cpl_minixml.h"
  32. // Clone and Serialize are allowed to fail if GetRowCount()*GetColCount() greater
  33. // than this number
  34. #define RAT_MAX_ELEM_FOR_CLONE 1000000
  35. /************************************************************************/
  36. /* GDALRasterAttributeTable */
  37. /************************************************************************/
  38. //! Raster Attribute Table interface.
  39. class GDALDefaultRasterAttributeTable;
  40. class CPL_DLL GDALRasterAttributeTable
  41. {
  42. public:
  43. virtual ~GDALRasterAttributeTable();
  44. /**
  45. * \brief Copy Raster Attribute Table
  46. *
  47. * Creates a new copy of an existing raster attribute table. The new copy
  48. * becomes the responsibility of the caller to destroy.
  49. * May fail (return NULL) if the attribute table is too large to clone
  50. * (GetRowCount() * GetColCount() > RAT_MAX_ELEM_FOR_CLONE)
  51. *
  52. * This method is the same as the C function GDALRATClone().
  53. *
  54. * @return new copy of the RAT as an in-memory implementation.
  55. */
  56. virtual GDALDefaultRasterAttributeTable *Clone() const = 0;
  57. /**
  58. * \brief Fetch table column count.
  59. *
  60. * This method is the same as the C function GDALRATGetColumnCount().
  61. *
  62. * @return the number of columns.
  63. */
  64. virtual int GetColumnCount() const = 0;
  65. /**
  66. * \brief Fetch name of indicated column.
  67. *
  68. * This method is the same as the C function GDALRATGetNameOfCol().
  69. *
  70. * @param iCol the column index (zero based).
  71. *
  72. * @return the column name or an empty string for invalid column numbers.
  73. */
  74. virtual const char *GetNameOfCol( int ) const = 0;
  75. /**
  76. * \brief Fetch column usage value.
  77. *
  78. * This method is the same as the C function GDALRATGetUsageOfCol().
  79. *
  80. * @param iCol the column index (zero based).
  81. *
  82. * @return the column usage, or GFU_Generic for improper column numbers.
  83. */
  84. virtual GDALRATFieldUsage GetUsageOfCol( int ) const = 0;
  85. /**
  86. * \brief Fetch column type.
  87. *
  88. * This method is the same as the C function GDALRATGetTypeOfCol().
  89. *
  90. * @param iCol the column index (zero based).
  91. *
  92. * @return column type or GFT_Integer if the column index is illegal.
  93. */
  94. virtual GDALRATFieldType GetTypeOfCol( int ) const = 0;
  95. /**
  96. * \brief Fetch column index for given usage.
  97. *
  98. * Returns the index of the first column of the requested usage type, or -1
  99. * if no match is found.
  100. *
  101. * This method is the same as the C function GDALRATGetUsageOfCol().
  102. *
  103. * @param eUsage usage type to search for.
  104. *
  105. * @return column index, or -1 on failure.
  106. */
  107. virtual int GetColOfUsage( GDALRATFieldUsage ) const = 0;
  108. /**
  109. * \brief Fetch row count.
  110. *
  111. * This method is the same as the C function GDALRATGetRowCount().
  112. *
  113. * @return the number of rows.
  114. */
  115. virtual int GetRowCount() const = 0;
  116. /**
  117. * \brief Fetch field value as a string.
  118. *
  119. * The value of the requested column in the requested row is returned
  120. * as a string. If the field is numeric, it is formatted as a string
  121. * using default rules, so some precision may be lost.
  122. *
  123. * The returned string is temporary and cannot be expected to be
  124. * available after the next GDAL call.
  125. *
  126. * This method is the same as the C function GDALRATGetValueAsString().
  127. *
  128. * @param iRow row to fetch (zero based).
  129. * @param iField column to fetch (zero based).
  130. *
  131. * @return field value.
  132. */
  133. virtual const char *GetValueAsString( int iRow, int iField ) const = 0;
  134. /**
  135. * \brief Fetch field value as a integer.
  136. *
  137. * The value of the requested column in the requested row is returned
  138. * as an integer. Non-integer fields will be converted to integer with
  139. * the possibility of data loss.
  140. *
  141. * This method is the same as the C function GDALRATGetValueAsInt().
  142. *
  143. * @param iRow row to fetch (zero based).
  144. * @param iField column to fetch (zero based).
  145. *
  146. * @return field value
  147. */
  148. virtual int GetValueAsInt( int iRow, int iField ) const = 0;
  149. /**
  150. * \brief Fetch field value as a double.
  151. *
  152. * The value of the requested column in the requested row is returned
  153. * as a double. Non double fields will be converted to double with
  154. * the possibility of data loss.
  155. *
  156. * This method is the same as the C function GDALRATGetValueAsDouble().
  157. *
  158. * @param iRow row to fetch (zero based).
  159. * @param iField column to fetch (zero based).
  160. *
  161. * @return field value
  162. */
  163. virtual double GetValueAsDouble( int iRow, int iField ) const = 0;
  164. /**
  165. * \brief Set field value from string.
  166. *
  167. * The indicated field (column) on the indicated row is set from the
  168. * passed value. The value will be automatically converted for other field
  169. * types, with a possible loss of precision.
  170. *
  171. * This method is the same as the C function GDALRATSetValueAsString().
  172. *
  173. * @param iRow row to fetch (zero based).
  174. * @param iField column to fetch (zero based).
  175. * @param pszValue the value to assign.
  176. */
  177. virtual void SetValue( int iRow, int iField, const char *pszValue ) = 0;
  178. /**
  179. * \brief Set field value from integer.
  180. *
  181. * The indicated field (column) on the indicated row is set from the
  182. * passed value. The value will be automatically converted for other field
  183. * types, with a possible loss of precision.
  184. *
  185. * This method is the same as the C function GDALRATSetValueAsInteger().
  186. *
  187. * @param iRow row to fetch (zero based).
  188. * @param iField column to fetch (zero based).
  189. * @param nValue the value to assign.
  190. */
  191. virtual void SetValue( int iRow, int iField, int nValue ) = 0;
  192. /**
  193. * \brief Set field value from double.
  194. *
  195. * The indicated field (column) on the indicated row is set from the
  196. * passed value. The value will be automatically converted for other field
  197. * types, with a possible loss of precision.
  198. *
  199. * This method is the same as the C function GDALRATSetValueAsDouble().
  200. *
  201. * @param iRow row to fetch (zero based).
  202. * @param iField column to fetch (zero based).
  203. * @param dfValue the value to assign.
  204. */
  205. virtual void SetValue( int iRow, int iField, double dfValue) = 0;
  206. /**
  207. * \brief Determine whether changes made to this RAT are reflected directly in the dataset
  208. *
  209. * If this returns FALSE then GDALRasterBand.SetDefaultRAT() should be called. Otherwise
  210. * this is unnecessary since changes to this object are reflected in the dataset.
  211. *
  212. * This method is the same as the C function GDALRATChangesAreWrittenToFile().
  213. *
  214. */
  215. virtual int ChangesAreWrittenToFile() = 0;
  216. virtual CPLErr ValuesIO(GDALRWFlag eRWFlag, int iField, int iStartRow, int iLength, double *pdfData);
  217. virtual CPLErr ValuesIO(GDALRWFlag eRWFlag, int iField, int iStartRow, int iLength, int *pnData);
  218. virtual CPLErr ValuesIO(GDALRWFlag eRWFlag, int iField, int iStartRow, int iLength, char **papszStrList);
  219. virtual void SetRowCount( int iCount );
  220. virtual int GetRowOfValue( double dfValue ) const;
  221. virtual int GetRowOfValue( int nValue ) const;
  222. virtual CPLErr CreateColumn( const char *pszFieldName,
  223. GDALRATFieldType eFieldType,
  224. GDALRATFieldUsage eFieldUsage );
  225. virtual CPLErr SetLinearBinning( double dfRow0Min, double dfBinSize );
  226. virtual int GetLinearBinning( double *pdfRow0Min, double *pdfBinSize ) const;
  227. /**
  228. * \brief Serialize
  229. *
  230. * May fail (return NULL) if the attribute table is too large to serialize
  231. * (GetRowCount() * GetColCount() > RAT_MAX_ELEM_FOR_CLONE)
  232. */
  233. virtual CPLXMLNode *Serialize() const;
  234. virtual void *SerializeJSON() const;
  235. virtual CPLErr XMLInit( CPLXMLNode *, const char * );
  236. virtual CPLErr InitializeFromColorTable( const GDALColorTable * );
  237. virtual GDALColorTable *TranslateToColorTable( int nEntryCount = -1 );
  238. virtual void DumpReadable( FILE * = NULL );
  239. };
  240. /************************************************************************/
  241. /* GDALRasterAttributeField */
  242. /* */
  243. /* (private) */
  244. /************************************************************************/
  245. class GDALRasterAttributeField
  246. {
  247. public:
  248. CPLString sName;
  249. GDALRATFieldType eType;
  250. GDALRATFieldUsage eUsage;
  251. std::vector<GInt32> anValues;
  252. std::vector<double> adfValues;
  253. std::vector<CPLString> aosValues;
  254. };
  255. /************************************************************************/
  256. /* GDALDefaultRasterAttributeTable */
  257. /************************************************************************/
  258. //! Raster Attribute Table container.
  259. class CPL_DLL GDALDefaultRasterAttributeTable : public GDALRasterAttributeTable
  260. {
  261. private:
  262. std::vector<GDALRasterAttributeField> aoFields;
  263. int bLinearBinning;
  264. double dfRow0Min;
  265. double dfBinSize;
  266. void AnalyseColumns();
  267. int bColumnsAnalysed;
  268. int nMinCol;
  269. int nMaxCol;
  270. int nRowCount;
  271. CPLString osWorkingResult;
  272. public:
  273. GDALDefaultRasterAttributeTable();
  274. GDALDefaultRasterAttributeTable(const GDALDefaultRasterAttributeTable&);
  275. ~GDALDefaultRasterAttributeTable();
  276. GDALDefaultRasterAttributeTable *Clone() const;
  277. virtual int GetColumnCount() const;
  278. virtual const char *GetNameOfCol( int ) const;
  279. virtual GDALRATFieldUsage GetUsageOfCol( int ) const;
  280. virtual GDALRATFieldType GetTypeOfCol( int ) const;
  281. virtual int GetColOfUsage( GDALRATFieldUsage ) const;
  282. virtual int GetRowCount() const;
  283. virtual const char *GetValueAsString( int iRow, int iField ) const;
  284. virtual int GetValueAsInt( int iRow, int iField ) const;
  285. virtual double GetValueAsDouble( int iRow, int iField ) const;
  286. virtual void SetValue( int iRow, int iField, const char *pszValue );
  287. virtual void SetValue( int iRow, int iField, double dfValue);
  288. virtual void SetValue( int iRow, int iField, int nValue );
  289. virtual int ChangesAreWrittenToFile();
  290. virtual void SetRowCount( int iCount );
  291. virtual int GetRowOfValue( double dfValue ) const;
  292. virtual int GetRowOfValue( int nValue ) const;
  293. virtual CPLErr CreateColumn( const char *pszFieldName,
  294. GDALRATFieldType eFieldType,
  295. GDALRATFieldUsage eFieldUsage );
  296. virtual CPLErr SetLinearBinning( double dfRow0Min, double dfBinSize );
  297. virtual int GetLinearBinning( double *pdfRow0Min, double *pdfBinSize ) const;
  298. };
  299. #endif /* ndef GDAL_RAT_H_INCLUDED */