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

📄 mitab_mapheaderblock.cpp

📁 mitab,读取MapInfo的地图文件
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/**********************************************************************
 * $Id: mitab_mapheaderblock.cpp,v 1.33 2008/02/01 19:36:31 dmorissette Exp $
 *
 * Name:     mitab_mapheaderblock.cpp
 * Project:  MapInfo TAB Read/Write library
 * Language: C++
 * Purpose:  Implementation of the TABHeaderBlock class used to handle
 *           reading/writing of the .MAP files' header block
 * Author:   Daniel Morissette, dmorissette@dmsolutions.ca
 *
 **********************************************************************
 * Copyright (c) 1999-2002, Daniel Morissette
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 **********************************************************************
 *
 * $Log: mitab_mapheaderblock.cpp,v $
 * Revision 1.33  2008/02/01 19:36:31  dmorissette
 * Initial support for V800 REGION and MULTIPLINE (bug 1496)
 *
 * Revision 1.32  2006/11/28 18:49:08  dmorissette
 * Completed changes to split TABMAPObjectBlocks properly and produce an
 * optimal spatial index (bug 1585)
 *
 * Revision 1.31  2005/09/29 20:16:54  dmorissette
 *  Support for writing affine projection params in .MAP header (AJD, bug 1155)
 *
 * Revision 1.30  2005/05/12 20:46:15  dmorissette
 * Initialize m_sProj.nDatumId in InitNewBlock(). (hss/geh)
 *
 * Revision 1.29  2005/03/22 23:24:54  dmorissette
 * Added support for datum id in .MAP header (bug 910)
 *
 * Revision 1.28  2004/12/15 22:52:49  dmorissette
 * Revert back to using doubles for range check in CoordSys2Int(). Hopefully
 * I got it right this time. (bug 894)
 *
 * Revision 1.27  2004/12/08 23:27:35  dmorissette
 * Fixed coordinates rounding error in Coordsys2Int() (bug 894)
 *
 * Revision 1.26  2004/06/30 20:29:04  dmorissette
 * Fixed refs to old address danmo@videotron.ca
 *
 * Revision 1.25  2003/08/12 23:17:21  dmorissette
 * Added reading of v500+ coordsys affine params (Anthony D. - Encom)
 *
 * Revision 1.24  2002/06/28 18:32:37  julien
 * Add SetSpatialFilter() in TABSeamless class (Bug 164, MapServer)
 * Use double for comparison in Coordsys2Int() in mitab_mapheaderblock.cpp
 *
 * Revision 1.23  2002/04/25 16:05:24  julien
 * Disabled the overflow warning in SetCoordFilter() by adding bIgnoreOverflow
 * variable in Coordsys2Int of the TABMAPFile class and TABMAPHeaderBlock class
 *
 * Revision 1.22  2002/03/26 01:48:40  daniel
 * Added Multipoint object type (V650)
 *
 * Revision 1.21  2001/12/05 22:23:06  daniel
 * Can't use rint() on Windows... replace rint() with (int)(val+0.5)
 *
 * Revision 1.20  2001/12/05 21:56:15  daniel
 * Mod. CoordSys2Int() to use rint() for double to integer coord. conversion.
 *
 * Revision 1.19  2001/11/19 15:05:42  daniel
 * Prevent writing of coordinates outside of the +/-1e9 integer bounds.
 *
 * Revision 1.18  2000/12/07 03:58:20  daniel
 * Pass first arg of pow() as double
 *
 * Revision 1.17  2000/09/19 19:35:53  daniel
 * Set default scale/displacement when reading V100 headers
 *
 * Revision 1.16  2000/07/10 14:56:52  daniel
 * Handle m_nOriginQuadrant==0 as quadrant 3 (reverse x and y axis)
 *
 * Revision 1.15  2000/03/13 05:59:25  daniel
 * Switch from V400 to V500 .MAP header (1024 bytes)
 *
 * Revision 1.14  2000/02/28 17:01:05  daniel
 * Use a #define for header version number
 *
 * Revision 1.13  2000/02/07 18:09:10  daniel
 * OOpppps ... test on version number was reversed!
 *
 * Revision 1.12  2000/02/07 17:41:02  daniel
 * Ignore the values of 5 last datum params in version=200 headers
 *
 * Revision 1.11  2000/01/15 22:30:44  daniel
 * Switch to MIT/X-Consortium OpenSource license
 *
 * Revision 1.10  2000/01/15 05:37:47  daniel
 * Use a #define for default quadrant value in new files
 *
 * Revision 1.9  1999/10/19 16:27:10  warmerda
 * Default unitsid to 7 (meters) instead of 0 (miles).
 *
 * Revision 1.8  1999/10/19 06:05:35  daniel
 * Removed obsolete code segments in the coord. conversion functions.
 *
 * Revision 1.7  1999/10/06 13:21:37  daniel
 * Reworked int<->coordsys coords. conversion... hopefully it's OK this time!
 *
 * Revision 1.6  1999/10/01 03:47:38  daniel
 * Better defaults for header fields, and more complete Dump() for debugging
 *
 * Revision 1.5  1999/09/29 04:25:03  daniel
 * Set default scale so that default coord range is +/-1000000.000
 *
 * Revision 1.4  1999/09/26 14:59:36  daniel
 * Implemented write support
 *
 * Revision 1.3  1999/09/21 03:36:33  warmerda
 * slight modification to dump precision
 *
 * Revision 1.2  1999/09/16 02:39:16  daniel
 * Completed read support for most feature types
 *
 * Revision 1.1  1999/07/12 04:18:24  daniel
 * Initial checkin
 *
 **********************************************************************/

