⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tabmapheaderblock.cpp

📁 linux下一款GIS程序源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// tabmapheaderblock.cpp: implementation of the TABMAPHeaderBlock class.////////////////////////////////////////////////////////////////////////#include "tabmapheaderblock.h"#include "ugk_errhandle.h"#include "ugk_memopr.h"static UGKByte  gabyObjLenArray[ HDR_OBJ_LEN_ARRAY_SIZE  ] = {     0x00,0x0a,0x0e,0x15,0x0e,0x16,0x1b,0xa2,     0xa6,0xab,0x1a,0x2a,0x2f,0xa5,0xa9,0xb5,     0xa7,0xb5,0xd9,0x0f,0x17,0x23,0x13,0x1f,     0x2b,0x0f,0x17,0x23,0x4f,0x57,0x63,0x9c,     0xa4,0xa9,0xa0,0xa8,0xad,0xa4,0xa8,0xad,     0x16,0x1a,0x39,0x0d,0x11,0x37,0xa5,0xa9,     0xb5,0xa4,0xa8,0xad,0xb2,0xb6,0xdc,0xbd,     0xbd,0xf4 };/********************************************************************** *                   TABMAPHeaderBlock::TABMAPHeaderBlock() * * Constructor. **********************************************************************/TABMAPHeaderBlock::TABMAPHeaderBlock(TABAccess eAccessMode /*= TABRead*/):    TABRawBinBlock(eAccessMode, TRUE){    int i;    /*-----------------------------------------------------------------     * Set acceptable default values for member vars.     *----------------------------------------------------------------*/    m_nMAPVersionNumber = HDR_VERSION_NUMBER;    m_nBlockSize = HDR_DATA_BLOCK_SIZE;    m_dCoordsys2DistUnits = 1.0;    m_nXMin = -1000000000;    m_nYMin = -1000000000;    m_nXMax = 1000000000;    m_nYMax = 1000000000;    m_bIntBoundsOverflow = FALSE;    m_nFirstIndexBlock = 0;    m_nFirstGarbageBlock = 0;    m_nFirstToolBlock = 0;    m_numPointObjects = 0;     m_numLineObjects = 0;    m_numRegionObjects = 0;    m_numTextObjects = 0;    m_nMaxCoordBufSize = 0;    m_nDistUnitsCode = 7;       // Meters --距离单位代码    m_nMaxSpIndexDepth = 0;    m_nCoordPrecision = 3;      // ??? 3 Digits of precision    m_nCoordOriginQuadrant = HDR_DEF_UGK_QUADRANT; // ???    m_nReflectXAxisCoord = HDR_DEF_REFLECTXAXIS; //??    m_nMaxObjLenArrayId = HDR_OBJ_LEN_ARRAY_SIZE-1;  // See gabyObjLenArray[]    m_numPenDefs = 0;    m_numBrushDefs = 0;    m_numSymbolDefs = 0;    m_numFontDefs = 0;    m_numMapToolBlocks = 0;    m_sProj.nProjId  = 0;    m_sProj.nEllipsoidId = 0;    m_sProj.nUnitsId = 7;    m_XScale = 1000.0;  // Default coord range (before SetCoordSysBounds())     m_YScale = 1000.0;  // will be [-1000000.000 .. 1000000.000]    m_XDispl = 0.0;    m_YDispl = 0.0;    for(i=0; i<6; i++)        m_sProj.adProjParams[i] = 0.0;    m_sProj.dDatumShiftX = 0.0;    m_sProj.dDatumShiftY = 0.0;    m_sProj.dDatumShiftZ = 0.0;    for(i=0; i<5; i++)        m_sProj.adDatumParams[i] = 0.0;    m_sProj.nAffineFlag = 0;    // Only in version 500 and up}/********************************************************************** *                   TABMAPHeaderBlock::~TABMAPHeaderBlock() * * Destructor. **********************************************************************/TABMAPHeaderBlock::~TABMAPHeaderBlock(){}/********************************************************************** *                   TABMAPHeaderBlock::InitBlockFromData() * * Perform some initialization on the block after its binary data has * been set or changed (or loaded from a file). * * Returns 0 if succesful or -1 if an error happened, in which case  * UGKError() will have been called. **********************************************************************/int     TABMAPHeaderBlock::InitBlockFromData(UGKByte *pabyBuf, int nSize,                                          UGKBool bMakeCopy /* = TRUE */,                                         FILE *fpSrc /* = NULL */,                                          int nOffset /* = 0 */){    int i, nStatus;    UGKInt32 nMagicCookie;    /*-----------------------------------------------------------------     * First of all, we must call the base class' InitBlockFromData()     *----------------------------------------------------------------*/    nStatus = TABRawBinBlock::InitBlockFromData(pabyBuf, nSize, bMakeCopy,                                            fpSrc, nOffset);    if (nStatus != 0)           return nStatus;    /*-----------------------------------------------------------------     * Validate block type     * Header blocks have a magic cookie at byte 0x100     *----------------------------------------------------------------*/    GotoByteInBlock(0x100);    nMagicCookie = ReadInt32();    if (nMagicCookie != HDR_MAGIC_COOKIE)    {        UGKError(ET_Failure, UGKErr_FileIO,               "ReadFromFile(): Invalid Magic Cookie: got %d expected %d",                  nMagicCookie, HDR_MAGIC_COOKIE);        UGK_Free(m_pabyBuf);        return -1;    }    /*-----------------------------------------------------------------     * Init member variables     * Instead of having over 30 get/set methods, we'll make all data      * members public and we will initialize them here.       * For this reason, this class should be used with care.     *----------------------------------------------------------------*/    GotoByteInBlock(0x104);    m_nMAPVersionNumber = ReadInt16();    m_nBlockSize = ReadInt16();    m_dCoordsys2DistUnits = ReadDouble();    m_nXMin = ReadInt32();    m_nYMin = ReadInt32();    m_nXMax = ReadInt32();    m_nYMax = ReadInt32();    GotoByteInBlock(0x130);     // Skip 16 unknown bytes     m_nFirstIndexBlock = ReadInt32();    m_nFirstGarbageBlock = ReadInt32();    m_nFirstToolBlock = ReadInt32();    m_numPointObjects = ReadInt32();    m_numLineObjects = ReadInt32();    m_numRegionObjects = ReadInt32();    m_numTextObjects = ReadInt32();    m_nMaxCoordBufSize = ReadInt32();    GotoByteInBlock(0x15e);     // Skip 14 unknown bytes    m_nDistUnitsCode = ReadByte();    m_nMaxSpIndexDepth = ReadByte();  //索引树深度    m_nCoordPrecision = ReadByte();    m_nCoordOriginQuadrant = ReadByte();    m_nReflectXAxisCoord = ReadByte();    m_nMaxObjLenArrayId = ReadByte();    // See gabyObjLenArray[]    m_numPenDefs = ReadByte();    m_numBrushDefs = ReadByte();    m_numSymbolDefs = ReadByte();    m_numFontDefs = ReadByte();    m_numMapToolBlocks = ReadInt16();    GotoByteInBlock(0x16d);     // Skip 1 unknown bytes    m_sProj.nProjId  = ReadByte();    m_sProj.nEllipsoidId = ReadByte();    m_sProj.nUnitsId = ReadByte();    m_XScale = ReadDouble();    m_YScale = ReadDouble();    m_XDispl = ReadDouble();    m_YDispl = ReadDouble();      /* In V.100 files, the scale and displacement do not appear to be set.     * we'll use m_nCoordPrecision to define the scale factor instead.     */    if (m_nMAPVersionNumber <= 100)    {        m_XScale = m_YScale = pow(10.0, m_nCoordPrecision);        m_XDispl = m_YDispl = 0.0;    }    for(i=0; i<6; i++)        m_sProj.adProjParams[i] = ReadDouble();    m_sProj.dDatumShiftX = ReadDouble();    m_sProj.dDatumShiftY = ReadDouble();    m_sProj.dDatumShiftZ = ReadDouble();    for(i=0; i<5; i++)    {        /* In V.200 files, the next 5 datum params are unused and they         * sometimes contain junk bytes... in this case we set adDatumParams[]         * to 0 for the rest of the lib to be happy.         */        m_sProj.adDatumParams[i] = ReadDouble();        if (m_nMAPVersionNumber <= 200)            m_sProj.adDatumParams[i] = 0.0;    }    m_sProj.nAffineFlag = 0;    if (m_nMAPVersionNumber >= 500 && m_nSizeUsed > 512)    {        // Read Affine parameters A,B,C,D,E,F         // only if version 500+ and block is larger than 512 bytes        int nInUse = ReadByte();        if (nInUse)        {            m_sProj.nAffineFlag = 1;            m_sProj.nAffineUnits = ReadByte();            GotoByteInBlock(0x0208); // Skip unused bytes            m_sProj.dAffineParamA = ReadDouble();            m_sProj.dAffineParamB = ReadDouble();            m_sProj.dAffineParamC = ReadDouble();            m_sProj.dAffineParamD = ReadDouble();            m_sProj.dAffineParamE = ReadDouble();            m_sProj.dAffineParamF = ReadDouble();        }    }    return 0;}/********************************************************************** *                   TABMAPHeaderBlock::Int2Coordsys() * * Convert from long integer (internal) to coordinates system units * as defined in the file's coordsys clause. * * Note that the false easting/northing and the conversion factor from * datum to coordsys units are not included in the calculation. * * Returns 0 on success, -1 on error. **********************************************************************/int TABMAPHeaderBlock::Int2Coordsys(UGKInt32 nX, UGKInt32 nY,                                     double &dX, double &dY){    if (m_pabyBuf == NULL)        return -1;    // For some obscure reason, some guy decided that it would be     // more fun to be able to define our own origin quadrant!    //    // In version 100 .tab files (version 400 .map), it is possible to have     // a quadrant 0 and it should be treated the same way as quadrant 3    if (m_nCoordOriginQuadrant==2 || m_nCoordOriginQuadrant==3 ||        m_nCoordOriginQuadrant==0 )        dX = -1.0 * (nX + m_XDispl) / m_XScale;    else        dX = (nX - m_XDispl) / m_XScale;    if (m_nCoordOriginQuadrant==3 || m_nCoordOriginQuadrant==4||        m_nCoordOriginQuadrant==0)        dY = -1.0 * (nY + m_YDispl) / m_YScale;    else        dY = (nY - m_YDispl) / m_YScale;//printf("Int2Coordsys: (%d, %d) -> (%.10g, %.10g)\n", nX, nY, dX, dY);    return 0;}/********************************************************************** *                   TABMAPHeaderBlock::Coordsys2Int() * * Convert from coordinates system units as defined in the file's  * coordsys clause to long integer (internal) coordinates. * * Note that the false easting/northing and the conversion factor from * datum to coordsys units are not included in the calculation. * * Returns 0 on success, -1 on error. **********************************************************************/int TABMAPHeaderBlock::Coordsys2Int(double dX, double dY,                                     UGKInt32 &nX, UGKInt32 &nY,                                    UGKBool bIgnoreOverflow /*=FALSE*/){    if (m_pabyBuf == NULL)        return -1;    // For some obscure reason, some guy decided that it would be     // more fun to be able to define our own origin quadrant!    //    // In version 100 .tab files (version 400 .map), it is possible to have     // a quadrant 0 and it should be treated the same way as quadrant 3    /*-----------------------------------------------------------------     * NOTE: double value must be use here, the limit of integer value      * have been reach some times due to the very big number used here.     *----------------------------------------------------------------*/    double dTempX, dTempY;    if (m_nCoordOriginQuadrant==2 || m_nCoordOriginQuadrant==3 ||        m_nCoordOriginQuadrant==0 )        dTempX = (double)((-1.0*dX*m_XScale - m_XDispl)+0.5);    else        dTempX = (double)((dX*m_XScale + m_XDispl)+0.5);    if (m_nCoordOriginQuadrant==3 || m_nCoordOriginQuadrant==4 ||        m_nCoordOriginQuadrant==0 )        dTempY = (double)((-1.0*dY*m_YScale - m_YDispl)+0.5);    else        dTempY = (double)((dY*m_YScale + m_YDispl)+0.5);//printf("Coordsys2Int: (%10g, %10g) -> (%d, %d)\n", dX, dY, nX, nY);    /*-----------------------------------------------------------------     * Make sure we'll never output coordinates outside of the valid     * integer coordinates range: (-1e9, -1e9) - (1e9, 1e9)     * Integer coordinates outside of that range will confuse MapInfo.     *----------------------------------------------------------------*/    UGKBool bIntBoundsOverflow = FALSE;    if (dTempX < -1000000000)    {        dTempX = -1000000000;        bIntBoundsOverflow = TRUE;    }    if (dTempX > 1000000000)    {        dTempX = 1000000000;        bIntBoundsOverflow = TRUE;    }    if (dTempY < -1000000000)    {        dTempY = -1000000000;        bIntBoundsOverflow = TRUE;    }    if (dTempY > 1000000000)    {        dTempY = 1000000000;        bIntBoundsOverflow = TRUE;    }    nX = (UGKInt32) dTempX;    nY = (UGKInt32) dTempY;    if (bIntBoundsOverflow && !bIgnoreOverflow)    {        m_bIntBoundsOverflow = TRUE;    }    return 0;}/********************************************************************** *                   TABMAPHeaderBlock::ComprInt2Coordsys() * * Convert from compressed integer (internal) to coordinates system units * as defined in the file's coordsys clause. * The difference between long integer and compressed integer coords is * that compressed coordinates are scaled displacement relative to an  * object centroid. * * Note that the false easting/northing and the conversion factor from * datum to coordsys units are not included in the calculation. * * Returns 0 on success, -1 on error. **********************************************************************/int TABMAPHeaderBlock::ComprInt2Coordsys(UGKInt32 nCenterX, UGKInt32 nCenterY,                                          int nDeltaX, int nDeltaY,                                          double &dX, double &dY){    if (m_pabyBuf == NULL)        return -1;    return Int2Coordsys(nCenterX+nDeltaX, nCenterY+nDeltaY, dX, dY);}/********************************************************************** *                   TABMAPHeaderBlock::Int2CoordsysDist() * * Convert a pair of X and Y size (or distance) value from long integer * (internal) to coordinates system units as defined in the file's  * coordsys clause. * * The difference with Int2Coordsys() is that this function only applies * the scaling factor: it does not apply the displacement. * * Since the calculations on the X and Y values are independent, either * one can be omitted (i.e. passed as 0) * * Returns 0 on success, -1 on error. **********************************************************************/int TABMAPHeaderBlock::Int2CoordsysDist(UGKInt32 nX, UGKInt32 nY,                                     double &dX, double &dY){    if (m_pabyBuf == NULL)        return -1;    dX = nX / m_XScale;    dY = nY / m_YScale;    return 0;}/********************************************************************** *                   TABMAPHeaderBlock::Coordsys2IntDist() * * Convert a pair of X and Y size (or distance) values from coordinates * system units as defined in the file's coordsys clause to long integer * (internal) coordinates. * * The difference with Coordsys2Int() is that this function only applies * the scaling factor: it does not apply the displacement. * * Since the calculations on the X and Y values are independent, either * one can be omitted (i.e. passed as 0) * * Returns 0 on success, -1 on error. **********************************************************************/int TABMAPHeaderBlock::Coordsys2IntDist(double dX, double dY,                                         UGKInt32 &nX, UGKInt32 &nY){    if (m_pabyBuf == NULL)        return -1;    nX = (UGKInt32)(dX*m_XScale);    nY = (UGKInt32)(dY*m_YScale);    return 0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -