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

📄 shape2ogr.cpp

📁 用于读取TAB、MIF、SHP文件的类
💻 CPP
📖 第 1 页 / 共 4 页
字号:
        {            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 );        SHPDestroyObject( psShape );                CPLFree( papoRings );        CPLFree( panRingStart );        CPLFree( padfX );        CPLFree( padfY );        CPLFree( padfZ );    }    else    {        /* do nothing for multipatch */        return OGRERR_UNSUPPORTED_GEOMETRY_TYPE;    }    return OGRERR_NONE;}/************************************************************************//*                       SHPReadOGRFeatureDefn()                        *//************************************************************************/OGRFeatureDefn *SHPReadOGRFeatureDefn( const char * pszName,                                       SHPHandle hSHP, DBFHandle hDBF ){    OGRFeatureDefn      *poDefn = new OGRFeatureDefn( pszName );    int                 iField;    poDefn->Reference();    for( iField = 0;          hDBF != NULL && iField < DBFGetFieldCount( hDBF );          iField++ )    {        char            szFieldName[20];        int             nWidth, nPrecision;        DBFFieldType    eDBFType;        OGRFieldDefn    oField("", OFTInteger);        char            chNativeType;        chNativeType = DBFGetNativeFieldType( hDBF, iField );        eDBFType = DBFGetFieldInfo( hDBF, iField, szFieldName,                                    &nWidth, &nPrecision );        oField.SetName( szFieldName );        oField.SetWidth( nWidth );        oField.SetPrecision( nPrecision );        if( chNativeType == 'D' )        {            /* XXX - mloskot:             * Shapefile date has following 8-chars long format: 20060101.             * OGR splits it as YYYY/MM/DD, so 2 additional characters are required.             * Is this correct assumtion? What about time part of date?             * Shouldn't this format look as datetime: YYYY/MM/DD HH:MM:SS             * with 4 additional characters?             */            oField.SetWidth( nWidth + 2 );            oField.SetType( OFTDate );        }        else if( eDBFType == FTDouble )            oField.SetType( OFTReal );        else if( eDBFType == FTInteger )            oField.SetType( OFTInteger );        else            oField.SetType( OFTString );        poDefn->AddFieldDefn( &oField );    }    if( hSHP == NULL )        poDefn->SetGeomType( wkbNone );    else    {        switch( hSHP->nShapeType )        {          case SHPT_POINT:          case SHPT_POINTM:            poDefn->SetGeomType( wkbPoint );            break;          case SHPT_POINTZ:            poDefn->SetGeomType( wkbPoint25D );            break;          case SHPT_ARC:          case SHPT_ARCM:            poDefn->SetGeomType( wkbLineString );            break;          case SHPT_ARCZ:            poDefn->SetGeomType( wkbLineString25D );            break;          case SHPT_MULTIPOINT:          case SHPT_MULTIPOINTM:            poDefn->SetGeomType( wkbMultiPoint );            break;          case SHPT_MULTIPOINTZ:            poDefn->SetGeomType( wkbMultiPoint25D );            break;          case SHPT_POLYGON:          case SHPT_POLYGONM:            poDefn->SetGeomType( wkbPolygon );            break;          case SHPT_POLYGONZ:            poDefn->SetGeomType( wkbPolygon25D );            break;                    }    }    return poDefn;}/************************************************************************//*                         SHPReadOGRFeature()                          *//************************************************************************/OGRFeature *SHPReadOGRFeature( SHPHandle hSHP, DBFHandle hDBF,                               OGRFeatureDefn * poDefn, int iShape ){    if( iShape < 0         || (hSHP != NULL && iShape >= hSHP->nRecords)        || (hDBF != NULL && iShape >= hDBF->nRecords) )    {        CPLError( CE_Failure, CPLE_AppDefined,                   "Attempt to read shape with feature id (%d) out of available"                  " range.", iShape );        return NULL;    }    if( hDBF && DBFIsRecordDeleted( hDBF, iShape ) )    {        CPLError( CE_Failure, CPLE_AppDefined,                   "Attempt to read shape with feature id (%d), but it is marked deleted.",                  iShape );        return NULL;    }    OGRFeature  *poFeature = new OGRFeature( poDefn );/* -------------------------------------------------------------------- *//*      Fetch geometry from Shapefile to OGRFeature.                    *//* -------------------------------------------------------------------- */    if( hSHP != NULL )    {        OGRGeometry* poGeometry = NULL;        poGeometry = SHPReadOGRObject( hSHP, iShape );        /*         * NOTE - mloskot:         * Two possibilities are expected here (bot hare tested by GDAL Autotests):         * 1. Read valid geometry and assign it directly.         * 2. Read and assign null geometry if it can not be read correctly from a shapefile         *         * It's NOT required here to test poGeometry == NULL.         */        poFeature->SetGeometryDirectly( poGeometry );    }/* -------------------------------------------------------------------- *//*      Fetch feature attributes to OGRFeature fields.                  *//* -------------------------------------------------------------------- */    for( int iField = 0; iField < poDefn->GetFieldCount(); iField++ )    {        // Skip null fields.        if( DBFIsAttributeNULL( hDBF, iShape, iField ) )            continue;        switch( poDefn->GetFieldDefn(iField)->GetType() )        {          case OFTString:            poFeature->SetField( iField,                                 DBFReadStringAttribute( hDBF, iShape,                                                         iField ) );            break;          case OFTInteger:            poFeature->SetField( iField,                                 DBFReadIntegerAttribute( hDBF, iShape,                                                          iField ) );            break;          case OFTReal:            poFeature->SetField( iField,                                 DBFReadDoubleAttribute( hDBF, iShape,                                                         iField ) );            break;          case OFTDate:          {              OGRField sFld;              int nFullDate =                   DBFReadIntegerAttribute( hDBF, iShape, iField );                            memset( &sFld, 0, sizeof(sFld) );              sFld.Date.Year = nFullDate / 10000;              sFld.Date.Month = (nFullDate / 100) % 100;              sFld.Date.Day = nFullDate % 100;                            poFeature->SetField( iField, &sFld );          }          break;          default:            CPLAssert( FALSE );        }    }    if( poFeature != NULL )        poFeature->SetFID( iShape );    return( poFeature );}/************************************************************************//*                         SHPWriteOGRFeature()                         *//*                                                                      *//*      Write to an existing feature in a shapefile, or create a new    *//*      feature.                                                        *//************************************************************************/OGRErr SHPWriteOGRFeature( SHPHandle hSHP, DBFHandle hDBF,                           OGRFeatureDefn * poDefn,                            OGRFeature * poFeature ){#ifdef notdef/* -------------------------------------------------------------------- *//*      Don't write objects with missing geometry.                      *//* -------------------------------------------------------------------- */    if( poFeature->GetGeometryRef() == NULL && hSHP != NULL )    {        CPLError( CE_Failure, CPLE_AppDefined,                  "Attempt to write feature without geometry not supported"                  " for shapefile driver." );                return OGRERR_UNSUPPORTED_GEOMETRY_TYPE;    }#endif/* -------------------------------------------------------------------- *//*      Write the geometry.                                             *//* -------------------------------------------------------------------- */    OGRErr      eErr;    if( hSHP != NULL )    {        eErr = SHPWriteOGRObject( hSHP, poFeature->GetFID(),                                  poFeature->GetGeometryRef() );        if( eErr != OGRERR_NONE )            return eErr;    }    /* -------------------------------------------------------------------- *//*      If this is a new feature, establish it's feature id.            *//* -------------------------------------------------------------------- */    if( poFeature->GetFID() == OGRNullFID )        poFeature->SetFID( DBFGetRecordCount( hDBF ) );/* -------------------------------------------------------------------- *//*      If this is the first feature to be written, verify that we      *//*      have at least one attribute in the DBF file.  If not, create    *//*      a dummy FID attribute to satisfy the requirement that there     *//*      be at least one attribute.                                      *//* -------------------------------------------------------------------- */    if( DBFGetRecordCount( hDBF ) == 0 && DBFGetFieldCount( hDBF ) == 0 )    {        CPLDebug( "OGR",                "Created dummy FID field for shapefile since schema is empty.");        DBFAddField( hDBF, "FID", FTInteger, 11, 0 );    }/* -------------------------------------------------------------------- *//*      Write out dummy field value if it exists.                       *//* -------------------------------------------------------------------- */    if( DBFGetFieldCount( hDBF ) == 1 && poDefn->GetFieldCount() == 0 )    {        DBFWriteIntegerAttribute( hDBF, poFeature->GetFID(), 0,                                   poFeature->GetFID() );    }/* -------------------------------------------------------------------- *//*      Write all the fields.                                           *//* -------------------------------------------------------------------- */    for( int iField = 0; iField < poDefn->GetFieldCount(); iField++ )    {        if( !poFeature->IsFieldSet( iField ) )        {            DBFWriteNULLAttribute( hDBF, poFeature->GetFID(), iField );            continue;        }        switch( poDefn->GetFieldDefn(iField)->GetType() )        {          case OFTString:            DBFWriteStringAttribute( hDBF, poFeature->GetFID(), iField,                                      poFeature->GetFieldAsString( iField ));            break;          case OFTInteger:            DBFWriteIntegerAttribute( hDBF, poFeature->GetFID(), iField,                                       poFeature->GetFieldAsInteger(iField) );            break;          case OFTReal:            DBFWriteDoubleAttribute( hDBF, poFeature->GetFID(), iField,                                      poFeature->GetFieldAsDouble(iField) );            break;          case OFTDate:          {              int  nYear, nMonth, nDay;              if( poFeature->GetFieldAsDateTime( iField, &nYear, &nMonth, &nDay,                                                 NULL, NULL, NULL, NULL ) )              {                  DBFWriteIntegerAttribute( hDBF, poFeature->GetFID(), iField,                                             nYear*10000 + nMonth*100 + nDay );              }          }          break;          default:            CPLAssert( FALSE );        }    }    return OGRERR_NONE;}

⌨️ 快捷键说明

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