#include "mitab.h"

/*---------------------------------------------------------------------
 * Set various constants used in generating the header block.
 *--------------------------------------------------------------------*/
#define HDR_MAGIC_COOKIE        42424242
#define HDR_VERSION_NUMBER      500
#define HDR_DATA_BLOCK_SIZE     512

#define HDR_DEF_ORG_QUADRANT    1       // N-E Quadrant
#define HDR_DEF_REFLECTXAXIS    0

/*---------------------------------------------------------------------
 * The header block starts with an array of map object lenght constants.
 *--------------------------------------------------------------------*/
#define HDR_OBJ_LEN_ARRAY_SIZE   73
static GByte  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,0x2b,0x2f,0x55,0xc8,0xcc,0xd8,
            0xc7,0xcb,0xd0,0xd3,0xd7,0xfd,0xc2,0xc2,
            0xf9};



/*=====================================================================
 *                      class TABMAPHeaderBlock
 *====================================================================*/


/**********************************************************************
 *                   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_ORG_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
    m_sProj.nAffineUnits  = 7;
    m_sProj.dAffineParamA = 0.0;
    m_sProj.dAffineParamB = 0.0;
    m_sProj.dAffineParamC = 0.0;
    m_sProj.dAffineParamD = 0.0;
    m_sProj.dAffineParamE = 0.0;
    m_sProj.dAffineParamF = 0.0;
}

/**********************************************************************
 *                   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 
 * CPLError() will have been called.
 **********************************************************************/
int     TABMAPHeaderBlock::InitBlockFromData(GByte *pabyBuf, 
                                             int nBlockSize, int nSizeUsed, 
                                             GBool bMakeCopy /* = TRUE */,
                                             FILE *fpSrc /* = NULL */, 
                                             int nOffset /* = 0 */)
{
    int i, nStatus;
    GInt32 nMagicCookie;

    /*-----------------------------------------------------------------
     * First of all, we must call the base class' InitBlockFromData()
     *----------------------------------------------------------------*/
    nStatus = TABRawBinBlock::InitBlockFromData(pabyBuf, 
                                                nBlockSize, nSizeUsed,
                                                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)
    {
        CPLError(CE_Failure, CPLE_FileIO,
              "ReadFromFile(): Invalid Magic Cookie: got %d expected %d",
                 nMagicCookie, HDR_MAGIC_COOKIE);
        CPLFree(m_pabyBuf);
        m_pabyBuf = NULL;
        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();

    /* DatumId was never set (always 0) until MapInfo 7.8. See bug 910
     * MAP Version Number is 500 in this case.
     */
    if (m_nMAPVersionNumber >= 500)
        m_sProj.nDatumId  = ReadInt16();
    else
    {
        ReadInt16();    // Skip.
        m_sProj.nDatumId = 0;
    }
    ReadByte();         // Skip unknown byte
    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.

⌨️ 快捷键说明

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