Skip to content
Permalink
Browse files
msSLDApplySLD(): tidy-up code. No functional change
  • Loading branch information
rouault committed Oct 24, 2019
1 parent 346c167 commit 420dcb114e4085066d739f41ff3c8fb0ffa73721
Showing 1 changed file with 112 additions and 104 deletions.
@@ -151,23 +151,13 @@ int msSLDApplySLD(mapObj *map, const char *psSLDXML, int iLayer, const char *psz

#ifdef USE_OGR

int nLayers = 0;
layerObj *pasLayers = NULL;
int i, j, k, z, iClass;
int bUseSpecificLayer = 0;
const char *pszTmp = NULL;
int nSLDLayers = 0;
layerObj *pasSLDLayers = NULL;
int nStatus = MS_SUCCESS;
/*const char *pszSLDNotSupported = NULL;*/
char *tmpfilename = NULL;
const char *pszFullName = NULL;
char szTmp[512];
char *pszTmp1=NULL;
char *pszTmp2 = NULL;
char *pszBuffer = NULL;
layerObj *lp = NULL;

pasLayers = msSLDParseSLD(map, psSLDXML, &nLayers);
if( pasLayers == NULL ) {

pasSLDLayers = msSLDParseSLD(map, psSLDXML, &nSLDLayers);
if( pasSLDLayers == NULL ) {
errorObj* psError = msGetErrorObj();
if( psError && psError->code != MS_NOERR )
return MS_FAILURE;
@@ -177,20 +167,22 @@ int msSLDApplySLD(mapObj *map, const char *psSLDXML, int iLayer, const char *psz
/* If the same layer is given more that once, we need to */
/* duplicate it. */
/* -------------------------------------------------------------------- */
if (pasLayers && nLayers>0) {
int l,m;
for (m=0; m<nLayers; m++) {
if (pasSLDLayers && nSLDLayers>0) {
int i;
int m;
for (m=0; m<nSLDLayers; m++) {
int l;
layerObj *psTmpLayer=NULL;
int nIndex;
char tmpId[128];
nIndex = msGetLayerIndex(map, pasLayers[m].name);
if(pasLayers[m].name == NULL) continue;
for (l=0; l<nLayers; l++) {
if(pasLayers[l].name == NULL)
nIndex = msGetLayerIndex(map, pasSLDLayers[m].name);
if(pasSLDLayers[m].name == NULL) continue;
for (l=0; l<nSLDLayers; l++) {
if(pasSLDLayers[l].name == NULL)
continue;


if (m !=l && strcasecmp(pasLayers[m].name, pasLayers[l].name)== 0 &&
if (m !=l && strcasecmp(pasSLDLayers[m].name, pasSLDLayers[l].name)== 0 &&
nIndex != -1) {
psTmpLayer = (layerObj *) malloc(sizeof(layerObj));
initLayer(psTmpLayer, map);
@@ -205,92 +197,106 @@ int msSLDApplySLD(mapObj *map, const char *psSLDXML, int iLayer, const char *psz
if (psTmpLayer->name)
msFree(psTmpLayer->name);
psTmpLayer->name = msStrdup(tmpId);
msFree(pasLayers[l].name);
pasLayers[l].name = msStrdup(tmpId);
msFree(pasSLDLayers[l].name);
pasSLDLayers[l].name = msStrdup(tmpId);
msInsertLayer(map, psTmpLayer, -1);
MS_REFCNT_DECR(psTmpLayer);
}
}
}
for (i=0; i<map->numlayers; i++) {
layerObj *lp = NULL;
const char *pszWMSLayerName = NULL;
int j;
int bUseSpecificLayer = 0;

if (iLayer >=0 && iLayer< map->numlayers) {
i = iLayer;
bUseSpecificLayer = 1;
}

lp = GET_LAYER(map, i);

/* compare layer name to wms_name as well */
pszTmp = msOWSLookupMetadata(&(GET_LAYER(map, i)->metadata), "MO", "name");
pszWMSLayerName = msOWSLookupMetadata(&(lp->metadata), "MO", "name");

for (j=0; j<nSLDLayers; j++) {
layerObj* sldLayer = &pasSLDLayers[j];

for (j=0; j<nLayers; j++) {
/* -------------------------------------------------------------------- */
/* copy : - class */
/* - layer's labelitem */
/* -------------------------------------------------------------------- */
if ((pasLayers[j].name && pszStyleLayerName == NULL &&
((strcasecmp(GET_LAYER(map, i)->name, pasLayers[j].name) == 0 ||
(pszTmp && strcasecmp(pszTmp, pasLayers[j].name) == 0))||
(GET_LAYER(map, i)->group &&
strcasecmp(GET_LAYER(map, i)->group, pasLayers[j].name) == 0))) ||
(bUseSpecificLayer && pszStyleLayerName && pasLayers[j].name &&
strcasecmp(pasLayers[j].name, pszStyleLayerName) == 0)) {
if ((sldLayer->name && pszStyleLayerName == NULL &&
((strcasecmp(lp->name, sldLayer->name) == 0 ||
(pszWMSLayerName && strcasecmp(pszWMSLayerName, sldLayer->name) == 0))||
(lp->group &&
strcasecmp(lp->group, sldLayer->name) == 0))) ||
(bUseSpecificLayer && pszStyleLayerName && sldLayer->name &&
strcasecmp(sldLayer->name, pszStyleLayerName) == 0)) {
#ifdef notdef
/*this is a test code if we decide to flag some layers as not supporting SLD*/
pszSLDNotSupported = msOWSLookupMetadata(&(GET_LAYER(map, i)->metadata), "M", "SLD_NOT_SUPPORTED");
pszSLDNotSupported = msOWSLookupMetadata(&(lp->metadata), "M", "SLD_NOT_SUPPORTED");
if (pszSLDNotSupported) {
msSetError(MS_WMSERR, "Layer %s does not support SLD", "msSLDApplySLD", pasLayers[j].name);
msSetError(MS_WMSERR, "Layer %s does not support SLD", "msSLDApplySLD", sldLayer->name);
nsStatus = MS_FAILURE;
goto sld_cleanup;
}
#endif

if ( pasLayers[j].numclasses > 0) {
GET_LAYER(map, i)->type = pasLayers[j].type;
if ( sldLayer->numclasses > 0) {
int iClass = 0;
int k;

for(k=0; k<GET_LAYER(map, i)->numclasses; k++) {
if (GET_LAYER(map, i)->class[k] != NULL) {
GET_LAYER(map, i)->class[k]->layer=NULL;
if (freeClass(GET_LAYER(map, i)->class[k]) == MS_SUCCESS ) {
msFree(GET_LAYER(map, i)->class[k]);
GET_LAYER(map, i)->class[k] = NULL;
lp->type = sldLayer->type;

for(k=0; k<lp->numclasses; k++) {
if (lp->class[k] != NULL) {
lp->class[k]->layer=NULL;
if (freeClass(lp->class[k]) == MS_SUCCESS ) {
msFree(lp->class[k]);
lp->class[k] = NULL;
}
}
}

GET_LAYER(map, i)->numclasses = 0;
lp->numclasses = 0;

/*unset the classgroup on the layer if it was set. This allows the layer to render
with all the classes defined in the SLD*/
msFree(GET_LAYER(map, i)->classgroup);
GET_LAYER(map, i)->classgroup = NULL;
msFree(lp->classgroup);
lp->classgroup = NULL;

iClass = 0;
for (k=0; k < pasLayers[j].numclasses; k++) {
if (msGrowLayerClasses(GET_LAYER(map, i)) == NULL)
for (k=0; k < sldLayer->numclasses; k++) {
if (msGrowLayerClasses(lp) == NULL)
return MS_FAILURE;

initClass(GET_LAYER(map, i)->class[iClass]);
msCopyClass(GET_LAYER(map, i)->class[iClass],
pasLayers[j].class[k], NULL);
GET_LAYER(map, i)->class[iClass]->layer = GET_LAYER(map, i);
GET_LAYER(map, i)->numclasses++;
initClass(lp->class[iClass]);
msCopyClass(lp->class[iClass],
sldLayer->class[k], NULL);
lp->class[iClass]->layer = lp;
lp->numclasses++;

/*aliases may have been used as part of the sld text symbolizer for
label element. Try to process it if that is the case #3114*/
if (msLayerOpen(GET_LAYER(map, i)) == MS_SUCCESS &&
msLayerGetItems(GET_LAYER(map, i)) == MS_SUCCESS) {
if (GET_LAYER(map, i)->class[iClass]->text.string) {
for(z=0; z<GET_LAYER(map, i)->numitems; z++) {
if (!GET_LAYER(map, i)->items[z] || strlen(GET_LAYER(map, i)->items[z]) <= 0)
if (msLayerOpen(lp) == MS_SUCCESS &&
msLayerGetItems(lp) == MS_SUCCESS) {
if (lp->class[iClass]->text.string) {
int z;
for(z=0; z<lp->numitems; z++) {
const char* pszFullName;
char szTmp[512];
char* pszTmp1;
if (!lp->items[z] || strlen(lp->items[z]) <= 0)
continue;
snprintf(szTmp, sizeof(szTmp), "%s_alias", GET_LAYER(map, i)->items[z]);
pszFullName = msOWSLookupMetadata(&(GET_LAYER(map, i)->metadata), "G", szTmp);
pszTmp1 = msStrdup( GET_LAYER(map, i)->class[iClass]->text.string);
snprintf(szTmp, sizeof(szTmp), "%s_alias", lp->items[z]);
pszFullName = msOWSLookupMetadata(&(lp->metadata), "G", szTmp);
pszTmp1 = msStrdup( lp->class[iClass]->text.string);
if (pszFullName != NULL && (strstr(pszTmp1, pszFullName) != NULL)) {
char *tmpstr1= NULL;
tmpstr1 = msReplaceSubstring(pszTmp1, pszFullName, GET_LAYER(map, i)->items[z]);
pszTmp2 = (char *)malloc(sizeof(char)*(strlen(tmpstr1)+3));
char *tmpstr1= msReplaceSubstring(pszTmp1, pszFullName, lp->items[z]);
char* pszTmp2 = (char *)malloc(sizeof(char)*(strlen(tmpstr1)+3));
sprintf(pszTmp2,"(%s)",tmpstr1);
msLoadExpressionString(&(GET_LAYER(map, i)->class[iClass]->text), pszTmp2);
msLoadExpressionString(&(lp->class[iClass]->text), pszTmp2);
msFree(pszTmp2);
}
msFree(pszTmp1);
@@ -302,69 +308,66 @@ int msSLDApplySLD(mapObj *map, const char *psSLDXML, int iLayer, const char *psz
}
} else {
/*this is probably an SLD that uses Named styles*/
if (pasLayers[j].classgroup) {
for (k=0; k<GET_LAYER(map, i)->numclasses; k++) {
if (GET_LAYER(map, i)->class[k]->group &&
strcasecmp(GET_LAYER(map, i)->class[k]->group,
pasLayers[j].classgroup) == 0)
if (sldLayer->classgroup) {
int k;
for (k=0; k<lp->numclasses; k++) {
if (lp->class[k]->group &&
strcasecmp(lp->class[k]->group,
sldLayer->classgroup) == 0)
break;
}
if (k < GET_LAYER(map, i)->numclasses) {
msFree( GET_LAYER(map, i)->classgroup);
GET_LAYER(map, i)->classgroup = msStrdup(pasLayers[j].classgroup);
if (k < lp->numclasses) {
msFree( lp->classgroup);
lp->classgroup = msStrdup(sldLayer->classgroup);
} else {
/* TODO we throw an exception ?*/
}
}
}
if (pasLayers[j].labelitem) {
if (GET_LAYER(map, i)->labelitem)
free(GET_LAYER(map, i)->labelitem);
if (sldLayer->labelitem) {
if (lp->labelitem)
free(lp->labelitem);

GET_LAYER(map, i)->labelitem = msStrdup(pasLayers[j].labelitem);
lp->labelitem = msStrdup(sldLayer->labelitem);
}

if (pasLayers[j].classitem) {
if (GET_LAYER(map, i)->classitem)
free(GET_LAYER(map, i)->classitem);
if (sldLayer->classitem) {
if (lp->classitem)
free(lp->classitem);

GET_LAYER(map, i)->classitem = msStrdup(pasLayers[j].classitem);
lp->classitem = msStrdup(sldLayer->classitem);
}

/* opacity for sld raster */
if (GET_LAYER(map, i)->type == MS_LAYER_RASTER &&
pasLayers[j].compositer && pasLayers[j].compositer->opacity != 100)
msSetLayerOpacity(GET_LAYER(map, i), pasLayers[j].compositer->opacity);
if (lp->type == MS_LAYER_RASTER &&
sldLayer->compositer && sldLayer->compositer->opacity != 100)
msSetLayerOpacity(lp, sldLayer->compositer->opacity);

/* mark as auto-generate SLD */
if (GET_LAYER(map, i)->connectiontype == MS_WMS)
msInsertHashTable(&(GET_LAYER(map, i)->metadata),
if (lp->connectiontype == MS_WMS)
msInsertHashTable(&(lp->metadata),
"wms_sld_body", "auto" );

lp = GET_LAYER(map, i);

/* The SLD might have a FeatureTypeConstraint */
if( pasLayers[j].filter.type == MS_EXPRESSION )
if( sldLayer->filter.type == MS_EXPRESSION )
{
if( lp->filter.string && lp->filter.type == MS_EXPRESSION )
{
pszBuffer = msStringConcatenate(NULL, "((");
char* pszBuffer = msStringConcatenate(NULL, "((");
pszBuffer = msStringConcatenate(pszBuffer, lp->filter.string);
pszBuffer = msStringConcatenate(pszBuffer, ") AND (");
pszBuffer = msStringConcatenate(pszBuffer, pasLayers[j].filter.string);
pszBuffer = msStringConcatenate(pszBuffer, sldLayer->filter.string);
pszBuffer = msStringConcatenate(pszBuffer, "))");
msFreeExpression(&lp->filter);
msInitExpression(&lp->filter);
lp->filter.string = pszBuffer;
lp->filter.type = MS_EXPRESSION;

pszBuffer = NULL;
}
else
{
msFreeExpression(&lp->filter);
msInitExpression(&lp->filter);
lp->filter.string = msStrdup(pasLayers[j].filter.string);
lp->filter.string = msStrdup(sldLayer->filter.string);
lp->filter.type = MS_EXPRESSION;
}
}
@@ -379,11 +382,14 @@ int msSLDApplySLD(mapObj *map, const char *psSLDXML, int iLayer, const char *psz
/* check first that all classes have an expression type. That is
the only way we can concatenate them and set the filter
expression */
int k;
for (k=0; k<lp->numclasses; k++) {
if (lp->class[k]->expression.type != MS_EXPRESSION)
break;
}
if (k == lp->numclasses) {
char szTmp[512];
char* pszBuffer = NULL;
for (k=0; k<lp->numclasses; k++) {
if (pszBuffer == NULL)
snprintf(szTmp, sizeof(szTmp), "%s", "(("); /* we a building a string expression, explicitly set type below */
@@ -403,7 +409,6 @@ int msSLDApplySLD(mapObj *map, const char *psSLDXML, int iLayer, const char *psz
lp->filter.type = MS_EXPRESSION;

msFree(pszBuffer);
pszBuffer = NULL;
}
}
}
@@ -421,11 +426,11 @@ int msSLDApplySLD(mapObj *map, const char *psSLDXML, int iLayer, const char *psz
/* -------------------------------------------------------------------- */
if (ppszLayerNames) {
char *pszTmp = NULL;
for (i=0; i<nLayers; i++) {
if (pasLayers[i].name) {
for (i=0; i<nSLDLayers; i++) {
if (pasSLDLayers[i].name) {
if (pszTmp !=NULL)
pszTmp = msStringConcatenate(pszTmp, ",");
pszTmp = msStringConcatenate(pszTmp, pasLayers[i].name);
pszTmp = msStringConcatenate(pszTmp, pasSLDLayers[i].name);

}
}
@@ -439,12 +444,15 @@ int msSLDApplySLD(mapObj *map, const char *psSLDXML, int iLayer, const char *psz
#ifdef notdef
sld_cleanup:
#endif
for (i=0; i<nLayers; i++)
freeLayer(&pasLayers[i]);
msFree(pasLayers);
{
int i;
for (i=0; i<nSLDLayers; i++)
freeLayer(&pasSLDLayers[i]);
msFree(pasSLDLayers);
}

if(map->debug == MS_DEBUGLEVEL_VVV) {
tmpfilename = msTmpFile(map, map->mappath, NULL, "_sld.map");
char* tmpfilename = msTmpFile(map, map->mappath, NULL, "_sld.map");
if (tmpfilename == NULL) {
tmpfilename = msTmpFile(map, NULL, NULL, "_sld.map" );
}

0 comments on commit 420dcb1

Please sign in to comment.