libpcidsk
pcidsk_shape.h
00001 /******************************************************************************
00002  *
00003  * Purpose:  PCIDSK Vector Shape interface.  Declaration.
00004  * 
00005  ******************************************************************************
00006  * Copyright (c) 2009
00007  * PCI Geomatics, 50 West Wilmot Street, Richmond Hill, Ont, Canada
00008  *
00009  * Permission is hereby granted, free of charge, to any person obtaining a
00010  * copy of this software and associated documentation files (the "Software"),
00011  * to deal in the Software without restriction, including without limitation
00012  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00013  * and/or sell copies of the Software, and to permit persons to whom the
00014  * Software is furnished to do so, subject to the following conditions:
00015  *
00016  * The above copyright notice and this permission notice shall be included
00017  * in all copies or substantial portions of the Software.
00018  *
00019  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00020  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00021  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00022  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00023  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00024  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00025  * DEALINGS IN THE SOFTWARE.
00026  ****************************************************************************/
00027 
00028 #ifndef __INCLUDE_PCIDSK_SHAPE_H
00029 #define __INCLUDE_PCIDSK_SHAPE_H
00030 
00031 #include <string>
00032 #include <vector>
00033 #include <cstdlib>
00034 #include <cstring>
00035 
00036 namespace PCIDSK
00037 {
00038 
00040     typedef int32 ShapeId;
00041 
00042     static const ShapeId NullShapeId = -1;
00043 
00045     typedef struct 
00046     {
00047         double x;
00048         double y;
00049         double z;
00050     } ShapeVertex;
00051 
00052 /************************************************************************/
00053 /*                            ShapeFieldType                            */
00054 /************************************************************************/
00056     typedef enum  // These deliberately match GDBFieldType values.
00057     {
00058         FieldTypeNone = 0,
00059         FieldTypeFloat = 1,
00060         FieldTypeDouble = 2,
00061         FieldTypeString = 3,
00062         FieldTypeInteger = 4,
00063         FieldTypeCountedInt = 5
00064     } ShapeFieldType;
00065 
00066 /************************************************************************/
00067 /*                         ShapeFieldTypeName()                         */
00068 /************************************************************************/
00074     inline std::string ShapeFieldTypeName( ShapeFieldType type )
00075     {
00076         switch( type ) {
00077           case FieldTypeNone: return "None";
00078           case FieldTypeFloat: return "Float";
00079           case FieldTypeDouble: return "Double";
00080           case FieldTypeString: return "String";
00081           case FieldTypeInteger: return "Integer";
00082           case FieldTypeCountedInt: return "CountedInt";
00083         }
00084         return "";
00085     }
00086     
00087 
00088 /************************************************************************/
00089 /*                              ShapeField                              */
00090 /************************************************************************/
00104     class ShapeField
00105     {
00106       private:
00107         ShapeFieldType  type; // use FieldTypeNone for NULL fields.
00108 
00109         union
00110         {
00111             float       float_val;
00112             double      double_val;
00113             char       *string_val;
00114             int32       integer_val;
00115             int32      *integer_list_val;
00116         } v;
00117         
00118       public:
00120         ShapeField() 
00121             { v.string_val = NULL; type = FieldTypeNone; }
00122 
00124         ShapeField( const ShapeField &src )
00125             { v.string_val = NULL; type = FieldTypeNone; *this = src; }
00126 
00127         ~ShapeField() 
00128             { Clear(); }
00129 
00131         ShapeField &operator=( const ShapeField &src )
00132             {
00133                 switch( src.GetType() )
00134                 {
00135                   case FieldTypeFloat: 
00136                     SetValue( src.GetValueFloat() );
00137                     break;
00138                   case FieldTypeDouble: 
00139                     SetValue( src.GetValueDouble() );
00140                     break;
00141                   case FieldTypeInteger: 
00142                     SetValue( src.GetValueInteger() );
00143                     break;
00144                   case FieldTypeCountedInt: 
00145                     SetValue( src.GetValueCountedInt() );
00146                     break;
00147                   case FieldTypeString:
00148                     SetValue( src.GetValueString() );
00149                     break;
00150                   case FieldTypeNone:
00151                     Clear();
00152                     break;
00153                 }
00154                 return *this;
00155             }
00156 
00158         void Clear()
00159             { 
00160                 if( (type == FieldTypeString || type == FieldTypeCountedInt)
00161                     && v.string_val != NULL ) 
00162                 {
00163                     free( v.string_val );
00164                     v.string_val = NULL;
00165                 }
00166                 type = FieldTypeNone;
00167             }
00168 
00170         ShapeFieldType  GetType() const
00171             { return type; }
00172 
00174         void SetValue( int32 val ) 
00175             { 
00176                 Clear(); 
00177                 type = FieldTypeInteger; 
00178                 v.integer_val = val; 
00179             }
00180 
00182         void SetValue( const std::vector<int32> &val )
00183             { 
00184                 Clear();
00185                 type = FieldTypeCountedInt; 
00186                 v.integer_list_val = (int32*)
00187                     malloc(sizeof(int32) * (val.size()+1) );
00188                 v.integer_list_val[0] = val.size();
00189                 memcpy( v.integer_list_val+1, &(val[0]), 
00190                         sizeof(int32) * val.size() ); 
00191             }
00192 
00194         void SetValue( const std::string &val )
00195             { 
00196                 Clear(); 
00197                 type = FieldTypeString; 
00198                 v.string_val = strdup(val.c_str()); 
00199             }
00200 
00202         void SetValue( double val )
00203             { 
00204                 Clear(); 
00205                 type = FieldTypeDouble; 
00206                 v.double_val = val; 
00207             }
00208 
00210         void SetValue( float val )
00211             { 
00212                 Clear(); 
00213                 type = FieldTypeFloat; 
00214                 v.float_val = val; 
00215             }
00216 
00218         int32 GetValueInteger() const
00219             { if( type == FieldTypeInteger ) return v.integer_val; else return 0; }
00221         std::vector<int32> GetValueCountedInt() const
00222             { 
00223                 std::vector<int32> result;
00224                 if( type == FieldTypeCountedInt )
00225                 {
00226                     result.resize( v.integer_list_val[0] );
00227                     memcpy( &(result[0]), &(v.integer_list_val[1]), 
00228                             (v.integer_list_val[0]) * sizeof(int32) );
00229                 }
00230                 return result;
00231             }
00233         std::string GetValueString() const
00234             { if( type == FieldTypeString ) return v.string_val; else return ""; }
00236         float GetValueFloat() const
00237             { if( type == FieldTypeFloat ) return v.float_val; else return 0.0; }
00239         double GetValueDouble() const
00240             { if( type == FieldTypeDouble ) return v.double_val; else return 0.0; }
00241     };
00242 
00243 } // end namespace PCIDSK
00244 
00245 #endif // __INCLUDE_PCIDSK_SHAPE_H

Generated for GDAL by doxygen 1.7.5.