Beispiel #1
0
void kml2stylestring (
    StylePtr poKmlStyle,
    OGRStyleMgr * poOgrSM )

{

    OGRStyleMgr * poOgrNewSM ;
    OGRStyleTool *poOgrST = NULL;
    OGRStyleTool *poOgrTmpST = NULL;
    int i;

    poOgrNewSM = new OGRStyleMgr( NULL );
    
    /***** linestyle / pen *****/

    if ( poKmlStyle->has_linestyle (  ) ) {

        poOgrNewSM->InitStyleString ( NULL );
        
        LineStylePtr poKmlLineStyle = poKmlStyle->get_linestyle (  );

        poOgrTmpST = NULL;
        for ( i = 0; i < poOgrSM->GetPartCount ( NULL ); i++ ) {
            poOgrST = poOgrSM->GetPart ( i, NULL );

            if ( !poOgrST )
                continue;
        
            if ( poOgrST->GetType ( ) == OGRSTCPen ) {
                poOgrTmpST = poOgrST;
            }
            else {
                poOgrNewSM->AddPart ( poOgrST );
                delete poOgrST;
            }
        }
        
        OGRStylePen *poOgrStylePen = kml2pen ( poKmlLineStyle,
                                               ( OGRStylePen *) poOgrTmpST);
        
        poOgrNewSM->AddPart ( poOgrStylePen );

        delete poOgrStylePen;
        poOgrSM->InitStyleString ( poOgrNewSM->GetStyleString(NULL) );
        
    }

    /***** polystyle / brush *****/

    if ( poKmlStyle->has_polystyle (  ) ) {

        poOgrNewSM->InitStyleString ( NULL );

        PolyStylePtr poKmlPolyStyle = poKmlStyle->get_polystyle (  );

        poOgrTmpST = NULL;
        for ( i = 0; i < poOgrSM->GetPartCount ( NULL ); i++ ) {
            poOgrST = poOgrSM->GetPart ( i, NULL );

            if ( !poOgrST )
                continue;
        
            if ( poOgrST->GetType ( ) == OGRSTCBrush ) {
                poOgrTmpST = poOgrST;
            }
            else {
                poOgrNewSM->AddPart ( poOgrST );
                delete poOgrST;
            }
        }

        OGRStyleBrush *poOgrStyleBrush = kml2brush ( poKmlPolyStyle,
                                                     ( OGRStyleBrush *) poOgrTmpST );

        poOgrNewSM->AddPart ( poOgrStyleBrush );

        delete poOgrStyleBrush;
        poOgrSM->InitStyleString ( poOgrNewSM->GetStyleString(NULL) );

    }

    /***** iconstyle / symbol *****/

    if ( poKmlStyle->has_iconstyle (  ) ) {
        
        poOgrNewSM->InitStyleString ( NULL );

        IconStylePtr poKmlIconStyle = poKmlStyle->get_iconstyle (  );

        poOgrTmpST = NULL;
        for ( i = 0; i < poOgrSM->GetPartCount ( NULL ); i++ ) {
            poOgrST = poOgrSM->GetPart ( i, NULL );

            if ( !poOgrST )
                continue;
        
            if ( poOgrST->GetType ( ) == OGRSTCSymbol ) {
                poOgrTmpST = poOgrST;
            }
            else {
                poOgrNewSM->AddPart ( poOgrST );
                delete poOgrST;
            }
        }

        OGRStyleSymbol *poOgrStyleSymbol = kml2symbol ( poKmlIconStyle,
                                                     ( OGRStyleSymbol *) poOgrTmpST );

        poOgrNewSM->AddPart ( poOgrStyleSymbol );

        delete poOgrStyleSymbol;
        poOgrSM->InitStyleString ( poOgrNewSM->GetStyleString(NULL) );

    }

    /***** labelstyle / label *****/

    if ( poKmlStyle->has_labelstyle (  ) ) {
        
        poOgrNewSM->InitStyleString ( NULL );

        LabelStylePtr poKmlLabelStyle = poKmlStyle->get_labelstyle (  );

        poOgrTmpST = NULL;
        for ( i = 0; i < poOgrSM->GetPartCount ( NULL ); i++ ) {
            poOgrST = poOgrSM->GetPart ( i, NULL );

            if ( !poOgrST )
                continue;
        
            if ( poOgrST->GetType ( ) == OGRSTCLabel ) {
                poOgrTmpST = poOgrST;
            }
            else {
                poOgrNewSM->AddPart ( poOgrST );
                delete poOgrST;
            }
        }

        OGRStyleLabel *poOgrStyleLabel = kml2label ( poKmlLabelStyle,
                                                     ( OGRStyleLabel *) poOgrTmpST );

        poOgrNewSM->AddPart ( poOgrStyleLabel );

        delete poOgrStyleLabel;
        poOgrSM->InitStyleString ( poOgrNewSM->GetStyleString(NULL) );

    }

    delete poOgrNewSM;

}
void kml2featurestyle (
    FeaturePtr poKmlFeature,
    OGRLIBKMLDataSource * poOgrDS,
    OGRLayer * poOgrLayer,
    OGRFeature * poOgrFeat )
{

    /***** does the placemark have a style url? *****/

    if (    poKmlFeature->has_styleurl (  ) ) {

        const string poKmlStyleUrl = poKmlFeature->get_styleurl (  );

        /***** is the name in the layer style table *****/

        char *pszUrl = CPLStrdup ( poKmlStyleUrl.c_str (  ) );

        OGRStyleTable *poOgrSTBLLayer;
        const char *pszTest = NULL;

        /***** is it a layer style ? *****/

        if ( *pszUrl == '#'
             && ( poOgrSTBLLayer = poOgrLayer->GetStyleTable (  ) ) != NULL )
        {
             pszTest = poOgrSTBLLayer->Find ( pszUrl + 1 );
        }

        if ( pszTest ) {

            /***** should we resolve the style *****/

            const char *pszResolve = CPLGetConfigOption ( "LIBKML_RESOLVE_STYLE", "no" );

            if (CPLTestBool(pszResolve)) {

                poOgrFeat->SetStyleString ( pszTest );
            }

            else {

                *pszUrl = '@';

                poOgrFeat->SetStyleString( pszUrl );

            }

        }

        /***** is it a dataset style? *****/

        else {

            int nPathLen = static_cast<int>(strlen ( poOgrDS->GetStylePath (  ) ));

            if (    nPathLen == 0
                 || EQUALN ( pszUrl, poOgrDS->GetStylePath (  ), nPathLen ))
            {

                /***** should we resolve the style *****/

                const char *pszResolve = CPLGetConfigOption ( "LIBKML_RESOLVE_STYLE", "no" );

                if ( CPLTestBool(pszResolve)
                     && ( poOgrSTBLLayer = poOgrDS->GetStyleTable (  ) ) != NULL
                     && ( pszTest = poOgrSTBLLayer->Find ( pszUrl + nPathLen + 1) ) != NULL
                   )
                {

                    poOgrFeat->SetStyleString ( pszTest );
                }

                else {

                    pszUrl[nPathLen] = '@';
                    poOgrFeat->SetStyleString ( pszUrl + nPathLen );
                }

            }

            /**** its someplace else *****/

            else {

                const char *pszFetch = CPLGetConfigOption ( "LIBKML_EXTERNAL_STYLE", "no" );

                if ( CPLTestBool(pszFetch) ) {

                    /***** load up the style table *****/

                    char *pszUrlTmp = CPLStrdup(pszUrl);
                    char *pszPound = strchr(pszUrlTmp, '#');
                    if (pszPound != NULL) {
                        *pszPound = '\0';
                    }

                    /***** try it as a url then a file *****/

                    VSILFILE *fp = NULL;

                    if (    (fp = VSIFOpenL( CPLFormFilename( "/vsicurl/",
                                                              pszUrlTmp,
                                                              NULL),
                                             "r" )) != NULL
                        ||  (fp = VSIFOpenL( pszUrlTmp, "r" )) != NULL )
                    {

                        char szbuf[1025];
                        std::string oStyle = "";

                        /***** loop, read and copy to a string *****/

                        size_t nRead;

                        do {

                            nRead = VSIFReadL(szbuf, 1, sizeof(szbuf) - 1, fp);

                            if (nRead == 0)
                                break;

                            /***** copy buf to the string *****/

                            szbuf[nRead] = '\0';
                            oStyle.append( szbuf );

                        } while (!VSIFEofL(fp));

                        VSIFCloseL(fp);

                        /***** parse the kml into the ds style table *****/

                        if ( poOgrDS->ParseIntoStyleTable (&oStyle, pszUrlTmp)) {

                            kml2featurestyle (poKmlFeature,
                                              poOgrDS,
                                              poOgrLayer,
                                              poOgrFeat );
                        }

                        else {

                            /***** if failed just store the url *****/

                            poOgrFeat->SetStyleString ( pszUrl );
                        }
                    }
                    CPLFree(pszUrlTmp);
                }

                else {

                    poOgrFeat->SetStyleString ( pszUrl );
                }
            }

        }
        CPLFree ( pszUrl );

    }

    /***** does the placemark have a style selector *****/

   if ( poKmlFeature->has_styleselector (  ) ) {

        StyleSelectorPtr poKmlStyleSelector =
            poKmlFeature->get_styleselector (  );

        /***** is the style a style? *****/

        if ( poKmlStyleSelector->IsA ( kmldom::Type_Style ) ) {
            StylePtr poKmlStyle = AsStyle ( poKmlStyleSelector );

            OGRStyleMgr *poOgrSM = new OGRStyleMgr;

            /***** if were resolving style the feature *****/
            /***** might already have styling to add too *****/

            const char *pszResolve = CPLGetConfigOption ( "LIBKML_RESOLVE_STYLE", "no" );
            if (CPLTestBool(pszResolve)) {
                 poOgrSM->InitFromFeature ( poOgrFeat );
            }
            else {

                /***** if featyurestyle gets a name tool this needs changed to the above *****/

                poOgrSM->InitStyleString ( NULL );
            }

            /***** read the style *****/

            kml2stylestring ( poKmlStyle, poOgrSM );

            /***** add the style to the feature *****/

            poOgrFeat->SetStyleString(poOgrSM->GetStyleString(NULL));

            delete poOgrSM;
        }

        /***** is the style a stylemap? *****/

        else if ( poKmlStyleSelector->IsA ( kmldom::Type_StyleMap ) ) {
            /* todo need to figure out what to do with a style map */
        }


    }
}