📄 ugkfeature.cpp
字号:
// ugkfeature.cpp: implementation of the UGKFeature class.////////////////////////////////////////////////////////////////////////#include "ugkfeature.h"#include "ugk_memopr.h"#include "ugk_string.h"/************************************************************************//* UGKFeature() *//************************************************************************//** * Constructor * * Note that the UGKFeature will increment the reference count of it's * defining UGKFeatureDefn. Destruction of the UGKFeatureDefn before * destruction of all UGKFeatures that depend on it is likely to result in * a crash. * * @param poDefnIn feature class (layer) definition to which the feature will * adhere. */UGKFeature::UGKFeature( UGKFeatureDefn * poDefnIn ){ m_pszStyleString = NULL; m_poStyleTable = NULL; poDefnIn->Reference(); poDefn = poDefnIn; nFID = UGKNullFID; poGeometry = NULL; pauFields = (UGKField *) UGK_Calloc( poDefn->GetFieldCount(), sizeof(UGKField) ); for( int i = 0; i < poDefn->GetFieldCount(); i++ ) { pauFields[i].Set.nMarker1 = UGKUnsetMarker;//设置为默认值 pauFields[i].Set.nMarker2 = UGKUnsetMarker;//这样标志未赋值 }}/************************************************************************//* ~UGKFeature() *//************************************************************************/UGKFeature::~UGKFeature(){ poDefn->Dereference(); if( poGeometry != NULL ) delete poGeometry; for( int i = 0; i < poDefn->GetFieldCount(); i++ ) { UGKFieldDefn *poFDefn = poDefn->GetFieldDefn(i); if( !IsFieldSet(i) )//判断此字段是否已经设置 continue; switch( poFDefn->GetType() )//根据不同字段类型来释放内存 { case OFTString: if( pauFields[i].String != NULL ) UGK_Free( pauFields[i].String ); break; case OFTStringList: FreeStrList( pauFields[i].StringList.paList ); break; case OFTIntegerList: case OFTRealList: UGK_Free( pauFields[i].IntegerList.paList ); break; default: break; } } UGK_Free( pauFields ); UGK_Free(m_pszStyleString);}/************************************************************************//* CreateFeature() *//************************************************************************//** * Feature factory. * * * @param poDefn Feature definition defining schema. * * @return new feature object with null fields and no geometry. May be * deleted with delete. */UGKFeature *UGKFeature::CreateFeature( UGKFeatureDefn *poDefn ){ return new UGKFeature( poDefn );}/************************************************************************//* DestroyFeature() *//************************************************************************//** * Destroy feature * * * @param poFeature the feature to delete. */void UGKFeature::DestroyFeature( UGKFeature *poFeature ){ delete poFeature;}/************************************************************************//* SetGeometryDirectly() *//************************************************************************//** * Set feature geometry. * * This method updates the features geometry, and operate exactly as * SetGeometry(), except that this method assumes ownership of the * passed geometry. * * @param poGeomIn new geometry to apply to feature. * * @return UGKERR_NONE if successful, or UGK_UNSUPPORTED_GEOMETRY_TYPE if * the geometry type is illegal for the UGKFeatureDefn (checking not yet * implemented). */ UGKErr UGKFeature::SetGeometryDirectly( UGKGeometry * poGeomIn ){ if( poGeometry != NULL ) delete poGeometry; poGeometry = poGeomIn; // I should be verifying that the geometry matches the defn's type. return UGKERR_NONE;}/************************************************************************//* SetGeometry() *//************************************************************************//** * Set feature geometry. * * This method updates the features geometry, and operate exactly as * SetGeometryDirectly(), except that this method does not assume ownership * of the passed geometry, but instead makes a copy of it. * * @param poGeomIn new geometry to apply to feature. * * @return UGKERR_NONE if successful, or UGK_UNSUPPORTED_GEOMETRY_TYPE if * the geometry type is illegal for the UGKFeatureDefn (checking not yet * implemented). */ UGKErr UGKFeature::SetGeometry( UGKGeometry * poGeomIn ){ if( poGeometry != NULL ) delete poGeometry; if( poGeomIn != NULL ) poGeometry = poGeomIn->clone(); else poGeometry = NULL; // I should be verifying that the geometry matches the defn's type. return UGKERR_NONE;}/************************************************************************//* StealGeometry() *//************************************************************************//** * Take away ownership of geometry. * * Fetch the geometry from this feature, and clear the reference to the * geometry on the feature. This is a mechanism for the application to * take over ownship of the geometry from the feature without copying. * Sort of an inverse to SetGeometryDirectly(). * * After this call the UGKFeature will have a NULL geometry. * * @return the pointer to the geometry. */UGKGeometry *UGKFeature::StealGeometry(){ UGKGeometry *poReturn = poGeometry; poGeometry = NULL; return poReturn;}/************************************************************************//* Clone() *//************************************************************************//** * Duplicate feature. * * The newly created feature is owned by the caller, and will have it's own * reference to the UGKFeatureDefn. * * @return new feature, exactly matching this feature. */UGKFeature *UGKFeature::Clone(){ UGKFeature *poNew = new UGKFeature( poDefn ); poNew->SetGeometry( poGeometry );//设置几何体 for( int i = 0; i < poDefn->GetFieldCount(); i++ )//设置字段 { poNew->SetField( i, pauFields + i ); } if( GetStyleString() != NULL ) //设置Style名 poNew->SetStyleString(GetStyleString()); poNew->SetFID( GetFID() ); //设置ID return poNew;}/************************************************************************//* UnsetField() *//************************************************************************//** 清除一个字段 将其标志为未设置 * Clear a field, marking it as unset. * * @param iField the field to unset. */void UGKFeature::UnsetField( int iField ){ UGKFieldDefn *poFDefn = poDefn->GetFieldDefn( iField ); //获取指定的字段的定义 assert( poFDefn != NULL || iField == -1 ); if( poFDefn == NULL || !IsFieldSet(iField) ) return; switch( poFDefn->GetType() ) { case OFTRealList: case OFTIntegerList: UGK_Free( pauFields[iField].IntegerList.paList ); break; case OFTStringList: FreeStrList( pauFields[iField].StringList.paList ); break; case OFTString: UGK_Free( pauFields[iField].String ); break; default: break; } pauFields[iField].Set.nMarker1 = UGKUnsetMarker; pauFields[iField].Set.nMarker2 = UGKUnsetMarker;}/************************************************************************//* GetFieldAsInteger() *//* 获取字段的整数值 *//************************************************************************//** * Fetch field value as integer. * * OFTString features will be translated using atoi(). OFTReal fields * will be cast to integer. Other field types, or errors will result in * a return value of zero. * * @param iField the field to fetch, from 0 to GetFieldCount()-1. * * @return the field value. */int UGKFeature::GetFieldAsInteger( int iField ){ UGKFieldDefn *poFDefn = poDefn->GetFieldDefn( iField ); assert( poFDefn != NULL || iField == -1 ); if( poFDefn == NULL ) return 0; if( !IsFieldSet(iField) ) return 0; if( poFDefn->GetType() == OFTInteger ) return pauFields[iField].Integer; else if( poFDefn->GetType() == OFTReal ) return (int) pauFields[iField].Real; else if( poFDefn->GetType() == OFTString ) { if( pauFields[iField].String == NULL ) return 0; else return atoi(pauFields[iField].String); } else return 0;}/************************************************************************//* GetFieldAsDouble() *//* 获取字段的双精度值 *//************************************************************************//** * Fetch field value as a double. * * OFTString features will be translated using atof(). OFTInteger fields * will be cast to double. Other field types, or errors will result in * a return value of zero. * * @param iField the field to fetch, from 0 to GetFieldCount()-1. * * @return the field value. */double UGKFeature::GetFieldAsDouble( int iField ){ UGKFieldDefn *poFDefn = poDefn->GetFieldDefn( iField ); assert( poFDefn != NULL || iField == -1 ); if( poFDefn == NULL ) return 0.0; if( !IsFieldSet(iField) ) return 0.0; if( poFDefn->GetType() == OFTReal ) return pauFields[iField].Real; else if( poFDefn->GetType() == OFTInteger ) return pauFields[iField].Integer; else if( poFDefn->GetType() == OFTString ) { if( pauFields[iField].String == NULL ) return 0; else return atof(pauFields[iField].String); } else return 0.0;}/************************************************************************//* GetFieldAsString() *//************************************************************************//** * Fetch field value as a string. * * OFTReal and OFTInteger fields will be translated to string using * sprintf(), but not necessarily using the established formatting rules. * Other field types, or errors will result in a return value of zero. * * @param iField the field to fetch, from 0 to GetFieldCount()-1. * * @return the field value. This string is internal, and should not be * modified, or freed. It's lifetime may be very brief. */const char *UGKFeature::GetFieldAsString( int iField ){ UGKFieldDefn *poFDefn = poDefn->GetFieldDefn( iField ); static char szTempBuffer[80]; assert( poFDefn != NULL || iField == -1 ); if( poFDefn == NULL ) return ""; if( !IsFieldSet(iField) ) return ""; if( poFDefn->GetType() == OFTString ) { if( pauFields[iField].String == NULL ) return ""; else return pauFields[iField].String; } else if( poFDefn->GetType() == OFTInteger ) { sprintf( szTempBuffer, "%d", pauFields[iField].Integer ); return szTempBuffer; } else if( poFDefn->GetType() == OFTReal ) { char szFormat[64]; if( poFDefn->GetWidth() != 0 ) { sprintf( szFormat, "%%%d.%df", poFDefn->GetWidth(), poFDefn->GetPrecision() ); } else strcpy( szFormat, "%.16g" ); sprintf( szTempBuffer, szFormat, pauFields[iField].Real );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -