ogrdgnlayer.cpp

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

CPP
1,058
字号
/****************************************************************************** * $Id: ogrdgnlayer.cpp 10646 2007-01-18 02:38:10Z warmerdam $ * * Project:  OpenGIS Simple Features Reference Implementation * Purpose:  Implements OGRDGNLayer class. * Author:   Frank Warmerdam, warmerdam@pobox.com * ****************************************************************************** * Copyright (c) 2000, Frank Warmerdam (warmerdam@pobox.com) * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. ****************************************************************************/#include "ogr_dgn.h"#include "cpl_conv.h"#include "ogr_featurestyle.h"#include "ogr_api.h"CPL_CVSID("$Id: ogrdgnlayer.cpp 10646 2007-01-18 02:38:10Z warmerdam $");/************************************************************************//*                           OGRDGNLayer()                              *//************************************************************************/OGRDGNLayer::OGRDGNLayer( const char * pszName, DGNHandle hDGN,                          int bUpdate )    {    this->hDGN = hDGN;    this->bUpdate = bUpdate;/* -------------------------------------------------------------------- *//*      Work out what link format we are using.                         *//* -------------------------------------------------------------------- */    OGRFieldType eLinkFieldType;    pszLinkFormat = (char *) CPLGetConfigOption( "DGN_LINK_FORMAT", "FIRST" );    if( EQUAL(pszLinkFormat,"FIRST") )        eLinkFieldType = OFTInteger;    else if( EQUAL(pszLinkFormat,"LIST") )        eLinkFieldType = OFTIntegerList;    else if( EQUAL(pszLinkFormat,"STRING") )        eLinkFieldType = OFTString;    else    {        CPLError( CE_Warning, CPLE_AppDefined,                   "DGN_LINK_FORMAT=%s, but only FIRST, LIST or STRING supported.",                  pszLinkFormat );        pszLinkFormat = "FIRST";        eLinkFieldType = OFTInteger;    }    pszLinkFormat = CPLStrdup(pszLinkFormat);/* -------------------------------------------------------------------- *//*      Create the feature definition.                                  *//* -------------------------------------------------------------------- */    poFeatureDefn = new OGRFeatureDefn( pszName );    poFeatureDefn->Reference();        OGRFieldDefn        oField( "", OFTInteger );/* -------------------------------------------------------------------- *//*      Element type                                                    *//* -------------------------------------------------------------------- */    oField.SetName( "Type" );    oField.SetType( OFTInteger );    oField.SetWidth( 2 );    oField.SetPrecision( 0 );    poFeatureDefn->AddFieldDefn( &oField );/* -------------------------------------------------------------------- *//*      Level number.                                                   *//* -------------------------------------------------------------------- */    oField.SetName( "Level" );    oField.SetType( OFTInteger );    oField.SetWidth( 2 );    oField.SetPrecision( 0 );    poFeatureDefn->AddFieldDefn( &oField );/* -------------------------------------------------------------------- *//*      graphic group                                                   *//* -------------------------------------------------------------------- */    oField.SetName( "GraphicGroup" );    oField.SetType( OFTInteger );    oField.SetWidth( 4 );    oField.SetPrecision( 0 );    poFeatureDefn->AddFieldDefn( &oField );/* -------------------------------------------------------------------- *//*      ColorIndex                                                      *//* -------------------------------------------------------------------- */    oField.SetName( "ColorIndex" );    oField.SetType( OFTInteger );    oField.SetWidth( 3 );    oField.SetPrecision( 0 );    poFeatureDefn->AddFieldDefn( &oField );/* -------------------------------------------------------------------- *//*      Weight                                                          *//* -------------------------------------------------------------------- */    oField.SetName( "Weight" );    oField.SetType( OFTInteger );    oField.SetWidth( 2 );    oField.SetPrecision( 0 );    poFeatureDefn->AddFieldDefn( &oField );/* -------------------------------------------------------------------- *//*      Style                                                           *//* -------------------------------------------------------------------- */    oField.SetName( "Style" );    oField.SetType( OFTInteger );    oField.SetWidth( 1 );    oField.SetPrecision( 0 );    poFeatureDefn->AddFieldDefn( &oField );/* -------------------------------------------------------------------- *//*      EntityNum                                                       *//* -------------------------------------------------------------------- */    oField.SetName( "EntityNum" );    oField.SetType( eLinkFieldType );    oField.SetWidth( 0 );    oField.SetPrecision( 0 );    poFeatureDefn->AddFieldDefn( &oField );/* -------------------------------------------------------------------- *//*      MSLink                                                          *//* -------------------------------------------------------------------- */    oField.SetName( "MSLink" );    oField.SetType( eLinkFieldType );    oField.SetWidth( 0 );    oField.SetPrecision( 0 );    poFeatureDefn->AddFieldDefn( &oField );/* -------------------------------------------------------------------- *//*      Text                                                            *//* -------------------------------------------------------------------- */    oField.SetName( "Text" );    oField.SetType( OFTString );    oField.SetWidth( 0 );    oField.SetPrecision( 0 );    poFeatureDefn->AddFieldDefn( &oField );/* -------------------------------------------------------------------- *//*      Create template feature for evaluating simple expressions.      *//* -------------------------------------------------------------------- */    bHaveSimpleQuery = FALSE;    poEvalFeature = new OGRFeature( poFeatureDefn );    /* TODO: I am intending to keep track of simple attribute queries (ones       using only FID, Type and Level and short circuiting their operation       based on the index.  However, there are some complexities with       complex elements, and spatial queries that have caused me to put it       off for now.    */}/************************************************************************//*                           ~OGRDGNLayer()                             *//************************************************************************/OGRDGNLayer::~OGRDGNLayer(){    if( m_nFeaturesRead > 0 && poFeatureDefn != NULL )    {        CPLDebug( "Mem", "%d features read on layer '%s'.",                  (int) m_nFeaturesRead,                   poFeatureDefn->GetName() );    }    delete poEvalFeature;    poFeatureDefn->Release();    CPLFree( pszLinkFormat );}/************************************************************************//*                          SetSpatialFilter()                          *//************************************************************************/void OGRDGNLayer::SetSpatialFilter( OGRGeometry * poGeomIn ){    if( !InstallFilter(poGeomIn) )        return;    if( m_poFilterGeom != NULL )    {        DGNSetSpatialFilter( hDGN,                              m_sFilterEnvelope.MinX,                              m_sFilterEnvelope.MinY,                              m_sFilterEnvelope.MaxX,                              m_sFilterEnvelope.MaxY );    }    else    {        DGNSetSpatialFilter( hDGN, 0.0, 0.0, 0.0, 0.0 );    }    ResetReading();}/************************************************************************//*                            ResetReading()                            *//************************************************************************/void OGRDGNLayer::ResetReading(){    iNextShapeId = 0;    DGNRewind( hDGN );}/************************************************************************//*                             GetFeature()                             *//************************************************************************/OGRFeature *OGRDGNLayer::GetFeature( long nFeatureId ){    OGRFeature *poFeature;    DGNElemCore *psElement;    if( !DGNGotoElement( hDGN, nFeatureId ) )        return NULL;    // We should likely clear the spatial search region as it affects     // DGNReadElement() but I will defer that for now.     psElement = DGNReadElement( hDGN );    poFeature = ElementToFeature( psElement );    DGNFreeElement( hDGN, psElement );    if( poFeature == NULL )        return NULL;    if( poFeature->GetFID() != nFeatureId )    {        delete poFeature;        return NULL;    }    return poFeature;}/************************************************************************//*                           ConsiderBrush()                            *//*                                                                      *//*      Method to set the style for a polygon, including a brush if     *//*      appropriate.                                                    *//************************************************************************/void OGRDGNLayer::ConsiderBrush( DGNElemCore *psElement, const char *pszPen,                                 OGRFeature *poFeature ){    int         gv_red, gv_green, gv_blue;    char                szFullStyle[256];    int                 nFillColor;    if( DGNGetShapeFillInfo( hDGN, psElement, &nFillColor )         && DGNLookupColor( hDGN, nFillColor,                            &gv_red, &gv_green, &gv_blue ) )    {        sprintf( szFullStyle,                  "BRUSH(fc:#%02x%02x%02x,id:\"ogr-brush-0\")",                 gv_red, gv_green, gv_blue );                      if( nFillColor != psElement->color )        {            strcat( szFullStyle, ";" );            strcat( szFullStyle, pszPen );        }        poFeature->SetStyleString( szFullStyle );    }    else        poFeature->SetStyleString( pszPen );}/************************************************************************//*                          ElementToFeature()                          *//************************************************************************/OGRFeature *OGRDGNLayer::ElementToFeature( DGNElemCore *psElement ){    OGRFeature  *poFeature = new OGRFeature( poFeatureDefn );    poFeature->SetFID( psElement->element_id );    poFeature->SetField( "Type", psElement->type );    poFeature->SetField( "Level", psElement->level );    poFeature->SetField( "GraphicGroup", psElement->graphic_group );    poFeature->SetField( "ColorIndex", psElement->color );    poFeature->SetField( "Weight", psElement->weight );    poFeature->SetField( "Style", psElement->style );        m_nFeaturesRead++;/* -------------------------------------------------------------------- *//*      Collect linkage information                                     *//* -------------------------------------------------------------------- */#define MAX_LINK 100        int anEntityNum[MAX_LINK], anMSLink[MAX_LINK];    unsigned char *pabyData;    int iLink=0, nLinkCount=0;    anEntityNum[0] = 0;    anMSLink[0] = 0;    pabyData = DGNGetLinkage( hDGN, psElement, iLink, NULL,                               anEntityNum+iLink, anMSLink+iLink, NULL );    while( pabyData && nLinkCount < MAX_LINK )    {        iLink++;        if( anEntityNum[nLinkCount] != 0 || anMSLink[nLinkCount] != 0 )            nLinkCount++;        anEntityNum[nLinkCount] = 0;        anMSLink[nLinkCount] = 0;        pabyData = DGNGetLinkage( hDGN, psElement, iLink, NULL,                                   anEntityNum+nLinkCount, anMSLink+nLinkCount,                                   NULL );    }/* -------------------------------------------------------------------- *//*      Apply attribute linkage to feature.                             *//* -------------------------------------------------------------------- */    if( nLinkCount > 0 )    {        if( EQUAL(pszLinkFormat,"FIRST") )        {            poFeature->SetField( "EntityNum", anEntityNum[0] );            poFeature->SetField( "MSLink", anMSLink[0] );

⌨️ 快捷键说明

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