gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

409 lines
15KB

  1. /******************************************************************************
  2. * $Id: gdal_rat.h 2519a7eb0e1649dbf8625ae8ffc7bb7c3ef9514b 2018-07-10 12:05:23 +0100 Robert Coup $
  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. #include "gdal_priv.h"
  33. // Clone and Serialize are allowed to fail if GetRowCount()*GetColCount()
  34. // greater than this number
  35. #define RAT_MAX_ELEM_FOR_CLONE 1000000
  36. /************************************************************************/
  37. /* GDALRasterAttributeTable */
  38. /************************************************************************/
  39. //! Raster Attribute Table interface.
  40. class GDALDefaultRasterAttributeTable;
  41. class CPL_DLL GDALRasterAttributeTable
  42. {
  43. public:
  44. virtual ~GDALRasterAttributeTable();
  45. /**
  46. * \brief Copy Raster Attribute Table
  47. *
  48. * Creates a new copy of an existing raster attribute table. The new copy
  49. * becomes the responsibility of the caller to destroy.
  50. * May fail (return nullptr) if the attribute table is too large to clone
  51. * (GetRowCount() * GetColCount() > RAT_MAX_ELEM_FOR_CLONE)
  52. *
  53. * This method is the same as the C function GDALRATClone().
  54. *
  55. * @return new copy of the RAT as an in-memory implementation.
  56. */
  57. virtual GDALRasterAttributeTable *Clone() const = 0;
  58. /**
  59. * \brief Fetch table column count.
  60. *
  61. * This method is the same as the C function GDALRATGetColumnCount().
  62. *
  63. * @return the number of columns.
  64. */
  65. virtual int GetColumnCount() const = 0;
  66. /**
  67. * \brief Fetch name of indicated column.
  68. *
  69. * This method is the same as the C function GDALRATGetNameOfCol().
  70. *
  71. * @param iCol the column index (zero based).
  72. *
  73. * @return the column name or an empty string for invalid column numbers.
  74. */
  75. virtual const char *GetNameOfCol( int iCol ) const = 0;
  76. /**
  77. * \brief Fetch column usage value.
  78. *
  79. * This method is the same as the C function GDALRATGetUsageOfCol().
  80. *
  81. * @param iCol the column index (zero based).
  82. *
  83. * @return the column usage, or GFU_Generic for improper column numbers.
  84. */
  85. virtual GDALRATFieldUsage GetUsageOfCol( int iCol ) const = 0;
  86. /**
  87. * \brief Fetch column type.
  88. *
  89. * This method is the same as the C function GDALRATGetTypeOfCol().
  90. *
  91. * @param iCol the column index (zero based).
  92. *
  93. * @return column type or GFT_Integer if the column index is illegal.
  94. */
  95. virtual GDALRATFieldType GetTypeOfCol( int iCol ) const = 0;
  96. /**
  97. * \brief Fetch column index for given usage.
  98. *
  99. * Returns the index of the first column of the requested usage type, or -1
  100. * if no match is found.
  101. *
  102. * This method is the same as the C function GDALRATGetUsageOfCol().
  103. *
  104. * @param eUsage usage type to search for.
  105. *
  106. * @return column index, or -1 on failure.
  107. */
  108. virtual int GetColOfUsage( GDALRATFieldUsage eUsage ) const = 0;
  109. /**
  110. * \brief Fetch row count.
  111. *
  112. * This method is the same as the C function GDALRATGetRowCount().
  113. *
  114. * @return the number of rows.
  115. */
  116. virtual int GetRowCount() const = 0;
  117. /**
  118. * \brief Fetch field value as a string.
  119. *
  120. * The value of the requested column in the requested row is returned
  121. * as a string. If the field is numeric, it is formatted as a string
  122. * using default rules, so some precision may be lost.
  123. *
  124. * The returned string is temporary and cannot be expected to be
  125. * available after the next GDAL call.
  126. *
  127. * This method is the same as the C function GDALRATGetValueAsString().
  128. *
  129. * @param iRow row to fetch (zero based).
  130. * @param iField column to fetch (zero based).
  131. *
  132. * @return field value.
  133. */
  134. virtual const char *GetValueAsString( int iRow, int iField ) const = 0;
  135. /**
  136. * \brief Fetch field value as a integer.
  137. *
  138. * The value of the requested column in the requested row is returned
  139. * as an integer. Non-integer fields will be converted to integer with
  140. * the possibility of data loss.
  141. *
  142. * This method is the same as the C function GDALRATGetValueAsInt().
  143. *
  144. * @param iRow row to fetch (zero based).
  145. * @param iField column to fetch (zero based).
  146. *
  147. * @return field value
  148. */
  149. virtual int GetValueAsInt( int iRow, int iField ) const = 0;
  150. /**
  151. * \brief Fetch field value as a double.
  152. *
  153. * The value of the requested column in the requested row is returned
  154. * as a double. Non double fields will be converted to double with
  155. * the possibility of data loss.
  156. *
  157. * This method is the same as the C function GDALRATGetValueAsDouble().
  158. *
  159. * @param iRow row to fetch (zero based).
  160. * @param iField column to fetch (zero based).
  161. *
  162. * @return field value
  163. */
  164. virtual double GetValueAsDouble( int iRow, int iField ) const = 0;
  165. /**
  166. * \brief Set field value from string.
  167. *
  168. * The indicated field (column) on the indicated row is set from the
  169. * passed value. The value will be automatically converted for other field
  170. * types, with a possible loss of precision.
  171. *
  172. * This method is the same as the C function GDALRATSetValueAsString().
  173. *
  174. * @param iRow row to fetch (zero based).
  175. * @param iField column to fetch (zero based).
  176. * @param pszValue the value to assign.
  177. */
  178. virtual void SetValue( int iRow, int iField,
  179. const char *pszValue ) = 0;
  180. /**
  181. * \brief Set field value from integer.
  182. *
  183. * The indicated field (column) on the indicated row is set from the
  184. * passed value. The value will be automatically converted for other field
  185. * types, with a possible loss of precision.
  186. *
  187. * This method is the same as the C function GDALRATSetValueAsInteger().
  188. *
  189. * @param iRow row to fetch (zero based).
  190. * @param iField column to fetch (zero based).
  191. * @param nValue the value to assign.
  192. */
  193. virtual void SetValue( int iRow, int iField, int nValue ) = 0;
  194. /**
  195. * \brief Set field value from double.
  196. *
  197. * The indicated field (column) on the indicated row is set from the
  198. * passed value. The value will be automatically converted for other field
  199. * types, with a possible loss of precision.
  200. *
  201. * This method is the same as the C function GDALRATSetValueAsDouble().
  202. *
  203. * @param iRow row to fetch (zero based).
  204. * @param iField column to fetch (zero based).
  205. * @param dfValue the value to assign.
  206. */
  207. virtual void SetValue( int iRow, int iField, double dfValue) = 0;
  208. /**
  209. * \brief Determine whether changes made to this RAT are reflected directly
  210. * in the dataset
  211. *
  212. * If this returns FALSE then GDALRasterBand.SetDefaultRAT() should be
  213. * called. Otherwise this is unnecessary since changes to this object are
  214. * reflected in the dataset.
  215. *
  216. * This method is the same as the C function
  217. * GDALRATChangesAreWrittenToFile().
  218. *
  219. */
  220. virtual int ChangesAreWrittenToFile() = 0;
  221. /**
  222. * \brief Set the RAT table type.
  223. *
  224. * Set whether the RAT is thematic or athematic (continuous).
  225. *
  226. * @since GDAL 2.4
  227. */
  228. virtual CPLErr SetTableType(const GDALRATTableType eInTableType) = 0;
  229. /**
  230. * \brief Get the RAT table type.
  231. *
  232. * Indicates whether the RAT is thematic or athematic (continuous).
  233. *
  234. * @since GDAL 2.4
  235. * @return table type
  236. */
  237. virtual GDALRATTableType GetTableType() const = 0;
  238. virtual CPLErr ValuesIO( GDALRWFlag eRWFlag, int iField,
  239. int iStartRow, int iLength,
  240. double *pdfData);
  241. virtual CPLErr ValuesIO( GDALRWFlag eRWFlag, int iField,
  242. int iStartRow, int iLength, int *pnData);
  243. virtual CPLErr ValuesIO( GDALRWFlag eRWFlag, int iField,
  244. int iStartRow, int iLength,
  245. char **papszStrList);
  246. virtual void SetRowCount( int iCount );
  247. virtual int GetRowOfValue( double dfValue ) const;
  248. virtual int GetRowOfValue( int nValue ) const;
  249. virtual CPLErr CreateColumn( const char *pszFieldName,
  250. GDALRATFieldType eFieldType,
  251. GDALRATFieldUsage eFieldUsage );
  252. virtual CPLErr SetLinearBinning( double dfRow0Min,
  253. double dfBinSize );
  254. virtual int GetLinearBinning( double *pdfRow0Min,
  255. double *pdfBinSize ) const;
  256. /**
  257. * \brief Serialize
  258. *
  259. * May fail (return nullptr) if the attribute table is too large to serialize
  260. * (GetRowCount() * GetColCount() > RAT_MAX_ELEM_FOR_CLONE)
  261. */
  262. virtual CPLXMLNode *Serialize() const;
  263. virtual void *SerializeJSON() const;
  264. virtual CPLErr XMLInit( CPLXMLNode *, const char * );
  265. virtual CPLErr InitializeFromColorTable( const GDALColorTable * );
  266. virtual GDALColorTable *TranslateToColorTable( int nEntryCount = -1 );
  267. virtual void DumpReadable( FILE * = nullptr );
  268. /** Convert a GDALRasterAttributeTable* to a GDALRasterAttributeTableH.
  269. * @since GDAL 2.3
  270. */
  271. static inline GDALRasterAttributeTableH ToHandle(GDALRasterAttributeTable* poRAT)
  272. { return static_cast<GDALRasterAttributeTableH>(poRAT); }
  273. /** Convert a GDALRasterAttributeTableH to a GDALRasterAttributeTable*.
  274. * @since GDAL 2.3
  275. */
  276. static inline GDALRasterAttributeTable* FromHandle(GDALRasterAttributeTableH hRAT)
  277. { return static_cast<GDALRasterAttributeTable*>(hRAT); }
  278. /**
  279. * \brief Remove statistics from the RAT.
  280. *
  281. * @since GDAL 2.4
  282. */
  283. virtual void RemoveStatistics() = 0;
  284. };
  285. /************************************************************************/
  286. /* GDALRasterAttributeField */
  287. /* */
  288. /* (private) */
  289. /************************************************************************/
  290. //! @cond Doxygen_Suppress
  291. class GDALRasterAttributeField
  292. {
  293. public:
  294. CPLString sName{};
  295. GDALRATFieldType eType = GFT_Integer;
  296. GDALRATFieldUsage eUsage = GFU_Generic;
  297. std::vector<GInt32> anValues{};
  298. std::vector<double> adfValues{};
  299. std::vector<CPLString> aosValues{};
  300. };
  301. //! @endcond
  302. /************************************************************************/
  303. /* GDALDefaultRasterAttributeTable */
  304. /************************************************************************/
  305. //! Raster Attribute Table container.
  306. class CPL_DLL GDALDefaultRasterAttributeTable : public GDALRasterAttributeTable
  307. {
  308. private:
  309. std::vector<GDALRasterAttributeField> aoFields{};
  310. int bLinearBinning = false; // TODO(schwehr): Can this be a bool?
  311. double dfRow0Min = -0.5;
  312. double dfBinSize = 1.0;
  313. GDALRATTableType eTableType;
  314. void AnalyseColumns();
  315. int bColumnsAnalysed = false; // TODO(schwehr): Can this be a bool?
  316. int nMinCol = -1;
  317. int nMaxCol = -1;
  318. int nRowCount = 0;
  319. CPLString osWorkingResult{};
  320. public:
  321. GDALDefaultRasterAttributeTable();
  322. ~GDALDefaultRasterAttributeTable() override;
  323. GDALDefaultRasterAttributeTable *Clone() const override;
  324. int GetColumnCount() const override;
  325. const char *GetNameOfCol( int ) const override;
  326. GDALRATFieldUsage GetUsageOfCol( int ) const override;
  327. GDALRATFieldType GetTypeOfCol( int ) const override;
  328. int GetColOfUsage( GDALRATFieldUsage ) const override;
  329. int GetRowCount() const override;
  330. const char *GetValueAsString( int iRow, int iField ) const override;
  331. int GetValueAsInt( int iRow, int iField ) const override;
  332. double GetValueAsDouble( int iRow, int iField ) const override;
  333. void SetValue( int iRow, int iField,
  334. const char *pszValue ) override;
  335. void SetValue( int iRow, int iField, double dfValue) override;
  336. void SetValue( int iRow, int iField, int nValue ) override;
  337. int ChangesAreWrittenToFile() override;
  338. void SetRowCount( int iCount ) override;
  339. int GetRowOfValue( double dfValue ) const override;
  340. int GetRowOfValue( int nValue ) const override;
  341. CPLErr CreateColumn( const char *pszFieldName,
  342. GDALRATFieldType eFieldType,
  343. GDALRATFieldUsage eFieldUsage ) override;
  344. CPLErr SetLinearBinning( double dfRow0Min,
  345. double dfBinSize ) override;
  346. int GetLinearBinning( double *pdfRow0Min,
  347. double *pdfBinSize ) const override;
  348. CPLErr SetTableType(const GDALRATTableType eInTableType) override;
  349. GDALRATTableType GetTableType() const override;
  350. void RemoveStatistics() override;
  351. };
  352. #endif /* ndef GDAL_RAT_H_INCLUDED */
上海开阖软件有限公司 沪ICP备12045867号-1