1 : /******************************************************************************
2 : * $Id: ogrfielddefn.cpp 24286 2012-04-21 19:17:26Z rouault $
3 : *
4 : * Project: OpenGIS Simple Features Reference Implementation
5 : * Purpose: The OGRFieldDefn class implementation.
6 : * Author: Frank Warmerdam, warmerda@home.com
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 1999, Les Technologies SoftMap Inc.
10 : *
11 : * Permission is hereby granted, free of charge, to any person obtaining a
12 : * copy of this software and associated documentation files (the "Software"),
13 : * to deal in the Software without restriction, including without limitation
14 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 : * and/or sell copies of the Software, and to permit persons to whom the
16 : * Software is furnished to do so, subject to the following conditions:
17 : *
18 : * The above copyright notice and this permission notice shall be included
19 : * in all copies or substantial portions of the Software.
20 : *
21 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 : * DEALINGS IN THE SOFTWARE.
28 : ****************************************************************************/
29 :
30 : #include "ogr_feature.h"
31 : #include "ogr_api.h"
32 : #include "ogr_p.h"
33 :
34 : CPL_CVSID("$Id: ogrfielddefn.cpp 24286 2012-04-21 19:17:26Z rouault $");
35 :
36 : /************************************************************************/
37 : /* OGRFieldDefn() */
38 : /************************************************************************/
39 :
40 : /**
41 : * \brief Constructor.
42 : *
43 : * @param pszNameIn the name of the new field.
44 : * @param eTypeIn the type of the new field.
45 : */
46 :
47 33981 : OGRFieldDefn::OGRFieldDefn( const char * pszNameIn, OGRFieldType eTypeIn )
48 :
49 : {
50 33981 : Initialize( pszNameIn, eTypeIn );
51 33981 : }
52 :
53 : /************************************************************************/
54 : /* OGRFieldDefn() */
55 : /************************************************************************/
56 :
57 : /**
58 : * \brief Constructor.
59 : *
60 : * Create by cloning an existing field definition.
61 : *
62 : * @param poPrototype the field definition to clone.
63 : */
64 :
65 43845 : OGRFieldDefn::OGRFieldDefn( OGRFieldDefn *poPrototype )
66 :
67 : {
68 43845 : Initialize( poPrototype->GetNameRef(), poPrototype->GetType() );
69 :
70 43845 : SetJustify( poPrototype->GetJustify() );
71 43845 : SetWidth( poPrototype->GetWidth() );
72 43845 : SetPrecision( poPrototype->GetPrecision() );
73 : // SetDefault( poPrototype->GetDefaultRef() );
74 43845 : bIgnore = FALSE;
75 43845 : }
76 :
77 : /************************************************************************/
78 : /* OGR_Fld_Create() */
79 : /************************************************************************/
80 : /**
81 : * \brief Create a new field definition.
82 : *
83 : * This function is the same as the CPP method OGRFieldDefn::OGRFieldDefn().
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 :
90 4841 : OGRFieldDefnH OGR_Fld_Create( const char *pszName, OGRFieldType eType )
91 :
92 : {
93 4841 : return (OGRFieldDefnH) (new OGRFieldDefn(pszName,eType));
94 : }
95 :
96 : /************************************************************************/
97 : /* Initialize() */
98 : /************************************************************************/
99 :
100 77826 : void OGRFieldDefn::Initialize( const char * pszNameIn, OGRFieldType eTypeIn )
101 :
102 : {
103 77826 : pszName = CPLStrdup( pszNameIn );
104 77826 : eType = eTypeIn;
105 77826 : eJustify = OJUndefined;
106 :
107 77826 : nWidth = 0; // should these be defined in some particular way
108 77826 : nPrecision = 0; // for numbers?
109 :
110 77826 : memset( &uDefault, 0, sizeof(OGRField) );
111 77826 : }
112 :
113 : /************************************************************************/
114 : /* ~OGRFieldDefn() */
115 : /************************************************************************/
116 :
117 77826 : OGRFieldDefn::~OGRFieldDefn()
118 :
119 : {
120 77826 : CPLFree( pszName );
121 77826 : }
122 :
123 : /************************************************************************/
124 : /* OGR_Fld_Destroy() */
125 : /************************************************************************/
126 : /**
127 : * \brief Destroy a field definition.
128 : *
129 : * @param hDefn handle to the field definition to destroy.
130 : */
131 :
132 4841 : void OGR_Fld_Destroy( OGRFieldDefnH hDefn )
133 :
134 : {
135 4841 : delete (OGRFieldDefn *) hDefn;
136 4841 : }
137 :
138 : /************************************************************************/
139 : /* SetName() */
140 : /************************************************************************/
141 :
142 : /**
143 : * \brief Reset the name of this field.
144 : *
145 : * This method is the same as the C function OGR_Fld_SetName().
146 : *
147 : * @param pszNameIn the new name to apply.
148 : */
149 :
150 13930 : void OGRFieldDefn::SetName( const char * pszNameIn )
151 :
152 : {
153 13930 : CPLFree( pszName );
154 13930 : pszName = CPLStrdup( pszNameIn );
155 13930 : }
156 :
157 : /************************************************************************/
158 : /* OGR_Fld_SetName() */
159 : /************************************************************************/
160 : /**
161 : * \brief Reset the name of this field.
162 : *
163 : * This function is the same as the CPP method OGRFieldDefn::SetName().
164 : *
165 : * @param hDefn handle to the field definition to apply the new name to.
166 : * @param pszName the new name to apply.
167 : */
168 :
169 1 : void OGR_Fld_SetName( OGRFieldDefnH hDefn, const char *pszName )
170 :
171 : {
172 1 : ((OGRFieldDefn *) hDefn)->SetName( pszName );
173 1 : }
174 :
175 : /************************************************************************/
176 : /* GetNameRef() */
177 : /************************************************************************/
178 :
179 : /**
180 : * \fn const char *OGRFieldDefn::GetNameRef();
181 : *
182 : * \brief Fetch name of this field.
183 : *
184 : * This method is the same as the C function OGR_Fld_GetNameRef().
185 : *
186 : * @return pointer to an internal name string that should not be freed or
187 : * modified.
188 : */
189 :
190 : /************************************************************************/
191 : /* OGR_Fld_GetNameRef() */
192 : /************************************************************************/
193 : /**
194 : * \brief Fetch name of this field.
195 : *
196 : * This function is the same as the CPP method OGRFieldDefn::GetNameRef().
197 : *
198 : * @param hDefn handle to the field definition.
199 : * @return the name of the field definition.
200 : *
201 : */
202 :
203 803 : const char *OGR_Fld_GetNameRef( OGRFieldDefnH hDefn )
204 :
205 : {
206 803 : return ((OGRFieldDefn *) hDefn)->GetNameRef();
207 : }
208 :
209 : /************************************************************************/
210 : /* GetType() */
211 : /************************************************************************/
212 :
213 : /**
214 : * \fn OGRFieldType OGRFieldDefn::GetType();
215 : *
216 : * \brief Fetch type of this field.
217 : *
218 : * This method is the same as the C function OGR_Fld_GetType().
219 : *
220 : * @return field type.
221 : */
222 :
223 : /************************************************************************/
224 : /* OGR_Fld_GetType() */
225 : /************************************************************************/
226 : /**
227 : * \brief Fetch type of this field.
228 : *
229 : * This function is the same as the CPP method OGRFieldDefn::GetType().
230 : *
231 : * @param hDefn handle to the field definition to get type from.
232 : * @return field type.
233 : */
234 :
235 4697 : OGRFieldType OGR_Fld_GetType( OGRFieldDefnH hDefn )
236 :
237 : {
238 4697 : return ((OGRFieldDefn *) hDefn)->GetType();
239 : }
240 :
241 : /************************************************************************/
242 : /* SetType() */
243 : /************************************************************************/
244 :
245 : /**
246 : * \fn void OGRFieldDefn::SetType( OGRFieldType eType );
247 : *
248 : * \brief Set the type of this field.
249 : * This should never be done to an OGRFieldDefn
250 : * that is already part of an OGRFeatureDefn.
251 : *
252 : * This method is the same as the C function OGR_Fld_SetType().
253 : *
254 : * @param eType the new field type.
255 : */
256 :
257 : /************************************************************************/
258 : /* OGR_Fld_SetType() */
259 : /************************************************************************/
260 : /**
261 : * \brief Set the type of this field.
262 : * This should never be done to an OGRFieldDefn
263 : * that is already part of an OGRFeatureDefn.
264 : *
265 : * This function is the same as the CPP method OGRFieldDefn::SetType().
266 : *
267 : * @param hDefn handle to the field definition to set type to.
268 : * @param eType the new field type.
269 : */
270 :
271 0 : void OGR_Fld_SetType( OGRFieldDefnH hDefn, OGRFieldType eType )
272 :
273 : {
274 0 : ((OGRFieldDefn *) hDefn)->SetType( eType );
275 0 : }
276 :
277 : /************************************************************************/
278 : /* SetDefault() */
279 : /************************************************************************/
280 :
281 : /**
282 : * \brief Set default field value.
283 : *
284 : * Currently use of OGRFieldDefn "defaults" is discouraged. This feature
285 : * may be fleshed out in the future.
286 : *
287 : */
288 :
289 0 : void OGRFieldDefn::SetDefault( const OGRField * puDefaultIn )
290 :
291 : {
292 0 : switch( eType )
293 : {
294 : case OFTInteger:
295 : case OFTReal:
296 0 : uDefault = *puDefaultIn;
297 0 : break;
298 :
299 : case OFTString:
300 : // CPLFree( uDefault.String );
301 : // uDefault.String = CPLStrdup( puDefaultIn->String );
302 0 : break;
303 :
304 : default:
305 : // add handling for other complex types.
306 0 : CPLAssert( FALSE );
307 : break;
308 : }
309 0 : }
310 :
311 : /************************************************************************/
312 : /* GetFieldTypeName() */
313 : /************************************************************************/
314 :
315 : /**
316 : * \brief Fetch human readable name for a field type.
317 : *
318 : * This static method is the same as the C function OGR_GetFieldTypeName().
319 : *
320 : * @param eType the field type to get name for.
321 : *
322 : * @return pointer to an internal static name string. It should not be
323 : * modified or freed.
324 : */
325 :
326 559 : const char * OGRFieldDefn::GetFieldTypeName( OGRFieldType eType )
327 :
328 : {
329 559 : switch( eType )
330 : {
331 : case OFTInteger:
332 13 : return "Integer";
333 :
334 : case OFTReal:
335 337 : return "Real";
336 :
337 : case OFTString:
338 190 : return "String";
339 :
340 : case OFTIntegerList:
341 6 : return "IntegerList";
342 :
343 : case OFTRealList:
344 3 : return "RealList";
345 :
346 : case OFTStringList:
347 1 : return "StringList";
348 :
349 : case OFTBinary:
350 1 : return "Binary";
351 :
352 : case OFTDate:
353 2 : return "Date";
354 :
355 : case OFTTime:
356 2 : return "Time";
357 :
358 : case OFTDateTime:
359 2 : return "DateTime";
360 :
361 : default:
362 2 : return "(unknown)";
363 : }
364 : }
365 :
366 : /************************************************************************/
367 : /* OGR_GetFieldTypeName() */
368 : /************************************************************************/
369 : /**
370 : * \brief Fetch human readable name for a field type.
371 : *
372 : * This function is the same as the CPP method
373 : * OGRFieldDefn::GetFieldTypeName().
374 : *
375 : * @param eType the field type to get name for.
376 : * @return the name.
377 : */
378 :
379 326 : const char *OGR_GetFieldTypeName( OGRFieldType eType )
380 :
381 : {
382 326 : return OGRFieldDefn::GetFieldTypeName( eType );
383 : }
384 :
385 : /************************************************************************/
386 : /* GetJustify() */
387 : /************************************************************************/
388 :
389 : /**
390 : * \fn OGRJustification OGRFieldDefn::GetJustify();
391 : *
392 : * \brief Get the justification for this field.
393 : *
394 : * This method is the same as the C function OGR_Fld_GetJustify().
395 : *
396 : * @return the justification.
397 : */
398 :
399 : /************************************************************************/
400 : /* OGR_Fld_GetJustify() */
401 : /************************************************************************/
402 : /**
403 : * \brief Get the justification for this field.
404 : *
405 : * This function is the same as the CPP method OGRFieldDefn::GetJustify().
406 : *
407 : * @param hDefn handle to the field definition to get justification from.
408 : * @return the justification.
409 : */
410 :
411 0 : OGRJustification OGR_Fld_GetJustify( OGRFieldDefnH hDefn )
412 :
413 : {
414 0 : return ((OGRFieldDefn *) hDefn)->GetJustify();
415 : }
416 :
417 : /************************************************************************/
418 : /* SetJustify() */
419 : /************************************************************************/
420 :
421 : /**
422 : * \fn void OGRFieldDefn::SetJustify( OGRJustification eJustify );
423 : *
424 : * \brief Set the justification for this field.
425 : *
426 : * This method is the same as the C function OGR_Fld_SetJustify().
427 : *
428 : * @param eJustify the new justification.
429 : */
430 :
431 : /************************************************************************/
432 : /* OGR_Fld_SetJustify() */
433 : /************************************************************************/
434 : /**
435 : * \brief Set the justification for this field.
436 : *
437 : * This function is the same as the CPP method OGRFieldDefn::SetJustify().
438 : *
439 : * @param hDefn handle to the field definition to set justification to.
440 : * @param eJustify the new justification.
441 : */
442 :
443 0 : void OGR_Fld_SetJustify( OGRFieldDefnH hDefn, OGRJustification eJustify )
444 :
445 : {
446 0 : ((OGRFieldDefn *) hDefn)->SetJustify( eJustify );
447 0 : }
448 :
449 : /************************************************************************/
450 : /* GetWidth() */
451 : /************************************************************************/
452 :
453 : /**
454 : * \fn int OGRFieldDefn::GetWidth();
455 : *
456 : * \brief Get the formatting width for this field.
457 : *
458 : * This method is the same as the C function OGR_Fld_GetWidth().
459 : *
460 : * @return the width, zero means no specified width.
461 : */
462 :
463 : /************************************************************************/
464 : /* OGR_Fld_GetWidth() */
465 : /************************************************************************/
466 : /**
467 : * \brief Get the formatting width for this field.
468 : *
469 : * This function is the same as the CPP method OGRFieldDefn::GetWidth().
470 : *
471 : * @param hDefn handle to the field definition to get width from.
472 : * @return the width, zero means no specified width.
473 : */
474 :
475 520 : int OGR_Fld_GetWidth( OGRFieldDefnH hDefn )
476 :
477 : {
478 520 : return ((OGRFieldDefn *) hDefn)->GetWidth();
479 : }
480 :
481 : /************************************************************************/
482 : /* SetWidth() */
483 : /************************************************************************/
484 :
485 : /**
486 : * \fn void OGRFieldDefn::SetWidth( int nWidth );
487 : *
488 : * \brief Set the formatting width for this field in characters.
489 : *
490 : * This method is the same as the C function OGR_Fld_SetWidth().
491 : *
492 : * @param nWidth the new width.
493 : */
494 :
495 : /************************************************************************/
496 : /* OGR_Fld_SetWidth() */
497 : /************************************************************************/
498 : /**
499 : * \brief Set the formatting width for this field in characters.
500 : *
501 : * This function is the same as the CPP method OGRFieldDefn::SetWidth().
502 : *
503 : * @param hDefn handle to the field definition to set width to.
504 : * @param nNewWidth the new width.
505 : */
506 :
507 204 : void OGR_Fld_SetWidth( OGRFieldDefnH hDefn, int nNewWidth )
508 :
509 : {
510 204 : ((OGRFieldDefn *) hDefn)->SetWidth( nNewWidth );
511 204 : }
512 :
513 : /************************************************************************/
514 : /* GetPrecision() */
515 : /************************************************************************/
516 :
517 : /**
518 : * \fn int OGRFieldDefn::GetPrecision();
519 : *
520 : * \brief Get the formatting precision for this field.
521 : * This should normally be
522 : * zero for fields of types other than OFTReal.
523 : *
524 : * This method is the same as the C function OGR_Fld_GetPrecision().
525 : *
526 : * @return the precision.
527 : */
528 :
529 : /************************************************************************/
530 : /* OGR_Fld_GetPrecision() */
531 : /************************************************************************/
532 : /**
533 : * \brief Get the formatting precision for this field.
534 : * This should normally be
535 : * zero for fields of types other than OFTReal.
536 : *
537 : * This function is the same as the CPP method OGRFieldDefn::GetPrecision().
538 : *
539 : * @param hDefn handle to the field definition to get precision from.
540 : * @return the precision.
541 : */
542 :
543 210 : int OGR_Fld_GetPrecision( OGRFieldDefnH hDefn )
544 :
545 : {
546 210 : return ((OGRFieldDefn *) hDefn)->GetPrecision();
547 : }
548 :
549 : /************************************************************************/
550 : /* SetPrecision() */
551 : /************************************************************************/
552 :
553 : /**
554 : * \fn void OGRFieldDefn::SetPrecision( int nPrecision );
555 : *
556 : * \brief Set the formatting precision for this field in characters.
557 : *
558 : * This should normally be zero for fields of types other than OFTReal.
559 : *
560 : * This method is the same as the C function OGR_Fld_SetPrecision().
561 : *
562 : * @param nPrecision the new precision.
563 : */
564 :
565 : /************************************************************************/
566 : /* OGR_Fld_SetPrecision() */
567 : /************************************************************************/
568 : /**
569 : * \brief Set the formatting precision for this field in characters.
570 : *
571 : * This should normally be zero for fields of types other than OFTReal.
572 : *
573 : * This function is the same as the CPP method OGRFieldDefn::SetPrecision().
574 : *
575 : * @param hDefn handle to the field definition to set precision to.
576 : * @param nPrecision the new precision.
577 : */
578 :
579 133 : void OGR_Fld_SetPrecision( OGRFieldDefnH hDefn, int nPrecision )
580 :
581 : {
582 133 : ((OGRFieldDefn *) hDefn)->SetPrecision( nPrecision );
583 133 : }
584 :
585 : /************************************************************************/
586 : /* Set() */
587 : /************************************************************************/
588 :
589 : /**
590 : * \brief Set defining parameters for a field in one call.
591 : *
592 : * This method is the same as the C function OGR_Fld_Set().
593 : *
594 : * @param pszNameIn the new name to assign.
595 : * @param eTypeIn the new type (one of the OFT values like OFTInteger).
596 : * @param nWidthIn the preferred formatting width. Defaults to zero indicating
597 : * undefined.
598 : * @param nPrecisionIn number of decimals places for formatting, defaults to
599 : * zero indicating undefined.
600 : * @param eJustifyIn the formatting justification (OJLeft or OJRight), defaults
601 : * to OJUndefined.
602 : */
603 :
604 4938 : void OGRFieldDefn::Set( const char *pszNameIn,
605 : OGRFieldType eTypeIn,
606 : int nWidthIn, int nPrecisionIn,
607 : OGRJustification eJustifyIn )
608 : {
609 4938 : SetName( pszNameIn );
610 4938 : SetType( eTypeIn );
611 4938 : SetWidth( nWidthIn );
612 4938 : SetPrecision( nPrecisionIn );
613 4938 : SetJustify( eJustifyIn );
614 4938 : }
615 :
616 : /************************************************************************/
617 : /* OGR_Fld_Set() */
618 : /************************************************************************/
619 : /**
620 : * \brief Set defining parameters for a field in one call.
621 : *
622 : * This function is the same as the CPP method OGRFieldDefn::Set().
623 : *
624 : * @param hDefn handle to the field definition to set to.
625 : * @param pszNameIn the new name to assign.
626 : * @param eTypeIn the new type (one of the OFT values like OFTInteger).
627 : * @param nWidthIn the preferred formatting width. Defaults to zero indicating
628 : * undefined.
629 : * @param nPrecisionIn number of decimals places for formatting, defaults to
630 : * zero indicating undefined.
631 : * @param eJustifyIn the formatting justification (OJLeft or OJRight), defaults
632 : * to OJUndefined.
633 : */
634 :
635 0 : void OGR_Fld_Set( OGRFieldDefnH hDefn, const char *pszNameIn,
636 : OGRFieldType eTypeIn,
637 : int nWidthIn, int nPrecisionIn,
638 : OGRJustification eJustifyIn )
639 :
640 : {
641 : ((OGRFieldDefn *) hDefn)->Set( pszNameIn, eTypeIn, nWidthIn,
642 0 : nPrecisionIn, eJustifyIn );
643 0 : }
644 :
645 : /************************************************************************/
646 : /* IsIgnored() */
647 : /************************************************************************/
648 :
649 : /**
650 : * \fn int OGRFieldDefn::IsIgnored();
651 : *
652 : * \brief Return whether this field should be omitted when fetching features
653 : *
654 : * This method is the same as the C function OGR_Fld_IsIgnored().
655 : *
656 : * @return ignore state
657 : */
658 :
659 : /************************************************************************/
660 : /* OGR_Fld_IsIgnored() */
661 : /************************************************************************/
662 :
663 : /**
664 : * \brief Return whether this field should be omitted when fetching features
665 : *
666 : * This method is the same as the C++ method OGRFieldDefn::IsIgnored().
667 : *
668 : * @param hDefn handle to the field definition
669 : * @return ignore state
670 : */
671 :
672 6 : int OGR_Fld_IsIgnored( OGRFieldDefnH hDefn )
673 : {
674 6 : return ((OGRFieldDefn *) hDefn)->IsIgnored();
675 : }
676 :
677 : /************************************************************************/
678 : /* SetIgnored() */
679 : /************************************************************************/
680 :
681 : /**
682 : * \fn void OGRFieldDefn::SetIgnored( int ignore );
683 : *
684 : * \brief Set whether this field should be omitted when fetching features
685 : *
686 : * This method is the same as the C function OGR_Fld_SetIgnored().
687 : *
688 : * @param ignore ignore state
689 : */
690 :
691 : /************************************************************************/
692 : /* OGR_Fld_SetIgnored() */
693 : /************************************************************************/
694 :
695 : /**
696 : * \brief Set whether this field should be omitted when fetching features
697 : *
698 : * This method is the same as the C function OGRFieldDefn::SetIgnored().
699 : *
700 : * @param hDefn handle to the field definition
701 : * @param ignore ignore state
702 : */
703 :
704 0 : void OGR_Fld_SetIgnored( OGRFieldDefnH hDefn, int ignore )
705 : {
706 0 : ((OGRFieldDefn *) hDefn)->SetIgnored( ignore );
707 0 : }
708 :
709 : /************************************************************************/
710 : /* IsSame() */
711 : /************************************************************************/
712 :
713 : /**
714 : * \brief Test if the field definition is identical to the other one.
715 : *
716 : * @param poOtherFieldDefn the other field definition to compare to.
717 : * @return TRUE if the field definition is identical to the other one.
718 : */
719 :
720 196 : int OGRFieldDefn::IsSame( const OGRFieldDefn * poOtherFieldDefn ) const
721 : {
722 : return (strcmp(pszName, poOtherFieldDefn->pszName) == 0 &&
723 : eType == poOtherFieldDefn->eType &&
724 : nWidth == poOtherFieldDefn->nWidth &&
725 196 : nPrecision == poOtherFieldDefn->nPrecision);
726 : }
|