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

📄 shpopen.c

📁 用于读取TAB、MIF、SHP文件的类
💻 C
📖 第 1 页 / 共 5 页
字号:
            padfMaxBound[i] = psSHP->adBoundsMax[i];    }}/************************************************************************//*                             SHPCreate()                              *//*                                                                      *//*      Create a new shape file and return a handle to the open         *//*      shape file with read/write access.                              *//************************************************************************/SHPHandle SHPAPI_CALLSHPCreate( const char * pszLayer, int nShapeType ){    char	*pszBasename, *pszFullname;    int		i;    FILE	*fpSHP, *fpSHX;    uchar     	abyHeader[100];    int32	i32;    double	dValue;    /* -------------------------------------------------------------------- *//*      Establish the byte order on this system.                        *//* -------------------------------------------------------------------- */    i = 1;    if( *((uchar *) &i) == 1 )        bBigEndian = FALSE;    else        bBigEndian = TRUE;/* -------------------------------------------------------------------- *//*	Compute the base (layer) name.  If there is any extension	*//*	on the passed in filename we will strip it off.			*//* -------------------------------------------------------------------- */    pszBasename = (char *) malloc(strlen(pszLayer)+5);    strcpy( pszBasename, pszLayer );    for( i = strlen(pszBasename)-1; 	 i > 0 && pszBasename[i] != '.' && pszBasename[i] != '/'	       && pszBasename[i] != '\\';	 i-- ) {}    if( pszBasename[i] == '.' )        pszBasename[i] = '\0';/* -------------------------------------------------------------------- *//*      Open the two files so we can write their headers.               *//* -------------------------------------------------------------------- */    pszFullname = (char *) malloc(strlen(pszBasename) + 5);    sprintf( pszFullname, "%s.shp", pszBasename );    fpSHP = fopen(pszFullname, "wb" );    if( fpSHP == NULL )    {#ifdef USE_CPL        CPLError( CE_Failure, CPLE_AppDefined,                   "Failed to create file %s.",                  pszFullname );#endif        return( NULL );    }    sprintf( pszFullname, "%s.shx", pszBasename );    fpSHX = fopen(pszFullname, "wb" );    if( fpSHX == NULL )    {#ifdef USE_CPL        CPLError( CE_Failure, CPLE_AppDefined,                   "Failed to create file %s.",                  pszFullname );#endif        return( NULL );    }    free( pszFullname );    free( pszBasename );/* -------------------------------------------------------------------- *//*      Prepare header block for .shp file.                             *//* -------------------------------------------------------------------- */    for( i = 0; i < 100; i++ )      abyHeader[i] = 0;    abyHeader[2] = 0x27;				/* magic cookie */    abyHeader[3] = 0x0a;    i32 = 50;						/* 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 = nShapeType;					/* shape type */    ByteCopy( &i32, abyHeader+32, 4 );    if( bBigEndian ) SwapWord( 4, abyHeader+32 );    dValue = 0.0;					/* set bounds */    ByteCopy( &dValue, abyHeader+36, 8 );    ByteCopy( &dValue, abyHeader+44, 8 );    ByteCopy( &dValue, abyHeader+52, 8 );    ByteCopy( &dValue, abyHeader+60, 8 );/* -------------------------------------------------------------------- *//*      Write .shp file header.                                         *//* -------------------------------------------------------------------- */    if( fwrite( abyHeader, 100, 1, fpSHP ) != 1 )    {#ifdef USE_CPL        CPLError( CE_Failure, CPLE_AppDefined,                   "Failed to write .shp header." );#endif        return NULL;    }/* -------------------------------------------------------------------- *//*      Prepare, and write .shx file header.                            *//* -------------------------------------------------------------------- */    i32 = 50;						/* file size */    ByteCopy( &i32, abyHeader+24, 4 );    if( !bBigEndian ) SwapWord( 4, abyHeader+24 );        if( fwrite( abyHeader, 100, 1, fpSHX ) != 1 )    {#ifdef USE_CPL        CPLError( CE_Failure, CPLE_AppDefined,                   "Failed to write .shx header." );#endif        return NULL;    }/* -------------------------------------------------------------------- *//*      Close the files, and then open them as regular existing files.  *//* -------------------------------------------------------------------- */    fclose( fpSHP );    fclose( fpSHX );    return( SHPOpen( pszLayer, "r+b" ) );}/************************************************************************//*                           _SHPSetBounds()                            *//*                                                                      *//*      Compute a bounds rectangle for a shape, and set it into the     *//*      indicated location in the record.                               *//************************************************************************/static void	_SHPSetBounds( uchar * pabyRec, SHPObject * psShape ){    ByteCopy( &(psShape->dfXMin), pabyRec +  0, 8 );    ByteCopy( &(psShape->dfYMin), pabyRec +  8, 8 );    ByteCopy( &(psShape->dfXMax), pabyRec + 16, 8 );    ByteCopy( &(psShape->dfYMax), pabyRec + 24, 8 );    if( bBigEndian )    {        SwapWord( 8, pabyRec + 0 );        SwapWord( 8, pabyRec + 8 );        SwapWord( 8, pabyRec + 16 );        SwapWord( 8, pabyRec + 24 );    }}/************************************************************************//*                         SHPComputeExtents()                          *//*                                                                      *//*      Recompute the extents of a shape.  Automatically done by        *//*      SHPCreateObject().                                              *//************************************************************************/void SHPAPI_CALLSHPComputeExtents( SHPObject * psObject ){    int		i;    /* -------------------------------------------------------------------- *//*      Build extents for this object.                                  *//* -------------------------------------------------------------------- */    if( psObject->nVertices > 0 )    {        psObject->dfXMin = psObject->dfXMax = psObject->padfX[0];        psObject->dfYMin = psObject->dfYMax = psObject->padfY[0];        psObject->dfZMin = psObject->dfZMax = psObject->padfZ[0];        psObject->dfMMin = psObject->dfMMax = psObject->padfM[0];    }        for( i = 0; i < psObject->nVertices; i++ )    {        psObject->dfXMin = MIN(psObject->dfXMin, psObject->padfX[i]);        psObject->dfYMin = MIN(psObject->dfYMin, psObject->padfY[i]);        psObject->dfZMin = MIN(psObject->dfZMin, psObject->padfZ[i]);        psObject->dfMMin = MIN(psObject->dfMMin, psObject->padfM[i]);        psObject->dfXMax = MAX(psObject->dfXMax, psObject->padfX[i]);        psObject->dfYMax = MAX(psObject->dfYMax, psObject->padfY[i]);        psObject->dfZMax = MAX(psObject->dfZMax, psObject->padfZ[i]);        psObject->dfMMax = MAX(psObject->dfMMax, psObject->padfM[i]);    }}/************************************************************************//*                          SHPCreateObject()                           *//*                                                                      *//*      Create a shape object.  It should be freed with                 *//*      SHPDestroyObject().                                             *//************************************************************************/SHPObject SHPAPI_CALL1(*)SHPCreateObject( int nSHPType, int nShapeId, int nParts,                 const int * panPartStart, const int * panPartType,                 int nVertices, const double *padfX, const double *padfY,                 const double * padfZ, const double * padfM ){    SHPObject	*psObject;    int		i, bHasM, bHasZ;    psObject = (SHPObject *) calloc(1,sizeof(SHPObject));    psObject->nSHPType = nSHPType;    psObject->nShapeId = nShapeId;    psObject->bMeasureIsUsed = FALSE;/* -------------------------------------------------------------------- *//*	Establish whether this shape type has M, and Z values.		*//* -------------------------------------------------------------------- */    if( nSHPType == SHPT_ARCM        || nSHPType == SHPT_POINTM        || nSHPType == SHPT_POLYGONM        || nSHPType == SHPT_MULTIPOINTM )    {        bHasM = TRUE;        bHasZ = FALSE;    }    else if( nSHPType == SHPT_ARCZ             || nSHPType == SHPT_POINTZ             || nSHPType == SHPT_POLYGONZ             || nSHPType == SHPT_MULTIPOINTZ             || nSHPType == SHPT_MULTIPATCH )    {        bHasM = TRUE;        bHasZ = TRUE;    }    else    {        bHasM = FALSE;        bHasZ = FALSE;    }/* -------------------------------------------------------------------- *//*      Capture parts.  Note that part type is optional, and            *//*      defaults to ring.                                               *//* -------------------------------------------------------------------- */    if( nSHPType == SHPT_ARC || nSHPType == SHPT_POLYGON        || nSHPType == SHPT_ARCM || nSHPType == SHPT_POLYGONM        || nSHPType == SHPT_ARCZ || nSHPType == SHPT_POLYGONZ        || nSHPType == SHPT_MULTIPATCH )    {        psObject->nParts = MAX(1,nParts);        psObject->panPartStart = (int *)            malloc(sizeof(int) * psObject->nParts);        psObject->panPartType = (int *)            malloc(sizeof(int) * psObject->nParts);        psObject->panPartStart[0] = 0;        psObject->panPartType[0] = SHPP_RING;                for( i = 0; i < nParts; i++ )        {            psObject->panPartStart[i] = panPartStart[i];            if( panPartType != NULL )                psObject->panPartType[i] = panPartType[i];            else                psObject->panPartType[i] = SHPP_RING;        }        if( psObject->panPartStart[0] != 0 )        {#ifdef USE_CPL            CPLError( CE_Failure, CPLE_AppDefined,                      "panPartStart[0] != 0, patching internally.  Please fix your code!\n" );#else            fprintf( stderr, "panPartStart[0] != 0, patching internally.  Please fix your code!\n" );#endif            psObject->panPartStart[0] = 0;        }    }/* -------------------------------------------------------------------- *//*      Capture vertices.  Note that Z and M are optional, but X and    *//*      Y are not.                                                      *//* -------------------------------------------------------------------- */    if( nVertices > 0 )    {        psObject->padfX = (double *) calloc(sizeof(double),nVertices);        psObject->padfY = (double *) calloc(sizeof(double),nVertices);        psObject->padfZ = (double *) calloc(sizeof(double),nVertices);        psObject->padfM = (double *) calloc(sizeof(double),nVertices);        assert( padfX != NULL );        assert( padfY != NULL );            for( i = 0; i < nVertices; i++ )        {            psObject->padfX[i] = padfX[i];            psObject->padfY[i] = padfY[i];            if( padfZ != NULL && bHasZ )                psObject->padfZ[i] = padfZ[i];            if( padfM != NULL && bHasM )                psObject->padfM[i] = padfM[i];        }        if( padfM != NULL && bHasM )            psObject->bMeasureIsUsed = TRUE;    }/* -------------------------------------------------------------------- *//*      Compute the extents.                                            *//* -------------------------------------------------------------------- */    psObject->nVertices = nVertices;    SHPComputeExtents( psObject );    return( psObject );}/************************************************************************//*                       SHPCreateSimpleObject()                        *//*                                                                      *//*      Create a simple (common) shape object.  Destroy with            *//*      SHPDestroyObject().                                             *//************************************************************************/SHPObject SHPAPI_CALL1(*)SHPCreateSimpleObject( int nSHPType, int nVertices,                       const double * padfX, const double * padfY,                       const double * padfZ ){    return( SHPCreateObject( nSHPType, -1, 0, NULL, NULL,                             nVertices, padfX, padfY, padfZ, NULL ) );}                                  /************************************************************************//*                           SHPWriteObject()                           *//*                                                                      *//*      Write out the vertices of a new structure.  Note that it is     *//*      only possible to write vertices at the end of the file.         *//************************************************************************/int SHPAPI_CALLSHPWriteObject(SHPHandle psSHP, int nShapeId, SHPObject * psObject )		      

⌨️ 快捷键说明

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