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

📄 shape2ogr.cpp

📁 用于读取TAB、MIF、SHP文件的类
💻 CPP
📖 第 1 页 / 共 4 页
字号:
                        poOGRPoly->addRingDirectly( poRing );                    }                }            }            else            {                 OGRGeometryCollection *poOGRGeCo = NULL;                poOGR = poOGRGeCo = new OGRMultiPolygon();                for ( iRing = 0; iRing < nOuter; iRing++ )                {                    /* Outer rings */                    int oRing;                     OGRPolygon    *poOGRPoly = NULL;                    OGRLinearRing *poRing = NULL;                    oRing = outer[iRing];                    /* Outer */                    poOGRPoly = new OGRPolygon();                    poRing = CreateLinearRing ( psShape, oRing );                    poOGRPoly->addRingDirectly( poRing );                    /* Inner */                    for( int iRing2 = 0; iRing2 < psShape->nParts; iRing2++ )                    {                        if ( outside[iRing2] == oRing )                        {                            poRing = CreateLinearRing ( psShape, iRing2 );                            poOGRPoly->addRingDirectly( poRing );                        }                    }                                        poOGRGeCo->addGeometryDirectly (poOGRPoly);                }            }            CPLFree( direction );            CPLFree( outer );            CPLFree( outside );        } /* End of multipart polygon processing. */        if( psShape->nSHPType == SHPT_POLYGON )        {            poOGR->setCoordinateDimension( 2 );        }    }/* -------------------------------------------------------------------- *//*      Otherwise for now we just ignore the object.  Eventually we     *//*      should implement multipatch.                                    *//* -------------------------------------------------------------------- */    else    {        if( psShape->nSHPType != SHPT_NULL )        {            CPLDebug( "OGR", "Unsupported shape type in SHPReadOGRObject()" );        }        /* nothing returned */    }    /* -------------------------------------------------------------------- *//*      Cleanup shape, and set feature id.                              *//* -------------------------------------------------------------------- */    SHPDestroyObject( psShape );    return poOGR;}/************************************************************************//*                         SHPWriteOGRObject()                          *//************************************************************************/OGRErr SHPWriteOGRObject( SHPHandle hSHP, int iShape, OGRGeometry *poGeom ){/* ==================================================================== *//*      Write "shape" with no geometry.                                 *//* ==================================================================== */    if( poGeom == NULL )    {        SHPObject       *psShape;        psShape = SHPCreateSimpleObject( SHPT_NULL, 0, NULL, NULL, NULL );        SHPWriteObject( hSHP, iShape, psShape );        SHPDestroyObject( psShape );    }/* ==================================================================== *//*      Write point geometry.                                           *//* ==================================================================== */    else if( hSHP->nShapeType == SHPT_POINT             || hSHP->nShapeType == SHPT_POINTM             || hSHP->nShapeType == SHPT_POINTZ )    {        SHPObject       *psShape;        OGRPoint        *poPoint = (OGRPoint *) poGeom;        double          dfX, dfY, dfZ = 0;        if( poGeom->getGeometryType() != wkbPoint            && poGeom->getGeometryType() != wkbPoint25D )                {            CPLError( CE_Failure, CPLE_AppDefined,                      "Attempt to write non-point (%s) geometry to"                      " point shapefile.",                      poGeom->getGeometryName() );            return OGRERR_UNSUPPORTED_GEOMETRY_TYPE;        }        dfX = poPoint->getX();        dfY = poPoint->getY();        dfZ = poPoint->getZ();                psShape = SHPCreateSimpleObject( hSHP->nShapeType, 1,                                         &dfX, &dfY, &dfZ );        SHPWriteObject( hSHP, iShape, psShape );        SHPDestroyObject( psShape );    }/* ==================================================================== *//*      MultiPoint.                                                     *//* ==================================================================== */    else if( hSHP->nShapeType == SHPT_MULTIPOINT             || hSHP->nShapeType == SHPT_MULTIPOINTM             || hSHP->nShapeType == SHPT_MULTIPOINTZ )    {        OGRMultiPoint   *poMP = (OGRMultiPoint *) poGeom;        double          *padfX, *padfY, *padfZ;        int             iPoint;        SHPObject       *psShape;        if( wkbFlatten(poGeom->getGeometryType()) != wkbMultiPoint )        {            CPLError( CE_Failure, CPLE_AppDefined,                      "Attempt to write non-multipoint (%s) geometry to "                      "multipoint shapefile.",                      poGeom->getGeometryName() );            return OGRERR_UNSUPPORTED_GEOMETRY_TYPE;        }        padfX = (double *) CPLMalloc(sizeof(double)*poMP->getNumGeometries());        padfY = (double *) CPLMalloc(sizeof(double)*poMP->getNumGeometries());        padfZ = (double *) CPLCalloc(sizeof(double),poMP->getNumGeometries());        for( iPoint = 0; iPoint < poMP->getNumGeometries(); iPoint++ )        {            OGRPoint    *poPoint = (OGRPoint *) poMP->getGeometryRef(iPoint);                        padfX[iPoint] = poPoint->getX();            padfY[iPoint] = poPoint->getY();            padfZ[iPoint] = poPoint->getZ();        }        psShape = SHPCreateSimpleObject( hSHP->nShapeType,                                         poMP->getNumGeometries(),                                         padfX, padfY, padfZ );        SHPWriteObject( hSHP, iShape, psShape );        SHPDestroyObject( psShape );                CPLFree( padfX );        CPLFree( padfY );        CPLFree( padfZ );    }/* ==================================================================== *//*      Arcs from simple line strings.                                  *//* ==================================================================== */    else if( (hSHP->nShapeType == SHPT_ARC              || hSHP->nShapeType == SHPT_ARCM              || hSHP->nShapeType == SHPT_ARCZ)             && wkbFlatten(poGeom->getGeometryType()) == wkbLineString )    {        OGRLineString   *poArc = (OGRLineString *) poGeom;        double          *padfX, *padfY, *padfZ;        int             iPoint;        SHPObject       *psShape;        if( poGeom->getGeometryType() != wkbLineString            && poGeom->getGeometryType() != wkbLineString25D )        {            CPLError( CE_Failure, CPLE_AppDefined,                      "Attempt to write non-linestring (%s) geometry to "                      "ARC type shapefile.",                      poGeom->getGeometryName() );            return OGRERR_UNSUPPORTED_GEOMETRY_TYPE;        }        padfX = (double *) CPLMalloc(sizeof(double)*poArc->getNumPoints());        padfY = (double *) CPLMalloc(sizeof(double)*poArc->getNumPoints());        padfZ = (double *) CPLCalloc(sizeof(double),poArc->getNumPoints());        for( iPoint = 0; iPoint < poArc->getNumPoints(); iPoint++ )        {            padfX[iPoint] = poArc->getX( iPoint );            padfY[iPoint] = poArc->getY( iPoint );            padfZ[iPoint] = poArc->getZ( iPoint );        }        psShape = SHPCreateSimpleObject( hSHP->nShapeType,                                         poArc->getNumPoints(),                                         padfX, padfY, padfZ );        SHPWriteObject( hSHP, iShape, psShape );        SHPDestroyObject( psShape );                CPLFree( padfX );        CPLFree( padfY );        CPLFree( padfZ );    }/* ==================================================================== *//*      Arcs - Try to treat as MultiLineString.                         *//* ==================================================================== */    else if( hSHP->nShapeType == SHPT_ARC             || hSHP->nShapeType == SHPT_ARCM             || hSHP->nShapeType == SHPT_ARCZ )    {        OGRMultiLineString *poML;        double          *padfX=NULL, *padfY=NULL, *padfZ=NULL;        int             iGeom, iPoint, nPointCount = 0;        SHPObject       *psShape;        int             *panRingStart;        poML = (OGRMultiLineString *)             OGRGeometryFactory::forceToMultiLineString( poGeom->clone() );        if( wkbFlatten(poML->getGeometryType()) != wkbMultiLineString )        {            delete poML;            CPLError( CE_Failure, CPLE_AppDefined,                      "Attempt to write non-linestring (%s) geometry to "                      "ARC type shapefile.",                      poGeom->getGeometryName() );            return OGRERR_UNSUPPORTED_GEOMETRY_TYPE;        }        panRingStart = (int *)             CPLMalloc(sizeof(int) * poML->getNumGeometries());        for( iGeom = 0; iGeom < poML->getNumGeometries(); iGeom++ )        {            OGRLineString *poArc = (OGRLineString *)                poML->getGeometryRef(iGeom);            int nNewPoints = poArc->getNumPoints();            panRingStart[iGeom] = nPointCount;            padfX = (double *)                 CPLRealloc( padfX, sizeof(double)*(nNewPoints+nPointCount) );            padfY = (double *)                 CPLRealloc( padfY, sizeof(double)*(nNewPoints+nPointCount) );            padfZ = (double *)                 CPLRealloc( padfZ, sizeof(double)*(nNewPoints+nPointCount) );            for( iPoint = 0; iPoint < nNewPoints; iPoint++ )            {                padfX[nPointCount] = poArc->getX( iPoint );                padfY[nPointCount] = poArc->getY( iPoint );                padfZ[nPointCount] = poArc->getZ( iPoint );                nPointCount++;            }        }                psShape = SHPCreateObject( hSHP->nShapeType, iShape,                                    poML->getNumGeometries(),                                    panRingStart, NULL,                                   nPointCount, padfX, padfY, padfZ, NULL);        SHPWriteObject( hSHP, iShape, psShape );        SHPDestroyObject( psShape );        CPLFree( padfX );        CPLFree( padfY );        CPLFree( padfZ );        delete poML;    }/* ==================================================================== *//*      Polygons/MultiPolygons                                          *//* ==================================================================== */    else if( hSHP->nShapeType == SHPT_POLYGON             || hSHP->nShapeType == SHPT_POLYGONM             || hSHP->nShapeType == SHPT_POLYGONZ )    {        OGRPolygon      *poPoly;        OGRLinearRing   *poRing, **papoRings=NULL;        double          *padfX=NULL, *padfY=NULL, *padfZ=NULL;        int             iPoint, iRing, nRings, nVertex=0, *panRingStart;        SHPObject       *psShape;        /* Collect list of rings */        if( wkbFlatten(poGeom->getGeometryType()) == wkbPolygon )        {            poPoly =  (OGRPolygon *) poGeom;            if( poPoly->getExteriorRing() == NULL )            {                CPLDebug( "OGR",                           "Ignore POLYGON EMPTY in shapefile writer." );                nRings = 0;            }            else            {                nRings = poPoly->getNumInteriorRings()+1;                papoRings = (OGRLinearRing **) CPLMalloc(sizeof(void*)*nRings);                for( iRing = 0; iRing < nRings; iRing++ )                {                    if( iRing == 0 )                        papoRings[iRing] = poPoly->getExteriorRing();                    else                        papoRings[iRing] = poPoly->getInteriorRing( iRing-1 );                }            }        }        else if( wkbFlatten(poGeom->getGeometryType()) == wkbMultiPolygon                 || wkbFlatten(poGeom->getGeometryType())                                                 == wkbGeometryCollection )        {            OGRGeometryCollection *poGC = (OGRGeometryCollection *) poGeom;            int         iGeom;            nRings = 0;            for( iGeom=0; iGeom < poGC->getNumGeometries(); iGeom++ )            {                poPoly =  (OGRPolygon *) poGC->getGeometryRef( iGeom );                if( wkbFlatten(poPoly->getGeometryType()) != wkbPolygon )                {                    CPLFree( papoRings );                    CPLError( CE_Failure, CPLE_AppDefined,                              "Attempt to write non-polygon (%s) geometry to "                              " type shapefile.",                              poGeom->getGeometryName() );                    return OGRERR_UNSUPPORTED_GEOMETRY_TYPE;                }                if( poPoly->getExteriorRing() == NULL )                {                    CPLDebug( "OGR",                               "Ignore POLYGON EMPTY in shapefile writer." );                    continue;                }                papoRings = (OGRLinearRing **) CPLRealloc(papoRings,                      sizeof(void*) * (nRings+poPoly->getNumInteriorRings()+1));                for( iRing = 0;                      iRing < poPoly->getNumInteriorRings()+1;                      iRing++ )                {                    if( iRing == 0 )                        papoRings[nRings+iRing] = poPoly->getExteriorRing();                    else                        papoRings[nRings+iRing] =                             poPoly->getInteriorRing( iRing-1 );                }                nRings += poPoly->getNumInteriorRings()+1;            }        }        else         {            CPLError( CE_Failure, CPLE_AppDefined,                      "Attempt to write non-polygon (%s) geometry to "                      " type shapefile.",                      poGeom->getGeometryName() );            return OGRERR_UNSUPPORTED_GEOMETRY_TYPE;        }/* -------------------------------------------------------------------- *//*      If we only had emptypolygons or unacceptable geometries         *//*      write NULL geometry object.                                     *//* -------------------------------------------------------------------- */        if( nRings == 0 )

⌨️ 快捷键说明

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