ogr_feature.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /******************************************************************************
  2. * $Id: ogr_feature.h 28968 2015-04-21 19:00:02Z rouault $
  3. *
  4. * Project: OpenGIS Simple Features Reference Implementation
  5. * Purpose: Class for representing a whole feature, and layer schemas.
  6. * Author: Frank Warmerdam, warmerdam@pobox.com
  7. *
  8. ******************************************************************************
  9. * Copyright (c) 1999, Les Technologies SoftMap Inc.
  10. * Copyright (c) 2008-2013, Even Rouault <even dot rouault at mines-paris dot org>
  11. *
  12. * Permission is hereby granted, free of charge, to any person obtaining a
  13. * copy of this software and associated documentation files (the "Software"),
  14. * to deal in the Software without restriction, including without limitation
  15. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  16. * and/or sell copies of the Software, and to permit persons to whom the
  17. * Software is furnished to do so, subject to the following conditions:
  18. *
  19. * The above copyright notice and this permission notice shall be included
  20. * in all copies or substantial portions of the Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  23. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  25. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  27. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  28. * DEALINGS IN THE SOFTWARE.
  29. ****************************************************************************/
  30. #ifndef _OGR_FEATURE_H_INCLUDED
  31. #define _OGR_FEATURE_H_INCLUDED
  32. #include "ogr_geometry.h"
  33. #include "ogr_featurestyle.h"
  34. #include "cpl_atomic_ops.h"
  35. /**
  36. * \file ogr_feature.h
  37. *
  38. * Simple feature classes.
  39. */
  40. /************************************************************************/
  41. /* OGRFieldDefn */
  42. /************************************************************************/
  43. /**
  44. * Definition of an attribute of an OGRFeatureDefn. A field is described by :
  45. * <ul>
  46. * <li>a name. See SetName() / GetNameRef()</li>
  47. * <li>a type: OFTString, OFTInteger, OFTReal, ... See SetType() / GetType()</li>
  48. * <li>a subtype (optional): OFSTBoolean, ... See SetSubType() / GetSubType()</li>
  49. * <li>a width (optional): maximal number of characters. See SetWidth() / GetWidth()</li>
  50. * <li>a precision (optional): number of digits after decimal point. See SetPrecision() / GetPrecision()</li>
  51. * <li>a NOT NULL constraint (optional). See SetNullable() / IsNullable()</li>
  52. * <li>a default value (optional). See SetDefault() / GetDefault()</li>
  53. * <li>a boolean to indicate whether it should be ignored when retrieving features. See SetIgnored() / IsIgnored()</li>
  54. * </ul>
  55. */
  56. class CPL_DLL OGRFieldDefn
  57. {
  58. private:
  59. char *pszName;
  60. OGRFieldType eType;
  61. OGRJustification eJustify;
  62. int nWidth; /* zero is variable */
  63. int nPrecision;
  64. char *pszDefault;
  65. int bIgnore;
  66. OGRFieldSubType eSubType;
  67. int bNullable;
  68. void Initialize( const char *, OGRFieldType );
  69. public:
  70. OGRFieldDefn( const char *, OGRFieldType );
  71. OGRFieldDefn( OGRFieldDefn * );
  72. ~OGRFieldDefn();
  73. void SetName( const char * );
  74. const char *GetNameRef() { return pszName; }
  75. OGRFieldType GetType() { return eType; }
  76. void SetType( OGRFieldType eTypeIn );
  77. static const char *GetFieldTypeName( OGRFieldType );
  78. OGRFieldSubType GetSubType() { return eSubType; }
  79. void SetSubType( OGRFieldSubType eSubTypeIn );
  80. static const char *GetFieldSubTypeName( OGRFieldSubType );
  81. OGRJustification GetJustify() { return eJustify; }
  82. void SetJustify( OGRJustification eJustifyIn )
  83. { eJustify = eJustifyIn; }
  84. int GetWidth() { return nWidth; }
  85. void SetWidth( int nWidthIn ) { nWidth = MAX(0,nWidthIn); }
  86. int GetPrecision() { return nPrecision; }
  87. void SetPrecision( int nPrecisionIn )
  88. { nPrecision = nPrecisionIn; }
  89. void Set( const char *, OGRFieldType, int = 0, int = 0,
  90. OGRJustification = OJUndefined );
  91. void SetDefault( const char* );
  92. const char *GetDefault() const;
  93. int IsDefaultDriverSpecific() const;
  94. int IsIgnored() { return bIgnore; }
  95. void SetIgnored( int bIgnoreIn ) { bIgnore = bIgnoreIn; }
  96. int IsNullable() const { return bNullable; }
  97. void SetNullable( int bNullableIn ) { bNullable = bNullableIn; }
  98. int IsSame( const OGRFieldDefn * ) const;
  99. };
  100. /************************************************************************/
  101. /* OGRGeomFieldDefn */
  102. /************************************************************************/
  103. /**
  104. * Definition of a geometry field of an OGRFeatureDefn. A geometry field is
  105. * described by :
  106. * <ul>
  107. * <li>a name. See SetName() / GetNameRef()</li>
  108. * <li>a type: wkbPoint, wkbLineString, ... See SetType() / GetType()</li>
  109. * <li>a spatial reference system (optional). See SetSpatialRef() / GetSpatialRef()</li>
  110. * <li>a NOT NULL constraint (optional). See SetNullable() / IsNullable()</li>
  111. * <li>a boolean to indicate whether it should be ignored when retrieving features. See SetIgnored() / IsIgnored()</li>
  112. * </ul>
  113. *
  114. * @since OGR 1.11
  115. */
  116. class CPL_DLL OGRGeomFieldDefn
  117. {
  118. protected:
  119. char *pszName;
  120. OGRwkbGeometryType eGeomType; /* all values possible except wkbNone */
  121. OGRSpatialReference* poSRS;
  122. int bIgnore;
  123. int bNullable;
  124. void Initialize( const char *, OGRwkbGeometryType );
  125. public:
  126. OGRGeomFieldDefn(const char *pszNameIn,
  127. OGRwkbGeometryType eGeomTypeIn);
  128. OGRGeomFieldDefn( OGRGeomFieldDefn * );
  129. virtual ~OGRGeomFieldDefn();
  130. void SetName( const char * );
  131. const char *GetNameRef() { return pszName; }
  132. OGRwkbGeometryType GetType() { return eGeomType; }
  133. void SetType( OGRwkbGeometryType eTypeIn );
  134. virtual OGRSpatialReference* GetSpatialRef();
  135. void SetSpatialRef(OGRSpatialReference* poSRSIn);
  136. int IsIgnored() { return bIgnore; }
  137. void SetIgnored( int bIgnoreIn ) { bIgnore = bIgnoreIn; }
  138. int IsNullable() const { return bNullable; }
  139. void SetNullable( int bNullableIn ) { bNullable = bNullableIn; }
  140. int IsSame( OGRGeomFieldDefn * );
  141. };
  142. /************************************************************************/
  143. /* OGRFeatureDefn */
  144. /************************************************************************/
  145. /**
  146. * Definition of a feature class or feature layer.
  147. *
  148. * This object contains schema information for a set of OGRFeatures. In
  149. * table based systems, an OGRFeatureDefn is essentially a layer. In more
  150. * object oriented approaches (such as SF CORBA) this can represent a class
  151. * of features but doesn't necessarily relate to all of a layer, or just one
  152. * layer.
  153. *
  154. * This object also can contain some other information such as a name and
  155. * potentially other metadata.
  156. *
  157. * It is essentially a collection of field descriptions (OGRFieldDefn class).
  158. * Starting with GDAL 1.11, in addition to attribute fields, it can also
  159. * contain multiple geometry fields (OGRGeomFieldDefn class).
  160. *
  161. * It is reasonable for different translators to derive classes from
  162. * OGRFeatureDefn with additional translator specific information.
  163. */
  164. class CPL_DLL OGRFeatureDefn
  165. {
  166. protected:
  167. volatile int nRefCount;
  168. int nFieldCount;
  169. OGRFieldDefn **papoFieldDefn;
  170. int nGeomFieldCount;
  171. OGRGeomFieldDefn **papoGeomFieldDefn;
  172. char *pszFeatureClassName;
  173. int bIgnoreStyle;
  174. public:
  175. OGRFeatureDefn( const char * pszName = NULL );
  176. virtual ~OGRFeatureDefn();
  177. virtual const char *GetName();
  178. virtual int GetFieldCount();
  179. virtual OGRFieldDefn *GetFieldDefn( int i );
  180. virtual int GetFieldIndex( const char * );
  181. virtual void AddFieldDefn( OGRFieldDefn * );
  182. virtual OGRErr DeleteFieldDefn( int iField );
  183. virtual OGRErr ReorderFieldDefns( int* panMap );
  184. virtual int GetGeomFieldCount();
  185. virtual OGRGeomFieldDefn *GetGeomFieldDefn( int i );
  186. virtual int GetGeomFieldIndex( const char * );
  187. virtual void AddGeomFieldDefn( OGRGeomFieldDefn *, int bCopy = TRUE );
  188. virtual OGRErr DeleteGeomFieldDefn( int iGeomField );
  189. virtual OGRwkbGeometryType GetGeomType();
  190. virtual void SetGeomType( OGRwkbGeometryType );
  191. virtual OGRFeatureDefn *Clone();
  192. int Reference() { return CPLAtomicInc(&nRefCount); }
  193. int Dereference() { return CPLAtomicDec(&nRefCount); }
  194. int GetReferenceCount() { return nRefCount; }
  195. void Release();
  196. virtual int IsGeometryIgnored();
  197. virtual void SetGeometryIgnored( int bIgnore );
  198. virtual int IsStyleIgnored() { return bIgnoreStyle; }
  199. virtual void SetStyleIgnored( int bIgnore ) { bIgnoreStyle = bIgnore; }
  200. virtual int IsSame( OGRFeatureDefn * poOtherFeatureDefn );
  201. static OGRFeatureDefn *CreateFeatureDefn( const char *pszName = NULL );
  202. static void DestroyFeatureDefn( OGRFeatureDefn * );
  203. };
  204. /************************************************************************/
  205. /* OGRFeature */
  206. /************************************************************************/
  207. /**
  208. * A simple feature, including geometry and attributes.
  209. */
  210. class CPL_DLL OGRFeature
  211. {
  212. private:
  213. GIntBig nFID;
  214. OGRFeatureDefn *poDefn;
  215. OGRGeometry **papoGeometries;
  216. OGRField *pauFields;
  217. protected:
  218. char * m_pszStyleString;
  219. OGRStyleTable *m_poStyleTable;
  220. char * m_pszTmpFieldValue;
  221. public:
  222. OGRFeature( OGRFeatureDefn * );
  223. virtual ~OGRFeature();
  224. OGRFeatureDefn *GetDefnRef() { return poDefn; }
  225. OGRErr SetGeometryDirectly( OGRGeometry * );
  226. OGRErr SetGeometry( OGRGeometry * );
  227. OGRGeometry *GetGeometryRef();
  228. OGRGeometry *StealGeometry();
  229. int GetGeomFieldCount()
  230. { return poDefn->GetGeomFieldCount(); }
  231. OGRGeomFieldDefn *GetGeomFieldDefnRef( int iField )
  232. { return poDefn->GetGeomFieldDefn(iField); }
  233. int GetGeomFieldIndex( const char * pszName)
  234. { return poDefn->GetGeomFieldIndex(pszName); }
  235. OGRGeometry* GetGeomFieldRef(int iField);
  236. OGRGeometry* StealGeometry(int iField);
  237. OGRGeometry* GetGeomFieldRef(const char* pszFName);
  238. OGRErr SetGeomFieldDirectly( int iField, OGRGeometry * );
  239. OGRErr SetGeomField( int iField, OGRGeometry * );
  240. OGRFeature *Clone();
  241. virtual OGRBoolean Equal( OGRFeature * poFeature );
  242. int GetFieldCount() { return poDefn->GetFieldCount(); }
  243. OGRFieldDefn *GetFieldDefnRef( int iField )
  244. { return poDefn->GetFieldDefn(iField); }
  245. int GetFieldIndex( const char * pszName)
  246. { return poDefn->GetFieldIndex(pszName);}
  247. int IsFieldSet( int iField );
  248. void UnsetField( int iField );
  249. OGRField *GetRawFieldRef( int i ) { return pauFields + i; }
  250. int GetFieldAsInteger( int i );
  251. GIntBig GetFieldAsInteger64( int i );
  252. double GetFieldAsDouble( int i );
  253. const char *GetFieldAsString( int i );
  254. const int *GetFieldAsIntegerList( int i, int *pnCount );
  255. const GIntBig *GetFieldAsInteger64List( int i, int *pnCount );
  256. const double *GetFieldAsDoubleList( int i, int *pnCount );
  257. char **GetFieldAsStringList( int i );
  258. GByte *GetFieldAsBinary( int i, int *pnCount );
  259. int GetFieldAsDateTime( int i,
  260. int *pnYear, int *pnMonth, int *pnDay,
  261. int *pnHour, int *pnMinute, int *pnSecond,
  262. int *pnTZFlag );
  263. int GetFieldAsDateTime( int i,
  264. int *pnYear, int *pnMonth, int *pnDay,
  265. int *pnHour, int *pnMinute, float *pfSecond,
  266. int *pnTZFlag );
  267. int GetFieldAsInteger( const char *pszFName )
  268. { return GetFieldAsInteger( GetFieldIndex(pszFName) ); }
  269. GIntBig GetFieldAsInteger64( const char *pszFName )
  270. { return GetFieldAsInteger64( GetFieldIndex(pszFName) ); }
  271. double GetFieldAsDouble( const char *pszFName )
  272. { return GetFieldAsDouble( GetFieldIndex(pszFName) ); }
  273. const char *GetFieldAsString( const char *pszFName )
  274. { return GetFieldAsString( GetFieldIndex(pszFName) ); }
  275. const int *GetFieldAsIntegerList( const char *pszFName,
  276. int *pnCount )
  277. { return GetFieldAsIntegerList( GetFieldIndex(pszFName),
  278. pnCount ); }
  279. const GIntBig *GetFieldAsInteger64List( const char *pszFName,
  280. int *pnCount )
  281. { return GetFieldAsInteger64List( GetFieldIndex(pszFName),
  282. pnCount ); }
  283. const double *GetFieldAsDoubleList( const char *pszFName,
  284. int *pnCount )
  285. { return GetFieldAsDoubleList( GetFieldIndex(pszFName),
  286. pnCount ); }
  287. char **GetFieldAsStringList( const char *pszFName )
  288. { return GetFieldAsStringList(GetFieldIndex(pszFName)); }
  289. void SetField( int i, int nValue );
  290. void SetField( int i, GIntBig nValue );
  291. void SetField( int i, double dfValue );
  292. void SetField( int i, const char * pszValue );
  293. void SetField( int i, int nCount, int * panValues );
  294. void SetField( int i, int nCount, const GIntBig * panValues );
  295. void SetField( int i, int nCount, double * padfValues );
  296. void SetField( int i, char ** papszValues );
  297. void SetField( int i, OGRField * puValue );
  298. void SetField( int i, int nCount, GByte * pabyBinary );
  299. void SetField( int i, int nYear, int nMonth, int nDay,
  300. int nHour=0, int nMinute=0, float fSecond=0.f,
  301. int nTZFlag = 0 );
  302. void SetField( const char *pszFName, int nValue )
  303. { SetField( GetFieldIndex(pszFName), nValue ); }
  304. void SetField( const char *pszFName, GIntBig nValue )
  305. { SetField( GetFieldIndex(pszFName), nValue ); }
  306. void SetField( const char *pszFName, double dfValue )
  307. { SetField( GetFieldIndex(pszFName), dfValue ); }
  308. void SetField( const char *pszFName, const char * pszValue)
  309. { SetField( GetFieldIndex(pszFName), pszValue ); }
  310. void SetField( const char *pszFName, int nCount,
  311. int * panValues )
  312. { SetField(GetFieldIndex(pszFName),nCount,panValues);}
  313. void SetField( const char *pszFName, int nCount,
  314. const GIntBig * panValues )
  315. { SetField(GetFieldIndex(pszFName),nCount,panValues);}
  316. void SetField( const char *pszFName, int nCount,
  317. double * padfValues )
  318. {SetField(GetFieldIndex(pszFName),nCount,padfValues);}
  319. void SetField( const char *pszFName, char ** papszValues )
  320. { SetField( GetFieldIndex(pszFName), papszValues); }
  321. void SetField( const char *pszFName, OGRField * puValue )
  322. { SetField( GetFieldIndex(pszFName), puValue ); }
  323. void SetField( const char *pszFName,
  324. int nYear, int nMonth, int nDay,
  325. int nHour=0, int nMinute=0, float fSecond=0.f,
  326. int nTZFlag = 0 )
  327. { SetField( GetFieldIndex(pszFName),
  328. nYear, nMonth, nDay,
  329. nHour, nMinute, fSecond, nTZFlag ); }
  330. GIntBig GetFID() { return nFID; }
  331. virtual OGRErr SetFID( GIntBig nFIDIn );
  332. void DumpReadable( FILE *, char** papszOptions = NULL );
  333. OGRErr SetFrom( OGRFeature *, int = TRUE);
  334. OGRErr SetFrom( OGRFeature *, int *, int = TRUE );
  335. OGRErr SetFieldsFrom( OGRFeature *, int *, int = TRUE );
  336. OGRErr RemapFields( OGRFeatureDefn *poNewDefn,
  337. int *panRemapSource );
  338. OGRErr RemapGeomFields( OGRFeatureDefn *poNewDefn,
  339. int *panRemapSource );
  340. int Validate( int nValidateFlags,
  341. int bEmitError );
  342. void FillUnsetWithDefault(int bNotNullableOnly,
  343. char** papszOptions );
  344. virtual const char *GetStyleString();
  345. virtual void SetStyleString( const char * );
  346. virtual void SetStyleStringDirectly( char * );
  347. virtual OGRStyleTable *GetStyleTable() { return m_poStyleTable; }
  348. virtual void SetStyleTable(OGRStyleTable *poStyleTable);
  349. virtual void SetStyleTableDirectly(OGRStyleTable *poStyleTable);
  350. static OGRFeature *CreateFeature( OGRFeatureDefn * );
  351. static void DestroyFeature( OGRFeature * );
  352. };
  353. /************************************************************************/
  354. /* OGRFeatureQuery */
  355. /************************************************************************/
  356. class OGRLayer;
  357. class swq_expr_node;
  358. class swq_custom_func_registrar;
  359. class CPL_DLL OGRFeatureQuery
  360. {
  361. private:
  362. OGRFeatureDefn *poTargetDefn;
  363. void *pSWQExpr;
  364. char **FieldCollector( void *, char ** );
  365. GIntBig *EvaluateAgainstIndices( swq_expr_node*, OGRLayer *, GIntBig& nFIDCount);
  366. int CanUseIndex( swq_expr_node*, OGRLayer * );
  367. public:
  368. OGRFeatureQuery();
  369. ~OGRFeatureQuery();
  370. OGRErr Compile( OGRFeatureDefn *, const char *,
  371. int bCheck = TRUE, swq_custom_func_registrar* poCustomFuncRegistrar = NULL );
  372. int Evaluate( OGRFeature * );
  373. GIntBig *EvaluateAgainstIndices( OGRLayer *, OGRErr * );
  374. int CanUseIndex( OGRLayer * );
  375. char **GetUsedFields();
  376. void *GetSWQExpr() { return pSWQExpr; }
  377. };
  378. #endif /* ndef _OGR_FEATURE_H_INCLUDED */