www.pudn.com > mitab.rar > ogrfeaturestyle.cpp
/******************************************************************************
* $Id: ogrfeaturestyle.cpp,v 1.14 2005/10/12 19:16:59 jlacroix Exp $
*
* Project: OpenGIS Simple Features Reference Implementation
* Purpose: Feature Representation string API
* Author: Stephane Villeneuve, stephane.v@videotron.ca
*
******************************************************************************
* Copyright (c) 2000-2001, Stephane Villeneuve
*
* 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.
******************************************************************************
*
* $Log: ogrfeaturestyle.cpp,v $
* Revision 1.14 2005/10/12 19:16:59 jlacroix
* Add comment in the OGRStyleTool::Parse function
*
* Revision 1.13 2004/12/02 18:24:12 fwarmerdam
* added support for fontname on symbol, per bug 684
*
* Revision 1.12 2004/05/11 00:39:43 warmerda
* make asStyle*[] using methods non-inline
*
* Revision 1.11 2004/02/20 22:41:14 warmerda
* Fixed GetRGBFromString() to use 255 as the default alpha value instead
* of zero. Fixes: http://bugzilla.remotesensing.org/show_bug.cgi?id=306
*
* Revision 1.10 2003/09/29 15:12:20 warmerda
* Fixed memory leaks in the pszValue's of the m_pasStyleValue lists.
*
* Revision 1.9 2002/06/25 14:47:31 warmerda
* CPL_DLL export style api
*
* Revision 1.8 2002/04/11 21:02:20 warmerda
* Fix memory leak in case of parse error reported by Wanshou Jiang.
*
* Revision 1.7 2002/01/16 04:00:25 warmerda
* use CSLTokenizeString2() and avoid discarding quotes when splitting stuff
*
* Revision 1.6 2001/07/18 05:03:05 warmerda
* added CPL_CVSID
*
* Revision 1.5 2001/07/03 04:20:26 danmo
* Allow empty strings in style string param values, and replaced printf messages
* in the code with CPLError() calls.
*
* Revision 1.4 2001/07/03 03:20:55 danmo
* Avoid losing Scale value during OGRStyleTool::Parse().
*
* Revision 1.3 2001/01/19 21:10:47 warmerda
* replaced tabs
*
* Revision 1.2 2000/12/07 03:52:47 danmo
* Unix port - handle strstr() return value as const char *
*
* Revision 1.1 2000/08/18 21:26:01 svillene
* OGR Representation
*
*/
#include "cpl_conv.h"
#include "cpl_string.h"
#include "ogr_feature.h"
#include "ogr_featurestyle.h"
CPL_CVSID("$Id: ogrfeaturestyle.cpp,v 1.14 2005/10/12 19:16:59 jlacroix Exp $");
CPL_C_START
void OGRFeatureStylePuller() {}
CPL_C_END
/****************************************************************************/
/* Class Parameter (used in the String) */
/* */
/* The order of all parameter MUST be the same than in the definition */
/****************************************************************************/
OGRStyleParamId asStylePen[] = {{OGRSTPenColor,"c",FALSE,OGRSTypeString},
{OGRSTPenWidth,"w",TRUE,OGRSTypeDouble},
{OGRSTPenPattern,"p",TRUE,OGRSTypeString},
{OGRSTPenId,"id",FALSE,OGRSTypeString},
{OGRSTPenPerOffset,"dp",TRUE,OGRSTypeDouble},
{OGRSTPenCap,"cap",FALSE,OGRSTypeString},
{OGRSTPenJoin,"j",FALSE,OGRSTypeString},
{OGRSTPenPriority, "l", FALSE, OGRSTypeInteger
}};
OGRStyleParamId asStyleBrush[] = {{OGRSTBrushFColor,"fc",FALSE,OGRSTypeString},
{OGRSTBrushBColor,"bc",FALSE,OGRSTypeString},
{OGRSTBrushId,"id",FALSE,OGRSTypeString},
{OGRSTBrushAngle,"a",TRUE,OGRSTypeDouble},
{OGRSTBrushSize,"s",TRUE,OGRSTypeDouble},
{OGRSTBrushDx,"dx",TRUE,OGRSTypeDouble},
{OGRSTBrushDy,"dy",TRUE,OGRSTypeDouble},
{OGRSTBrushPriority,"l",FALSE,OGRSTypeInteger
}};
OGRStyleParamId asStyleSymbol[] =
{
{OGRSTSymbolId,"id",FALSE,OGRSTypeString},
{OGRSTSymbolAngle,"a",FALSE,OGRSTypeDouble},
{OGRSTSymbolColor,"c",FALSE,OGRSTypeString},
{OGRSTSymbolSize,"s",TRUE,OGRSTypeDouble},
{OGRSTSymbolDx,"dx",TRUE,OGRSTypeDouble},
{OGRSTSymbolDy,"dy",TRUE,OGRSTypeDouble},
{OGRSTSymbolStep,"ds",TRUE,OGRSTypeDouble},
{OGRSTSymbolPerp,"dp",TRUE,OGRSTypeDouble},
{OGRSTSymbolOffset,"di",TRUE,OGRSTypeDouble},
{OGRSTSymbolPriority,"l",FALSE,OGRSTypeInteger},
{OGRSTSymbolFontName,"f",FALSE,OGRSTypeString}
};
OGRStyleParamId asStyleLabel[] = {{OGRSTLabelFontName,"f",FALSE,OGRSTypeString},
{OGRSTLabelSize,"s",TRUE,OGRSTypeDouble},
{OGRSTLabelTextString,"t",FALSE,
OGRSTypeString},
{OGRSTLabelAngle,"a",FALSE,OGRSTypeDouble},
{OGRSTLabelFColor,"c",FALSE,OGRSTypeString},
{OGRSTLabelBColor,"b",FALSE,OGRSTypeString},
{OGRSTLabelPlacement,"m",FALSE,
OGRSTypeString},
{OGRSTLabelAnchor,"p",FALSE,OGRSTypeInteger},
{OGRSTLabelDx,"dx",TRUE,OGRSTypeDouble},
{OGRSTLabelDy,"dy",TRUE,OGRSTypeDouble},
{OGRSTLabelPerp,"dp",TRUE,OGRSTypeDouble},
{OGRSTLabelBold,"bo",FALSE,OGRSTypeInteger},
{OGRSTLabelItalic,"it",FALSE,OGRSTypeInteger},
{OGRSTLabelUnderline,"un",FALSE,
OGRSTypeInteger},
{OGRSTLabelPriority,"l",FALSE,OGRSTypeInteger
}};
/* ======================================================================== */
/* OGRStyleMgr */
/* ======================================================================== */
/****************************************************************************/
/* OGRStyleMgr::OGRStyleMgr(OGRStyleTable *poDataSetStyleTable) */
/* */
/****************************************************************************/
OGRStyleMgr::OGRStyleMgr(OGRStyleTable *poDataSetStyleTable)
{
m_poDataSetStyleTable = poDataSetStyleTable;
m_pszStyleString = NULL;
}
/****************************************************************************/
/* OGRStyleMgr::~OGRStyleMgr() */
/* */
/****************************************************************************/
OGRStyleMgr::~OGRStyleMgr()
{
if (m_pszStyleString)
CPLFree(m_pszStyleString);
}
/****************************************************************************/
/* GBool OGRStyleMgr::SetFeatureStyleString(OGRFeature *poFeature, */
/* char *pszStyleString, */
/* GBool bNoMatching) */
/* Set the gived representation to the feature, */
/* if bNoMatching == TRUE, don't try to find it in the styletable */
/* otherwize, we will use the name defined in the styletable */
/****************************************************************************/
GBool OGRStyleMgr::SetFeatureStyleString(OGRFeature *poFeature,
const char *pszStyleString,
GBool bNoMatching)
{
const char *pszName;
if (poFeature == FALSE)
return FALSE;
if (pszStyleString == NULL)
poFeature->SetStyleString("");
else if (bNoMatching == TRUE)
poFeature->SetStyleString(pszStyleString);
else if ((pszName = GetStyleName(pszStyleString)) != NULL)
poFeature->SetStyleString(pszName);
else
poFeature->SetStyleString(pszStyleString);
return TRUE;
}
/****************************************************************************/
/* const char *OGRStyleMgr::InitFromFeature(OGRFeature *) */
/* */
/****************************************************************************/
const char *OGRStyleMgr::InitFromFeature(OGRFeature *poFeature)
{
CPLFree(m_pszStyleString);
m_pszStyleString = NULL;
if (poFeature)
InitStyleString(poFeature->GetStyleString());
else
m_pszStyleString = NULL;
return m_pszStyleString;
}
/****************************************************************************/
/* GBool OGRStyleMgr::InitStyleString(char *pszStyleString) */
/* */
/****************************************************************************/
GBool OGRStyleMgr::InitStyleString(const char *pszStyleString)
{
CPLFree(m_pszStyleString);
m_pszStyleString = NULL;
if (pszStyleString && pszStyleString[0] == '@')
m_pszStyleString = CPLStrdup(GetStyleByName(pszStyleString));
else
m_pszStyleString = NULL;
if (m_pszStyleString == NULL && pszStyleString)
m_pszStyleString = CPLStrdup(pszStyleString);
return TRUE;
}
/****************************************************************************/
/* const char *OGRStyleMgr::GetStyleName(const char *pszStyleString) */
/* */
/****************************************************************************/
const char *OGRStyleMgr::GetStyleName(const char *pszStyleString)
{
// SECURITY: the unit and the value for all parameter should be the same,
// a text comparaison is executed .
const char *pszStyle;
if (pszStyleString)
pszStyle = pszStyleString;
else
pszStyle = m_pszStyleString;
if (pszStyle)
{
if (m_poDataSetStyleTable)
return m_poDataSetStyleTable->GetStyleName(pszStyle);
}
return NULL;
}
/****************************************************************************/
/* const char *OGRStyleMgr::GetStyleByName(const char *pszStyleName) */
/* */
/****************************************************************************/
const char *OGRStyleMgr::GetStyleByName(const char *pszStyleName)
{
if (m_poDataSetStyleTable)
{
return m_poDataSetStyleTable->Find(pszStyleName);
}
return NULL;
}
/****************************************************************************/
/* GBool OGRStyleMgr::AddStyle(char *pszStyleName, */
/* char *pszStyleString) */
/* */
/****************************************************************************/
GBool OGRStyleMgr::AddStyle(const char *pszStyleName,
const char *pszStyleString)
{
const char *pszStyle;
if (pszStyleString)
pszStyle = pszStyleString;
else
pszStyle = m_pszStyleString;
if (m_poDataSetStyleTable)
{
return m_poDataSetStyleTable->AddStyle(pszStyleName, pszStyle);
}
return FALSE;
}
/****************************************************************************/
/* const char *OGRStyleMgr::GetStyleString(OGRFeature *) */
/* */
/****************************************************************************/
const char *OGRStyleMgr::GetStyleString(OGRFeature *poFeature)
{
if (poFeature == NULL)
return m_pszStyleString;
else
return InitFromFeature(poFeature);
}
GBool OGRStyleMgr::AddPart(const char *pszPart)
{
char *pszTmp;
if (pszPart)
{
if (m_pszStyleString)
{
pszTmp = CPLStrdup(CPLSPrintf("%s;%s",m_pszStyleString,
pszPart));
CPLFree(m_pszStyleString);
m_pszStyleString = pszTmp;
}
else
{
pszTmp= CPLStrdup(CPLSPrintf("%s",pszPart));
CPLFree(m_pszStyleString);
m_pszStyleString = pszTmp;
}
return TRUE;
}
return FALSE;
}
/****************************************************************************/
/* GBool OGRStyleMgr::AddPart(OGRStyleTool *) */
/* Add a new part in the current style */
/****************************************************************************/
GBool OGRStyleMgr::AddPart(OGRStyleTool *poStyleTool)
{
char *pszTmp;
if (poStyleTool)
{
if (m_pszStyleString)
{
pszTmp = CPLStrdup(CPLSPrintf("%s;%s",m_pszStyleString,
poStyleTool->GetStyleString()));
CPLFree(m_pszStyleString);
m_pszStyleString = pszTmp;
}
else
{
pszTmp= CPLStrdup(CPLSPrintf("%s",
poStyleTool->GetStyleString()));
CPLFree(m_pszStyleString);
m_pszStyleString = pszTmp;
}
return TRUE;
}
return FALSE;
}
/****************************************************************************/
/* int OGRStyleMgr::GetPartCount(const char *pszStyleString) */
/* return the number of part in the stylestring */
/****************************************************************************/
int OGRStyleMgr::GetPartCount(const char *pszStyleString)
{
const char *pszPart;
int nPartCount = 1;
const char *pszString;
const char *pszStrTmp;
if (pszStyleString != NULL)
pszString = pszStyleString;
else
pszString = m_pszStyleString;
if (pszString == NULL)
return 0;
pszStrTmp = pszString;
while ((pszPart = strstr(pszStrTmp,";")) != NULL)
{
pszStrTmp = &pszPart[1];
nPartCount++;
}
return nPartCount;
}
/****************************************************************************/
/* OGRStyleTool *OGRStyleMgr::GetPart(int hPartId, */
/* const char *pszStyleString) */
/* */
/* Return a StyleTool of the type of the wanted part, could return NULL */
/****************************************************************************/
OGRStyleTool *OGRStyleMgr::GetPart(int hPartId,
const char *pszStyleString)
{
char **papszStyleString;
const char *pszStyle;
const char *pszString;
OGRStyleTool *poStyleTool = NULL;
if (pszStyleString)
pszStyle = pszStyleString;
else
pszStyle = m_pszStyleString;
if (pszStyle == NULL)
return NULL;
papszStyleString = CSLTokenizeString2(pszStyle, ";",
CSLT_HONOURSTRINGS
| CSLT_PRESERVEQUOTES
| CSLT_PRESERVEESCAPES );
pszString = CSLGetField(papszStyleString,hPartId);
if (pszString || strlen(pszString) >0)
{
poStyleTool = CreateStyleToolFromStyleString(pszString);
if (poStyleTool)
poStyleTool->SetStyleString(pszString);
CSLDestroy(papszStyleString);
return poStyleTool;
}
else
{
CSLDestroy(papszStyleString);
return NULL;
}
}
/****************************************************************************/
/* OGRStyleTool *CreateStyleToolFromStyleString(const char *pszStyleString) */
/* */
/* create a Style tool from the gived StyleString, it should contain only a */
/* part of a StyleString */
/****************************************************************************/
OGRStyleTool *OGRStyleMgr::CreateStyleToolFromStyleString(const char *
pszStyleString)
{
char **papszToken = CSLTokenizeString2(pszStyleString,"();",
CSLT_HONOURSTRINGS
| CSLT_PRESERVEQUOTES
| CSLT_PRESERVEESCAPES );
OGRStyleTool *poStyleTool;
if (CSLCount(papszToken) <2)
poStyleTool = NULL;
else if (EQUAL(papszToken[0],"PEN"))
poStyleTool = new OGRStylePen();
else if (EQUAL(papszToken[0],"BRUSH"))
poStyleTool = new OGRStyleBrush();
else if (EQUAL(papszToken[0],"SYMBOL"))
poStyleTool = new OGRStyleSymbol();
else if (EQUAL(papszToken[0],"LABEL"))
poStyleTool = new OGRStyleLabel();
else
poStyleTool = NULL;
CSLDestroy( papszToken );
return poStyleTool;
}
/* ======================================================================== */
/* OGRStyleTable */
/* Object Used to manage and store a styletable */
/* ======================================================================== */
/****************************************************************************/
/* OGRStyleTable::OGRStyleTable() */
/* */
/****************************************************************************/
OGRStyleTable::OGRStyleTable()
{
m_papszStyleTable = NULL;
}
/****************************************************************************/
/* OGRStyleTable::~OGRStyleTable() */
/* */
/****************************************************************************/
OGRStyleTable::~OGRStyleTable()
{
Clear();
}
/****************************************************************************/
/* void OGRStyleTable::Clear() */
/* */
/****************************************************************************/
void OGRStyleTable::Clear()
{
if (m_papszStyleTable)
CSLDestroy(m_papszStyleTable);
m_papszStyleTable = NULL;
}
/****************************************************************************/
/* const char *OGRStyleTable::GetStyleName(const char *pszStyleString) */
/* */
/* return the Name of a gived stylestring otherwise NULL */
/****************************************************************************/
const char *OGRStyleTable::GetStyleName(const char *pszStyleString)
{
int i;
const char *pszStyleStringBegin;
static char *pszName = NULL;
char *pszTmp;
if (pszName)
CPLFree(pszName);
pszName = NULL;
for (i=0;i=3)
return TRUE;
else
return FALSE;
}
/****************************************************************************/
/* OGRSTClassId OGRStyleTool::GetSpecificId */
/* return -1, if the wanted type is not found, ex: */
/* if you want ogr-pen value, pszWanted should be ogr-pen(case sensitive)*/
/****************************************************************************/
int OGRStyleTool::GetSpecificId(const char *pszId, const char *pszWanted)
{
const char *pszRealWanted = pszWanted;
const char *pszFound;
int nValue = -1;
if (pszWanted == NULL || strlen(pszWanted) == 0)
pszRealWanted = "ogr-pen";
if (pszId == NULL)
return -1;
if ((pszFound = strstr(pszId, pszRealWanted)) != NULL)
{
// We found the string, it could be no value after it, use default one
nValue = 0;
if (pszFound[strlen(pszRealWanted)] == '-' )
nValue =atoi(&pszFound[strlen(pszRealWanted)+1]);
}
return nValue;
}
/****************************************************************************/
/* OGRSTClassId OGRStyleTool::GetType() */
/* */
/****************************************************************************/
OGRSTClassId OGRStyleTool::GetType()
{
return m_eClassId;
}
/****************************************************************************/
/* void OGRStyleTool::SetUnit(OGRSTUnitId,double dfScale) */
/* */
/****************************************************************************/
void OGRStyleTool::SetUnit(OGRSTUnitId eUnit,double dfScale)
{
m_dfScale = dfScale;
m_eUnit = eUnit;
}
/****************************************************************************/
/* GBool OGRStyleTool::Parse(OGRStyleParamId *pasStyle, */
/* OGRStyleValue *pasValue, */
/* int nCount) */
/* */
/****************************************************************************/
GBool OGRStyleTool::Parse(OGRStyleParamId *pasStyle,
OGRStyleValue *pasValue,
int nCount)
{
int i,j;
char **papszToken; // Token to contains StyleString Type and content
char **papszToken2; // Token that will contains StyleString elements
OGRSTUnitId eLastUnit;
if (IsStyleParsed() == TRUE)
return TRUE;
StyleParsed();
if (m_pszStyleString == NULL)
return FALSE;
// Tokenize the String to get the Type and the content
// Example: Type(elem1:val2,elem2:val2)
papszToken = CSLTokenizeString2(m_pszStyleString,"()",
CSLT_HONOURSTRINGS
| CSLT_PRESERVEQUOTES
| CSLT_PRESERVEESCAPES );
if (CSLCount(papszToken) > 2 || CSLCount(papszToken) == 0)
{
CSLDestroy( papszToken );
CPLError(CE_Failure, CPLE_AppDefined,
"Error in the format of the StyleTool %s\n",m_pszStyleString);
return FALSE;
}
// Tokenize the content of the StyleString to get every component in it.
papszToken2 = CSLTokenizeString2(papszToken[1],":,",
CSLT_HONOURSTRINGS
| CSLT_ALLOWEMPTYTOKENS );
if (CSLCount(papszToken2) %2 != 0)
{
CSLDestroy( papszToken );
CSLDestroy( papszToken2 );
CPLError(CE_Failure, CPLE_AppDefined,
"Error in the StyleTool String %s\n",m_pszStyleString);
return FALSE;
}
// Valid that we have the right StyleString for this feature type.
switch (GetType())
{
case OGRSTCPen:
if (!EQUAL(papszToken[0],"PEN"))
{
CPLError(CE_Failure, CPLE_AppDefined,
"Error in the Type of StyleTool %s should be a PEN Type\n",
papszToken[0]);
CSLDestroy( papszToken );
CSLDestroy( papszToken2 );
return FALSE;
}
break;
case OGRSTCBrush:
if (!EQUAL(papszToken[0],"BRUSH"))
{
CPLError(CE_Failure, CPLE_AppDefined,
"Error in the Type of StyleTool %s should be a BRUSH Type\n",
papszToken[0]);
CSLDestroy( papszToken );
CSLDestroy( papszToken2 );
return FALSE;
}
break;
case OGRSTCSymbol:
if (!EQUAL(papszToken[0],"SYMBOL"))
{
CPLError(CE_Failure, CPLE_AppDefined,
"Error in the Type of StyleTool %s should be a SYMBOL Type\n",
papszToken[0]);
CSLDestroy( papszToken );
CSLDestroy( papszToken2 );
return FALSE;
}
break;
case OGRSTCLabel:
if (!EQUAL(papszToken[0],"LABEL"))
{
CPLError(CE_Failure, CPLE_AppDefined,
"Error in the Type of StyleTool %s should be a LABEL Type\n",
papszToken[0]);
CSLDestroy( papszToken );
CSLDestroy( papszToken2 );
return FALSE;
}
break;
default:
CPLError(CE_Failure, CPLE_AppDefined,
"Error in the Type of StyleTool, Type undetermined\n");
CSLDestroy( papszToken );
CSLDestroy( papszToken2 );
return FALSE;
break;
}
i=0;
////////////////////////////////////////////////////////////////////////
// Here we will loop on each element in the StyleString. If it's
// a valid element, we will add it in the StyleTool with
// SetParamStr().
//
// It's important to note that the SetInternalUnit...() is use to update
// the unit of the StyleTool param (m_eUnit).
// See OGRStyleTool::SetParamStr().
// There's a StyleTool unit (m_eUnit), which is the output unit, and each
// parameter of the style have its own unit value (the input unit). Here we
// set m_eUnit to the input unit and in SetParamStr(), we will use thi
// value to set the input unit. Then after the loop we will reset m_eUnit
// to it's original value. (Yes it's a side effect / black magic)
//
// The pasStyle variable is a global variable passed in argument to the
// function. See at the top of this file the four OGRStyleParamId
// variable. They are used to register the valid parameter of each
// StyleTool.
////////////////////////////////////////////////////////////////////////
// Save Scale and output Units because the parsing code will alter
// the values
eLastUnit = m_eUnit;
double dSavedScale = m_dfScale;
while (i < CSLCount(papszToken2))
{
for (j=0;j