Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: OpenGIS Simple Features Reference Implementation
4 : * Purpose: The OGRGeomFieldDefn class implementation.
5 : * Author: Even Rouault, <even dot rouault at spatialys.com>
6 : *
7 : ******************************************************************************
8 : * Copyright (c) 2013, Even Rouault <even dot rouault at spatialys.com>
9 : *
10 : * SPDX-License-Identifier: MIT
11 : ****************************************************************************/
12 :
13 : #include "cpl_port.h"
14 : #include "ogr_api.h"
15 :
16 : #include <cstring>
17 :
18 : #include "cpl_conv.h"
19 : #include "cpl_error.h"
20 : #include "ogr_core.h"
21 : #include "ogr_feature.h"
22 : #include "ogr_p.h"
23 : #include "ogr_spatialref.h"
24 : #include "ogr_srs_api.h"
25 : #include "ograpispy.h"
26 :
27 : /************************************************************************/
28 : /* OGRGeomFieldDefn() */
29 : /************************************************************************/
30 :
31 : /**
32 : * \brief Constructor.
33 : *
34 : * @param pszNameIn the name of the new field.
35 : * @param eGeomTypeIn the type of the new field.
36 : *
37 : * @since GDAL 1.11
38 : */
39 :
40 108881 : OGRGeomFieldDefn::OGRGeomFieldDefn(const char *pszNameIn,
41 108881 : OGRwkbGeometryType eGeomTypeIn)
42 :
43 : {
44 108881 : Initialize(pszNameIn, eGeomTypeIn);
45 108881 : }
46 :
47 : /************************************************************************/
48 : /* OGRGeomFieldDefn() */
49 : /************************************************************************/
50 :
51 : /**
52 : * \brief Constructor.
53 : *
54 : * Create by cloning an existing geometry field definition.
55 : *
56 : * @param poPrototype the geometry field definition to clone.
57 : *
58 : * @since GDAL 1.11
59 : */
60 :
61 15607 : OGRGeomFieldDefn::OGRGeomFieldDefn(const OGRGeomFieldDefn *poPrototype)
62 :
63 : {
64 15607 : Initialize(poPrototype->GetNameRef(), poPrototype->GetType());
65 15607 : const OGRSpatialReference *poSRSSrc = poPrototype->GetSpatialRef();
66 15607 : if (poSRSSrc)
67 : {
68 4011 : OGRSpatialReference *l_poSRS = poSRSSrc->Clone();
69 4011 : SetSpatialRef(l_poSRS);
70 4011 : l_poSRS->Release();
71 : }
72 15607 : SetNullable(poPrototype->IsNullable());
73 15607 : SetCoordinatePrecision(poPrototype->GetCoordinatePrecision());
74 15607 : }
75 :
76 : /************************************************************************/
77 : /* OGR_GFld_Create() */
78 : /************************************************************************/
79 : /**
80 : * \brief Create a new field geometry definition.
81 : *
82 : * This function is the same as the CPP method
83 : * OGRGeomFieldDefn::OGRGeomFieldDefn().
84 : *
85 : * @param pszName the name of the new field definition.
86 : * @param eType the type of the new field definition.
87 : * @return handle to the new field definition.
88 : *
89 : * @since GDAL 1.11
90 : */
91 :
92 172 : OGRGeomFieldDefnH OGR_GFld_Create(const char *pszName, OGRwkbGeometryType eType)
93 :
94 : {
95 172 : return OGRGeomFieldDefn::ToHandle(new OGRGeomFieldDefn(pszName, eType));
96 : }
97 :
98 : /************************************************************************/
99 : /* Initialize() */
100 : /************************************************************************/
101 :
102 : //! @cond Doxygen_Suppress
103 124488 : void OGRGeomFieldDefn::Initialize(const char *pszNameIn,
104 : OGRwkbGeometryType eTypeIn)
105 :
106 : {
107 124488 : pszName = CPLStrdup(pszNameIn);
108 124488 : eGeomType = eTypeIn;
109 124488 : }
110 :
111 : //! @endcond
112 :
113 : /************************************************************************/
114 : /* ~OGRGeomFieldDefn() */
115 : /************************************************************************/
116 :
117 200329 : OGRGeomFieldDefn::~OGRGeomFieldDefn()
118 :
119 : {
120 124468 : CPLFree(pszName);
121 :
122 124468 : if (nullptr != poSRS)
123 27162 : const_cast<OGRSpatialReference *>(poSRS)->Release();
124 200329 : }
125 :
126 : /************************************************************************/
127 : /* OGRGeomFieldDefn::OGRGeomFieldDefn() */
128 : /************************************************************************/
129 :
130 : /**
131 : * @brief OGRGeomFieldDefn::OGRGeomFieldDefn Copy constructor
132 : * @param oOther the OGRGeomFieldDefn to copy.
133 : * @since GDAL 3.11
134 : */
135 3 : OGRGeomFieldDefn::OGRGeomFieldDefn(const OGRGeomFieldDefn &oOther)
136 6 : : pszName(CPLStrdup(oOther.pszName)), eGeomType(oOther.eGeomType),
137 3 : poSRS(nullptr), bIgnore(oOther.bIgnore), bNullable(oOther.bNullable),
138 3 : m_bSealed(oOther.m_bSealed), m_oCoordPrecision(oOther.m_oCoordPrecision)
139 : {
140 3 : if (oOther.poSRS)
141 : {
142 1 : poSRS = oOther.poSRS->Clone();
143 : }
144 3 : }
145 :
146 : /************************************************************************/
147 : /* OGRGeomFieldDefn::operator=() */
148 : /************************************************************************/
149 :
150 : /**
151 : * Copy assignment operator
152 : * @param oOther the OGRGeomFieldDefn to copy.
153 : * @return a reference to the current object.
154 : * @since GDAL 3.11
155 : */
156 0 : OGRGeomFieldDefn &OGRGeomFieldDefn::operator=(const OGRGeomFieldDefn &oOther)
157 : {
158 0 : if (&oOther != this)
159 : {
160 0 : SetName(oOther.pszName);
161 0 : SetType(oOther.eGeomType);
162 0 : SetSpatialRef(oOther.poSRS);
163 0 : SetNullable(oOther.bNullable);
164 0 : m_oCoordPrecision = oOther.m_oCoordPrecision;
165 0 : m_bSealed = oOther.m_bSealed;
166 0 : bIgnore = oOther.bIgnore;
167 : }
168 0 : return *this;
169 : }
170 :
171 : /************************************************************************/
172 : /* OGR_GFld_Destroy() */
173 : /************************************************************************/
174 : /**
175 : * \brief Destroy a geometry field definition.
176 : *
177 : * @param hDefn handle to the geometry field definition to destroy.
178 : *
179 : * @since GDAL 1.11
180 : */
181 :
182 172 : void OGR_GFld_Destroy(OGRGeomFieldDefnH hDefn)
183 :
184 : {
185 172 : VALIDATE_POINTER0(hDefn, "OGR_GFld_Destroy");
186 :
187 172 : delete OGRGeomFieldDefn::FromHandle(hDefn);
188 : }
189 :
190 : /************************************************************************/
191 : /* SetName() */
192 : /************************************************************************/
193 :
194 : /**
195 : * \brief Reset the name of this field.
196 : *
197 : * This method is the same as the C function OGR_GFld_SetName().
198 : *
199 : * Note that once a OGRGeomFieldDefn has been added to a layer definition with
200 : * OGRLayer::AddGeomFieldDefn(), its setter methods should not be called on the
201 : * object returned with OGRLayer::GetLayerDefn()->GetGeomFieldDefn(). Instead,
202 : * OGRLayer::AlterGeomFieldDefn() should be called on a new instance of
203 : * OGRFieldDefn, for drivers that support AlterFieldDefn().
204 : *
205 : * @param pszNameIn the new name to apply.
206 : *
207 : * @since GDAL 1.11
208 : */
209 :
210 3482 : void OGRGeomFieldDefn::SetName(const char *pszNameIn)
211 :
212 : {
213 3482 : if (m_bSealed)
214 : {
215 5 : CPLError(CE_Failure, CPLE_AppDefined,
216 : "OGRGeomFieldDefn::SetName() not allowed on a sealed object");
217 5 : return;
218 : }
219 3477 : if (pszName != pszNameIn)
220 : {
221 3477 : CPLFree(pszName);
222 3477 : pszName = CPLStrdup(pszNameIn);
223 : }
224 : }
225 :
226 : /************************************************************************/
227 : /* OGR_GFld_SetName() */
228 : /************************************************************************/
229 : /**
230 : * \brief Reset the name of this field.
231 : *
232 : * This function is the same as the CPP method OGRGeomFieldDefn::SetName().
233 : *
234 : * Note that once a OGRGeomFieldDefn has been added to a layer definition with
235 : * OGRLayer::AddGeomFieldDefn(), its setter methods should not be called on the
236 : * object returned with OGRLayer::GetLayerDefn()->GetGeomFieldDefn(). Instead,
237 : * OGRLayer::AlterGeomFieldDefn() should be called on a new instance of
238 : * OGRFieldDefn, for drivers that support AlterFieldDefn().
239 : *
240 : * @param hDefn handle to the geometry field definition to apply the
241 : * new name to.
242 : * @param pszName the new name to apply.
243 : *
244 : * @since GDAL 1.11
245 : */
246 :
247 4 : void OGR_GFld_SetName(OGRGeomFieldDefnH hDefn, const char *pszName)
248 :
249 : {
250 4 : VALIDATE_POINTER0(hDefn, "OGR_GFld_SetName");
251 :
252 4 : OGRGeomFieldDefn::FromHandle(hDefn)->SetName(pszName);
253 : }
254 :
255 : /************************************************************************/
256 : /* GetNameRef() */
257 : /************************************************************************/
258 :
259 : /**
260 : * \fn const char *OGRGeomFieldDefn::GetNameRef();
261 : *
262 : * \brief Fetch name of this field.
263 : *
264 : * This method is the same as the C function OGR_GFld_GetNameRef().
265 : *
266 : * @return pointer to an internal name string that should not be freed or
267 : * modified.
268 : *
269 : * @since GDAL 1.11
270 : */
271 :
272 : /************************************************************************/
273 : /* OGR_GFld_GetNameRef() */
274 : /************************************************************************/
275 : /**
276 : * \brief Fetch name of this field.
277 : *
278 : * This function is the same as the CPP method OGRGeomFieldDefn::GetNameRef().
279 : *
280 : * @param hDefn handle to the geometry field definition.
281 : * @return the name of the geometry field definition.
282 : *
283 : * @since GDAL 1.11
284 : */
285 :
286 310 : const char *OGR_GFld_GetNameRef(OGRGeomFieldDefnH hDefn)
287 :
288 : {
289 310 : VALIDATE_POINTER1(hDefn, "OGR_GFld_GetNameRef", "");
290 :
291 : #ifdef OGRAPISPY_ENABLED
292 310 : if (bOGRAPISpyEnabled)
293 2 : OGRAPISpy_GFld_GetXXXX(hDefn, "GetNameRef");
294 : #endif
295 :
296 310 : return OGRGeomFieldDefn::FromHandle(hDefn)->GetNameRef();
297 : }
298 :
299 : /************************************************************************/
300 : /* GetType() */
301 : /************************************************************************/
302 :
303 : /**
304 : * \fn OGRwkbGeometryType OGRGeomFieldDefn::GetType() const;
305 : *
306 : * \brief Fetch geometry type of this field.
307 : *
308 : * This method is the same as the C function OGR_GFld_GetType().
309 : *
310 : * @return field geometry type.
311 : *
312 : * @since GDAL 1.11
313 : */
314 :
315 : /************************************************************************/
316 : /* OGR_GFld_GetType() */
317 : /************************************************************************/
318 : /**
319 : * \brief Fetch geometry type of this field.
320 : *
321 : * This function is the same as the CPP method OGRGeomFieldDefn::GetType().
322 : *
323 : * @param hDefn handle to the geometry field definition to get type from.
324 : * @return field geometry type.
325 : *
326 : * @since GDAL 1.11
327 : */
328 :
329 85 : OGRwkbGeometryType OGR_GFld_GetType(OGRGeomFieldDefnH hDefn)
330 :
331 : {
332 85 : VALIDATE_POINTER1(hDefn, "OGR_GFld_GetType", wkbUnknown);
333 :
334 : #ifdef OGRAPISPY_ENABLED
335 85 : if (bOGRAPISpyEnabled)
336 2 : OGRAPISpy_GFld_GetXXXX(hDefn, "GetType");
337 : #endif
338 :
339 85 : OGRwkbGeometryType eType = OGRGeomFieldDefn::FromHandle(hDefn)->GetType();
340 85 : if (OGR_GT_IsNonLinear(eType) && !OGRGetNonLinearGeometriesEnabledFlag())
341 : {
342 1 : eType = OGR_GT_GetLinear(eType);
343 : }
344 85 : return eType;
345 : }
346 :
347 : /************************************************************************/
348 : /* SetType() */
349 : /************************************************************************/
350 :
351 : /**
352 : * \brief Set the geometry type of this field.
353 : * This should never be done to an OGRGeomFieldDefn
354 : * that is already part of an OGRFeatureDefn.
355 : *
356 : * This method is the same as the C function OGR_GFld_SetType().
357 : *
358 : * Note that once a OGRGeomFieldDefn has been added to a layer definition with
359 : * OGRLayer::AddGeomFieldDefn(), its setter methods should not be called on the
360 : * object returned with OGRLayer::GetLayerDefn()->GetGeomFieldDefn(). Instead,
361 : * OGRLayer::AlterGeomFieldDefn() should be called on a new instance of
362 : * OGRFieldDefn, for drivers that support AlterFieldDefn().
363 : *
364 : * @param eTypeIn the new field geometry type.
365 : *
366 : * @since GDAL 1.11
367 : */
368 :
369 21579 : void OGRGeomFieldDefn::SetType(OGRwkbGeometryType eTypeIn)
370 :
371 : {
372 21579 : if (m_bSealed)
373 : {
374 1 : CPLError(CE_Failure, CPLE_AppDefined,
375 : "OGRGeomFieldDefn::SetType() not allowed on a sealed object");
376 1 : return;
377 : }
378 21578 : eGeomType = eTypeIn;
379 : }
380 :
381 : /************************************************************************/
382 : /* OGR_GFld_SetType() */
383 : /************************************************************************/
384 : /**
385 : * \brief Set the geometry type of this field.
386 : * This should never be done to an OGRGeomFieldDefn
387 : * that is already part of an OGRFeatureDefn.
388 : *
389 : * This function is the same as the CPP method OGRGeomFieldDefn::SetType().
390 : *
391 : * Note that once a OGRGeomFieldDefn has been added to a layer definition with
392 : * OGRLayer::AddGeomFieldDefn(), its setter methods should not be called on the
393 : * object returned with OGRLayer::GetLayerDefn()->GetGeomFieldDefn(). Instead,
394 : * OGRLayer::AlterGeomFieldDefn() should be called on a new instance of
395 : * OGRFieldDefn, for drivers that support AlterFieldDefn().
396 : *
397 : * @param hDefn handle to the geometry field definition to set type to.
398 : * @param eType the new field geometry type.
399 : *
400 : * @since GDAL 1.11
401 : */
402 :
403 1 : void OGR_GFld_SetType(OGRGeomFieldDefnH hDefn, OGRwkbGeometryType eType)
404 :
405 : {
406 1 : VALIDATE_POINTER0(hDefn, "OGR_GFld_SetType");
407 :
408 1 : OGRGeomFieldDefn::FromHandle(hDefn)->SetType(eType);
409 : }
410 :
411 : /************************************************************************/
412 : /* IsIgnored() */
413 : /************************************************************************/
414 :
415 : /**
416 : * \fn int OGRGeomFieldDefn::IsIgnored() const;
417 : *
418 : * \brief Return whether this field should be omitted when fetching features
419 : *
420 : * This method is the same as the C function OGR_GFld_IsIgnored().
421 : *
422 : * @return ignore state
423 : *
424 : * @since GDAL 1.11
425 : */
426 :
427 : /************************************************************************/
428 : /* OGR_GFld_IsIgnored() */
429 : /************************************************************************/
430 :
431 : /**
432 : * \brief Return whether this field should be omitted when fetching features
433 : *
434 : * This method is the same as the C++ method OGRGeomFieldDefn::IsIgnored().
435 : *
436 : * @param hDefn handle to the geometry field definition
437 : * @return ignore state
438 : *
439 : * @since GDAL 1.11
440 : */
441 :
442 6 : int OGR_GFld_IsIgnored(OGRGeomFieldDefnH hDefn)
443 : {
444 6 : VALIDATE_POINTER1(hDefn, "OGR_GFld_IsIgnored", FALSE);
445 :
446 6 : return OGRGeomFieldDefn::FromHandle(hDefn)->IsIgnored();
447 : }
448 :
449 : /************************************************************************/
450 : /* SetIgnored() */
451 : /************************************************************************/
452 :
453 : /**
454 : * \fn void OGRGeomFieldDefn::SetIgnored( int ignore );
455 : *
456 : * \brief Set whether this field should be omitted when fetching features
457 : *
458 : * This method is the same as the C function OGR_GFld_SetIgnored().
459 : *
460 : * This method should not be called on a object returned with
461 : * OGRLayer::GetLayerDefn()->GetGeomFieldDefn(). Instead, the
462 : * OGRLayer::SetIgnoredFields() method should be called.
463 : *
464 : * @param ignore ignore state
465 : *
466 : * @since GDAL 1.11
467 : */
468 :
469 : /************************************************************************/
470 : /* OGR_GFld_SetIgnored() */
471 : /************************************************************************/
472 :
473 : /**
474 : * \brief Set whether this field should be omitted when fetching features
475 : *
476 : * This method is the same as the C++ method OGRGeomFieldDefn::SetIgnored().
477 : *
478 : * This method should not be called on a object returned with
479 : * OGRLayer::GetLayerDefn()->GetGeomFieldDefn(). Instead, the
480 : * OGRLayer::SetIgnoredFields() method should be called.
481 : *
482 : * @param hDefn handle to the geometry field definition
483 : * @param ignore ignore state
484 : *
485 : * @since GDAL 1.11
486 : */
487 :
488 1 : void OGR_GFld_SetIgnored(OGRGeomFieldDefnH hDefn, int ignore)
489 : {
490 1 : VALIDATE_POINTER0(hDefn, "OGR_GFld_SetIgnored");
491 :
492 1 : OGRGeomFieldDefn::FromHandle(hDefn)->SetIgnored(ignore);
493 : }
494 :
495 : /************************************************************************/
496 : /* GetSpatialRef() */
497 : /************************************************************************/
498 : /**
499 : * \brief Fetch spatial reference system of this field.
500 : *
501 : * This method is the same as the C function OGR_GFld_GetSpatialRef().
502 : *
503 : * @return field spatial reference system.
504 : *
505 : * @since GDAL 1.11
506 : */
507 :
508 425918 : const OGRSpatialReference *OGRGeomFieldDefn::GetSpatialRef() const
509 : {
510 425918 : return poSRS;
511 : }
512 :
513 : /************************************************************************/
514 : /* OGR_GFld_GetSpatialRef() */
515 : /************************************************************************/
516 :
517 : /**
518 : * \brief Fetch spatial reference system of this field.
519 : *
520 : * This function is the same as the C++ method
521 : * OGRGeomFieldDefn::GetSpatialRef().
522 : *
523 : * @param hDefn handle to the geometry field definition
524 : *
525 : * @return a reference to the field spatial reference system.
526 : * It should not be modified.
527 : *
528 : * @since GDAL 1.11
529 : */
530 :
531 81 : OGRSpatialReferenceH OGR_GFld_GetSpatialRef(OGRGeomFieldDefnH hDefn)
532 : {
533 81 : VALIDATE_POINTER1(hDefn, "OGR_GFld_GetSpatialRef", nullptr);
534 :
535 : #ifdef OGRAPISPY_ENABLED
536 81 : if (bOGRAPISpyEnabled)
537 2 : OGRAPISpy_GFld_GetXXXX(hDefn, "GetSpatialRef");
538 : #endif
539 :
540 81 : return OGRSpatialReference::ToHandle(const_cast<OGRSpatialReference *>(
541 81 : OGRGeomFieldDefn::FromHandle(hDefn)->GetSpatialRef()));
542 : }
543 :
544 : /************************************************************************/
545 : /* SetSpatialRef() */
546 : /************************************************************************/
547 :
548 : /**
549 : * \brief Set the spatial reference of this field.
550 : *
551 : * This method is the same as the C function OGR_GFld_SetSpatialRef().
552 : *
553 : * This method drops the reference of the previously set SRS object and
554 : * acquires a new reference on the passed object (if non-NULL).
555 : *
556 : * Note that once a OGRGeomFieldDefn has been added to a layer definition with
557 : * OGRLayer::AddGeomFieldDefn(), its setter methods should not be called on the
558 : * object returned with OGRLayer::GetLayerDefn()->GetGeomFieldDefn(). Instead,
559 : * OGRLayer::AlterGeomFieldDefn() should be called on a new instance of
560 : * OGRFieldDefn, for drivers that support AlterFieldDefn().
561 : *
562 : * @param poSRSIn the new SRS to apply.
563 : *
564 : * @since GDAL 1.11
565 : */
566 39814 : void OGRGeomFieldDefn::SetSpatialRef(const OGRSpatialReference *poSRSIn)
567 : {
568 :
569 39814 : if (m_bSealed)
570 : {
571 1 : CPLError(
572 : CE_Failure, CPLE_AppDefined,
573 : "OGRGeomFieldDefn::SetSpatialRef() not allowed on a sealed object");
574 1 : return;
575 : }
576 :
577 39813 : if (poSRS == poSRSIn)
578 : {
579 13459 : return;
580 : }
581 :
582 26354 : if (poSRS != nullptr)
583 90 : const_cast<OGRSpatialReference *>(poSRS)->Release();
584 :
585 26354 : poSRS = poSRSIn;
586 :
587 26354 : if (poSRS != nullptr)
588 26344 : const_cast<OGRSpatialReference *>(poSRS)->Reference();
589 : }
590 :
591 : /************************************************************************/
592 : /* OGR_GFld_SetSpatialRef() */
593 : /************************************************************************/
594 :
595 : /**
596 : * \brief Set the spatial reference of this field.
597 : *
598 : * This function is the same as the C++ method
599 : * OGRGeomFieldDefn::SetSpatialRef().
600 : *
601 : * This function drops the reference of the previously set SRS object and
602 : * acquires a new reference on the passed object (if non-NULL).
603 : *
604 : * Note that once a OGRGeomFieldDefn has been added to a layer definition with
605 : * OGRLayer::AddGeomFieldDefn(), its setter methods should not be called on the
606 : * object returned with OGRLayer::GetLayerDefn()->GetGeomFieldDefn(). Instead,
607 : * OGRLayer::AlterGeomFieldDefn() should be called on a new instance of
608 : * OGRFieldDefn, for drivers that support AlterFieldDefn().
609 : *
610 : * @param hDefn handle to the geometry field definition
611 : * @param hSRS the new SRS to apply.
612 : *
613 : * @since GDAL 1.11
614 : */
615 :
616 48 : void OGR_GFld_SetSpatialRef(OGRGeomFieldDefnH hDefn, OGRSpatialReferenceH hSRS)
617 : {
618 48 : VALIDATE_POINTER0(hDefn, "OGR_GFld_SetSpatialRef");
619 :
620 48 : OGRGeomFieldDefn::FromHandle(hDefn)->SetSpatialRef(
621 : reinterpret_cast<OGRSpatialReference *>(hSRS));
622 : }
623 :
624 : /************************************************************************/
625 : /* IsSame() */
626 : /************************************************************************/
627 :
628 : /**
629 : * \brief Test if the geometry field definition is identical to the other one.
630 : *
631 : * @param poOtherFieldDefn the other field definition to compare to.
632 : * @return TRUE if the geometry field definition is identical to the other one.
633 : *
634 : * @since GDAL 1.11
635 : */
636 :
637 337 : int OGRGeomFieldDefn::IsSame(const OGRGeomFieldDefn *poOtherFieldDefn) const
638 : {
639 993 : if (!(strcmp(GetNameRef(), poOtherFieldDefn->GetNameRef()) == 0 &&
640 656 : GetType() == poOtherFieldDefn->GetType() &&
641 319 : IsNullable() == poOtherFieldDefn->IsNullable() &&
642 319 : m_oCoordPrecision.dfXYResolution ==
643 319 : poOtherFieldDefn->m_oCoordPrecision.dfXYResolution &&
644 317 : m_oCoordPrecision.dfZResolution ==
645 317 : poOtherFieldDefn->m_oCoordPrecision.dfZResolution &&
646 316 : m_oCoordPrecision.dfMResolution ==
647 316 : poOtherFieldDefn->m_oCoordPrecision.dfMResolution))
648 22 : return FALSE;
649 315 : const OGRSpatialReference *poMySRS = GetSpatialRef();
650 315 : const OGRSpatialReference *poOtherSRS = poOtherFieldDefn->GetSpatialRef();
651 581 : return ((poMySRS == poOtherSRS) ||
652 266 : (poMySRS != nullptr && poOtherSRS != nullptr &&
653 580 : poMySRS->IsSame(poOtherSRS)));
654 : }
655 :
656 : /************************************************************************/
657 : /* IsNullable() */
658 : /************************************************************************/
659 :
660 : /**
661 : * \fn int OGRGeomFieldDefn::IsNullable() const
662 : *
663 : * \brief Return whether this geometry field can receive null values.
664 : *
665 : * By default, fields are nullable.
666 : *
667 : * Even if this method returns FALSE (i.e not-nullable field), it doesn't mean
668 : * that OGRFeature::IsFieldSet() will necessary return TRUE, as fields can be
669 : * temporary unset and null/not-null validation is usually done when
670 : * OGRLayer::CreateFeature()/SetFeature() is called.
671 : *
672 : * Note that not-nullable geometry fields might also contain 'empty' geometries.
673 : *
674 : * This method is the same as the C function OGR_GFld_IsNullable().
675 : *
676 : * @return TRUE if the field is authorized to be null.
677 : * @since GDAL 2.0
678 : */
679 :
680 : /************************************************************************/
681 : /* OGR_GFld_IsNullable() */
682 : /************************************************************************/
683 :
684 : /**
685 : * \brief Return whether this geometry field can receive null values.
686 : *
687 : * By default, fields are nullable.
688 : *
689 : * Even if this method returns FALSE (i.e not-nullable field), it doesn't mean
690 : * that OGRFeature::IsFieldSet() will necessary return TRUE, as fields can be
691 : * temporary unset and null/not-null validation is usually done when
692 : * OGRLayer::CreateFeature()/SetFeature() is called.
693 : *
694 : * Note that not-nullable geometry fields might also contain 'empty' geometries.
695 : *
696 : * This method is the same as the C++ method OGRGeomFieldDefn::IsNullable().
697 : *
698 : * Note that once a OGRGeomFieldDefn has been added to a layer definition with
699 : * OGRLayer::AddGeomFieldDefn(), its setter methods should not be called on the
700 : * object returned with OGRLayer::GetLayerDefn()->GetGeomFieldDefn(). Instead,
701 : * OGRLayer::AlterGeomFieldDefn() should be called on a new instance of
702 : * OGRFieldDefn, for drivers that support AlterFieldDefn().
703 : *
704 : * @param hDefn handle to the field definition
705 : * @return TRUE if the field is authorized to be null.
706 : * @since GDAL 2.0
707 : */
708 :
709 85 : int OGR_GFld_IsNullable(OGRGeomFieldDefnH hDefn)
710 : {
711 85 : return OGRGeomFieldDefn::FromHandle(hDefn)->IsNullable();
712 : }
713 :
714 : /************************************************************************/
715 : /* SetNullable() */
716 : /************************************************************************/
717 :
718 : /**
719 : * \fn void OGRGeomFieldDefn::SetNullable( int bNullableIn );
720 : *
721 : * \brief Set whether this geometry field can receive null values.
722 : *
723 : * By default, fields are nullable, so this method is generally called with
724 : * FALSE to set a not-null constraint.
725 : *
726 : * Drivers that support writing not-null constraint will advertise the
727 : * GDAL_DCAP_NOTNULL_GEOMFIELDS driver metadata item.
728 : *
729 : * This method is the same as the C function OGR_GFld_SetNullable().
730 : *
731 : * Note that once a OGRGeomFieldDefn has been added to a layer definition with
732 : * OGRLayer::AddGeomFieldDefn(), its setter methods should not be called on the
733 : * object returned with OGRLayer::GetLayerDefn()->GetGeomFieldDefn(). Instead,
734 : * OGRLayer::AlterGeomFieldDefn() should be called on a new instance of
735 : * OGRFieldDefn, for drivers that support AlterFieldDefn().
736 : *
737 : * @param bNullableIn FALSE if the field must have a not-null constraint.
738 : * @since GDAL 2.0
739 : */
740 24238 : void OGRGeomFieldDefn::SetNullable(int bNullableIn)
741 : {
742 24238 : if (m_bSealed)
743 : {
744 1 : CPLError(
745 : CE_Failure, CPLE_AppDefined,
746 : "OGRGeomFieldDefn::SetNullable() not allowed on a sealed object");
747 1 : return;
748 : }
749 24237 : bNullable = bNullableIn;
750 : }
751 :
752 : /************************************************************************/
753 : /* OGR_GFld_SetNullable() */
754 : /************************************************************************/
755 :
756 : /**
757 : * \brief Set whether this geometry field can receive null values.
758 : *
759 : * By default, fields are nullable, so this method is generally called with
760 : * FALSE to set a not-null constraint.
761 : *
762 : * Drivers that support writing not-null constraint will advertise the
763 : * GDAL_DCAP_NOTNULL_GEOMFIELDS driver metadata item.
764 : *
765 : * This method is the same as the C++ method OGRGeomFieldDefn::SetNullable().
766 : *
767 : * @param hDefn handle to the field definition
768 : * @param bNullableIn FALSE if the field must have a not-null constraint.
769 : * @since GDAL 2.0
770 : */
771 :
772 23 : void OGR_GFld_SetNullable(OGRGeomFieldDefnH hDefn, int bNullableIn)
773 : {
774 23 : OGRGeomFieldDefn::FromHandle(hDefn)->SetNullable(bNullableIn);
775 23 : }
776 :
777 : /************************************************************************/
778 : /* GetCoordinatePrecision() */
779 : /************************************************************************/
780 :
781 : /**
782 : * \fn int OGRGeomFieldDefn::GetCoordinatePrecision() const
783 : *
784 : * \brief Return the coordinate precision associated to this geometry field.
785 : *
786 : * This method is the same as the C function OGR_GFld_GetCoordinatePrecision().
787 : *
788 : * @return the coordinate precision
789 : * @since GDAL 3.9
790 : */
791 :
792 : /************************************************************************/
793 : /* OGR_GFld_GetCoordinatePrecision() */
794 : /************************************************************************/
795 :
796 : /**
797 : * \brief Return the coordinate precision associated to this geometry field.
798 : *
799 : * This method is the same as the C++ method OGRGeomFieldDefn::GetCoordinatePrecision()
800 : *
801 : * @param hDefn handle to the field definition
802 : * @return the coordinate precision
803 : * @since GDAL 3.9
804 : */
805 :
806 : OGRGeomCoordinatePrecisionH
807 44 : OGR_GFld_GetCoordinatePrecision(OGRGeomFieldDefnH hDefn)
808 : {
809 : return const_cast<OGRGeomCoordinatePrecision *>(
810 44 : &(OGRGeomFieldDefn::FromHandle(hDefn)->GetCoordinatePrecision()));
811 : }
812 :
813 : /************************************************************************/
814 : /* SetCoordinatePrecision() */
815 : /************************************************************************/
816 :
817 : /**
818 : * \brief Set coordinate precision associated to this geometry field.
819 : *
820 : * This method is the same as the C function OGR_GFld_SetCoordinatePrecision().
821 : *
822 : * Note that once a OGRGeomFieldDefn has been added to a layer definition with
823 : * OGRLayer::AddGeomFieldDefn(), its setter methods should not be called on the
824 : * object returned with OGRLayer::GetLayerDefn()->GetGeomFieldDefn().
825 : *
826 : * @param prec Coordinate precision
827 : * @since GDAL 3.9
828 : */
829 21858 : void OGRGeomFieldDefn::SetCoordinatePrecision(
830 : const OGRGeomCoordinatePrecision &prec)
831 : {
832 21858 : if (m_bSealed)
833 : {
834 0 : CPLError(CE_Failure, CPLE_AppDefined,
835 : "OGRGeomFieldDefn::SetCoordinatePrecision() not allowed on a "
836 : "sealed object");
837 0 : return;
838 : }
839 21858 : m_oCoordPrecision = prec;
840 : }
841 :
842 : /************************************************************************/
843 : /* OGR_GFld_SetCoordinatePrecision() */
844 : /************************************************************************/
845 :
846 : /**
847 : * \brief Set coordinate precision associated to this geometry field.
848 : *
849 : * This method is the same as the C++ method OGRGeomFieldDefn::SetCoordinatePrecision()
850 : *
851 : * Note that once a OGRGeomFieldDefn has been added to a layer definition with
852 : * OGRLayer::AddGeomFieldDefn(), its setter methods should not be called on the
853 : * object returned with OGRLayer::GetLayerDefn()->GetGeomFieldDefn().
854 : *
855 : * @param hDefn handle to the field definition. Must not be NULL.
856 : * @param hGeomCoordPrec Coordinate precision. Must not be NULL.
857 : * @since GDAL 3.9
858 : */
859 19 : void OGR_GFld_SetCoordinatePrecision(OGRGeomFieldDefnH hDefn,
860 : OGRGeomCoordinatePrecisionH hGeomCoordPrec)
861 : {
862 19 : VALIDATE_POINTER0(hGeomCoordPrec, "OGR_GFld_SetCoordinatePrecision");
863 19 : OGRGeomFieldDefn::FromHandle(hDefn)->SetCoordinatePrecision(
864 : *hGeomCoordPrec);
865 : }
866 :
867 : /************************************************************************/
868 : /* OGRGeomFieldDefn::Seal() */
869 : /************************************************************************/
870 :
871 : /** Seal a OGRGeomFieldDefn.
872 : *
873 : * A sealed OGRGeomFieldDefn can not be modified while it is sealed.
874 : *
875 : * This method should only be called by driver implementations.
876 : *
877 : * @since GDAL 3.9
878 : */
879 28747 : void OGRGeomFieldDefn::Seal()
880 : {
881 28747 : m_bSealed = true;
882 28747 : }
883 :
884 : /************************************************************************/
885 : /* OGRGeomFieldDefn::Unseal() */
886 : /************************************************************************/
887 :
888 : /** Unseal a OGRGeomFieldDefn.
889 : *
890 : * Undo OGRGeomFieldDefn::Seal()
891 : *
892 : * Using GetTemporaryUnsealer() is recommended for most use cases.
893 : *
894 : * This method should only be called by driver implementations.
895 : *
896 : * @since GDAL 3.9
897 : */
898 17387 : void OGRGeomFieldDefn::Unseal()
899 : {
900 17387 : m_bSealed = false;
901 17387 : }
902 :
903 : /************************************************************************/
904 : /* OGRGeomFieldDefn::GetTemporaryUnsealer() */
905 : /************************************************************************/
906 :
907 : /** Return an object that temporary unseals the OGRGeomFieldDefn
908 : *
909 : * The returned object calls Unseal() initially, and when it is destroyed
910 : * it calls Seal().
911 : *
912 : * This method should only be called by driver implementations.
913 : *
914 : * It is also possible to use the helper method whileUnsealing(). Example:
915 : * whileUnsealing(poGeomFieldDefn)->some_method()
916 : *
917 : * @since GDAL 3.9
918 : */
919 602 : OGRGeomFieldDefn::TemporaryUnsealer OGRGeomFieldDefn::GetTemporaryUnsealer()
920 : {
921 602 : return TemporaryUnsealer(this);
922 : }
|