Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: GDAL/OGR Geography Network support (Geographic Network Model)
4 : * Purpose: GNM layer class.
5 : * Authors: Mikhail Gusev (gusevmihs at gmail dot com)
6 : * Dmitry Baryshnikov, polimax@mail.ru
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2014, Mikhail Gusev
10 : * Copyright (c) 2014-2015, NextGIS <info@nextgis.com>
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 : #include "gnm.h"
31 : #include "gnm_priv.h"
32 :
33 : /**
34 : * GNMGenericLayer
35 : */
36 25 : GNMGenericLayer::GNMGenericLayer(OGRLayer *poLayer,
37 25 : GNMGenericNetwork *poNetwork)
38 25 : : OGRLayer(), m_soLayerName(poLayer->GetName()), m_poLayer(poLayer),
39 25 : m_poNetwork(poNetwork)
40 : {
41 25 : }
42 :
43 : /**
44 : * ~GNMGenericLayer
45 : */
46 50 : GNMGenericLayer::~GNMGenericLayer()
47 : {
48 50 : }
49 :
50 0 : const char *GNMGenericLayer::GetFIDColumn()
51 : {
52 0 : return GNM_SYSFIELD_GFID;
53 : }
54 :
55 0 : const char *GNMGenericLayer::GetGeometryColumn()
56 : {
57 0 : return m_poLayer->GetGeometryColumn();
58 : }
59 :
60 0 : OGRErr GNMGenericLayer::SetIgnoredFields(CSLConstList papszFields)
61 : {
62 0 : return m_poLayer->SetIgnoredFields(papszFields);
63 : }
64 :
65 0 : OGRErr GNMGenericLayer::Intersection(OGRLayer *pLayerMethod,
66 : OGRLayer *pLayerResult,
67 : char **papszOptions,
68 : GDALProgressFunc pfnProgress,
69 : void *pProgressArg)
70 : {
71 0 : return m_poLayer->Intersection(pLayerMethod, pLayerResult, papszOptions,
72 0 : pfnProgress, pProgressArg);
73 : }
74 :
75 0 : OGRErr GNMGenericLayer::Union(OGRLayer *pLayerMethod, OGRLayer *pLayerResult,
76 : char **papszOptions, GDALProgressFunc pfnProgress,
77 : void *pProgressArg)
78 : {
79 0 : return m_poLayer->Union(pLayerMethod, pLayerResult, papszOptions,
80 0 : pfnProgress, pProgressArg);
81 : }
82 :
83 0 : OGRErr GNMGenericLayer::SymDifference(OGRLayer *pLayerMethod,
84 : OGRLayer *pLayerResult,
85 : char **papszOptions,
86 : GDALProgressFunc pfnProgress,
87 : void *pProgressArg)
88 : {
89 0 : return m_poLayer->Union(pLayerMethod, pLayerResult, papszOptions,
90 0 : pfnProgress, pProgressArg);
91 : }
92 :
93 0 : OGRErr GNMGenericLayer::Identity(OGRLayer *pLayerMethod, OGRLayer *pLayerResult,
94 : char **papszOptions,
95 : GDALProgressFunc pfnProgress,
96 : void *pProgressArg)
97 : {
98 0 : return m_poLayer->Union(pLayerMethod, pLayerResult, papszOptions,
99 0 : pfnProgress, pProgressArg);
100 : }
101 :
102 0 : OGRErr GNMGenericLayer::Update(OGRLayer *pLayerMethod, OGRLayer *pLayerResult,
103 : char **papszOptions,
104 : GDALProgressFunc pfnProgress, void *pProgressArg)
105 : {
106 0 : return m_poLayer->Update(pLayerMethod, pLayerResult, papszOptions,
107 0 : pfnProgress, pProgressArg);
108 : }
109 :
110 0 : OGRErr GNMGenericLayer::Clip(OGRLayer *pLayerMethod, OGRLayer *pLayerResult,
111 : char **papszOptions, GDALProgressFunc pfnProgress,
112 : void *pProgressArg)
113 : {
114 0 : return m_poLayer->Clip(pLayerMethod, pLayerResult, papszOptions,
115 0 : pfnProgress, pProgressArg);
116 : }
117 :
118 0 : OGRErr GNMGenericLayer::Erase(OGRLayer *pLayerMethod, OGRLayer *pLayerResult,
119 : char **papszOptions, GDALProgressFunc pfnProgress,
120 : void *pProgressArg)
121 : {
122 0 : return m_poLayer->Erase(pLayerMethod, pLayerResult, papszOptions,
123 0 : pfnProgress, pProgressArg);
124 : }
125 :
126 0 : GIntBig GNMGenericLayer::GetFeaturesRead()
127 : {
128 0 : return m_poLayer->GetFeaturesRead();
129 : }
130 :
131 0 : int GNMGenericLayer::AttributeFilterEvaluationNeedsGeometry()
132 : {
133 0 : return m_poLayer->AttributeFilterEvaluationNeedsGeometry();
134 : }
135 :
136 : //! @cond Doxygen_Suppress
137 0 : OGRErr GNMGenericLayer::InitializeIndexSupport(const char *pszVal)
138 : {
139 0 : return m_poLayer->InitializeIndexSupport(pszVal);
140 : }
141 :
142 0 : OGRLayerAttrIndex *GNMGenericLayer::GetIndex()
143 : {
144 0 : return m_poLayer->GetIndex();
145 : }
146 :
147 0 : OGRErr GNMGenericLayer::ISetFeature(OGRFeature *poFeature)
148 : {
149 0 : VALIDATE_POINTER1(poFeature, "GNMGenericLayer::ISetFeature", CE_Failure);
150 : std::map<GNMGFID, GIntBig>::iterator it =
151 0 : m_mnFIDMap.find(poFeature->GetFID());
152 0 : if (it == m_mnFIDMap.end())
153 : {
154 0 : CPLError(CE_Failure, CPLE_IllegalArg,
155 : "The FID " CPL_FRMT_GIB " is invalid", poFeature->GetFID());
156 0 : return OGRERR_NON_EXISTING_FEATURE;
157 : }
158 :
159 : // TODO: check connection rules if feature can be changed.
160 :
161 0 : poFeature->SetFID(it->second);
162 0 : return m_poLayer->SetFeature(poFeature);
163 : }
164 :
165 128 : OGRErr GNMGenericLayer::ICreateFeature(OGRFeature *poFeature)
166 : {
167 128 : VALIDATE_POINTER1(poFeature, "GNMGenericLayer::ICreateFeature", CE_Failure);
168 128 : GNMGFID nFID = m_poNetwork->GetNewGlobalFID();
169 128 : poFeature->SetFID(nFID);
170 128 : poFeature->SetField(GNM_SYSFIELD_GFID, nFID);
171 128 : poFeature->SetField(GNM_SYSFIELD_BLOCKED, GNM_BLOCK_NONE);
172 128 : if (m_poNetwork->AddFeatureGlobalFID(nFID, GetName()) != CE_None)
173 0 : return OGRERR_FAILURE;
174 128 : return m_poLayer->CreateFeature(poFeature);
175 : }
176 :
177 : //! @endcond
178 :
179 0 : OGRGeometry *GNMGenericLayer::GetSpatialFilter()
180 : {
181 0 : return m_poLayer->GetSpatialFilter();
182 : }
183 :
184 0 : void GNMGenericLayer::SetSpatialFilter(OGRGeometry *poGeometry)
185 : {
186 0 : m_poLayer->SetSpatialFilter(poGeometry);
187 0 : }
188 :
189 132 : void GNMGenericLayer::SetSpatialFilterRect(double dfMinX, double dfMinY,
190 : double dfMaxX, double dfMaxY)
191 : {
192 132 : m_poLayer->SetSpatialFilterRect(dfMinX, dfMinY, dfMaxX, dfMaxY);
193 132 : }
194 :
195 584 : void GNMGenericLayer::SetSpatialFilter(int iGeomField, OGRGeometry *poGeometry)
196 : {
197 584 : m_poLayer->SetSpatialFilter(iGeomField, poGeometry);
198 584 : }
199 :
200 0 : void GNMGenericLayer::SetSpatialFilterRect(int iGeomField, double dfMinX,
201 : double dfMinY, double dfMaxX,
202 : double dfMaxY)
203 : {
204 0 : m_poLayer->SetSpatialFilterRect(iGeomField, dfMinX, dfMinY, dfMaxX, dfMaxY);
205 0 : }
206 :
207 584 : OGRErr GNMGenericLayer::SetAttributeFilter(const char *pszFilter)
208 : {
209 584 : return m_poLayer->SetAttributeFilter(pszFilter);
210 : }
211 :
212 426 : void GNMGenericLayer::ResetReading()
213 : {
214 426 : m_poLayer->ResetReading();
215 426 : }
216 :
217 5313 : OGRFeature *GNMGenericLayer::GetNextFeature()
218 : {
219 5313 : OGRFeature *pFeature = m_poLayer->GetNextFeature();
220 5313 : if (nullptr == pFeature)
221 2 : return nullptr;
222 5311 : GNMGFID nGFID = pFeature->GetFieldAsGNMGFID(GNM_SYSFIELD_GFID);
223 5311 : m_mnFIDMap[nGFID] = pFeature->GetFID();
224 5311 : pFeature->SetFID(nGFID);
225 5311 : return pFeature;
226 : }
227 :
228 0 : OGRErr GNMGenericLayer::SetNextByIndex(GIntBig nIndex)
229 : {
230 0 : return m_poLayer->SetNextByIndex(nIndex);
231 : }
232 :
233 0 : OGRErr GNMGenericLayer::DeleteFeature(GIntBig nFID)
234 : {
235 0 : OGRFeature *poFeature = GetFeature(nFID);
236 0 : if (nullptr == poFeature)
237 0 : return CE_Failure;
238 :
239 0 : nFID = poFeature->GetFID();
240 0 : std::map<GNMGFID, GIntBig>::iterator it = m_mnFIDMap.find(nFID);
241 0 : if (it == m_mnFIDMap.end())
242 : {
243 0 : CPLError(CE_Failure, CPLE_IllegalArg,
244 : "The FID " CPL_FRMT_GIB " is invalid", nFID);
245 0 : return OGRERR_NON_EXISTING_FEATURE;
246 : }
247 :
248 0 : OGRFeature::DestroyFeature(poFeature);
249 :
250 : // delete from graph
251 0 : if (m_poNetwork->DisconnectFeaturesWithId((GNMGFID)nFID) != CE_None)
252 0 : return CE_Failure;
253 :
254 0 : return m_poLayer->DeleteFeature(it->second);
255 : }
256 :
257 1570 : const char *GNMGenericLayer::GetName()
258 : {
259 1570 : return m_soLayerName;
260 : }
261 :
262 8 : OGRwkbGeometryType GNMGenericLayer::GetGeomType()
263 : {
264 8 : return m_poLayer->GetGeomType();
265 : }
266 :
267 0 : int GNMGenericLayer::FindFieldIndex(const char *pszFieldName, int bExactMatch)
268 : {
269 0 : return m_poLayer->FindFieldIndex(pszFieldName, bExactMatch);
270 : }
271 :
272 0 : OGRSpatialReference *GNMGenericLayer::GetSpatialRef()
273 : {
274 0 : return m_poLayer->GetSpatialRef();
275 : }
276 :
277 0 : GIntBig GNMGenericLayer::GetFeatureCount(int bForce)
278 : {
279 0 : return m_poLayer->GetFeatureCount(bForce);
280 : }
281 :
282 0 : OGRErr GNMGenericLayer::GetExtent(OGREnvelope *psExtent, int bForce)
283 : {
284 0 : return m_poLayer->GetExtent(psExtent, bForce);
285 : }
286 :
287 0 : OGRErr GNMGenericLayer::GetExtent(int iGeomField, OGREnvelope *psExtent,
288 : int bForce)
289 : {
290 0 : return m_poLayer->GetExtent(iGeomField, psExtent, bForce);
291 : }
292 :
293 12 : int GNMGenericLayer::TestCapability(const char *pszCapability)
294 : {
295 12 : return m_poLayer->TestCapability(pszCapability);
296 : }
297 :
298 8 : OGRErr GNMGenericLayer::CreateField(const OGRFieldDefn *poField, int bApproxOK)
299 : {
300 8 : return m_poLayer->CreateField(poField, bApproxOK);
301 : }
302 :
303 0 : OGRErr GNMGenericLayer::DeleteField(int iField)
304 : {
305 0 : if (iField == FindFieldIndex(GNM_SYSFIELD_GFID, TRUE))
306 0 : return OGRERR_UNSUPPORTED_OPERATION;
307 0 : if (iField == FindFieldIndex(GNM_SYSFIELD_BLOCKED, TRUE))
308 0 : return OGRERR_UNSUPPORTED_OPERATION;
309 0 : return m_poLayer->DeleteField(iField);
310 : }
311 :
312 0 : OGRErr GNMGenericLayer::ReorderFields(int *panMap)
313 : {
314 0 : return m_poLayer->ReorderFields(panMap);
315 : }
316 :
317 0 : OGRErr GNMGenericLayer::AlterFieldDefn(int iField, OGRFieldDefn *poNewFieldDefn,
318 : int nFlagsIn)
319 : {
320 0 : if (iField == FindFieldIndex(GNM_SYSFIELD_GFID, TRUE))
321 0 : return OGRERR_UNSUPPORTED_OPERATION;
322 0 : if (iField == FindFieldIndex(GNM_SYSFIELD_BLOCKED, TRUE))
323 0 : return OGRERR_UNSUPPORTED_OPERATION;
324 0 : return m_poLayer->AlterFieldDefn(iField, poNewFieldDefn, nFlagsIn);
325 : }
326 :
327 0 : OGRErr GNMGenericLayer::CreateGeomField(const OGRGeomFieldDefn *poField,
328 : int bApproxOK)
329 : {
330 0 : return m_poLayer->CreateGeomField(poField, bApproxOK);
331 : }
332 :
333 21 : OGRErr GNMGenericLayer::SyncToDisk()
334 : {
335 21 : return m_poLayer->SyncToDisk();
336 : }
337 :
338 0 : OGRStyleTable *GNMGenericLayer::GetStyleTable()
339 : {
340 0 : return m_poLayer->GetStyleTable();
341 : }
342 :
343 0 : void GNMGenericLayer::SetStyleTableDirectly(OGRStyleTable *poStyleTable)
344 : {
345 0 : return m_poLayer->SetStyleTableDirectly(poStyleTable);
346 : }
347 :
348 0 : void GNMGenericLayer::SetStyleTable(OGRStyleTable *poStyleTable)
349 : {
350 0 : return m_poLayer->SetStyleTable(poStyleTable);
351 : }
352 :
353 0 : OGRErr GNMGenericLayer::StartTransaction()
354 : {
355 0 : return m_poLayer->StartTransaction();
356 : }
357 :
358 0 : OGRErr GNMGenericLayer::CommitTransaction()
359 : {
360 0 : return m_poLayer->CommitTransaction();
361 : }
362 :
363 0 : OGRErr GNMGenericLayer::RollbackTransaction()
364 : {
365 0 : return m_poLayer->RollbackTransaction();
366 : }
367 :
368 262 : OGRFeatureDefn *GNMGenericLayer::GetLayerDefn()
369 : {
370 : // TODO: hide GNM_SYSFIELD_GFID filed
371 262 : return m_poLayer->GetLayerDefn();
372 : }
|