Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Project: UK NTF Reader 4 : * Purpose: Implements OGRNTFLayer class. 5 : * Author: Frank Warmerdam, warmerdam@pobox.com 6 : * 7 : ****************************************************************************** 8 : * Copyright (c) 1999, Frank Warmerdam 9 : * 10 : * Permission is hereby granted, free of charge, to any person obtaining a 11 : * copy of this software and associated documentation files (the "Software"), 12 : * to deal in the Software without restriction, including without limitation 13 : * the rights to use, copy, modify, merge, publish, distribute, sublicense, 14 : * and/or sell copies of the Software, and to permit persons to whom the 15 : * Software is furnished to do so, subject to the following conditions: 16 : * 17 : * The above copyright notice and this permission notice shall be included 18 : * in all copies or substantial portions of the Software. 19 : * 20 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 21 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 23 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 25 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 : * DEALINGS IN THE SOFTWARE. 27 : ****************************************************************************/ 28 : 29 : #include "ntf.h" 30 : #include "cpl_conv.h" 31 : 32 : /************************************************************************/ 33 : /* OGRNTFLayer() */ 34 : /* */ 35 : /* Note that the OGRNTFLayer assumes ownership of the passed */ 36 : /* OGRFeatureDefn object. */ 37 : /************************************************************************/ 38 : 39 0 : OGRNTFLayer::OGRNTFLayer(OGRNTFDataSource *poDSIn, 40 : OGRFeatureDefn *poFeatureDefine, 41 0 : NTFFeatureTranslator pfnTranslatorIn) 42 : : poFeatureDefn(poFeatureDefine), pfnTranslator(pfnTranslatorIn), 43 : poDS(poDSIn), iCurrentReader(-1), nCurrentPos((vsi_l_offset)-1), 44 0 : nCurrentFID(1) 45 : { 46 0 : SetDescription(poFeatureDefn->GetName()); 47 0 : } 48 : 49 : /************************************************************************/ 50 : /* ~OGRNTFLayer() */ 51 : /************************************************************************/ 52 : 53 0 : OGRNTFLayer::~OGRNTFLayer() 54 : 55 : { 56 0 : if (m_nFeaturesRead > 0 && poFeatureDefn != nullptr) 57 : { 58 0 : CPLDebug("Mem", "%d features read on layer '%s'.", (int)m_nFeaturesRead, 59 0 : poFeatureDefn->GetName()); 60 : } 61 : 62 0 : if (poFeatureDefn) 63 0 : poFeatureDefn->Release(); 64 0 : } 65 : 66 : /************************************************************************/ 67 : /* ResetReading() */ 68 : /************************************************************************/ 69 : 70 0 : void OGRNTFLayer::ResetReading() 71 : 72 : { 73 0 : iCurrentReader = -1; 74 0 : nCurrentPos = (vsi_l_offset)-1; 75 0 : nCurrentFID = 1; 76 0 : } 77 : 78 : /************************************************************************/ 79 : /* GetNextFeature() */ 80 : /************************************************************************/ 81 : 82 0 : OGRFeature *OGRNTFLayer::GetNextFeature() 83 : 84 : { 85 0 : OGRFeature *poFeature = nullptr; 86 : 87 : /* -------------------------------------------------------------------- */ 88 : /* Have we processed all features already? */ 89 : /* -------------------------------------------------------------------- */ 90 0 : if (iCurrentReader == poDS->GetFileCount()) 91 0 : return nullptr; 92 : 93 : /* -------------------------------------------------------------------- */ 94 : /* Do we need to open a file? */ 95 : /* -------------------------------------------------------------------- */ 96 0 : if (iCurrentReader == -1) 97 : { 98 0 : iCurrentReader++; 99 0 : nCurrentPos = (vsi_l_offset)-1; 100 : } 101 : 102 0 : NTFFileReader *poCurrentReader = poDS->GetFileReader(iCurrentReader); 103 0 : if (poCurrentReader->GetFP() == nullptr) 104 : { 105 0 : poCurrentReader->Open(); 106 : } 107 : 108 : /* -------------------------------------------------------------------- */ 109 : /* Ensure we are reading on from the same point we were reading */ 110 : /* from for the last feature, even if some other access */ 111 : /* mechanism has moved the file pointer. */ 112 : /* -------------------------------------------------------------------- */ 113 0 : if (nCurrentPos != (vsi_l_offset)-1) 114 0 : poCurrentReader->SetFPPos(nCurrentPos, nCurrentFID); 115 : else 116 0 : poCurrentReader->Reset(); 117 : 118 : /* -------------------------------------------------------------------- */ 119 : /* Read features till we find one that satisfies our current */ 120 : /* spatial criteria. */ 121 : /* -------------------------------------------------------------------- */ 122 : while (true) 123 : { 124 0 : poFeature = poCurrentReader->ReadOGRFeature(this); 125 0 : if (poFeature == nullptr) 126 0 : break; 127 : 128 0 : m_nFeaturesRead++; 129 : 130 0 : if ((m_poFilterGeom == nullptr || 131 0 : poFeature->GetGeometryRef() == nullptr || 132 0 : FilterGeometry(poFeature->GetGeometryRef())) && 133 0 : (m_poAttrQuery == nullptr || m_poAttrQuery->Evaluate(poFeature))) 134 0 : break; 135 : 136 0 : delete poFeature; 137 : } 138 : 139 : /* -------------------------------------------------------------------- */ 140 : /* If we get NULL the file must be all consumed, advance to the */ 141 : /* next file that contains features for this layer. */ 142 : /* -------------------------------------------------------------------- */ 143 0 : if (poFeature == nullptr) 144 : { 145 0 : poCurrentReader->Close(); 146 : 147 0 : if (poDS->GetOption("CACHING") != nullptr && 148 0 : EQUAL(poDS->GetOption("CACHING"), "OFF")) 149 : { 150 0 : poCurrentReader->DestroyIndex(); 151 : } 152 : 153 0 : do 154 : { 155 0 : iCurrentReader++; 156 0 : } while (iCurrentReader < poDS->GetFileCount() && 157 0 : !poDS->GetFileReader(iCurrentReader)->TestForLayer(this)); 158 : 159 0 : nCurrentPos = (vsi_l_offset)-1; 160 0 : nCurrentFID = 1; 161 : 162 0 : poFeature = GetNextFeature(); 163 : } 164 : else 165 : { 166 0 : poCurrentReader->GetFPPos(&nCurrentPos, &nCurrentFID); 167 : } 168 : 169 0 : return poFeature; 170 : } 171 : 172 : /************************************************************************/ 173 : /* TestCapability() */ 174 : /************************************************************************/ 175 : 176 0 : int OGRNTFLayer::TestCapability(const char *pszCap) 177 : 178 : { 179 0 : if (EQUAL(pszCap, OLCZGeometries)) 180 0 : return TRUE; 181 : 182 0 : return FALSE; 183 : } 184 : 185 : /************************************************************************/ 186 : /* FeatureTranslate() */ 187 : /************************************************************************/ 188 : 189 0 : OGRFeature *OGRNTFLayer::FeatureTranslate(NTFFileReader *poReader, 190 : NTFRecord **papoGroup) 191 : 192 : { 193 0 : if (pfnTranslator == nullptr) 194 0 : return nullptr; 195 : 196 0 : return pfnTranslator(poReader, this, papoGroup); 197 : }