📄 shpopen.cpp
字号:
/******************************************************************************
* $Id: shpopen.c,v 1.39 2002/08/26 06:46:56 warmerda Exp $
*
* Project: Shapelib
* Purpose: Implementation of core Shapefile read/write functions.
* Author: Frank Warmerdam, warmerdam@pobox.com
*
******************************************************************************
* Copyright (c) 1999, 2001, Frank Warmerdam
*
* This software is available under the following "MIT Style" license,
* or at the option of the licensee under the LGPL (see LICENSE.LGPL). This
* option is discussed in more detail in shapelib.html.
*
* --
*
* 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: shpopen.c,v $
* Revision 1.39 2002/08/26 06:46:56 warmerda
* avoid c++ comments
*
* Revision 1.38 2002/05/07 16:43:39 warmerda
* Removed debugging printf.
*
* Revision 1.37 2002/04/10 17:35:22 warmerda
* fixed bug in ring reversal code
*
* Revision 1.36 2002/04/10 16:59:54 warmerda
* added SHPRewindObject
*
* Revision 1.35 2001/12/07 15:10:44 warmerda
* fix if .shx fails to open
*
* Revision 1.34 2001/11/01 16:29:55 warmerda
* move pabyRec into SHPInfo for thread safety
*
* Revision 1.33 2001/07/03 12:18:15 warmerda
* Improved cleanup if SHX not found, provied by Riccardo Cohen.
*
* Revision 1.32 2001/06/22 01:58:07 warmerda
* be more careful about establishing initial bounds in face of NULL shapes
*
* Revision 1.31 2001/05/31 19:35:29 warmerda
* added support for writing null shapes
*
* Revision 1.30 2001/05/28 12:46:29 warmerda
* Add some checking on reasonableness of record count when opening.
*
* Revision 1.29 2001/05/23 13:36:52 warmerda
* added use of SHPAPI_CALL
*
* Revision 1.28 2001/02/06 22:25:06 warmerda
* fixed memory leaks when SHPOpen() fails
*
* Revision 1.27 2000/07/18 15:21:33 warmerda
* added better enforcement of -1 for append in SHPWriteObject
*
* Revision 1.26 2000/02/16 16:03:51 warmerda
* added null shape support
*
* Revision 1.25 1999/12/15 13:47:07 warmerda
* Fixed record size settings in .shp file (was 4 words too long)
* Added stdlib.h.
*
* Revision 1.24 1999/11/05 14:12:04 warmerda
* updated license terms
*
* Revision 1.23 1999/07/27 00:53:46 warmerda
* added support for rewriting shapes
*
* Revision 1.22 1999/06/11 19:19:11 warmerda
* Cleanup pabyRec static buffer on SHPClose().
*
* Revision 1.21 1999/06/02 14:57:56 kshih
* Remove unused variables
*
* Revision 1.20 1999/04/19 21:04:17 warmerda
* Fixed syntax error.
*
* Revision 1.19 1999/04/19 21:01:57 warmerda
* Force access string to binary in SHPOpen().
*
* Revision 1.18 1999/04/01 18:48:07 warmerda
* Try upper case extensions if lower case doesn't work.
*
* Revision 1.17 1998/12/31 15:29:39 warmerda
* Disable writing measure values to multipatch objects if
* DISABLE_MULTIPATCH_MEASURE is defined.
*
* Revision 1.16 1998/12/16 05:14:33 warmerda
* Added support to write MULTIPATCH. Fixed reading Z coordinate of
* MULTIPATCH. Fixed record size written for all feature types.
*
* Revision 1.15 1998/12/03 16:35:29 warmerda
* r+b is proper binary access string, not rb+.
*
* Revision 1.14 1998/12/03 15:47:56 warmerda
* Fixed setting of nVertices in SHPCreateObject().
*
* Revision 1.13 1998/12/03 15:33:54 warmerda
* Made SHPCalculateExtents() separately callable.
*
* Revision 1.12 1998/11/11 20:01:50 warmerda
* Fixed bug writing ArcM/Z, and PolygonM/Z for big endian machines.
*
* Revision 1.11 1998/11/09 20:56:44 warmerda
* Fixed up handling of file wide bounds.
*
* Revision 1.10 1998/11/09 20:18:51 warmerda
* Converted to support 3D shapefiles, and use of SHPObject.
*
* Revision 1.9 1998/02/24 15:09:05 warmerda
* Fixed memory leak.
*
* Revision 1.8 1997/12/04 15:40:29 warmerda
* Fixed byte swapping of record number, and record length fields in the
* .shp file.
*
* Revision 1.7 1995/10/21 03:15:58 warmerda
* Added support for binary file access, the magic cookie 9997
* and tried to improve the int32 selection logic for 16bit systems.
*
* Revision 1.6 1995/09/04 04:19:41 warmerda
* Added fix for file bounds.
*
* Revision 1.5 1995/08/25 15:16:44 warmerda
* Fixed a couple of problems with big endian systems ... one with bounds
* and the other with multipart polygons.
*
* Revision 1.4 1995/08/24 18:10:17 warmerda
* Switch to use SfRealloc() to avoid problems with pre-ANSI realloc()
* functions (such as on the Sun).
*
* Revision 1.3 1995/08/23 02:23:15 warmerda
* Added support for reading bounds, and fixed up problems in setting the
* file wide bounds.
*
* Revision 1.2 1995/08/04 03:16:57 warmerda
* Added header.
*
*/
#include "StdAfx.h"
static char rcsid[] =
"$Id: shpopen.c,v 1.39 2002/08/26 06:46:56 warmerda Exp $";
#include "shapefil.h"
#include <math.h>
#include <limits.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
typedef unsigned char uchar;
#if UINT_MAX == 65535
typedef long int32;
#else
typedef int int32;
#endif
#ifndef FALSE
# define FALSE 0
# define TRUE 1
#endif
#define ByteCopy( a, b, c ) memcpy( b, a, c )
#ifndef MAX
# define MIN(a,b) ((a<b) ? a : b)
# define MAX(a,b) ((a>b) ? a : b)
#endif
static int bBigEndian;
/************************************************************************/
/* SwapWord() */
/* */
/* Swap a 2, 4 or 8 byte word. */
/************************************************************************/
static void SwapWord( int length, void * wordP )
{
int i;
uchar temp;
for( i=0; i < length/2; i++ )
{
temp = ((uchar *) wordP)[i];
((uchar *)wordP)[i] = ((uchar *) wordP)[length-i-1];
((uchar *) wordP)[length-i-1] = temp;
}
}
/************************************************************************/
/* SfRealloc() */
/* */
/* A realloc cover function that will access a NULL pointer as */
/* a valid input. */
/************************************************************************/
static void * SfRealloc( void * pMem, int nNewSize )
{
if( pMem == NULL )
return( (void *) malloc(nNewSize) );
else
return( (void *) realloc(pMem,nNewSize) );
}
/************************************************************************/
/* SHPWriteHeader() */
/* */
/* Write out a header for the .shp and .shx files as well as the */
/* contents of the index (.shx) file. */
/************************************************************************/
static void SHPWriteHeader( SHPHandle psSHP )
{
uchar abyHeader[100];
int i;
int32 i32;
double dValue;
int32 *panSHX;
/* -------------------------------------------------------------------- */
/* Prepare header block for .shp file. */
/* -------------------------------------------------------------------- */
for( i = 0; i < 100; i++ )
abyHeader[i] = 0;
abyHeader[2] = 0x27; /* magic cookie */
abyHeader[3] = 0x0a;
i32 = psSHP->nFileSize/2; /* file size */
ByteCopy( &i32, abyHeader+24, 4 );
if( !bBigEndian ) SwapWord( 4, abyHeader+24 );
i32 = 1000; /* version */
ByteCopy( &i32, abyHeader+28, 4 );
if( bBigEndian ) SwapWord( 4, abyHeader+28 );
i32 = psSHP->nShapeType; /* shape type */
ByteCopy( &i32, abyHeader+32, 4 );
if( bBigEndian ) SwapWord( 4, abyHeader+32 );
dValue = psSHP->adBoundsMin[0]; /* set bounds */
ByteCopy( &dValue, abyHeader+36, 8 );
if( bBigEndian ) SwapWord( 8, abyHeader+36 );
dValue = psSHP->adBoundsMin[1];
ByteCopy( &dValue, abyHeader+44, 8 );
if( bBigEndian ) SwapWord( 8, abyHeader+44 );
dValue = psSHP->adBoundsMax[0];
ByteCopy( &dValue, abyHeader+52, 8 );
if( bBigEndian ) SwapWord( 8, abyHeader+52 );
dValue = psSHP->adBoundsMax[1];
ByteCopy( &dValue, abyHeader+60, 8 );
if( bBigEndian ) SwapWord( 8, abyHeader+60 );
dValue = psSHP->adBoundsMin[2]; /* z */
ByteCopy( &dValue, abyHeader+68, 8 );
if( bBigEndian ) SwapWord( 8, abyHeader+68 );
dValue = psSHP->adBoundsMax[2];
ByteCopy( &dValue, abyHeader+76, 8 );
if( bBigEndian ) SwapWord( 8, abyHeader+76 );
dValue = psSHP->adBoundsMin[3]; /* m */
ByteCopy( &dValue, abyHeader+84, 8 );
if( bBigEndian ) SwapWord( 8, abyHeader+84 );
dValue = psSHP->adBoundsMax[3];
ByteCopy( &dValue, abyHeader+92, 8 );
if( bBigEndian ) SwapWord( 8, abyHeader+92 );
/* -------------------------------------------------------------------- */
/* Write .shp file header. */
/* -------------------------------------------------------------------- */
fseek( psSHP->fpSHP, 0, 0 );
fwrite( abyHeader, 100, 1, psSHP->fpSHP );
/* -------------------------------------------------------------------- */
/* Prepare, and write .shx file header. */
/* -------------------------------------------------------------------- */
i32 = (psSHP->nRecords * 2 * sizeof(int32) + 100)/2; /* file size */
ByteCopy( &i32, abyHeader+24, 4 );
if( !bBigEndian ) SwapWord( 4, abyHeader+24 );
fseek( psSHP->fpSHX, 0, 0 );
fwrite( abyHeader, 100, 1, psSHP->fpSHX );
/* -------------------------------------------------------------------- */
/* Write out the .shx contents. */
/* -------------------------------------------------------------------- */
panSHX = (int32 *) malloc(sizeof(int32) * 2 * psSHP->nRecords);
for( i = 0; i < psSHP->nRecords; i++ )
{
panSHX[i*2 ] = psSHP->panRecOffset[i]/2;
panSHX[i*2+1] = psSHP->panRecSize[i]/2;
if( !bBigEndian ) SwapWord( 4, panSHX+i*2 );
if( !bBigEndian ) SwapWord( 4, panSHX+i*2+1 );
}
fwrite( panSHX, sizeof(int32) * 2, psSHP->nRecords, psSHP->fpSHX );
free( panSHX );
}
/************************************************************************/
/* SHPOpen() */
/* */
/* Open the .shp and .shx files based on the basename of the */
/* files or either file name. */
/************************************************************************/
SHPHandle SHPAPI_CALL
SHPOpen( const char * pszLayer, const char * pszAccess )
{
char *pszFullname, *pszBasename;
SHPHandle psSHP;
uchar *pabyBuf;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -