PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/gdal-1.6.1-tcl_patched/ogr/ogrsf_frmts/s57/ogrs57layer.cpp

http://tcl-map.googlecode.com/
C++ | 316 lines | 166 code | 63 blank | 87 comment | 67 complexity | 10d4d8255caa5e2b7c881147809835de MD5 | raw file
Possible License(s): LGPL-2.0, GPL-3.0, BSD-3-Clause, LGPL-3.0
  1. /******************************************************************************
  2. * $Id: ogrs57layer.cpp 10645 2007-01-18 02:22:39Z warmerdam $
  3. *
  4. * Project: S-57 Translator
  5. * Purpose: Implements OGRS57Layer class.
  6. * Author: Frank Warmerdam, warmerdam@pobox.com
  7. *
  8. ******************************************************************************
  9. * Copyright (c) 1999, Frank Warmerdam
  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. #include "ogr_s57.h"
  30. #include "cpl_conv.h"
  31. #include "cpl_string.h"
  32. CPL_CVSID("$Id: ogrs57layer.cpp 10645 2007-01-18 02:22:39Z warmerdam $");
  33. /************************************************************************/
  34. /* OGRS57Layer() */
  35. /* */
  36. /* Note that the OGRS57Layer assumes ownership of the passed */
  37. /* OGRFeatureDefn object. */
  38. /************************************************************************/
  39. OGRS57Layer::OGRS57Layer( OGRS57DataSource *poDSIn,
  40. OGRFeatureDefn * poDefnIn,
  41. int nFeatureCountIn,
  42. int nOBJLIn)
  43. {
  44. poDS = poDSIn;
  45. nFeatureCount = nFeatureCountIn;
  46. poFeatureDefn = poDefnIn;
  47. nOBJL = nOBJLIn;
  48. nNextFEIndex = 0;
  49. nCurrentModule = -1;
  50. if( EQUAL(poDefnIn->GetName(),OGRN_VI) )
  51. nRCNM = RCNM_VI;
  52. else if( EQUAL(poDefnIn->GetName(),OGRN_VC) )
  53. nRCNM = RCNM_VC;
  54. else if( EQUAL(poDefnIn->GetName(),OGRN_VE) )
  55. nRCNM = RCNM_VE;
  56. else if( EQUAL(poDefnIn->GetName(),OGRN_VF) )
  57. nRCNM = RCNM_VF;
  58. else if( EQUAL(poDefnIn->GetName(),"DSID") )
  59. nRCNM = RCNM_DSID;
  60. else
  61. nRCNM = 100; /* feature */
  62. }
  63. /************************************************************************/
  64. /* ~OGRS57Layer() */
  65. /************************************************************************/
  66. OGRS57Layer::~OGRS57Layer()
  67. {
  68. if( m_nFeaturesRead > 0 && poFeatureDefn != NULL )
  69. {
  70. CPLDebug( "S57", "%d features read on layer '%s'.",
  71. (int) m_nFeaturesRead,
  72. poFeatureDefn->GetName() );
  73. }
  74. poFeatureDefn->Release();
  75. }
  76. /************************************************************************/
  77. /* ResetReading() */
  78. /************************************************************************/
  79. void OGRS57Layer::ResetReading()
  80. {
  81. nNextFEIndex = 0;
  82. nCurrentModule = -1;
  83. }
  84. /************************************************************************/
  85. /* GetNextUnfilteredFeature() */
  86. /************************************************************************/
  87. OGRFeature *OGRS57Layer::GetNextUnfilteredFeature()
  88. {
  89. OGRFeature *poFeature = NULL;
  90. /* -------------------------------------------------------------------- */
  91. /* Are we out of modules to request features from? */
  92. /* -------------------------------------------------------------------- */
  93. if( nCurrentModule >= poDS->GetModuleCount() )
  94. return NULL;
  95. /* -------------------------------------------------------------------- */
  96. /* Set the current position on the current module and fetch a */
  97. /* feature. */
  98. /* -------------------------------------------------------------------- */
  99. S57Reader *poReader = poDS->GetModule(nCurrentModule);
  100. if( poReader != NULL )
  101. {
  102. poReader->SetNextFEIndex( nNextFEIndex, nRCNM );
  103. poFeature = poReader->ReadNextFeature( poFeatureDefn );
  104. nNextFEIndex = poReader->GetNextFEIndex( nRCNM );
  105. }
  106. /* -------------------------------------------------------------------- */
  107. /* If we didn't get a feature we need to move onto the next file. */
  108. /* -------------------------------------------------------------------- */
  109. if( poFeature == NULL )
  110. {
  111. nCurrentModule++;
  112. poReader = poDS->GetModule(nCurrentModule);
  113. if( poReader != NULL && poReader->GetModule() == NULL )
  114. {
  115. if( !poReader->Open( FALSE ) )
  116. return NULL;
  117. }
  118. return GetNextUnfilteredFeature();
  119. }
  120. else
  121. {
  122. m_nFeaturesRead++;
  123. if( poFeature->GetGeometryRef() != NULL )
  124. poFeature->GetGeometryRef()->assignSpatialReference(
  125. GetSpatialRef() );
  126. }
  127. return poFeature;
  128. }
  129. /************************************************************************/
  130. /* GetNextFeature() */
  131. /************************************************************************/
  132. OGRFeature *OGRS57Layer::GetNextFeature()
  133. {
  134. OGRFeature *poFeature = NULL;
  135. /* -------------------------------------------------------------------- */
  136. /* Read features till we find one that satisfies our current */
  137. /* spatial criteria. */
  138. /* -------------------------------------------------------------------- */
  139. while( TRUE )
  140. {
  141. poFeature = GetNextUnfilteredFeature();
  142. if( poFeature == NULL )
  143. break;
  144. if( (m_poFilterGeom == NULL
  145. || FilterGeometry(poFeature->GetGeometryRef()) )
  146. && (m_poAttrQuery == NULL
  147. || m_poAttrQuery->Evaluate( poFeature )) )
  148. break;
  149. delete poFeature;
  150. }
  151. return poFeature;
  152. }
  153. /************************************************************************/
  154. /* TestCapability() */
  155. /************************************************************************/
  156. int OGRS57Layer::TestCapability( const char * pszCap )
  157. {
  158. if( EQUAL(pszCap,OLCRandomRead) )
  159. return FALSE;
  160. else if( EQUAL(pszCap,OLCSequentialWrite) )
  161. return TRUE;
  162. else if( EQUAL(pszCap,OLCRandomWrite) )
  163. return FALSE;
  164. else if( EQUAL(pszCap,OLCFastFeatureCount) )
  165. return TRUE;
  166. else if( EQUAL(pszCap,OLCFastGetExtent) )
  167. {
  168. OGREnvelope oEnvelope;
  169. return GetExtent( &oEnvelope, FALSE ) == OGRERR_NONE;
  170. }
  171. else if( EQUAL(pszCap,OLCFastSpatialFilter) )
  172. return FALSE;
  173. else
  174. return FALSE;
  175. }
  176. /************************************************************************/
  177. /* GetSpatialRef() */
  178. /************************************************************************/
  179. OGRSpatialReference *OGRS57Layer::GetSpatialRef()
  180. {
  181. return poDS->GetSpatialRef();
  182. }
  183. /************************************************************************/
  184. /* GetExtent() */
  185. /************************************************************************/
  186. OGRErr OGRS57Layer::GetExtent( OGREnvelope *psExtent, int bForce )
  187. {
  188. return poDS->GetDSExtent( psExtent, bForce );
  189. }
  190. /************************************************************************/
  191. /* GetFeatureCount() */
  192. /************************************************************************/
  193. int OGRS57Layer::GetFeatureCount (int bForce)
  194. {
  195. if( m_poFilterGeom != NULL || m_poAttrQuery != NULL
  196. || nFeatureCount == -1 )
  197. return OGRLayer::GetFeatureCount( bForce );
  198. else
  199. return nFeatureCount;
  200. }
  201. /************************************************************************/
  202. /* GetFeature() */
  203. /************************************************************************/
  204. OGRFeature *OGRS57Layer::GetFeature( long nFeatureId )
  205. {
  206. S57Reader *poReader = poDS->GetModule(0); // not multi-reader aware
  207. if( poReader != NULL )
  208. {
  209. OGRFeature *poFeature;
  210. poFeature = poReader->ReadFeature( nFeatureId, poFeatureDefn );
  211. if( poFeature != NULL && poFeature->GetGeometryRef() != NULL )
  212. poFeature->GetGeometryRef()->assignSpatialReference(
  213. GetSpatialRef() );
  214. return poFeature;
  215. }
  216. else
  217. return NULL;
  218. }
  219. /************************************************************************/
  220. /* CreateFeature() */
  221. /************************************************************************/
  222. OGRErr OGRS57Layer::CreateFeature( OGRFeature *poFeature )
  223. {
  224. /* -------------------------------------------------------------------- */
  225. /* Set RCNM if not already set. */
  226. /* -------------------------------------------------------------------- */
  227. int iRCNMFld = poFeature->GetFieldIndex( "RCNM" );
  228. if( iRCNMFld != -1 )
  229. {
  230. if( !poFeature->IsFieldSet( iRCNMFld ) )
  231. poFeature->SetField( iRCNMFld, nRCNM );
  232. else
  233. CPLAssert( poFeature->GetFieldAsInteger( iRCNMFld ) == nRCNM );
  234. }
  235. /* -------------------------------------------------------------------- */
  236. /* Set OBJL if not already set. */
  237. /* -------------------------------------------------------------------- */
  238. if( nOBJL != -1 )
  239. {
  240. int iOBJLFld = poFeature->GetFieldIndex( "OBJL" );
  241. if( !poFeature->IsFieldSet( iOBJLFld ) )
  242. poFeature->SetField( iOBJLFld, nOBJL );
  243. else
  244. CPLAssert( poFeature->GetFieldAsInteger( iOBJLFld ) == nOBJL );
  245. }
  246. /* -------------------------------------------------------------------- */
  247. /* Create the isolated node feature. */
  248. /* -------------------------------------------------------------------- */
  249. if( poDS->GetWriter()->WriteCompleteFeature( poFeature ) )
  250. return OGRERR_NONE;
  251. else
  252. return OGRERR_FAILURE;
  253. }