Debian Codegraph @

ogr/ogrsf_frmts/oci/ogrocilayer.cpp

raw
1 /******************************************************************************
2 *
3 * Project: Oracle Spatial Driver
4 * Purpose: Implementation of the OGROCILayer class. This is layer semantics
5 * shared between table accessors and ExecuteSQL() result
6 * pseudo-layers.
7 * Author: Frank Warmerdam, [email protected]
8 *
9 ******************************************************************************
10 * Copyright (c) 2002, Frank Warmerdam <[email protected]>
11 *
12 * SPDX-License-Identifier: MIT
13 ****************************************************************************/
14
15 #include "ogr_oci.h"
16 #include "cpl_conv.h"
17
18 #include <algorithm>
19
20 /************************************************************************/
21 /* OGROCILayer() */
22 /************************************************************************/
23
24 OGROCILayer::OGROCILayer(OGROCIDataSource *poDSIn) : poDS(poDSIn)
25
26 {
27 poFeatureDefn = nullptr;
28 poStatement = nullptr;
29
30 pszQueryStatement = nullptr;
31 nResultOffset = 0;
32 pszGeomName = nullptr;
33 iGeomColumn = -1;
34 pszFIDName = nullptr;
35 iFIDColumn = -1;
36
37 hLastGeom = nullptr;
38 hLastGeomInd = nullptr;
39
40 iNextShapeId = 0;
41 }
42
43 /************************************************************************/
44 /* ~OGROCILayer() */
45 /************************************************************************/
46
47 OGROCILayer::~OGROCILayer()
48
49 {
50 if (m_nFeaturesRead > 0 && poFeatureDefn != nullptr)
51 {
52 CPLDebug("OCI", "%d features read on layer '%s'.", (int)m_nFeaturesRead,
53 poFeatureDefn->GetName());
54 }
55
56 OGROCILayer::ResetReading();
57
58 CPLFree(pszGeomName);
59 pszGeomName = nullptr;
60
61 CPLFree(pszFIDName);
62 pszFIDName = nullptr;
63
64 CPLFree(pszQueryStatement);
65 pszQueryStatement = nullptr;
66
67 if (poFeatureDefn != nullptr)
68 poFeatureDefn->Release();
69 }
70
71 /************************************************************************/
72 /* ResetReading() */
73 /************************************************************************/
74
75 void OGROCILayer::ResetReading()
76
77 {
78 if (poStatement != nullptr)
79 delete poStatement;
80 poStatement = nullptr;
81
82 iNextShapeId = 0;
83 }
84
85 /************************************************************************/
86 /* GetNextFeature() */
87 /* */
88 /* By default we implement the full spatial and attribute query */
89 /* semantics manually here. The table query class will */
90 /* override this method and implement these inline, but the */
91 /* simple SELECT statement evaluator (OGROCISelectLayer) will */
92 /* depend us this code implementing additional spatial or */
93 /* attribute query semantics. */
94 /************************************************************************/
95
96 OGRFeature *OGROCILayer::GetNextFeature()
97
98 {
99 while (true)
100 {
101 OGRFeature *poFeature;
102
103 poFeature = GetNextRawFeature();
104 if (poFeature == nullptr)
105 return nullptr;
106
107 if ((m_poFilterGeom == nullptr ||
108 FilterGeometry(poFeature->GetGeometryRef())) &&
109 (m_poAttrQuery == nullptr || m_poAttrQuery->Evaluate(poFeature)))
110 return poFeature;
111
112 delete poFeature;
113 }
114 }
115
116 /************************************************************************/
117 /* GetNextRawFeature() */
118 /************************************************************************/
119
120 OGRFeature *OGROCILayer::GetNextRawFeature()
121
122 {
123 /* -------------------------------------------------------------------- */
124 /* Do we need to establish an initial query? */
125 /* -------------------------------------------------------------------- */
126 if (iNextShapeId == 0 && poStatement == nullptr)
127 {
128 if (!ExecuteQuery(pszQueryStatement))
129 return nullptr;
130 }
131
132 /* -------------------------------------------------------------------- */
133 /* Have we run out of query results, such that we have no */
134 /* statement left? */
135 /* -------------------------------------------------------------------- */
136 if (poStatement == nullptr)
137 return nullptr;
138
139 /* -------------------------------------------------------------------- */
140 /* Are we in some sort of error condition? */
141 /* -------------------------------------------------------------------- */
142 hLastGeom = nullptr;
143
144 char **papszResult = poStatement->SimpleFetchRow();
145
146 if (papszResult == nullptr)
147 {
148 iNextShapeId = std::max(1, iNextShapeId);
149 delete poStatement;
150 poStatement = nullptr;
151 return nullptr;
152 }
153
154 /* -------------------------------------------------------------------- */
155 /* Create a feature from the current result. */
156 /* -------------------------------------------------------------------- */
157 int iField;
158 OGRFeature *poFeature = new OGRFeature(poFeatureDefn);
159
160 poFeature->SetFID(iNextShapeId);
161 iNextShapeId++;
162 m_nFeaturesRead++;
163
164 if (iFIDColumn != -1 && papszResult[iFIDColumn] != nullptr)
165 poFeature->SetFID(atoi(papszResult[iFIDColumn]));
166
167 for (iField = 0; iField < poFeatureDefn->GetFieldCount(); iField++)
168 {
169 if (papszResult[iField] != nullptr)
170 poFeature->SetField(iField, papszResult[iField]);
171 else
172 poFeature->SetFieldNull(iField);
173 }
174
175 /* -------------------------------------------------------------------- */
176 /* Translate geometry if we have it. */
177 /* -------------------------------------------------------------------- */
178 if (iGeomColumn != -1)
179 {
180 poFeature->SetGeometryDirectly(TranslateGeometry());
181
182 OGROCISession *poSession = poDS->GetSession();
183
184 if (poFeature->GetGeometryRef() != nullptr && hLastGeom != nullptr)
185 poSession->Failed(OCIObjectFree(poSession->hEnv, poSession->hError,
186 static_cast<dvoid *>(hLastGeom),
187 (ub2)OCI_OBJECTFREE_FORCE));
188
189 hLastGeom = nullptr;
190 hLastGeomInd = nullptr;
191 }
192
193 nResultOffset++;
194
195 return poFeature;
196 }
197
198 /************************************************************************/
199 /* ExecuteQuery() */
200 /* */
201 /* This is invoke when the first request for a feature is */
202 /* made. It executes the query, and binds columns as needed. */
203 /* The OGROCIStatement is used for most of the work. */
204 /************************************************************************/
205
206 int OGROCILayer::ExecuteQuery(const char *pszReqQuery)
207
208 {
209 OGROCISession *poSession = poDS->GetSession();
210
211 CPLAssert(pszReqQuery != nullptr);
212 CPLAssert(poStatement == nullptr);
213
214 /* -------------------------------------------------------------------- */
215 /* Execute the query. */
216 /* -------------------------------------------------------------------- */
217 poStatement = new OGROCIStatement(poSession);
218 if (poStatement->Execute(pszReqQuery) != CE_None)
219 {
220 delete poStatement;
221 poStatement = nullptr;
222 return FALSE;
223 }
224 nResultOffset = 0;
225
226 /* -------------------------------------------------------------------- */
227 /* Do additional work binding the geometry column. */
228 /* -------------------------------------------------------------------- */
229 if (iGeomColumn != -1)
230 {
231 OCIDefine *hGDefine = nullptr;
232
233 if (poSession->Failed(
234 OCIDefineByPos(poStatement->GetStatement(), &hGDefine,
235 poSession->hError, (ub4)iGeomColumn + 1,
236 static_cast<dvoid *>(nullptr), (sb4)0, SQLT_NTY,
237 static_cast<dvoid *>(nullptr),
238 static_cast<ub2 *>(nullptr),
239 static_cast<ub2 *>(nullptr), (ub4)OCI_DEFAULT),
240 "OCIDefineByPos(geometry)"))
241 return FALSE;
242
243 if (poSession->Failed(
244 OCIDefineObject(hGDefine, poSession->hError,
245 poSession->hGeometryTDO,
246 reinterpret_cast<dvoid **>(&hLastGeom),
247 static_cast<ub4 *>(nullptr),
248 reinterpret_cast<dvoid **>(&hLastGeomInd),
249 static_cast<ub4 *>(nullptr)),
250 "OCIDefineObject"))
251 return FALSE;
252 }
253
254 return TRUE;
255 }
256
257 /************************************************************************/
258 /* TranslateGeometry() */
259 /************************************************************************/
260
261 OGRGeometry *OGROCILayer::TranslateGeometry()
262
263 {
264 OGROCISession *poSession = poDS->GetSession();
265
266 /* -------------------------------------------------------------------- */
267 /* Is the geometry NULL? */
268 /* -------------------------------------------------------------------- */
269 if (hLastGeom == nullptr || hLastGeomInd == nullptr ||
270 hLastGeomInd->_atomic == OCI_IND_NULL)
271 return nullptr;
272
273 /* -------------------------------------------------------------------- */
274 /* Get the size of the sdo_elem_info and sdo_ordinates arrays. */
275 /* -------------------------------------------------------------------- */
276 int nElemCount, nOrdCount;
277
278 if (poSession->Failed(
279 OCICollSize(poSession->hEnv, poSession->hError,
280 reinterpret_cast<OCIColl *>(hLastGeom->sdo_elem_info),
281 &nElemCount),
282 "OCICollSize(sdo_elem_info)"))
283 return nullptr;
284
285 if (poSession->Failed(
286 OCICollSize(poSession->hEnv, poSession->hError,
287 reinterpret_cast<OCIColl *>(hLastGeom->sdo_ordinates),
288 &nOrdCount),
289 "OCICollSize(sdo_ordinates)"))
290 return nullptr;
291
292 /* -------------------------------------------------------------------- */
293 /* Get the GType. */
294 /* -------------------------------------------------------------------- */
295 int nGType;
296
297 if (poSession->Failed(OCINumberToInt(poSession->hError,
298 &(hLastGeom->sdo_gtype),
299 (uword)sizeof(int), OCI_NUMBER_SIGNED,
300 static_cast<dvoid *>(&nGType)),
301 "OCINumberToInt(GType)"))
302 return nullptr;
303
304 /* -------------------------------------------------------------------- */
305 /* Establish the dimension. */
306 /* -------------------------------------------------------------------- */
307 int nDimension = std::max(2, (nGType / 1000));
308
309 /* -------------------------------------------------------------------- */
310 /* Handle point data directly from built-in point info. */
311 /* -------------------------------------------------------------------- */
312 if (ORA_GTYPE_MATCH(nGType, ORA_GTYPE_POINT) &&
313 hLastGeomInd->sdo_point._atomic == OCI_IND_NOTNULL &&
314 hLastGeomInd->sdo_point.x == OCI_IND_NOTNULL &&
315 hLastGeomInd->sdo_point.y == OCI_IND_NOTNULL)
316 {
317 double dfX, dfY, dfZ = 0.0;
318
319 OCINumberToReal(poSession->hError, &(hLastGeom->sdo_point.x),
320 (uword)sizeof(double), static_cast<dvoid *>(&dfX));
321 OCINumberToReal(poSession->hError, &(hLastGeom->sdo_point.y),
322 (uword)sizeof(double), static_cast<dvoid *>(&dfY));
323 if (hLastGeomInd->sdo_point.z == OCI_IND_NOTNULL)
324 OCINumberToReal(poSession->hError, &(hLastGeom->sdo_point.z),
325 (uword)sizeof(double), static_cast<dvoid *>(&dfZ));
326
327 if (nDimension == 3)
328 return new OGRPoint(dfX, dfY, dfZ);
329 else
330 return new OGRPoint(dfX, dfY);
331 }
332
333 /* -------------------------------------------------------------------- */
334 /* If this is a sort of container geometry, create the */
335 /* container now. */
336 /* -------------------------------------------------------------------- */
337 OGRGeometryCollection *poCollection = nullptr;
338 OGRPolygon *poPolygon = nullptr;
339
340 if (ORA_GTYPE_MATCH(nGType, ORA_GTYPE_POLYGON))
341 poPolygon = new OGRPolygon();
342 else if (ORA_GTYPE_MATCH(nGType, ORA_GTYPE_COLLECTION))
343 poCollection = new OGRGeometryCollection();
344 else if (ORA_GTYPE_MATCH(nGType, ORA_GTYPE_MULTIPOINT))
345 poCollection = new OGRMultiPoint();
346 else if (ORA_GTYPE_MATCH(nGType, ORA_GTYPE_MULTILINESTRING))
347 poCollection = new OGRMultiLineString();
348 else if (ORA_GTYPE_MATCH(nGType, ORA_GTYPE_MULTIPOLYGON))
349 poCollection = new OGRMultiPolygon();
350
351 /* ==================================================================== */
352 /* Loop over the component elements. */
353 /* ==================================================================== */
354 for (int iElement = 0; iElement < nElemCount; iElement += 3)
355 {
356 int nInterpretation, nEType;
357 int nStartOrdinal, nElemOrdCount;
358
359 LoadElementInfo(iElement, nElemCount, nOrdCount, &nEType,
360 &nInterpretation, &nStartOrdinal, &nElemOrdCount);
361
362 /* --------------------------------------------------------------------
363 */
364 /* Translate this element. */
365 /* --------------------------------------------------------------------
366 */
367 OGRGeometry *poGeom;
368
369 poGeom = TranslateGeometryElement(&iElement, nGType, nDimension, nEType,
370 nInterpretation, nStartOrdinal - 1,
371 nElemOrdCount);
372
373 if (poGeom == nullptr)
374 return nullptr;
375
376 /* --------------------------------------------------------------------
377 */
378 /* Based on GType do what is appropriate. */
379 /* --------------------------------------------------------------------
380 */
381 if (ORA_GTYPE_MATCH(nGType, ORA_GTYPE_LINESTRING))
382 {
383 CPLAssert(wkbFlatten(poGeom->getGeometryType()) == wkbLineString);
384 return poGeom;
385 }
386
387 else if (ORA_GTYPE_MATCH(nGType, ORA_GTYPE_POINT))
388 {
389 CPLAssert(wkbFlatten(poGeom->getGeometryType()) == wkbPoint);
390 return poGeom;
391 }
392
393 else if (ORA_GTYPE_MATCH(nGType, ORA_GTYPE_POLYGON))
394 {
395 CPLAssert(wkbFlatten(poGeom->getGeometryType()) == wkbLineString);
396 poPolygon->addRingDirectly(poGeom->toLinearRing());
397 }
398 else
399 {
400 CPLAssert(poCollection != nullptr);
401 if (wkbFlatten(poGeom->getGeometryType()) == wkbMultiPoint)
402 {
403 OGRMultiPoint *poMP = poGeom->toMultiPoint();
404 for (auto &&poPoint : *poMP)
405 poCollection->addGeometry(poPoint);
406 delete poMP;
407 }
408 else if (nEType % 1000 == 3)
409 {
410 /* its one poly ring, create new poly or add to existing */
411 if (nEType == 1003)
412 {
413 if (poPolygon != nullptr &&
414 poPolygon->getExteriorRing() != nullptr)
415 {
416 poCollection->addGeometryDirectly(poPolygon);
417 poPolygon = nullptr;
418 }
419
420 poPolygon = new OGRPolygon();
421 }
422
423 if (poPolygon != nullptr)
424 poPolygon->addRingDirectly(poGeom->toLinearRing());
425 else
426 {
427 CPLAssert(poPolygon != nullptr);
428 }
429 }
430 else
431 poCollection->addGeometryDirectly(poGeom);
432 }
433 }
434
435 if (poCollection != nullptr && poPolygon != nullptr)
436 poCollection->addGeometryDirectly(poPolygon);
437
438 /* -------------------------------------------------------------------- */
439 /* Return resulting collection geometry. */
440 /* -------------------------------------------------------------------- */
441 if (poCollection == nullptr)
442 return poPolygon;
443 else
444 return poCollection;
445 }
446
447 /************************************************************************/
448 /* LoadElementInfo() */
449 /* */
450 /* Fetch the start ordinal, count, EType and interpretation */
451 /* values for a particular element. */
452 /************************************************************************/
453
454 int OGROCILayer::LoadElementInfo(int iElement, int nElemCount,
455 int nTotalOrdCount, int *pnEType,
456 int *pnInterpretation, int *pnStartOrdinal,
457 int *pnElemOrdCount)
458
459 {
460 OGROCISession *poSession = poDS->GetSession();
461 boolean bExists;
462 OCINumber *hNumber;
463 /* -------------------------------------------------------------------- */
464 /* Get the details about element from the elem_info array. */
465 /* -------------------------------------------------------------------- */
466 OCICollGetElem(poSession->hEnv, poSession->hError,
467 reinterpret_cast<OCIColl *>(hLastGeom->sdo_elem_info),
468 (sb4)(iElement + 0), reinterpret_cast<boolean *>(&bExists),
469 reinterpret_cast<dvoid **>(&hNumber), nullptr);
470 OCINumberToInt(poSession->hError, hNumber, (uword)sizeof(ub4),
471 OCI_NUMBER_UNSIGNED, static_cast<dvoid *>(pnStartOrdinal));
472
473 OCICollGetElem(poSession->hEnv, poSession->hError,
474 reinterpret_cast<OCIColl *>(hLastGeom->sdo_elem_info),
475 (sb4)(iElement + 1), reinterpret_cast<boolean *>(&bExists),
476 reinterpret_cast<dvoid **>(&hNumber), nullptr);
477 OCINumberToInt(poSession->hError, hNumber, (uword)sizeof(ub4),
478 OCI_NUMBER_UNSIGNED, static_cast<dvoid *>(pnEType));
479
480 OCICollGetElem(poSession->hEnv, poSession->hError,
481 reinterpret_cast<OCIColl *>(hLastGeom->sdo_elem_info),
482 (sb4)(iElement + 2), reinterpret_cast<boolean *>(&bExists),
483 reinterpret_cast<dvoid **>(&hNumber), nullptr);
484 OCINumberToInt(poSession->hError, hNumber, (uword)sizeof(ub4),
485 OCI_NUMBER_UNSIGNED, static_cast<dvoid *>(pnInterpretation));
486
487 if (iElement < nElemCount - 3)
488 {
489 ub4 nNextStartOrdinal;
490
491 OCICollGetElem(poSession->hEnv, poSession->hError,
492 reinterpret_cast<OCIColl *>(hLastGeom->sdo_elem_info),
493 (sb4)(iElement + 3),
494 reinterpret_cast<boolean *>(&bExists),
495 reinterpret_cast<dvoid **>(&hNumber), nullptr);
496 OCINumberToInt(poSession->hError, hNumber, (uword)sizeof(ub4),
497 OCI_NUMBER_UNSIGNED,
498 static_cast<dvoid *>(&nNextStartOrdinal));
499
500 *pnElemOrdCount = nNextStartOrdinal - *pnStartOrdinal;
501 }
502 else
503 *pnElemOrdCount = nTotalOrdCount - *pnStartOrdinal + 1;
504
505 return TRUE;
506 }
507
508 /************************************************************************/
509 /* TranslateGeometryElement() */
510 /************************************************************************/
511
512 OGRGeometry *OGROCILayer::TranslateGeometryElement(int *piElement, int nGType,
513 int nDimension, int nEType,
514 int nInterpretation,
515 int nStartOrdinal,
516 int nElemOrdCount)
517
518 {
519 /* -------------------------------------------------------------------- */
520 /* Handle simple point. */
521 /* -------------------------------------------------------------------- */
522 if (nEType == 1 && nInterpretation == 1)
523 {
524 OGRPoint *poPoint = new OGRPoint();
525 double dfX, dfY, dfZ = 0.0;
526
527 GetOrdinalPoint(nStartOrdinal, nDimension, &dfX, &dfY, &dfZ);
528
529 poPoint->setX(dfX);
530 poPoint->setY(dfY);
531 if (nDimension == 3)
532 poPoint->setZ(dfZ);
533
534 return poPoint;
535 }
536
537 /* -------------------------------------------------------------------- */
538 /* Handle multipoint. */
539 /* -------------------------------------------------------------------- */
540 else if (nEType == 1 && nInterpretation > 1)
541 {
542 OGRMultiPoint *poMP = new OGRMultiPoint();
543 double dfX, dfY, dfZ = 0.0;
544 int i;
545
546 CPLAssert(nInterpretation == nElemOrdCount / nDimension);
547
548 for (i = 0; i < nInterpretation; i++)
549 {
550 GetOrdinalPoint(nStartOrdinal + i * nDimension, nDimension, &dfX,
551 &dfY, &dfZ);
552
553 OGRPoint *poPoint = (nDimension == 3) ? new OGRPoint(dfX, dfY, dfZ)
554 : new OGRPoint(dfX, dfY);
555 poMP->addGeometryDirectly(poPoint);
556 }
557 return poMP;
558 }
559
560 /* -------------------------------------------------------------------- */
561 /* Discard orientations for oriented points. */
562 /* -------------------------------------------------------------------- */
563 else if (nEType == 1 && nInterpretation == 0)
564 {
565 CPLDebug("OCI", "Ignoring orientations for oriented points.");
566 return nullptr;
567 }
568
569 /* -------------------------------------------------------------------- */
570 /* Handle line strings consisting of straight segments. */
571 /* -------------------------------------------------------------------- */
572 else if (nEType == 2 && nInterpretation == 1)
573 {
574 OGRLineString *poLS = new OGRLineString();
575 int nPointCount = nElemOrdCount / nDimension, i;
576
577 poLS->setNumPoints(nPointCount);
578
579 for (i = 0; i < nPointCount; i++)
580 {
581 double dfX, dfY, dfZ = 0.0;
582
583 GetOrdinalPoint(i * nDimension + nStartOrdinal, nDimension, &dfX,
584 &dfY, &dfZ);
585 if (nDimension == 3)
586 poLS->setPoint(i, dfX, dfY, dfZ);
587 else
588 poLS->setPoint(i, dfX, dfY);
589 }
590
591 return poLS;
592 }
593
594 /* -------------------------------------------------------------------- */
595 /* Handle line strings consisting of circular arcs. */
596 /* -------------------------------------------------------------------- */
597 else if (nEType == 2 && nInterpretation == 2)
598 {
599 OGRLineString *poLS = new OGRLineString();
600 int nPointCount = nElemOrdCount / nDimension, i;
601
602 for (i = 0; i < nPointCount - 2; i += 2)
603 {
604 double dfStartX, dfStartY, dfStartZ = 0.0;
605 double dfMidX, dfMidY, dfMidZ = 0.0;
606 double dfEndX, dfEndY, dfEndZ = 0.0;
607
608 GetOrdinalPoint(i * nDimension + nStartOrdinal, nDimension,
609 &dfStartX, &dfStartY, &dfStartZ);
610 GetOrdinalPoint((i + 1) * nDimension + nStartOrdinal, nDimension,
611 &dfMidX, &dfMidY, &dfMidZ);
612 GetOrdinalPoint((i + 2) * nDimension + nStartOrdinal, nDimension,
613 &dfEndX, &dfEndY, &dfEndZ);
614
615 OGROCIStrokeArcToOGRGeometry_Points(dfStartX, dfStartY, dfMidX,
616 dfMidY, dfEndX, dfEndY, 6.0,
617 FALSE, poLS);
618 }
619
620 return poLS;
621 }
622
623 /* -------------------------------------------------------------------- */
624 /* Handle polygon rings. Treat curves as if they were */
625 /* linestrings. */
626 /* -------------------------------------------------------------------- */
627 else if (nEType % 1000 == 3 && nInterpretation == 1)
628 {
629 OGRLinearRing *poLS = new OGRLinearRing();
630 int nPointCount = nElemOrdCount / nDimension, i;
631
632 poLS->setNumPoints(nPointCount);
633
634 for (i = 0; i < nPointCount; i++)
635 {
636 double dfX, dfY, dfZ = 0.0;
637
638 GetOrdinalPoint(i * nDimension + nStartOrdinal, nDimension, &dfX,
639 &dfY, &dfZ);
640 if (nDimension == 3)
641 poLS->setPoint(i, dfX, dfY, dfZ);
642 else
643 poLS->setPoint(i, dfX, dfY);
644 }
645
646 return poLS;
647 }
648
649 /* -------------------------------------------------------------------- */
650 /* Handle polygon rings made of circular arcs. */
651 /* -------------------------------------------------------------------- */
652 else if (nEType % 1000 == 3 && nInterpretation == 2)
653 {
654 OGRLineString *poLS = new OGRLinearRing();
655 int nPointCount = nElemOrdCount / nDimension, i;
656
657 for (i = 0; i < nPointCount - 2; i += 2)
658 {
659 double dfStartX, dfStartY, dfStartZ = 0.0;
660 double dfMidX, dfMidY, dfMidZ = 0.0;
661 double dfEndX, dfEndY, dfEndZ = 0.0;
662
663 GetOrdinalPoint(i * nDimension + nStartOrdinal, nDimension,
664 &dfStartX, &dfStartY, &dfStartZ);
665 GetOrdinalPoint((i + 1) * nDimension + nStartOrdinal, nDimension,
666 &dfMidX, &dfMidY, &dfMidZ);
667 GetOrdinalPoint((i + 2) * nDimension + nStartOrdinal, nDimension,
668 &dfEndX, &dfEndY, &dfEndZ);
669
670 OGROCIStrokeArcToOGRGeometry_Points(dfStartX, dfStartY, dfMidX,
671 dfMidY, dfEndX, dfEndY, 6.0,
672 FALSE, poLS);
673 }
674
675 return poLS;
676 }
677
678 /* -------------------------------------------------------------------- */
679 /* Handle rectangle definitions ... translate into a linear ring. */
680 /* -------------------------------------------------------------------- */
681 else if (nEType % 1000 == 3 && nInterpretation == 3)
682 {
683 OGRLinearRing *poLS = new OGRLinearRing();
684 double dfX1, dfY1, dfZ1 = 0.0;
685 double dfX2, dfY2, dfZ2 = 0.0;
686
687 GetOrdinalPoint(nStartOrdinal, nDimension, &dfX1, &dfY1, &dfZ1);
688 GetOrdinalPoint(nStartOrdinal + nDimension, nDimension, &dfX2, &dfY2,
689 &dfZ2);
690
691 poLS->setNumPoints(5);
692
693 poLS->setPoint(0, dfX1, dfY1, dfZ1);
694 poLS->setPoint(1, dfX2, dfY1, dfZ1);
695 poLS->setPoint(2, dfX2, dfY2, dfZ2);
696 poLS->setPoint(3, dfX1, dfY2, dfZ2);
697 poLS->setPoint(4, dfX1, dfY1, dfZ1);
698
699 return poLS;
700 }
701
702 /* -------------------------------------------------------------------- */
703 /* Handle circle definitions ... translate into a linear ring. */
704 /* -------------------------------------------------------------------- */
705 else if (nEType % 100 == 3 && nInterpretation == 4)
706 {
707 OGRLinearRing *poLS = new OGRLinearRing();
708 double dfX1, dfY1, dfZ1 = 0.0;
709 double dfX2, dfY2, dfZ2 = 0.0;
710 double dfX3, dfY3, dfZ3 = 0.0;
711
712 GetOrdinalPoint(nStartOrdinal, nDimension, &dfX1, &dfY1, &dfZ1);
713 GetOrdinalPoint(nStartOrdinal + nDimension, nDimension, &dfX2, &dfY2,
714 &dfZ2);
715 GetOrdinalPoint(nStartOrdinal + nDimension * 2, nDimension, &dfX3,
716 &dfY3, &dfZ3);
717
718 OGROCIStrokeArcToOGRGeometry_Points(dfX1, dfY1, dfX2, dfY2, dfX3, dfY3,
719 6.0, TRUE, poLS);
720
721 return poLS;
722 }
723
724 /* -------------------------------------------------------------------- */
725 /* Handle compound line strings and polygon rings. */
726 /* */
727 /* This is quite complicated since we need to consume several */
728 /* following elements, and merge the resulting geometries. */
729 /* -------------------------------------------------------------------- */
730 else if (nEType == 4 || nEType % 100 == 5)
731 {
732 int nSubElementCount = nInterpretation;
733 OGRLineString *poLS;
734 int nElemCount, nTotalOrdCount;
735 OGROCISession *poSession = poDS->GetSession();
736
737 if (poSession->Failed(OCICollSize(poSession->hEnv, poSession->hError,
738 reinterpret_cast<OCIColl *>(
739 hLastGeom->sdo_elem_info),
740 &nElemCount),
741 "OCICollSize(sdo_elem_info)"))
742 return nullptr;
743
744 if (poSession->Failed(OCICollSize(poSession->hEnv, poSession->hError,
745 reinterpret_cast<OCIColl *>(
746 hLastGeom->sdo_ordinates),
747 &nTotalOrdCount),
748 "OCICollSize(sdo_ordinates)"))
749 return nullptr;
750
751 if (nEType == 4)
752 poLS = new OGRLineString();
753 else
754 poLS = new OGRLinearRing();
755
756 for (*piElement += 3; nSubElementCount-- > 0; *piElement += 3)
757 {
758 LoadElementInfo(*piElement, nElemCount, nTotalOrdCount, &nEType,
759 &nInterpretation, &nStartOrdinal, &nElemOrdCount);
760
761 // Adjust for repeated end point except for last element.
762 if (nSubElementCount > 0)
763 nElemOrdCount += nDimension;
764
765 // translate element.
766 OGRGeometry *poGeom = TranslateGeometryElement(
767 piElement, nGType, nDimension, nEType, nInterpretation,
768 nStartOrdinal - 1, nElemOrdCount);
769 OGRLineString *poElemLS = dynamic_cast<OGRLineString *>(poGeom);
770
771 // Try to append to our aggregate linestring/ring
772 if (poElemLS)
773 {
774 if (poLS->getNumPoints() > 0)
775 {
776 CPLAssert(poElemLS->getX(0) ==
777 poLS->getX(poLS->getNumPoints() - 1) &&
778 poElemLS->getY(0) ==
779 poLS->getY(poLS->getNumPoints() - 1));
780
781 poLS->addSubLineString(poElemLS, 1);
782 }
783 else
784 poLS->addSubLineString(poElemLS, 0);
785 }
786 delete poGeom;
787 }
788
789 *piElement -= 3;
790 return poLS;
791 }
792
793 /* -------------------------------------------------------------------- */
794 /* Otherwise it is apparently unsupported. */
795 /* -------------------------------------------------------------------- */
796 else
797 {
798
799 CPLDebug("OCI", "Geometry with EType=%d, Interp=%d ignored.", nEType,
800 nInterpretation);
801 }
802
803 return nullptr;
804 }
805
806 /************************************************************************/
807 /* GetOrdinalPoint() */
808 /************************************************************************/
809
810 int OGROCILayer::GetOrdinalPoint(int iOrdinal, int nDimension, double *pdfX,
811 double *pdfY, double *pdfZ)
812
813 {
814 OGROCISession *poSession = poDS->GetSession();
815 boolean bExists;
816 OCINumber *hNumber;
817
818 OCICollGetElem(poSession->hEnv, poSession->hError,
819 reinterpret_cast<OCIColl *>(hLastGeom->sdo_ordinates),
820 (sb4)iOrdinal + 0, reinterpret_cast<boolean *>(&bExists),
821 reinterpret_cast<dvoid **>(&hNumber), nullptr);
822 OCINumberToReal(poSession->hError, hNumber, (uword)sizeof(double),
823 static_cast<dvoid *>(pdfX));
824 OCICollGetElem(poSession->hEnv, poSession->hError,
825 reinterpret_cast<OCIColl *>(hLastGeom->sdo_ordinates),
826 (sb4)iOrdinal + 1, reinterpret_cast<boolean *>(&bExists),
827 reinterpret_cast<dvoid **>(&hNumber), nullptr);
828 OCINumberToReal(poSession->hError, hNumber, (uword)sizeof(double),
829 static_cast<dvoid *>(pdfY));
830 if (nDimension == 3)
831 {
832 OCICollGetElem(poSession->hEnv, poSession->hError,
833 reinterpret_cast<OCIColl *>(hLastGeom->sdo_ordinates),
834 (sb4)iOrdinal + 2, reinterpret_cast<boolean *>(&bExists),
835 reinterpret_cast<dvoid **>(&hNumber), nullptr);
836 OCINumberToReal(poSession->hError, hNumber, (uword)sizeof(double),
837 static_cast<dvoid *>(pdfZ));
838 }
839
840 return TRUE;
841 }
842
843 /************************************************************************/
844 /* TestCapability() */
845 /************************************************************************/
846
847 int OGROCILayer::TestCapability(const char *pszCap) const
848
849 {
850 if (EQUAL(pszCap, OLCRandomRead))
851 return TRUE;
852
853 else if (EQUAL(pszCap, OLCFastFeatureCount))
854 return m_poFilterGeom == nullptr;
855
856 else if (EQUAL(pszCap, OLCFastSpatialFilter))
857 return TRUE;
858
859 else if (EQUAL(pszCap, OLCTransactions))
860 return TRUE;
861
862 else if (EQUAL(pszCap, OLCZGeometries))
863 return TRUE;
864
865 else
866 return FALSE;
867 }
868
869 /************************************************************************/
870 /* LookupTableSRID() */
871 /* */
872 /* Note that the table name may also be prefixed by the owner */
873 /* with a dot separator. */
874 /************************************************************************/
875
876 int OGROCILayer::LookupTableSRID()
877
878 {
879 /* -------------------------------------------------------------------- */
880 /* If we don't have a geometry column, there isn't much point */
881 /* in trying. */
882 /* -------------------------------------------------------------------- */
883 if (pszGeomName == nullptr)
884 return -1;
885
886 /* -------------------------------------------------------------------- */
887 /* Split out the owner if available. */
888 /* -------------------------------------------------------------------- */
889 const char *pszTableName = GetLayerDefn()->GetName();
890 char *pszOwner = nullptr;
891
892 if (strstr(pszTableName, ".") != nullptr)
893 {
894 pszOwner = CPLStrdup(pszTableName);
895 pszTableName = strstr(pszTableName, ".") + 1;
896
897 *(strstr(pszOwner, ".")) = '\0';
898 }
899
900 /* -------------------------------------------------------------------- */
901 /* Build our query command. */
902 /* -------------------------------------------------------------------- */
903 OGROCIStringBuf oCommand;
904
905 oCommand.Append("SELECT SRID FROM ALL_SDO_GEOM_METADATA "
906 "WHERE TABLE_NAME = UPPER(:table_name) AND COLUMN_NAME = "
907 "UPPER(:geometry_name)");
908
909 if (pszOwner != nullptr)
910 {
911 oCommand.Append(" AND OWNER = :owner");
912 }
913
914 /* -------------------------------------------------------------------- */
915 /* Execute query command. */
916 /* -------------------------------------------------------------------- */
917 OGROCIStatement oGetTables(poDS->GetSession());
918 int nSRID = -1;
919
920 if (oGetTables.Prepare(oCommand.GetString()) != CE_None)
921 return nSRID;
922
923 oGetTables.BindString(":table_name", pszTableName);
924 oGetTables.BindString(":geometry_name", pszGeomName);
925 if (pszOwner != nullptr)
926 {
927 oGetTables.BindString(":owner", pszOwner);
928 CPLFree(pszOwner);
929 }
930
931 if (oGetTables.Execute(nullptr) == CE_None)
932 {
933 char **papszRow = oGetTables.SimpleFetchRow();
934
935 if (papszRow != nullptr && papszRow[0] != nullptr)
936 nSRID = atoi(papszRow[0]);
937 }
938
939 return nSRID;
940 }
941
942 /************************************************************************/
943 /* GetFIDColumn() */
944 /************************************************************************/
945
946 const char *OGROCILayer::GetFIDColumn() const
947
948 {
949 if (pszFIDName != nullptr)
950 return pszFIDName;
951 else
952 return "";
953 }
954
955 /************************************************************************/
956 /* GetGeometryColumn() */
957 /************************************************************************/
958
959 const char *OGROCILayer::GetGeometryColumn() const
960
961 {
962 if (pszGeomName != nullptr)
963 return pszGeomName;
964 else
965 return "";
966 }
967
968 /************************************************************************/
969 /* GetDataset() */
970 /************************************************************************/
971
972 GDALDataset *OGROCILayer::GetDataset()
973 {
974 return poDS;
975 }