1 : /******************************************************************************
2 : * $Id: ogrfielddefn.cpp 16574 2009-03-14 13:09:10Z 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 16574 2009-03-14 13:09:10Z 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 24473 : OGRFieldDefn::OGRFieldDefn( const char * pszNameIn, OGRFieldType eTypeIn )
48 :
49 : {
50 24473 : Initialize( pszNameIn, eTypeIn );
51 24473 : }
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 21500 : OGRFieldDefn::OGRFieldDefn( OGRFieldDefn *poPrototype )
66 :
67 : {
68 21500 : Initialize( poPrototype->GetNameRef(), poPrototype->GetType() );
69 :
70 21500 : SetJustify( poPrototype->GetJustify() );
71 21500 : SetWidth( poPrototype->GetWidth() );
72 21500 : SetPrecision( poPrototype->GetPrecision() );
73 : // SetDefault( poPrototype->GetDefaultRef() );
74 21500 : }
75 :
76 : /************************************************************************/
77 : /* OGR_Fld_Create() */
78 : /************************************************************************/
79 : /**
80 : * \brief Create a new field definition.
81 : *
82 : * This function is the same as the CPP method OGRFieldDefn::OGRFieldDefn().
83 : *
84 : * @param pszName the name of the new field definition.
85 : * @param eType the type of the new field definition.
86 : * @return handle to the new field definition.
87 : */
88 :
89 267 : OGRFieldDefnH OGR_Fld_Create( const char *pszName, OGRFieldType eType )
90 :
91 : {
92 267 : return (OGRFieldDefnH) (new OGRFieldDefn(pszName,eType));
93 : }
94 :
95 : /************************************************************************/
96 : /* Initialize() */
97 : /************************************************************************/
98 :
99 45973 : void OGRFieldDefn::Initialize( const char * pszNameIn, OGRFieldType eTypeIn )
100 :
101 : {
102 45973 : pszName = CPLStrdup( pszNameIn );
103 45973 : eType = eTypeIn;
104 45973 : eJustify = OJUndefined;
105 :
106 45973 : nWidth = 0; // should these be defined in some particular way
107 45973 : nPrecision = 0; // for numbers?
108 :
109 45973 : memset( &uDefault, 0, sizeof(OGRField) );
110 45973 : }
111 :
112 : /************************************************************************/
113 : /* ~OGRFieldDefn() */
114 : /************************************************************************/
115 :
116 45973 : OGRFieldDefn::~OGRFieldDefn()
117 :
118 : {
119 45973 : CPLFree( pszName );
120 45973 : }
121 :
122 : /************************************************************************/
123 : /* OGR_Fld_Destroy() */
124 : /************************************************************************/
125 : /**
126 : * \brief Destroy a field definition.
127 : *
128 : * @param hDefn handle to the field definition to destroy.
129 : */
130 :
131 267 : void OGR_Fld_Destroy( OGRFieldDefnH hDefn )
132 :
133 : {
134 267 : delete (OGRFieldDefn *) hDefn;
135 267 : }
136 :
137 : /************************************************************************/
138 : /* SetName() */
139 : /************************************************************************/
140 :
141 : /**
142 : * \brief Reset the name of this field.
143 : *
144 : * This method is the same as the C function OGR_Fld_SetName().
145 : *
146 : * @param pszNameIn the new name to apply.
147 : */
148 :
149 2745 : void OGRFieldDefn::SetName( const char * pszNameIn )
150 :
151 : {
152 2745 : CPLFree( pszName );
153 2745 : pszName = CPLStrdup( pszNameIn );
154 2745 : }
155 :
156 : /************************************************************************/
157 : /* OGR_Fld_SetName() */
158 : /************************************************************************/
159 : /**
160 : * \brief Reset the name of this field.
161 : *
162 : * This function is the same as the CPP method OGRFieldDefn::SetName().
163 : *
164 : * @param hDefn handle to the field definition to apply the new name to.
165 : * @param pszName the new name to apply.
166 : */
167 :
168 1 : void OGR_Fld_SetName( OGRFieldDefnH hDefn, const char *pszName )
169 :
170 : {
171 1 : ((OGRFieldDefn *) hDefn)->SetName( pszName );
172 1 : }
173 :
174 : /************************************************************************/
175 : /* GetNameRef() */
176 : /************************************************************************/
177 :
178 : /**
179 : * \fn const char *OGRFieldDefn::GetNameRef();
180 : *
181 : * \brief Fetch name of this field.
182 : *
183 : * This method is the same as the C function OGR_Fld_GetNameRef().
184 : *
185 : * @return pointer to an internal name string that should not be freed or
186 : * modified.
187 : */
188 :
189 : /************************************************************************/
190 : /* OGR_Fld_GetNameRef() */
191 : /************************************************************************/
192 : /**
193 : * \brief Fetch name of this field.
194 : *
195 : * This function is the same as the CPP method OGRFieldDefn::GetNameRef().
196 : *
197 : * @param hDefn handle to the field definition.
198 : * @return the name of the field definition.
199 : *
200 : */
201 :
202 254 : const char *OGR_Fld_GetNameRef( OGRFieldDefnH hDefn )
203 :
204 : {
205 254 : return ((OGRFieldDefn *) hDefn)->GetNameRef();
206 : }
207 :
208 : /************************************************************************/
209 : /* GetType() */
210 : /************************************************************************/
211 :
212 : /**
213 : * \fn OGRFieldType OGRFieldDefn::GetType();
214 : *
215 : * \brief Fetch type of this field.
216 : *
217 : * This method is the same as the C function OGR_Fld_GetType().
218 : *
219 : * @return field type.
220 : */
221 :
222 : /************************************************************************/
223 : /* OGR_Fld_GetType() */
224 : /************************************************************************/
225 : /**
226 : * \brief Fetch type of this field.
227 : *
228 : * This function is the same as the CPP method OGRFieldDefn::GetType().
229 : *
230 : * @param hDefn handle to the field definition to get type from.
231 : * @return field type.
232 : */
233 :
234 1762 : OGRFieldType OGR_Fld_GetType( OGRFieldDefnH hDefn )
235 :
236 : {
237 1762 : return ((OGRFieldDefn *) hDefn)->GetType();
238 : }
239 :
240 : /************************************************************************/
241 : /* SetType() */
242 : /************************************************************************/
243 :
244 : /**
245 : * \fn void OGRFieldDefn::SetType( OGRFieldType eType );
246 : *
247 : * \brief Set the type of this field.
248 : * This should never be done to an OGRFieldDefn
249 : * that is already part of an OGRFeatureDefn.
250 : *
251 : * This method is the same as the C function OGR_Fld_SetType().
252 : *
253 : * @param eType the new field type.
254 : */
255 :
256 : /************************************************************************/
257 : /* OGR_Fld_SetType() */
258 : /************************************************************************/
259 : /**
260 : * \brief Set the type of this field.
261 : * This should never be done to an OGRFieldDefn
262 : * that is already part of an OGRFeatureDefn.
263 : *
264 : * This function is the same as the CPP method OGRFieldDefn::SetType().
265 : *
266 : * @param hDefn handle to the field definition to set type to.
267 : * @param eType the new field type.
268 : */
269 :
270 0 : void OGR_Fld_SetType( OGRFieldDefnH hDefn, OGRFieldType eType )
271 :
272 : {
273 0 : ((OGRFieldDefn *) hDefn)->SetType( eType );
274 0 : }
275 :
276 : /************************************************************************/
277 : /* SetDefault() */
278 : /************************************************************************/
279 :
280 : /**
281 : * \brief Set default field value.
282 : *
283 : * Currently use of OGRFieldDefn "defaults" is discouraged. This feature
284 : * may be fleshed out in the future.
285 : *
286 : */
287 :
288 0 : void OGRFieldDefn::SetDefault( const OGRField * puDefaultIn )
289 :
290 : {
291 0 : switch( eType )
292 : {
293 : case OFTInteger:
294 : case OFTReal:
295 0 : uDefault = *puDefaultIn;
296 0 : break;
297 :
298 : case OFTString:
299 : // CPLFree( uDefault.String );
300 : // uDefault.String = CPLStrdup( puDefaultIn->String );
301 0 : break;
302 :
303 : default:
304 : // add handling for other complex types.
305 0 : CPLAssert( FALSE );
306 : break;
307 : }
308 0 : }
309 :
310 : /************************************************************************/
311 : /* GetFieldTypeName() */
312 : /************************************************************************/
313 :
314 : /**
315 : * \brief Fetch human readable name for a field type.
316 : *
317 : * This static method is the same as the C function OGR_GetFieldTypeName().
318 : *
319 : * @param eType the field type to get name for.
320 : *
321 : * @return pointer to an internal static name string. It should not be
322 : * modified or freed.
323 : */
324 :
325 469 : const char * OGRFieldDefn::GetFieldTypeName( OGRFieldType eType )
326 :
327 : {
328 469 : switch( eType )
329 : {
330 : case OFTInteger:
331 5 : return "Integer";
332 :
333 : case OFTReal:
334 304 : return "Real";
335 :
336 : case OFTString:
337 155 : return "String";
338 :
339 : case OFTIntegerList:
340 2 : return "IntegerList";
341 :
342 : case OFTRealList:
343 0 : return "RealList";
344 :
345 : case OFTStringList:
346 0 : return "StringList";
347 :
348 : case OFTBinary:
349 0 : return "Binary";
350 :
351 : case OFTDate:
352 1 : return "Date";
353 :
354 : case OFTTime:
355 1 : return "Time";
356 :
357 : case OFTDateTime:
358 1 : return "DateTime";
359 :
360 : default:
361 0 : return "(unknown)";
362 : }
363 : }
364 :
365 : /************************************************************************/
366 : /* OGR_GetFieldTypeName() */
367 : /************************************************************************/
368 : /**
369 : * \brief Fetch human readable name for a field type.
370 : *
371 : * This function is the same as the CPP method
372 : * OGRFieldDefn::GetFieldTypeName().
373 : *
374 : * @param eType the field type to get name for.
375 : * @return the name.
376 : */
377 :
378 263 : const char *OGR_GetFieldTypeName( OGRFieldType eType )
379 :
380 : {
381 263 : return OGRFieldDefn::GetFieldTypeName( eType );
382 : }
383 :
384 : /************************************************************************/
385 : /* GetJustify() */
386 : /************************************************************************/
387 :
388 : /**
389 : * \fn OGRJustification OGRFieldDefn::GetJustify();
390 : *
391 : * \brief Get the justification for this field.
392 : *
393 : * This method is the same as the C function OGR_Fld_GetJustify().
394 : *
395 : * @return the justification.
396 : */
397 :
398 : /************************************************************************/
399 : /* OGR_Fld_GetJustify() */
400 : /************************************************************************/
401 : /**
402 : * \brief Get the justification for this field.
403 : *
404 : * This function is the same as the CPP method OGRFieldDefn::GetJustify().
405 : *
406 : * @param hDefn handle to the field definition to get justification from.
407 : * @return the justification.
408 : */
409 :
410 0 : OGRJustification OGR_Fld_GetJustify( OGRFieldDefnH hDefn )
411 :
412 : {
413 0 : return ((OGRFieldDefn *) hDefn)->GetJustify();
414 : }
415 :
416 : /************************************************************************/
417 : /* SetJustify() */
418 : /************************************************************************/
419 :
420 : /**
421 : * \fn void OGRFieldDefn::SetJustify( OGRJustification eJustify );
422 : *
423 : * \brief Set the justification for this field.
424 : *
425 : * This method is the same as the C function OGR_Fld_SetJustify().
426 : *
427 : * @param eJustify the new justification.
428 : */
429 :
430 : /************************************************************************/
431 : /* OGR_Fld_SetJustify() */
432 : /************************************************************************/
433 : /**
434 : * \brief Set the justification for this field.
435 : *
436 : * This function is the same as the CPP method OGRFieldDefn::SetJustify().
437 : *
438 : * @param hDefn handle to the field definition to set justification to.
439 : * @param eJustify the new justification.
440 : */
441 :
442 0 : void OGR_Fld_SetJustify( OGRFieldDefnH hDefn, OGRJustification eJustify )
443 :
444 : {
445 0 : ((OGRFieldDefn *) hDefn)->SetJustify( eJustify );
446 0 : }
447 :
448 : /************************************************************************/
449 : /* GetWidth() */
450 : /************************************************************************/
451 :
452 : /**
453 : * \fn int OGRFieldDefn::GetWidth();
454 : *
455 : * \brief Get the formatting width for this field.
456 : *
457 : * This method is the same as the C function OGR_Fld_GetWidth().
458 : *
459 : * @return the width, zero means no specified width.
460 : */
461 :
462 : /************************************************************************/
463 : /* OGR_Fld_GetWidth() */
464 : /************************************************************************/
465 : /**
466 : * \brief Get the formatting width for this field.
467 : *
468 : * This function is the same as the CPP method OGRFieldDefn::GetWidth().
469 : *
470 : * @param hDefn handle to the field definition to get width from.
471 : * @return the width, zero means no specified width.
472 : */
473 :
474 80 : int OGR_Fld_GetWidth( OGRFieldDefnH hDefn )
475 :
476 : {
477 80 : return ((OGRFieldDefn *) hDefn)->GetWidth();
478 : }
479 :
480 : /************************************************************************/
481 : /* SetWidth() */
482 : /************************************************************************/
483 :
484 : /**
485 : * \fn void OGRFieldDefn::SetWidth( int nWidth );
486 : *
487 : * \brief Set the formatting width for this field in characters.
488 : *
489 : * This method is the same as the C function OGR_Fld_SetWidth().
490 : *
491 : * @param nWidth the new width.
492 : */
493 :
494 : /************************************************************************/
495 : /* OGR_Fld_SetWidth() */
496 : /************************************************************************/
497 : /**
498 : * \brief Set the formatting width for this field in characters.
499 : *
500 : * This function is the same as the CPP method OGRFieldDefn::SetWidth().
501 : *
502 : * @param hDefn handle to the field definition to set width to.
503 : * @param nNewWidth the new width.
504 : */
505 :
506 33 : void OGR_Fld_SetWidth( OGRFieldDefnH hDefn, int nNewWidth )
507 :
508 : {
509 33 : ((OGRFieldDefn *) hDefn)->SetWidth( nNewWidth );
510 33 : }
511 :
512 : /************************************************************************/
513 : /* GetPrecision() */
514 : /************************************************************************/
515 :
516 : /**
517 : * \fn int OGRFieldDefn::GetPrecision();
518 : *
519 : * \brief Get the formatting precision for this field.
520 : * This should normally be
521 : * zero for fields of types other than OFTReal.
522 : *
523 : * This method is the same as the C function OGR_Fld_GetPrecision().
524 : *
525 : * @return the precision.
526 : */
527 :
528 : /************************************************************************/
529 : /* OGR_Fld_GetPrecision() */
530 : /************************************************************************/
531 : /**
532 : * \brief Get the formatting precision for this field.
533 : * This should normally be
534 : * zero for fields of types other than OFTReal.
535 : *
536 : * This function is the same as the CPP method OGRFieldDefn::GetPrecision().
537 : *
538 : * @param hDefn handle to the field definition to get precision from.
539 : * @return the precision.
540 : */
541 :
542 73 : int OGR_Fld_GetPrecision( OGRFieldDefnH hDefn )
543 :
544 : {
545 73 : return ((OGRFieldDefn *) hDefn)->GetPrecision();
546 : }
547 :
548 : /************************************************************************/
549 : /* SetPrecision() */
550 : /************************************************************************/
551 :
552 : /**
553 : * \fn void OGRFieldDefn::SetPrecision( int nPrecision );
554 : *
555 : * \brief Set the formatting precision for this field in characters.
556 : *
557 : * This should normally be zero for fields of types other than OFTReal.
558 : *
559 : * This method is the same as the C function OGR_Fld_SetPrecision().
560 : *
561 : * @param nPrecision the new precision.
562 : */
563 :
564 : /************************************************************************/
565 : /* OGR_Fld_SetPrecision() */
566 : /************************************************************************/
567 : /**
568 : * \brief Set the formatting precision for this field in characters.
569 : *
570 : * This should normally be zero for fields of types other than OFTReal.
571 : *
572 : * This function is the same as the CPP method OGRFieldDefn::SetPrecision().
573 : *
574 : * @param hDefn handle to the field definition to set precision to.
575 : * @param nPrecision the new precision.
576 : */
577 :
578 6 : void OGR_Fld_SetPrecision( OGRFieldDefnH hDefn, int nPrecision )
579 :
580 : {
581 6 : ((OGRFieldDefn *) hDefn)->SetPrecision( nPrecision );
582 6 : }
583 :
584 : /************************************************************************/
585 : /* Set() */
586 : /************************************************************************/
587 :
588 : /**
589 : * \brief Set defining parameters for a field in one call.
590 : *
591 : * This method is the same as the C function OGR_Fld_Set().
592 : *
593 : * @param pszNameIn the new name to assign.
594 : * @param eTypeIn the new type (one of the OFT values like OFTInteger).
595 : * @param nWidthIn the preferred formatting width. Defaults to zero indicating
596 : * undefined.
597 : * @param nPrecisionIn number of decimals places for formatting, defaults to
598 : * zero indicating undefined.
599 : * @param eJustifyIn the formatting justification (OJLeft or OJRight), defaults
600 : * to OJUndefined.
601 : */
602 :
603 : void OGRFieldDefn::Set( const char *pszNameIn,
604 : OGRFieldType eTypeIn,
605 : int nWidthIn, int nPrecisionIn,
606 449 : OGRJustification eJustifyIn )
607 : {
608 449 : SetName( pszNameIn );
609 449 : SetType( eTypeIn );
610 449 : SetWidth( nWidthIn );
611 449 : SetPrecision( nPrecisionIn );
612 449 : SetJustify( eJustifyIn );
613 449 : }
614 :
615 : /************************************************************************/
616 : /* OGR_Fld_Set() */
617 : /************************************************************************/
618 : /**
619 : * \brief Set defining parameters for a field in one call.
620 : *
621 : * This function is the same as the CPP method OGRFieldDefn::Set().
622 : *
623 : * @param hDefn handle to the field definition to set to.
624 : * @param pszNameIn the new name to assign.
625 : * @param eTypeIn the new type (one of the OFT values like OFTInteger).
626 : * @param nWidthIn the preferred formatting width. Defaults to zero indicating
627 : * undefined.
628 : * @param nPrecisionIn number of decimals places for formatting, defaults to
629 : * zero indicating undefined.
630 : * @param eJustifyIn the formatting justification (OJLeft or OJRight), defaults
631 : * to OJUndefined.
632 : */
633 :
634 : void OGR_Fld_Set( OGRFieldDefnH hDefn, const char *pszNameIn,
635 : OGRFieldType eTypeIn,
636 : int nWidthIn, int nPrecisionIn,
637 0 : OGRJustification eJustifyIn )
638 :
639 : {
640 : ((OGRFieldDefn *) hDefn)->Set( pszNameIn, eTypeIn, nWidthIn,
641 0 : nPrecisionIn, eJustifyIn );
642 0 : }
|