void addstylestring2kml ( const char *pszStyleString, StylePtr poKmlStyle, KmlFactory * poKmlFactory, PlacemarkPtr poKmlPlacemark, OGRFeature * poOgrFeat ) { LineStylePtr poKmlLineStyle = NULL; PolyStylePtr poKmlPolyStyle = NULL; IconStylePtr poKmlIconStyle = NULL; LabelStylePtr poKmlLabelStyle = NULL; /***** just bail now if stylestring is empty *****/ if ( !pszStyleString || !*pszStyleString ) { return; } /***** create and init a style mamager with the style string *****/ OGRStyleMgr *poOgrSM = new OGRStyleMgr; poOgrSM->InitStyleString ( pszStyleString ); /***** loop though the style parts *****/ int i; for ( i = 0; i < poOgrSM->GetPartCount ( NULL ); i++ ) { OGRStyleTool *poOgrST = poOgrSM->GetPart ( i, NULL ); if ( !poOgrST ) { continue; } switch ( poOgrST->GetType ( ) ) { case OGRSTCPen: { GBool nullcheck; poKmlLineStyle = poKmlFactory->CreateLineStyle ( ); OGRStylePen *poStylePen = ( OGRStylePen * ) poOgrST; /***** pen color *****/ int nR, nG, nB, nA; const char *pszcolor = poStylePen->Color ( nullcheck ); if ( !nullcheck && poStylePen->GetRGBFromString ( pszcolor, nR, nG, nB, nA ) ) { poKmlLineStyle->set_color ( Color32 ( nA, nB, nG, nR ) ); } double dfWidth = poStylePen->Width ( nullcheck ); if ( nullcheck ) dfWidth = 1.0; poKmlLineStyle->set_width ( dfWidth ); break; } case OGRSTCBrush: { GBool nullcheck; poKmlPolyStyle = poKmlFactory->CreatePolyStyle ( ); OGRStyleBrush *poStyleBrush = ( OGRStyleBrush * ) poOgrST; /***** brush color *****/ int nR, nG, nB, nA; const char *pszcolor = poStyleBrush->ForeColor ( nullcheck ); if ( !nullcheck && poStyleBrush->GetRGBFromString ( pszcolor, nR, nG, nB, nA ) ) { poKmlPolyStyle->set_color ( Color32 ( nA, nB, nG, nR ) ); } break; } case OGRSTCSymbol: { GBool nullcheck; GBool nullcheck2; OGRStyleSymbol *poStyleSymbol = ( OGRStyleSymbol * ) poOgrST; /***** id (kml icon) *****/ const char *pszId = poStyleSymbol->Id ( nullcheck ); if ( !nullcheck ) { if ( !poKmlIconStyle) poKmlIconStyle = poKmlFactory->CreateIconStyle ( ); /***** split it at the ,'s *****/ char **papszTokens = CSLTokenizeString2 ( pszId, ",", CSLT_HONOURSTRINGS | CSLT_STRIPLEADSPACES | CSLT_STRIPENDSPACES ); if ( papszTokens ) { /***** for lack of a better solution just take the first one *****/ //todo come up with a better idea if ( papszTokens[0] ) { IconStyleIconPtr poKmlIcon = poKmlFactory->CreateIconStyleIcon ( ); poKmlIcon->set_href ( papszTokens[0] ); poKmlIconStyle->set_icon ( poKmlIcon ); } CSLDestroy ( papszTokens ); } } /***** heading *****/ double heading = poStyleSymbol->Angle ( nullcheck ); if ( !nullcheck ) { if ( !poKmlIconStyle) poKmlIconStyle = poKmlFactory->CreateIconStyle ( ); poKmlIconStyle->set_heading ( heading ); } /***** scale *****/ double dfScale = poStyleSymbol->Size ( nullcheck ); if ( !nullcheck ) { if ( !poKmlIconStyle) poKmlIconStyle = poKmlFactory->CreateIconStyle ( ); poKmlIconStyle->set_scale ( dfScale ); } /***** color *****/ int nR, nG, nB, nA; const char *pszcolor = poStyleSymbol->Color ( nullcheck ); if ( !nullcheck && poOgrST->GetRGBFromString ( pszcolor, nR, nG, nB, nA ) ) { poKmlIconStyle->set_color ( Color32 ( nA, nB, nG, nR ) ); } /***** hotspot *****/ double dfDx = poStyleSymbol->SpacingX ( nullcheck ); double dfDy = poStyleSymbol->SpacingY ( nullcheck2 ); if ( !nullcheck && !nullcheck2 ) { if ( !poKmlIconStyle) poKmlIconStyle = poKmlFactory->CreateIconStyle ( ); HotSpotPtr poKmlHotSpot = poKmlFactory->CreateHotSpot ( ); poKmlHotSpot->set_x ( dfDx ); poKmlHotSpot->set_y ( dfDy ); poKmlIconStyle->set_hotspot ( poKmlHotSpot ); } break; } case OGRSTCLabel: { GBool nullcheck; GBool nullcheck2; poKmlLabelStyle = poKmlFactory->CreateLabelStyle ( ); OGRStyleLabel *poStyleLabel = ( OGRStyleLabel * ) poOgrST; /***** color *****/ int nR, nG, nB, nA; const char *pszcolor = poStyleLabel->ForeColor ( nullcheck ); if ( !nullcheck && poStyleLabel->GetRGBFromString ( pszcolor, nR, nG, nB, nA ) ) { poKmlLabelStyle->set_color ( Color32 ( nA, nB, nG, nR ) ); } /***** scale *****/ double dfScale = poStyleLabel->Stretch ( nullcheck ); if ( !nullcheck ) { dfScale /= 100.0; poKmlLabelStyle->set_scale ( dfScale ); } /***** heading *****/ double heading = poStyleLabel->Angle ( nullcheck ); if ( !nullcheck ) { if ( !poKmlIconStyle) { poKmlIconStyle = poKmlFactory->CreateIconStyle ( ); IconStyleIconPtr poKmlIcon = poKmlFactory->CreateIconStyleIcon ( ); poKmlIconStyle->set_icon ( poKmlIcon ); } poKmlIconStyle->set_heading ( heading ); } /***** hotspot *****/ double dfDx = poStyleLabel->SpacingX ( nullcheck ); double dfDy = poStyleLabel->SpacingY ( nullcheck2 ); if ( !nullcheck && !nullcheck2 ) { if ( !poKmlIconStyle) { poKmlIconStyle = poKmlFactory->CreateIconStyle ( ); IconStyleIconPtr poKmlIcon = poKmlFactory->CreateIconStyleIcon ( ); poKmlIconStyle->set_icon ( poKmlIcon ); } HotSpotPtr poKmlHotSpot = poKmlFactory->CreateHotSpot ( ); poKmlHotSpot->set_x ( dfDx ); poKmlHotSpot->set_y ( dfDy ); poKmlIconStyle->set_hotspot ( poKmlHotSpot ); } /***** label text *****/ const char *pszText = poStyleLabel->TextString ( nullcheck ); if ( !nullcheck ) { if ( poKmlPlacemark ) { poKmlPlacemark->set_name( pszText ); } } break; } case OGRSTCNone: default: break; } delete poOgrST; } if ( poKmlLineStyle ) poKmlStyle->set_linestyle ( poKmlLineStyle ); if ( poKmlPolyStyle ) poKmlStyle->set_polystyle ( poKmlPolyStyle ); if ( poKmlIconStyle ) poKmlStyle->set_iconstyle ( poKmlIconStyle ); if ( poKmlLabelStyle ) poKmlStyle->set_labelstyle ( poKmlLabelStyle ); delete poOgrSM; }
OGRErr OGRJMLWriterLayer::ICreateFeature( OGRFeature *poFeature ) { /* Finish column declaration if we haven't yet created a feature */ if( !bFeaturesWritten ) { if( bAddOGRStyleField && poFeatureDefn->GetFieldIndex("OGR_STYLE") < 0 ) { WriteColumnDeclaration( "OGR_STYLE", "STRING" ); } if( bAddRGBField && poFeatureDefn->GetFieldIndex("R_G_B") < 0 ) { WriteColumnDeclaration( "R_G_B", "STRING" ); } VSIFPrintfL(fp, "</ColumnDefinitions>\n</JCSGMLInputTemplate>\n<featureCollection>\n"); bFeaturesWritten = TRUE; } if( bClassicGML ) VSIFPrintfL(fp, " <featureMember>\n"); VSIFPrintfL(fp, " <feature>\n"); /* Add geometry */ VSIFPrintfL(fp, " <geometry>\n"); OGRGeometry* poGeom = poFeature->GetGeometryRef(); if( poGeom != NULL ) { char* pszGML = poGeom->exportToGML(); VSIFPrintfL(fp, " %s\n", pszGML); CPLFree(pszGML); } else { VSIFPrintfL(fp, " %s\n", "<gml:MultiGeometry></gml:MultiGeometry>"); } VSIFPrintfL(fp, " </geometry>\n"); /* Add fields */ for(int i=0;i<poFeature->GetFieldCount();i++) { char* pszName = OGRGetXML_UTF8_EscapedString( poFeatureDefn->GetFieldDefn(i)->GetNameRef() ); if( bClassicGML ) VSIFPrintfL(fp, " <%s>", pszName); else VSIFPrintfL(fp, " <property name=\"%s\">", pszName); if( poFeature->IsFieldSet(i) ) { OGRFieldType eType = poFeatureDefn->GetFieldDefn(i)->GetType(); if( eType == OFTString ) { char* pszValue = OGRGetXML_UTF8_EscapedString( poFeature->GetFieldAsString(i) ); VSIFPrintfL(fp, "%s", pszValue); CPLFree(pszValue); } else if( eType == OFTDateTime ) { int nYear, nMonth, nDay, nHour, nMinute, nTZFlag; float fSecond; poFeature->GetFieldAsDateTime(i, &nYear, &nMonth, &nDay, &nHour, &nMinute, &fSecond, &nTZFlag); /* When writing time zone, OpenJUMP expects .XXX seconds */ /* to be written */ if( nTZFlag > 1 || OGR_GET_MS(fSecond) != 0 ) VSIFPrintfL(fp, "%04d-%02d-%02dT%02d:%02d:%06.3f", nYear, nMonth, nDay, nHour, nMinute, fSecond); else VSIFPrintfL(fp, "%04d-%02d-%02dT%02d:%02d:%02d", nYear, nMonth, nDay, nHour, nMinute, (int)fSecond); if( nTZFlag > 1 ) { int nOffset = (nTZFlag - 100) * 15; int nHours = (int) (nOffset / 60); // round towards zero int nMinutes = ABS(nOffset - nHours * 60); if( nOffset < 0 ) { VSIFPrintfL(fp, "-" ); nHours = ABS(nHours); } else VSIFPrintfL(fp, "+" ); VSIFPrintfL(fp, "%02d%02d", nHours, nMinutes ); } } else { VSIFPrintfL(fp, "%s", poFeature->GetFieldAsString(i)); } } if( bClassicGML ) VSIFPrintfL(fp, "</%s>\n", pszName); else VSIFPrintfL(fp, "</property>\n"); CPLFree(pszName); } /* Add OGR_STYLE from feature style string (if asked) */ if( bAddOGRStyleField && poFeatureDefn->GetFieldIndex("OGR_STYLE") < 0 ) { if( bClassicGML ) VSIFPrintfL(fp, " <OGR_STYLE>"); else VSIFPrintfL(fp, " <property name=\"%s\">", "OGR_STYLE"); if( poFeature->GetStyleString() != NULL ) { char* pszValue = OGRGetXML_UTF8_EscapedString( poFeature->GetStyleString() ); VSIFPrintfL(fp, "%s", pszValue); CPLFree(pszValue); } if( bClassicGML ) VSIFPrintfL(fp, "</OGR_STYLE>\n"); else VSIFPrintfL(fp, "</property>\n"); } /* Derive R_G_B field from feature style string */ if( bAddRGBField && poFeatureDefn->GetFieldIndex("R_G_B") < 0 ) { if( bClassicGML ) VSIFPrintfL(fp, " <R_G_B>"); else VSIFPrintfL(fp, " <property name=\"%s\">", "R_G_B"); if( poFeature->GetStyleString() != NULL ) { OGRGeometry* poGeom = poFeature->GetGeometryRef(); OGRwkbGeometryType eGeomType = poGeom ? wkbFlatten(poGeom->getGeometryType()) : wkbUnknown; OGRStyleMgr oMgr; oMgr.InitFromFeature(poFeature); for(int i=0;i<oMgr.GetPartCount();i++) { OGRStyleTool* poTool = oMgr.GetPart(i); if( poTool != NULL ) { const char* pszColor = NULL; if( poTool->GetType() == OGRSTCPen && eGeomType != wkbPolygon && eGeomType != wkbMultiPolygon ) { GBool bIsNull; pszColor = ((OGRStylePen*)poTool)->Color(bIsNull); if( bIsNull ) pszColor = NULL; } else if( poTool->GetType() == OGRSTCBrush ) { GBool bIsNull; pszColor = ((OGRStyleBrush*)poTool)->ForeColor(bIsNull); if( bIsNull ) pszColor = NULL; } int R, G, B, A; if( pszColor != NULL && poTool->GetRGBFromString(pszColor, R, G, B, A) && A != 0 ) { VSIFPrintfL(fp, "%02X%02X%02X", R, G, B); } delete poTool; } } } if( bClassicGML ) VSIFPrintfL(fp, "</R_G_B>\n"); else VSIFPrintfL(fp, "</property>\n"); } VSIFPrintfL(fp, " </feature>\n"); if( bClassicGML ) VSIFPrintfL(fp, " </featureMember>\n"); poFeature->SetFID(nNextFID ++); return OGRERR_NONE; }