shape2ogr.cpp

来自「支持各种栅格图像和矢量图像读取的库」· C++ 代码 · 共 1,356 行 · 第 1/4 页

CPP
1,356
字号
/*      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 )        {            SHPObject       *psShape;                        psShape = SHPCreateSimpleObject( SHPT_NULL, 0, NULL, NULL, NULL );            SHPWriteObject( hSHP, iShape, psShape );            SHPDestroyObject( psShape );            return OGRERR_NONE;        }                /* count vertices */        nVertex = 0;        for( iRing = 0; iRing < nRings; iRing++ )            nVertex += papoRings[iRing]->getNumPoints();        panRingStart = (int *) CPLMalloc(sizeof(int) * nRings);        padfX = (double *) CPLMalloc(sizeof(double)*nVertex);        padfY = (double *) CPLMalloc(sizeof(double)*nVertex);        padfZ = (double *) CPLMalloc(sizeof(double)*nVertex);        /* collect vertices */        nVertex = 0;        for( iRing = 0; iRing < nRings; iRing++ )        {            poRing = papoRings[iRing];            panRingStart[iRing] = nVertex;            for( iPoint = 0; iPoint < poRing->getNumPoints(); iPoint++ )            {                padfX[nVertex] = poRing->getX( iPoint );                padfY[nVertex] = poRing->getY( iPoint );                padfZ[nVertex] = poRing->getZ( iPoint );                nVertex++;            }        }        psShape = SHPCreateObject( hSHP->nShapeType, iShape, nRings,                                   panRingStart, NULL,                                   nVertex, padfX, padfY, padfZ, NULL );        SHPRewindObject( hSHP, psShape );        SHPWriteObject( hSHP, iShape, psShape );

⌨️ 快捷键说明

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