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