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

📄 shpopen.c

📁 GIS系统支持库Geospatial Data Abstraction Library代码.GDAL is a translator library for raster geospatial dat
💻 C
📖 第 1 页 / 共 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;/* -------------------------------------------------------------------- *//*	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;        }    }/* -------------------------------------------------------------------- *//*      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];        }    }/* -------------------------------------------------------------------- *//*      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 )		      {    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;

⌨️ 快捷键说明

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