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

📄 shpopen.c

📁 用于读取TAB、MIF、SHP文件的类
💻 C
📖 第 1 页 / 共 5 页
字号:
{    int	       	nRecordOffset, i, nRecordSize=0;    uchar	*pabyRec;    int32	i32;    psSHP->bUpdated = TRUE;/* -------------------------------------------------------------------- *//*      Ensure that shape object matches the type of the file it is     *//*      being written to.                                               *//* -------------------------------------------------------------------- */    assert( psObject->nSHPType == psSHP->nShapeType             || psObject->nSHPType == SHPT_NULL );/* -------------------------------------------------------------------- *//*      Ensure that -1 is used for appends.  Either blow an             *//*      assertion, or if they are disabled, set the shapeid to -1       *//*      for appends.                                                    *//* -------------------------------------------------------------------- */    assert( nShapeId == -1             || (nShapeId >= 0 && nShapeId < psSHP->nRecords) );    if( nShapeId != -1 && nShapeId >= psSHP->nRecords )        nShapeId = -1;/* -------------------------------------------------------------------- *//*      Add the new entity to the in memory index.                      *//* -------------------------------------------------------------------- */    if( nShapeId == -1 && psSHP->nRecords+1 > psSHP->nMaxRecords )    {	psSHP->nMaxRecords =(int) ( psSHP->nMaxRecords * 1.3 + 100);	psSHP->panRecOffset = (int *)             SfRealloc(psSHP->panRecOffset,sizeof(int) * psSHP->nMaxRecords );	psSHP->panRecSize = (int *)             SfRealloc(psSHP->panRecSize,sizeof(int) * psSHP->nMaxRecords );    }/* -------------------------------------------------------------------- *//*      Initialize record.                                              *//* -------------------------------------------------------------------- */    pabyRec = (uchar *) malloc(psObject->nVertices * 4 * sizeof(double) 			       + psObject->nParts * 8 + 128);    /* -------------------------------------------------------------------- *//*  Extract vertices for a Polygon or Arc.				*//* -------------------------------------------------------------------- */    if( psObject->nSHPType == SHPT_POLYGON        || psObject->nSHPType == SHPT_POLYGONZ        || psObject->nSHPType == SHPT_POLYGONM        || psObject->nSHPType == SHPT_ARC         || psObject->nSHPType == SHPT_ARCZ        || psObject->nSHPType == SHPT_ARCM        || psObject->nSHPType == SHPT_MULTIPATCH )    {	int32		nPoints, nParts;	int    		i;	nPoints = psObject->nVertices;	nParts = psObject->nParts;	_SHPSetBounds( pabyRec + 12, psObject );	if( bBigEndian ) SwapWord( 4, &nPoints );	if( bBigEndian ) SwapWord( 4, &nParts );	ByteCopy( &nPoints, pabyRec + 40 + 8, 4 );	ByteCopy( &nParts, pabyRec + 36 + 8, 4 );        nRecordSize = 52;        /*         * Write part start positions.         */	ByteCopy( psObject->panPartStart, pabyRec + 44 + 8,                  4 * psObject->nParts );	for( i = 0; i < psObject->nParts; i++ )	{	    if( bBigEndian ) SwapWord( 4, pabyRec + 44 + 8 + 4*i );            nRecordSize += 4;	}        /*         * Write multipatch part types if needed.         */        if( psObject->nSHPType == SHPT_MULTIPATCH )        {            memcpy( pabyRec + nRecordSize, psObject->panPartType,                    4*psObject->nParts );            for( i = 0; i < psObject->nParts; i++ )            {                if( bBigEndian ) SwapWord( 4, pabyRec + nRecordSize );                nRecordSize += 4;            }        }        /*         * Write the (x,y) vertex values.         */	for( i = 0; i < psObject->nVertices; i++ )	{	    ByteCopy( psObject->padfX + i, pabyRec + nRecordSize, 8 );	    ByteCopy( psObject->padfY + i, pabyRec + nRecordSize + 8, 8 );	    if( bBigEndian )                SwapWord( 8, pabyRec + nRecordSize );            	    if( bBigEndian )                SwapWord( 8, pabyRec + nRecordSize + 8 );            nRecordSize += 2 * 8;	}        /*         * Write the Z coordinates (if any).         */        if( psObject->nSHPType == SHPT_POLYGONZ            || psObject->nSHPType == SHPT_ARCZ            || psObject->nSHPType == SHPT_MULTIPATCH )        {            ByteCopy( &(psObject->dfZMin), pabyRec + nRecordSize, 8 );            if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );            nRecordSize += 8;                        ByteCopy( &(psObject->dfZMax), pabyRec + nRecordSize, 8 );            if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );            nRecordSize += 8;            for( i = 0; i < psObject->nVertices; i++ )            {                ByteCopy( psObject->padfZ + i, pabyRec + nRecordSize, 8 );                if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );                nRecordSize += 8;            }        }        /*         * Write the M values, if any.         */        if( psObject->bMeasureIsUsed            && (psObject->nSHPType == SHPT_POLYGONM            || psObject->nSHPType == SHPT_ARCM#ifndef DISABLE_MULTIPATCH_MEASURE                        || psObject->nSHPType == SHPT_MULTIPATCH#endif                        || psObject->nSHPType == SHPT_POLYGONZ            || psObject->nSHPType == SHPT_ARCZ) )        {            ByteCopy( &(psObject->dfMMin), pabyRec + nRecordSize, 8 );            if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );            nRecordSize += 8;                        ByteCopy( &(psObject->dfMMax), pabyRec + nRecordSize, 8 );            if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );            nRecordSize += 8;            for( i = 0; i < psObject->nVertices; i++ )            {                ByteCopy( psObject->padfM + i, pabyRec + nRecordSize, 8 );                if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );                nRecordSize += 8;            }        }    }/* -------------------------------------------------------------------- *//*  Extract vertices for a MultiPoint.					*//* -------------------------------------------------------------------- */    else if( psObject->nSHPType == SHPT_MULTIPOINT             || psObject->nSHPType == SHPT_MULTIPOINTZ             || psObject->nSHPType == SHPT_MULTIPOINTM )    {	int32		nPoints;	int    		i;	nPoints = psObject->nVertices;        _SHPSetBounds( pabyRec + 12, psObject );	if( bBigEndian ) SwapWord( 4, &nPoints );	ByteCopy( &nPoints, pabyRec + 44, 4 );		for( i = 0; i < psObject->nVertices; i++ )	{	    ByteCopy( psObject->padfX + i, pabyRec + 48 + i*16, 8 );	    ByteCopy( psObject->padfY + i, pabyRec + 48 + i*16 + 8, 8 );	    if( bBigEndian ) SwapWord( 8, pabyRec + 48 + i*16 );	    if( bBigEndian ) SwapWord( 8, pabyRec + 48 + i*16 + 8 );	}	nRecordSize = 48 + 16 * psObject->nVertices;        if( psObject->nSHPType == SHPT_MULTIPOINTZ )        {            ByteCopy( &(psObject->dfZMin), pabyRec + nRecordSize, 8 );            if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );            nRecordSize += 8;            ByteCopy( &(psObject->dfZMax), pabyRec + nRecordSize, 8 );            if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );            nRecordSize += 8;                        for( i = 0; i < psObject->nVertices; i++ )            {                ByteCopy( psObject->padfZ + i, pabyRec + nRecordSize, 8 );                if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );                nRecordSize += 8;            }        }        if( psObject->bMeasureIsUsed            && (psObject->nSHPType == SHPT_MULTIPOINTZ            || psObject->nSHPType == SHPT_MULTIPOINTM) )        {            ByteCopy( &(psObject->dfMMin), pabyRec + nRecordSize, 8 );            if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );            nRecordSize += 8;            ByteCopy( &(psObject->dfMMax), pabyRec + nRecordSize, 8 );            if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );            nRecordSize += 8;                        for( i = 0; i < psObject->nVertices; i++ )            {                ByteCopy( psObject->padfM + i, pabyRec + nRecordSize, 8 );                if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );                nRecordSize += 8;            }        }    }/* -------------------------------------------------------------------- *//*      Write point.							*//* -------------------------------------------------------------------- */    else if( psObject->nSHPType == SHPT_POINT             || psObject->nSHPType == SHPT_POINTZ             || psObject->nSHPType == SHPT_POINTM )    {	ByteCopy( psObject->padfX, pabyRec + 12, 8 );	ByteCopy( psObject->padfY, pabyRec + 20, 8 );	if( bBigEndian ) SwapWord( 8, pabyRec + 12 );	if( bBigEndian ) SwapWord( 8, pabyRec + 20 );        nRecordSize = 28;                if( psObject->nSHPType == SHPT_POINTZ )        {            ByteCopy( psObject->padfZ, pabyRec + nRecordSize, 8 );            if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );            nRecordSize += 8;        }                if( psObject->bMeasureIsUsed            && (psObject->nSHPType == SHPT_POINTZ            || psObject->nSHPType == SHPT_POINTM) )        {            ByteCopy( psObject->padfM, pabyRec + nRecordSize, 8 );            if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );            nRecordSize += 8;        }    }/* -------------------------------------------------------------------- *//*      Not much to do for null geometries.                             *//* -------------------------------------------------------------------- */    else if( psObject->nSHPType == SHPT_NULL )    {        nRecordSize = 12;    }    else    {        /* unknown type */        assert( FALSE );    }/* -------------------------------------------------------------------- *//*      Establish where we are going to put this record. If we are      *//*      rewriting and existing record, and it will fit, then put it     *//*      back where the original came from.  Otherwise write at the end. *//* -------------------------------------------------------------------- */    if( nShapeId == -1 || psSHP->panRecSize[nShapeId] < nRecordSize-8 )    {        if( nShapeId == -1 )            nShapeId = psSHP->nRecords++;        psSHP->panRecOffset[nShapeId] = nRecordOffset = psSHP->nFileSize;        psSHP->panRecSize[nShapeId] = nRecordSize-8;        psSHP->nFileSize += nRecordSize;    }    else    {        nRecordOffset = psSHP->panRecOffset[nShapeId];        psSHP->panRecSize[nShapeId] = nRecordSize-8;    }    /* -------------------------------------------------------------------- *//*      Set the shape type, record number, and record size.             *//* -------------------------------------------------------------------- */    i32 = nShapeId+1;					/* record # */    if( !bBigEndian ) SwapWord( 4, &i32 );    ByteCopy( &i32, pabyRec, 4 );    i32 = (nRecordSize-8)/2;				/* record size */    if( !bBigEndian ) SwapWord( 4, &i32 );    ByteCopy( &i32, pabyRec + 4, 4 );    i32 = psObject->nSHPType;				/* shape type */    if( bBigEndian ) SwapWord( 4, &i32 );    ByteCopy( &i32, pabyRec + 8, 4 );/* -------------------------------------------------------------------- *//*      Write out record.                                               *//* -------------------------------------------------------------------- */    if( fseek( psSHP->fpSHP, nRecordOffset, 0 ) != 0        || fwrite( pabyRec, nRecordSize, 1, psSHP->fpSHP ) < 1 )    {#ifdef USE_CPL        CPLError( CE_Failure, CPLE_FileIO,                 "Error in fseek() or fwrite() writing object to .shp file." );#endif        free( pabyRec );        return -1;    }        free( pabyRec );/* -------------------------------------------------------------------- *//*	Expand file wide bounds based on this shape.			*//* -------------------------------------------------------------------- */    if( psSHP->adBoundsMin[0] == 0.0        && psSHP->adBoundsMax[0] == 0.0        && psSHP->adBoundsMin[1] == 0.0        && psSHP->adBoundsMax[1] == 0.0 )    {        if( psObject->nSHPType == SHPT_NULL || psObject->nVertices == 0 )        {            psSHP->adBoundsMin[0] = psSHP->adBoundsMax[0] = 0.0;            psSHP->adBoundsMin[1] = psSHP->adBoundsMax[1] = 0.0;            psSHP->adBoundsMin[2] = psSHP->adBoundsMax[2] = 0.0;            psSHP->adBoundsMin[3] = psSHP->adBoundsMax[3] = 0.0;        }        else        {            psSHP->adBoundsMin[0] = psSHP->adBoundsMax[0] = psObject->padfX[0];            psSHP->adBoundsMin[1] = psSHP->adBoundsMax[1] = psObject->padfY[0];            psSHP->adBoundsMin[2] = psSHP->adBoundsMax[2] = psObject->padfZ[0];            psSHP->adBoundsMin[3] = psSHP->adBoundsMax[3] = psObject->padfM[0];        }    }    for( i = 0; i < psObject->nVertices; i++ )

⌨️ 快捷键说明

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