📄 mitab_mapfile.cpp
字号:
/**********************************************************************
* $Id: mitab_mapfile.cpp,v 1.43 2008/02/20 21:35:30 dmorissette Exp $
*
* Name: mitab_mapfile.cpp
* Project: MapInfo TAB Read/Write library
* Language: C++
* Purpose: Implementation of the TABMAPFile class used to handle
* reading/writing of the .MAP files at the MapInfo object level
* 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_mapfile.cpp,v $
* Revision 1.43 2008/02/20 21:35:30 dmorissette
* Added support for V800 COLLECTION of large objects (bug 1496)
*
* Revision 1.42 2008/02/01 19:36:31 dmorissette
* Initial support for V800 REGION and MULTIPLINE (bug 1496)
*
* Revision 1.41 2007/11/08 18:57:56 dmorissette
* Upgrade of OGR and CPL libs to the version from GDAL/OGR 1.4.3
*
* Revision 1.40 2007/09/14 18:30:19 dmorissette
* Fixed the splitting of object blocks with the optimized spatial
* index mode that was producing files with misaligned bytes that
* confused MapInfo (bug 1732)
*
* Revision 1.39 2007/07/11 15:51:52 dmorissette
* Fixed duplicate 'int i' definition build errors in SplitObjBlock()
*
* Revision 1.38 2007/06/12 12:50:39 dmorissette
* Use Quick Spatial Index by default until bug 1732 is fixed (broken files
* produced by current coord block splitting technique).
*
* Revision 1.37 2007/06/05 13:23:57 dmorissette
* Fixed memory leak when writing .TAB with new (optimized) spatial index
* introduced in v1.6.0 (bug 1725)
*
* Revision 1.36 2007/03/21 21:15:56 dmorissette
* Added SetQuickSpatialIndexMode() which generates a non-optimal spatial
* index but results in faster write time (bug 1669)
*
* Revision 1.35 2006/11/28 18:49:08 dmorissette
* Completed changes to split TABMAPObjectBlocks properly and produce an
* optimal spatial index (bug 1585)
*
* Revision 1.34 2006/11/20 20:05:58 dmorissette
* First pass at improving generation of spatial index in .map file (bug 1585)
* New methods for insertion and splittung in the spatial index are done.
* Also implemented a method to dump the spatial index to .mif/.mid
* Still need to implement splitting of TABMapObjectBlock to get optimal
* results.
*
* Revision 1.33 2006/09/05 23:05:08 dmorissette
* Added TABMAPFile::DumpSpatialIndex() (bug 1585)
*
* Revision 1.32 2005/10/06 19:15:31 dmorissette
* Collections: added support for reading/writing pen/brush/symbol ids and
* for writing collection objects to .TAB/.MAP (bug 1126)
*
* Revision 1.31 2004/09/22 13:07:58 fwarmerdam
* fixed return value in LoadNextMatchingObjectBlock() per rso bug 615
*
* Revision 1.30 2004/06/30 20:29:04 dmorissette
* Fixed refs to old address danmo@videotron.ca
*
* Revision 1.29 2003/08/12 23:17:21 dmorissette
* Added reading of v500+ coordsys affine params (Anthony D. - Encom)
*
* Revision 1.28 2002/08/27 17:18:43 warmerda
* improved CPL error testing
*
* Revision 1.27 2002/07/30 13:54:12 julien
* TABMAPFile::GetFeatureId() now return -1 when there's no geometry. (bug 169)
*
* Revision 1.26 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.25 2002/03/26 19:27:43 daniel
* Got rid of tabs in source
*
* Revision 1.24 2002/03/26 01:48:40 daniel
* Added Multipoint object type (V650)
*
* Revision 1.23 2002/02/20 13:53:40 daniel
* Prevent an infinite loop of calls to LoadNextMatchingObjectBlock() in
* GetNextFeatureId() if no objects found in spatial index.
*
* Revision 1.22 2001/11/19 15:04:41 daniel
* Prevent writing of coordinates outside of the +/-1e9 integer bounds.
*
* Revision 1.21 2001/11/17 21:54:06 daniel
* Made several changes in order to support writing objects in 16 bits
* coordinate format. New TABMAPObjHdr-derived classes are used to hold
* object info in mem until block is full.
*
* Revision 1.20 2001/09/18 20:33:52 warmerda
* fixed case of spatial search on file with just one object block
*
* Revision 1.19 2001/09/14 03:23:55 warmerda
* Substantial upgrade to support spatial queries using spatial indexes
*
* Revision 1.18 2001/03/15 03:57:51 daniel
* Added implementation for new OGRLayer::GetExtent(), returning data MBR.
*
* Revision 1.17 2000/11/23 21:11:07 daniel
* OOpps... VC++ didn't like the way TABPenDef, etc. were initialized
*
* Revision 1.16 2000/11/23 20:47:46 daniel
* Use MI defaults for Pen, Brush, Font, Symbol instead of all zeros
*
* Revision 1.15 2000/11/22 04:03:10 daniel
* Added warning when objects written outside of the +/-1e9 int. coord. range
*
* Revision 1.14 2000/11/15 04:13:49 daniel
* Fixed writing of TABMAPToolBlock to allocate a new block when full
*
* Revision 1.13 2000/05/19 06:44:55 daniel
* Modified generation of spatial index to split index nodes and produce a
* more balanced tree.
*
* Revision 1.12 2000/03/13 05:58:01 daniel
* Create 1024 bytes V500 .MAP header + limit m_nMaxCoordBufSize for V450 obj.
*
* Revision 1.11 2000/02/28 17:00:00 daniel
* Added V450 object types
*
* Revision 1.10 2000/01/15 22:30:44 daniel
* Switch to MIT/X-Consortium OpenSource license
*
* Revision 1.9 1999/12/19 17:37:52 daniel
* Fixed memory leaks
*
* Revision 1.8 1999/11/14 04:43:31 daniel
* Support dataset with no .MAP/.ID files
*
* Revision 1.7 1999/10/19 22:57:17 daniel
* Create m_poCurObjBlock only when needed to avoid empty blocks in files
* and problems with MBR in header block of files with only "NONE" geometries
*
* Revision 1.6 1999/10/06 13:17:46 daniel
* Update m_nMaxCoordBufSize in header block
*
* Revision 1.5 1999/10/01 03:52:22 daniel
* Avoid producing an unused block in the file when closing it.
*
* Revision 1.4 1999/09/26 14:59:36 daniel
* Implemented write support
*
* Revision 1.3 1999/09/20 18:42:42 daniel
* Use binary access to open file.
*
* 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"
/*=====================================================================
* class TABMAPFile
*====================================================================*/
/**********************************************************************
* TABMAPFile::TABMAPFile()
*
* Constructor.
**********************************************************************/
TABMAPFile::TABMAPFile()
{
m_nMinTABVersion = 300;
m_fp = NULL;
m_pszFname = NULL;
m_poHeader = NULL;
m_poSpIndex = NULL;
m_poSpIndexLeaf = NULL;
/* See bug 1732: Optimized spatial index produces broken files because
* of the way CoordBlocks are split. For now we have to force using the
* Quick (old) spatial index mode by default until bug 1732 is fixed.
*/
m_bQuickSpatialIndexMode = TRUE;
// m_bQuickSpatialIndexMode = FALSE;
m_poCurObjBlock = NULL;
m_nCurObjPtr = -1;
m_nCurObjType = -1;
m_nCurObjId = -1;
m_poCurCoordBlock = NULL;
m_poToolDefTable = NULL;
}
/**********************************************************************
* TABMAPFile::~TABMAPFile()
*
* Destructor.
**********************************************************************/
TABMAPFile::~TABMAPFile()
{
Close();
}
/**********************************************************************
* TABMAPFile::Open()
*
* Open a .MAP file, and initialize the structures to be ready to read
* objects from it.
*
* Since .MAP and .ID files are optional, you can set bNoErrorMsg=TRUE to
* disable the error message and receive an return value of 1 if file
* cannot be opened.
* In this case, only the methods MoveToObjId() and GetCurObjType() can
* be used. They will behave as if the .ID file contained only null
* references, so all object will look like they have NONE geometries.
*
* Returns 0 on success, -1 on error.
**********************************************************************/
int TABMAPFile::Open(const char *pszFname, const char *pszAccess,
GBool bNoErrorMsg /* = FALSE */)
{
FILE *fp=NULL;
TABRawBinBlock *poBlock=NULL;
if (m_fp)
{
CPLError(CE_Failure, CPLE_FileIO,
"Open() failed: object already contains an open file");
return -1;
}
m_nMinTABVersion = 300;
m_fp = NULL;
m_poHeader = NULL;
m_poIdIndex = NULL;
m_poSpIndex = NULL;
m_poToolDefTable = NULL;
/*-----------------------------------------------------------------
* Validate access mode and make sure we use binary access.
*----------------------------------------------------------------*/
if (EQUALN(pszAccess, "r", 1))
{
m_eAccessMode = TABRead;
pszAccess = "rb";
}
else if (EQUALN(pszAccess, "w", 1))
{
m_eAccessMode = TABWrite;
pszAccess = "wb+";
}
else
{
CPLError(CE_Failure, CPLE_FileIO,
"Open() failed: access mode \"%s\" not supported", pszAccess);
return -1;
}
/*-----------------------------------------------------------------
* Open file
*----------------------------------------------------------------*/
fp = VSIFOpen(pszFname, pszAccess);
// TODO: In Read/Write mode we should also preload the chain of deleted
// blocks in the blockManager. Not needed for read-only or write-only.
m_oBlockManager.Reset();
if (fp != NULL && m_eAccessMode == TABRead)
{
/*-----------------------------------------------------------------
* Read access: try to read header block
* First try with a 512 bytes block to check the .map version.
* If it's version 500 or more then read again a 1024 bytes block
*----------------------------------------------------------------*/
poBlock = TABCreateMAPBlockFromFile(fp, 0, 512);
if (poBlock && poBlock->GetBlockClass() == TABMAP_HEADER_BLOCK &&
((TABMAPHeaderBlock*)poBlock)->m_nMAPVersionNumber >= 500)
{
// Version 500 or higher. Read 1024 bytes block instead of 512
delete poBlock;
poBlock = TABCreateMAPBlockFromFile(fp, 0, 1024);
}
if (poBlock==NULL || poBlock->GetBlockClass() != TABMAP_HEADER_BLOCK)
{
if (poBlock)
delete poBlock;
poBlock = NULL;
VSIFClose(fp);
CPLError(CE_Failure, CPLE_FileIO,
"Open() failed: %s does not appear to be a valid .MAP file",
pszFname);
return -1;
}
}
else if (fp != NULL && m_eAccessMode == TABWrite)
{
/*-----------------------------------------------------------------
* Write access: create a new header block
* .MAP files of Version 500 and up appear to have a 1024 bytes
* header. The last 512 bytes are usually all zeros.
*----------------------------------------------------------------*/
poBlock = new TABMAPHeaderBlock(m_eAccessMode);
poBlock->InitNewBlock(fp, 1024, m_oBlockManager.AllocNewBlock() );
// Alloc a second 512 bytes of space since oBlockManager deals
// with 512 bytes blocks.
m_oBlockManager.AllocNewBlock();
}
else if (bNoErrorMsg)
{
/*-----------------------------------------------------------------
* .MAP does not exist... produce no error message, but set
* the class members so that MoveToObjId() and GetCurObjType()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -