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

📄 shpopen.c

📁 包含详细例子的c#创建shape文件的开源码
💻 C
📖 第 1 页 / 共 5 页
字号:
        if( psSHP->panRecSize[hEntity]+8 >= nOffset + 16 + 8*nPoints )        {            memcpy( &(psShape->dfMMin), psSHP->pabyRec + nOffset, 8 );            memcpy( &(psShape->dfMMax), psSHP->pabyRec + nOffset + 8, 8 );            if( bBigEndian ) SwapWord( 8, &(psShape->dfMMin) );            if( bBigEndian ) SwapWord( 8, &(psShape->dfMMax) );            for( i = 0; i < nPoints; i++ )            {                memcpy( psShape->padfM + i,                        psSHP->pabyRec + nOffset + 16 + i*8, 8 );                if( bBigEndian ) SwapWord( 8, psShape->padfM + i );            }        }    }/* ==================================================================== *//*  Extract vertices for a MultiPoint.					*//* ==================================================================== */    else if( psShape->nSHPType == SHPT_MULTIPOINT             || psShape->nSHPType == SHPT_MULTIPOINTM             || psShape->nSHPType == SHPT_MULTIPOINTZ )    {	int32		nPoints;	int    		i, nOffset;	memcpy( &nPoints, psSHP->pabyRec + 44, 4 );	if( bBigEndian ) SwapWord( 4, &nPoints );	psShape->nVertices = nPoints;        psShape->padfX = (double *) calloc(nPoints,sizeof(double));        psShape->padfY = (double *) calloc(nPoints,sizeof(double));        psShape->padfZ = (double *) calloc(nPoints,sizeof(double));        psShape->padfM = (double *) calloc(nPoints,sizeof(double));	for( i = 0; i < nPoints; i++ )	{	    memcpy(psShape->padfX+i, psSHP->pabyRec + 48 + 16 * i, 8 );	    memcpy(psShape->padfY+i, psSHP->pabyRec + 48 + 16 * i + 8, 8 );	    if( bBigEndian ) SwapWord( 8, psShape->padfX + i );	    if( bBigEndian ) SwapWord( 8, psShape->padfY + i );	}        nOffset = 48 + 16*nPoints;/* -------------------------------------------------------------------- *//*	Get the X/Y bounds.						*//* -------------------------------------------------------------------- */        memcpy( &(psShape->dfXMin), psSHP->pabyRec + 8 +  4, 8 );        memcpy( &(psShape->dfYMin), psSHP->pabyRec + 8 + 12, 8 );        memcpy( &(psShape->dfXMax), psSHP->pabyRec + 8 + 20, 8 );        memcpy( &(psShape->dfYMax), psSHP->pabyRec + 8 + 28, 8 );	if( bBigEndian ) SwapWord( 8, &(psShape->dfXMin) );	if( bBigEndian ) SwapWord( 8, &(psShape->dfYMin) );	if( bBigEndian ) SwapWord( 8, &(psShape->dfXMax) );	if( bBigEndian ) SwapWord( 8, &(psShape->dfYMax) );/* -------------------------------------------------------------------- *//*      If we have a Z coordinate, collect that now.                    *//* -------------------------------------------------------------------- */        if( psShape->nSHPType == SHPT_MULTIPOINTZ )        {            memcpy( &(psShape->dfZMin), psSHP->pabyRec + nOffset, 8 );            memcpy( &(psShape->dfZMax), psSHP->pabyRec + nOffset + 8, 8 );            if( bBigEndian ) SwapWord( 8, &(psShape->dfZMin) );            if( bBigEndian ) SwapWord( 8, &(psShape->dfZMax) );            for( i = 0; i < nPoints; i++ )            {                memcpy( psShape->padfZ + i,                        psSHP->pabyRec + nOffset + 16 + i*8, 8 );                if( bBigEndian ) SwapWord( 8, psShape->padfZ + i );            }            nOffset += 16 + 8*nPoints;        }/* -------------------------------------------------------------------- *//*      If we have a M measure value, then read it now.  We assume      *//*      that the measure can be present for any shape if the size is    *//*      big enough, but really it will only occur for the Z shapes      *//*      (options), and the M shapes.                                    *//* -------------------------------------------------------------------- */        if( psSHP->panRecSize[hEntity]+8 >= nOffset + 16 + 8*nPoints )        {            memcpy( &(psShape->dfMMin), psSHP->pabyRec + nOffset, 8 );            memcpy( &(psShape->dfMMax), psSHP->pabyRec + nOffset + 8, 8 );            if( bBigEndian ) SwapWord( 8, &(psShape->dfMMin) );            if( bBigEndian ) SwapWord( 8, &(psShape->dfMMax) );            for( i = 0; i < nPoints; i++ )            {                memcpy( psShape->padfM + i,                        psSHP->pabyRec + nOffset + 16 + i*8, 8 );                if( bBigEndian ) SwapWord( 8, psShape->padfM + i );            }        }    }/* ==================================================================== *//*      Extract vertices for a point.                                   *//* ==================================================================== */    else if( psShape->nSHPType == SHPT_POINT             || psShape->nSHPType == SHPT_POINTM             || psShape->nSHPType == SHPT_POINTZ )    {        int	nOffset;	psShape->nVertices = 1;        psShape->padfX = (double *) calloc(1,sizeof(double));        psShape->padfY = (double *) calloc(1,sizeof(double));        psShape->padfZ = (double *) calloc(1,sizeof(double));        psShape->padfM = (double *) calloc(1,sizeof(double));	memcpy( psShape->padfX, psSHP->pabyRec + 12, 8 );	memcpy( psShape->padfY, psSHP->pabyRec + 20, 8 );	if( bBigEndian ) SwapWord( 8, psShape->padfX );	if( bBigEndian ) SwapWord( 8, psShape->padfY );        nOffset = 20 + 8;/* -------------------------------------------------------------------- *//*      If we have a Z coordinate, collect that now.                    *//* -------------------------------------------------------------------- */        if( psShape->nSHPType == SHPT_POINTZ )        {            memcpy( psShape->padfZ, psSHP->pabyRec + nOffset, 8 );            if( bBigEndian ) SwapWord( 8, psShape->padfZ );            nOffset += 8;        }/* -------------------------------------------------------------------- *//*      If we have a M measure value, then read it now.  We assume      *//*      that the measure can be present for any shape if the size is    *//*      big enough, but really it will only occur for the Z shapes      *//*      (options), and the M shapes.                                    *//* -------------------------------------------------------------------- */        if( psSHP->panRecSize[hEntity]+8 >= nOffset + 8 )        {            memcpy( psShape->padfM, psSHP->pabyRec + nOffset, 8 );            if( bBigEndian ) SwapWord( 8, psShape->padfM );        }/* -------------------------------------------------------------------- *//*      Since no extents are supplied in the record, we will apply      *//*      them from the single vertex.                                    *//* -------------------------------------------------------------------- */        psShape->dfXMin = psShape->dfXMax = psShape->padfX[0];        psShape->dfYMin = psShape->dfYMax = psShape->padfY[0];        psShape->dfZMin = psShape->dfZMax = psShape->padfZ[0];        psShape->dfMMin = psShape->dfMMax = psShape->padfM[0];    }    return( psShape );}/************************************************************************//*                            SHPTypeName()                             *//************************************************************************/const char SHPAPI_CALL1(*)SHPTypeName( int nSHPType ){    switch( nSHPType )    {      case SHPT_NULL:        return "NullShape";      case SHPT_POINT:        return "Point";      case SHPT_ARC:        return "Arc";      case SHPT_POLYGON:        return "Polygon";      case SHPT_MULTIPOINT:        return "MultiPoint";      case SHPT_POINTZ:        return "PointZ";      case SHPT_ARCZ:        return "ArcZ";      case SHPT_POLYGONZ:        return "PolygonZ";      case SHPT_MULTIPOINTZ:        return "MultiPointZ";      case SHPT_POINTM:        return "PointM";      case SHPT_ARCM:        return "ArcM";      case SHPT_POLYGONM:        return "PolygonM";      case SHPT_MULTIPOINTM:        return "MultiPointM";      case SHPT_MULTIPATCH:        return "MultiPatch";      default:        return "UnknownShapeType";    }}/************************************************************************//*                          SHPPartTypeName()                           *//************************************************************************/const char SHPAPI_CALL1(*)SHPPartTypeName( int nPartType ){    switch( nPartType )    {      case SHPP_TRISTRIP:        return "TriangleStrip";      case SHPP_TRIFAN:        return "TriangleFan";      case SHPP_OUTERRING:        return "OuterRing";      case SHPP_INNERRING:        return "InnerRing";      case SHPP_FIRSTRING:        return "FirstRing";      case SHPP_RING:        return "Ring";      default:        return "UnknownPartType";    }}/************************************************************************//*                          SHPDestroyObject()                          *//************************************************************************/void SHPAPI_CALL
SHPDestroyObject( SHPObject * psShape )

{
    if( psShape == NULL )
        return;
    
    if( psShape->padfX != NULL )
        free( psShape->padfX );
    if( psShape->padfY != NULL )
        free( psShape->padfY );
    if( psShape->padfZ != NULL )
        free( psShape->padfZ );
    if( psShape->padfM != NULL )
        free( psShape->padfM );

    if( psShape->panPartStart != NULL )
        free( psShape->panPartStart );
    if( psShape->panPartType != NULL )
        free( psShape->panPartType );

    free( psShape );
}
/************************************************************************//*                          SHPRewindObject()                           *//*                                                                      *//*      Reset the winding of polygon objects to adhere to the           *//*      specification.                                                  *//************************************************************************/int SHPAPI_CALLSHPRewindObject( SHPHandle hSHP, SHPObject * psObject ){    int  iOpRing, bAltered = 0;/* -------------------------------------------------------------------- *//*      Do nothing if this is not a polygon object.                     *//* -------------------------------------------------------------------- */    if( psObject->nSHPType != SHPT_POLYGON        && psObject->nSHPType != SHPT_POLYGONZ        && psObject->nSHPType != SHPT_POLYGONM )        return 0;/* -------------------------------------------------------------------- *//*      Process each of the rings.                                      *//* -------------------------------------------------------------------- */    for( iOpRing = 0; iOpRing < psObject->nParts; iOpRing++ )    {        int      bInner, iVert, nVertCount, nVertStart, iCheckRing;        double   dfSum, dfTestX, dfTestY;/* -------------------------------------------------------------------- *//*      Determine if this ring is an inner ring or an outer ring        *//*      relative to all the other rings.  For now we assume the         *//*      first ring is outer and all others are inner, but eventually    *//*      we need to fix this to handle multiple island polygons and      *//*      unordered sets of rings.                                        *//* -------------------------------------------------------------------- */        dfTestX = psObject->padfX[psObject->panPartStart[iOpRing]];        dfTestY = psObject->padfY[psObject->panPartStart[iOpRing]];        bInner = FALSE;        for( iCheckRing = 0; iCheckRing < psObject->nParts; iCheckRing++ )        {            int iEdge;            if( iCheckRing == iOpRing )                continue;            nVertStart = psObject->panPartStart[iCheckRing];            if( iCheckRing == psObject->nParts-1 )                nVertCount = psObject->nVertices                    - psObject->panPartStart[iCheckRing];            else                nVertCount = psObject->panPartStart[iCheckRing+1]                    - psObject->panPartStart[iCheckRing];            for( iEdge = 0; iEdge < nVertCount; iEdge++ )            {                int iNext;                if( iEdge < nVertCount-1 )                    iNext = iEdge+1;                else                    iNext = 0;                if( (psObject->padfY[iEdge+nVertStart] < dfTestY                     && psObject->padfY[iNext+nVertStart] >= dfTestY)                    || (psObject->padfY[iNext+nVertStart] < dfTestY                        && psObject->padfY[iEdge+nVertStart] >= dfTestY) )                {                    if( psObject->padfX[iEdge+nVertStart]                   

⌨️ 快捷键说明

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