OpenCPN Partial API docs
Loading...
Searching...
No Matches
ogrs57layer.cpp
1/******************************************************************************
2 *
3 * Project: S-57 Translator
4 * Purpose: Implements OGRS57Layer 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 */
30
31#include "ogr_s57.h"
32#include "gdal/cpl_conv.h"
33#include "gdal/cpl_string.h"
34
35/************************************************************************/
36/* OGRS57Layer() */
37/* */
38/* Note that the OGRS57Layer assumes ownership of the passed */
39/* OGRFeatureDefn object. */
40/************************************************************************/
41
42OGRS57Layer::OGRS57Layer(OGRS57DataSource *poDSIn, OGRFeatureDefn *poDefnIn,
43 int nFeatureCountIn, int nOBJLIn)
44
45{
46 poFilterGeom = NULL;
47
48 poDS = poDSIn;
49
50 nFeatureCount = nFeatureCountIn;
51
52 poFeatureDefn = poDefnIn;
53
54 nOBJL = nOBJLIn;
55
56 nNextFEIndex = 0;
57 nCurrentModule = -1;
58
59 if (EQUAL(poDefnIn->GetName(), OGRN_VI))
60 nRCNM = RCNM_VI;
61 else if (EQUAL(poDefnIn->GetName(), OGRN_VC))
62 nRCNM = RCNM_VC;
63 else if (EQUAL(poDefnIn->GetName(), OGRN_VE))
64 nRCNM = RCNM_VE;
65 else if (EQUAL(poDefnIn->GetName(), OGRN_VF))
66 nRCNM = RCNM_VF;
67 else
68 nRCNM = 100; /* feature */
69}
70
71/************************************************************************/
72/* ~OGRS57Layer() */
73/************************************************************************/
74
75OGRS57Layer::~OGRS57Layer()
76
77{
78 delete poFeatureDefn;
79
80 if (poFilterGeom != NULL) delete poFilterGeom;
81}
82
83/************************************************************************/
84/* SetSpatialFilter() */
85/************************************************************************/
86
87void OGRS57Layer::SetSpatialFilter(OGRGeometry *poGeomIn)
88
89{
90 if (poFilterGeom != NULL) {
91 delete poFilterGeom;
92 poFilterGeom = NULL;
93 }
94
95 if (poGeomIn != NULL) poFilterGeom = poGeomIn->clone();
96
97 if (nNextFEIndex != 0 || nCurrentModule != -1) ResetReading();
98}
99
100/************************************************************************/
101/* ResetReading() */
102/************************************************************************/
103
104void OGRS57Layer::ResetReading()
105
106{
107 nNextFEIndex = 0;
108 nCurrentModule = -1;
109}
110
111/************************************************************************/
112/* GetNextUnfilteredFeature() */
113/************************************************************************/
114
115OGRFeature *OGRS57Layer::GetNextUnfilteredFeature()
116
117{
118 OGRFeature *poFeature = NULL;
119
120 /* -------------------------------------------------------------------- */
121 /* Are we out of modules to request features from? */
122 /* -------------------------------------------------------------------- */
123 if (nCurrentModule >= poDS->GetModuleCount()) return NULL;
124
125 /* -------------------------------------------------------------------- */
126 /* Set the current position on the current module and fetch a */
127 /* feature. */
128 /* -------------------------------------------------------------------- */
129 S57Reader *poReader = poDS->GetModule(nCurrentModule);
130
131 if (poReader != NULL) {
132 poReader->SetNextFEIndex(nNextFEIndex, nRCNM);
133 poFeature = poReader->ReadNextFeature(poFeatureDefn);
134 nNextFEIndex = poReader->GetNextFEIndex(nRCNM);
135 }
136
137 /* -------------------------------------------------------------------- */
138 /* If we didn't get a feature we need to move onto the next file. */
139 /* -------------------------------------------------------------------- */
140 if (poFeature == NULL) {
141 nCurrentModule++;
142 poReader = poDS->GetModule(nCurrentModule);
143
144 if (poReader != NULL && poReader->GetModule() == NULL) {
145 if (!poReader->Open(FALSE)) return NULL;
146 }
147
148 return GetNextUnfilteredFeature();
149 } else {
150 if (poFeature->GetGeometryRef() != NULL)
151 poFeature->GetGeometryRef()->assignSpatialReference(GetSpatialRef());
152 }
153
154 return poFeature;
155}
156
157/************************************************************************/
158/* GetNextFeature() */
159/************************************************************************/
160
161OGRFeature *OGRS57Layer::GetNextFeature()
162
163{
164 OGRFeature *poFeature = NULL;
165
166 /* -------------------------------------------------------------------- */
167 /* Read features till we find one that satisfies our current */
168 /* spatial criteria. */
169 /* -------------------------------------------------------------------- */
170 while (TRUE) {
171 poFeature = GetNextUnfilteredFeature();
172 if (poFeature == NULL) break;
173 /*
174 if( (poFilterGeom == NULL
175 || poFeature->GetGeometryRef() == NULL
176 || poFilterGeom->Intersect( poFeature->GetGeometryRef() ) )
177 && (m_poAttrQuery == NULL
178 || m_poAttrQuery->Evaluate( poFeature )) )
179 break;
180 */
181 delete poFeature;
182 }
183
184 return poFeature;
185}
186
187/************************************************************************/
188/* TestCapability() */
189/************************************************************************/
190
191int OGRS57Layer::TestCapability(const char *pszCap)
192
193{
194 if (EQUAL(pszCap, OLCRandomRead))
195 return FALSE;
196
197 else if (EQUAL(pszCap, OLCSequentialWrite))
198 return TRUE;
199
200 else if (EQUAL(pszCap, OLCRandomWrite))
201 return FALSE;
202
203 else if (EQUAL(pszCap, OLCFastFeatureCount))
204 return TRUE;
205
206 else if (EQUAL(pszCap, OLCFastGetExtent)) {
207 OGREnvelope oEnvelope;
208
209 return GetExtent(&oEnvelope, FALSE) == OGRERR_NONE;
210 } else if (EQUAL(pszCap, OLCFastSpatialFilter))
211 return FALSE;
212
213 else
214 return FALSE;
215}
216
217/************************************************************************/
218/* GetSpatialRef() */
219/************************************************************************/
220
221OGRSpatialReference *OGRS57Layer::GetSpatialRef()
222
223{
224 return poDS->GetSpatialRef();
225}
226
227/************************************************************************/
228/* GetExtent() */
229/************************************************************************/
230
231OGRErr OGRS57Layer::GetExtent(OGREnvelope *psExtent, int bForce)
232
233{
234 return poDS->GetDSExtent(psExtent, bForce);
235}
236
237/************************************************************************/
238/* GetFeatureCount() */
239/************************************************************************/
240int OGRS57Layer::GetFeatureCount(int bForce) {
241 if (poFilterGeom != NULL /*|| m_poAttrQuery != NULL*/
242 || nFeatureCount == -1)
243 return OGRLayer::GetFeatureCount(bForce);
244 else
245 return nFeatureCount;
246}
247
248/************************************************************************/
249/* GetFeature() */
250/************************************************************************/
251
252OGRFeature *OGRS57Layer::GetFeature(long nFeatureId)
253
254{
255 S57Reader *poReader = poDS->GetModule(0); // not multi-reader aware
256
257 if (poReader != NULL) {
258 OGRFeature *poFeature;
259
260 poFeature = poReader->ReadFeature(nFeatureId, poFeatureDefn);
261 if (poFeature != NULL && poFeature->GetGeometryRef() != NULL)
262 poFeature->GetGeometryRef()->assignSpatialReference(GetSpatialRef());
263 return poFeature;
264 } else
265 return NULL;
266}
267
268/************************************************************************/
269/* CreateFeature() */
270/************************************************************************/
271
272OGRErr OGRS57Layer::CreateFeature(OGRFeature *poFeature)
273
274{
275 /* -------------------------------------------------------------------- */
276 /* Set RCNM if not already set. */
277 /* -------------------------------------------------------------------- */
278 int iRCNMFld = poFeature->GetFieldIndex("RCNM");
279
280 if (iRCNMFld != -1) {
281 if (!poFeature->IsFieldSet(iRCNMFld)) {
282 poFeature->SetField(iRCNMFld, nRCNM);
283 } else {
284 CPLAssert(poFeature->GetFieldAsInteger(iRCNMFld) == nRCNM);
285 }
286 }
287
288 /* -------------------------------------------------------------------- */
289 /* Set OBJL if not already set. */
290 /* -------------------------------------------------------------------- */
291 if (nOBJL != -1) {
292 int iOBJLFld = poFeature->GetFieldIndex("OBJL");
293
294 if (!poFeature->IsFieldSet(iOBJLFld)) {
295 poFeature->SetField(iOBJLFld, nOBJL);
296 } else {
297 CPLAssert(poFeature->GetFieldAsInteger(iOBJLFld) == nOBJL);
298 }
299 }
300
301 /* -------------------------------------------------------------------- */
302 /* Create the isolated node feature. */
303 /* -------------------------------------------------------------------- */
304 // if( poDS->GetWriter()->WriteCompleteFeature( poFeature ) )
305 // return OGRERR_NONE;
306 // else
307 return OGRERR_FAILURE;
308}
Definition: s57.h:144