1 : /******************************************************************************
2 : * $Id: ogrntflayer.cpp 10645 2007-01-18 02:22:39Z warmerdam $
3 : *
4 : * Project: UK NTF Reader
5 : * Purpose: Implements OGRNTFLayer 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 :
30 : #include "ntf.h"
31 : #include "cpl_conv.h"
32 :
33 : CPL_CVSID("$Id: ogrntflayer.cpp 10645 2007-01-18 02:22:39Z warmerdam $");
34 :
35 : /************************************************************************/
36 : /* OGRNTFLayer() */
37 : /* */
38 : /* Note that the OGRNTFLayer assumes ownership of the passed */
39 : /* OGRFeatureDefn object. */
40 : /************************************************************************/
41 :
42 8 : OGRNTFLayer::OGRNTFLayer( OGRNTFDataSource *poDSIn,
43 : OGRFeatureDefn * poFeatureDefine,
44 8 : NTFFeatureTranslator pfnTranslatorIn )
45 :
46 : {
47 8 : poDS = poDSIn;
48 8 : poFeatureDefn = poFeatureDefine;
49 8 : pfnTranslator = pfnTranslatorIn;
50 :
51 8 : iCurrentReader = -1;
52 8 : nCurrentPos = -1;
53 8 : nCurrentFID = 1;
54 8 : }
55 :
56 : /************************************************************************/
57 : /* ~OGRNTFLayer() */
58 : /************************************************************************/
59 :
60 16 : OGRNTFLayer::~OGRNTFLayer()
61 :
62 : {
63 8 : if( m_nFeaturesRead > 0 && poFeatureDefn != NULL )
64 : {
65 : CPLDebug( "Mem", "%d features read on layer '%s'.",
66 : (int) m_nFeaturesRead,
67 8 : poFeatureDefn->GetName() );
68 : }
69 :
70 8 : if( poFeatureDefn )
71 8 : poFeatureDefn->Release();
72 16 : }
73 :
74 : /************************************************************************/
75 : /* ResetReading() */
76 : /************************************************************************/
77 :
78 16 : void OGRNTFLayer::ResetReading()
79 :
80 : {
81 16 : iCurrentReader = -1;
82 16 : nCurrentPos = -1;
83 16 : nCurrentFID = 1;
84 16 : }
85 :
86 : /************************************************************************/
87 : /* GetNextFeature() */
88 : /************************************************************************/
89 :
90 31232 : OGRFeature *OGRNTFLayer::GetNextFeature()
91 :
92 : {
93 31232 : OGRFeature *poFeature = NULL;
94 :
95 : /* -------------------------------------------------------------------- */
96 : /* Have we processed all features already? */
97 : /* -------------------------------------------------------------------- */
98 31232 : if( iCurrentReader == poDS->GetFileCount() )
99 8 : return NULL;
100 :
101 : /* -------------------------------------------------------------------- */
102 : /* Do we need to open a file? */
103 : /* -------------------------------------------------------------------- */
104 31224 : if( iCurrentReader == -1 )
105 : {
106 11 : iCurrentReader++;
107 11 : nCurrentPos = -1;
108 : }
109 :
110 31224 : NTFFileReader *poCurrentReader = poDS->GetFileReader(iCurrentReader);
111 31224 : if( poCurrentReader->GetFP() == NULL )
112 : {
113 10 : poCurrentReader->Open();
114 : }
115 :
116 : /* -------------------------------------------------------------------- */
117 : /* Ensure we are reading on from the same point we were reading */
118 : /* from for the last feature, even if some other access */
119 : /* mechanism has moved the file pointer. */
120 : /* -------------------------------------------------------------------- */
121 31224 : if( nCurrentPos != -1 )
122 31213 : poCurrentReader->SetFPPos( nCurrentPos, nCurrentFID );
123 : else
124 11 : poCurrentReader->Reset();
125 :
126 : /* -------------------------------------------------------------------- */
127 : /* Read features till we find one that satisfies our current */
128 : /* spatial criteria. */
129 : /* -------------------------------------------------------------------- */
130 0 : while( TRUE )
131 : {
132 31224 : poFeature = poCurrentReader->ReadOGRFeature( this );
133 31224 : if( poFeature == NULL )
134 8 : break;
135 :
136 31216 : m_nFeaturesRead++;
137 :
138 31216 : if( (m_poFilterGeom == NULL
139 : || poFeature->GetGeometryRef() == NULL
140 : || FilterGeometry( poFeature->GetGeometryRef() ) )
141 : && (m_poAttrQuery == NULL
142 : || m_poAttrQuery->Evaluate( poFeature )) )
143 31216 : break;
144 :
145 0 : delete poFeature;
146 : }
147 :
148 : /* -------------------------------------------------------------------- */
149 : /* If we get NULL the file must be all consumed, advance to the */
150 : /* next file that contains features for this layer. */
151 : /* -------------------------------------------------------------------- */
152 31224 : if( poFeature == NULL )
153 : {
154 8 : poCurrentReader->Close();
155 :
156 8 : if( poDS->GetOption("CACHING") != NULL
157 : && EQUAL(poDS->GetOption("CACHING"),"OFF") )
158 : {
159 0 : poCurrentReader->DestroyIndex();
160 : }
161 :
162 8 : do {
163 8 : iCurrentReader++;
164 : } while( iCurrentReader < poDS->GetFileCount()
165 : && !poDS->GetFileReader(iCurrentReader)->TestForLayer(this) );
166 :
167 8 : nCurrentPos = -1;
168 8 : nCurrentFID = 1;
169 :
170 8 : poFeature = GetNextFeature();
171 : }
172 : else
173 : {
174 31216 : poCurrentReader->GetFPPos(&nCurrentPos, &nCurrentFID);
175 : }
176 :
177 31224 : return poFeature;
178 : }
179 :
180 : /************************************************************************/
181 : /* TestCapability() */
182 : /************************************************************************/
183 :
184 0 : int OGRNTFLayer::TestCapability( const char * pszCap )
185 :
186 : {
187 0 : if( EQUAL(pszCap,OLCRandomRead) )
188 0 : return FALSE;
189 :
190 0 : else if( EQUAL(pszCap,OLCSequentialWrite)
191 : || EQUAL(pszCap,OLCRandomWrite) )
192 0 : return FALSE;
193 :
194 0 : else if( EQUAL(pszCap,OLCFastFeatureCount) )
195 0 : return FALSE;
196 :
197 0 : else if( EQUAL(pszCap,OLCFastSpatialFilter) )
198 0 : return FALSE;
199 :
200 : else
201 0 : return FALSE;
202 : }
203 :
204 : /************************************************************************/
205 : /* FeatureTranslate() */
206 : /************************************************************************/
207 :
208 31216 : OGRFeature * OGRNTFLayer::FeatureTranslate( NTFFileReader *poReader,
209 : NTFRecord ** papoGroup )
210 :
211 : {
212 31216 : if( pfnTranslator == NULL )
213 0 : return NULL;
214 :
215 31216 : return pfnTranslator( poReader, this, papoGroup );
216 : }
217 :
218 : /************************************************************************/
219 : /* GetSpatialRef() */
220 : /************************************************************************/
221 :
222 6 : OGRSpatialReference *OGRNTFLayer::GetSpatialRef()
223 :
224 : {
225 6 : return poDS->GetSpatialRef();
226 : }
|