Permalink
Browse files

Fix usage of static string for epsg lookups (#4731)

  • Loading branch information...
tbonfort committed Nov 10, 2015
1 parent af3bd6e commit 4dd30cd194a70eeb69a2a3584b645a04db8b14ad
Showing with 295 additions and 208 deletions.
  1. +11 −8 mapcontext.c
  2. +16 −18 mapgml.c
  3. +22 −12 mapogcsos.c
  4. +40 −32 mapows.c
  5. +1 −1 mapows.h
  6. +79 −30 mapwcs.c
  7. +3 −2 mapwcs.h
  8. +2 −0 mapwcs11.c
  9. +9 −7 mapwcs20.c
  10. +43 −27 mapwfs.c
  11. +3 −2 mapwfslayer.c
  12. +41 −41 mapwms.c
  13. +25 −28 mapwmslayer.c
View
@@ -1302,7 +1302,8 @@ int msSaveMapContext(mapObj *map, char *filename)
int msWriteMapContext(mapObj *map, FILE *stream)
{
#if defined(USE_WMS_LYR) && defined(USE_OGR)
const char * version, *value;
const char * version;
char *pszEPSG;
char * tabspace=NULL, *pszValue, *pszChar,*pszSLD=NULL,*pszURL,*pszSLD2=NULL;
char *pszStyle, *pszCurrent, *pszStyleItem, *pszSLDBody;
char *pszEncodedVal;
@@ -1407,18 +1408,19 @@ int msWriteMapContext(mapObj *map, FILE *stream)
if(tabspace)
free(tabspace);
tabspace = msStrdup(" ");
value = msOWSGetEPSGProj(&(map->projection), &(map->web.metadata), "MO", MS_TRUE);
msOWSGetEPSGProj(&(map->projection), &(map->web.metadata), "MO", MS_TRUE, &pszEPSG);
msIO_fprintf( stream,
"%s<!-- Bounding box corners and spatial reference system -->\n",
tabspace );
if(!value || (strcasecmp(value, "(null)") == 0))
if(!pszEPSG || (strcasecmp(pszEPSG, "(null)") == 0))
msIO_fprintf(stream, "<!-- WARNING: Mandatory data 'projection' was missing in this context. -->\n");
pszEncodedVal = msEncodeHTMLEntities(value);
pszEncodedVal = msEncodeHTMLEntities(pszEPSG);
msIO_fprintf( stream, "%s<BoundingBox SRS=\"%s\" minx=\"%f\" miny=\"%f\" maxx=\"%f\" maxy=\"%f\"/>\n",
tabspace, pszEncodedVal, map->extent.minx, map->extent.miny,
map->extent.maxx, map->extent.maxy );
msFree(pszEncodedVal);
msFree(pszEPSG);
/* Title, name */
if( nVersion >= OWS_0_1_7 && nVersion < OWS_1_0_0 ) {
@@ -1625,14 +1627,15 @@ int msWriteMapContext(mapObj *map, FILE *stream)
GET_LAYER(map, i)->maxscaledenom);
/* Layer SRS */
pszValue = (char*)msOWSGetEPSGProj(&(GET_LAYER(map, i)->projection),
msOWSGetEPSGProj(&(GET_LAYER(map, i)->projection),
&(GET_LAYER(map, i)->metadata),
"MO", MS_FALSE);
if(pszValue && (strcasecmp(pszValue, "(null)") != 0)) {
pszEncodedVal = msEncodeHTMLEntities(pszValue);
"MO", MS_FALSE, &pszEPSG);
if(pszEPSG && (strcasecmp(pszEPSG, "(null)") != 0)) {
pszEncodedVal = msEncodeHTMLEntities(pszEPSG);
msIO_fprintf(stream, " <SRS>%s</SRS>\n", pszEncodedVal);
msFree(pszEncodedVal);
}
msFree(pszEPSG);
/* Format */
if(msLookupHashTable(&(GET_LAYER(map, i)->metadata),"wms_formatlist")==NULL &&
View
@@ -1390,7 +1390,7 @@ int msGMLWriteQuery(mapObj *map, char *filename, const char *namespaces)
FILE *stream=stdout; /* defaults to stdout */
char szPath[MS_MAXPATHLEN];
char *value;
const char *pszMapSRS = NULL;
char *pszMapSRS = NULL;
gmlGroupListObj *groupList=NULL;
gmlItemListObj *itemList=NULL;
@@ -1423,11 +1423,11 @@ int msGMLWriteQuery(mapObj *map, char *filename, const char *namespaces)
msOWSPrintEncodeMetadata(stream, &(map->web.metadata), namespaces, "description", OWS_NOERR, "\t<gml:description>%s</gml:description>\n", NULL);
/* Look up map SRS. We need an EPSG code for GML, if not then we get null and we'll fall back on the layer's SRS */
pszMapSRS = msOWSGetEPSGProj(&(map->projection), NULL, namespaces, MS_TRUE);
msOWSGetEPSGProj(&(map->projection), NULL, namespaces, MS_TRUE, &pszMapSRS);
/* step through the layers looking for query results */
for(i=0; i<map->numlayers; i++) {
const char *pszOutputSRS = NULL;
char *pszOutputSRS = NULL;
int nSRSDimension = 2;
const char* geomtype;
@@ -1438,7 +1438,7 @@ int msGMLWriteQuery(mapObj *map, char *filename, const char *namespaces)
#ifdef USE_PROJ
/* Determine output SRS, if map has none, then try using layer's native SRS */
if ((pszOutputSRS = pszMapSRS) == NULL) {
pszOutputSRS = msOWSGetEPSGProj(&(lp->projection), NULL, namespaces, MS_TRUE);
msOWSGetEPSGProj(&(lp->projection), NULL, namespaces, MS_TRUE, &pszOutputSRS);
if (pszOutputSRS == NULL) {
msSetError(MS_WMSERR, "No valid EPSG code in map or layer projection for GML output", "msGMLWriteQuery()");
continue; /* No EPSG code, cannot output this layer */
@@ -1557,12 +1557,16 @@ int msGMLWriteQuery(mapObj *map, char *filename, const char *namespaces)
/* msLayerClose(lp); */
}
if(pszOutputSRS!=pszMapSRS) {
msFree(pszOutputSRS);
}
} /* next layer */
/* end this document */
msOWSPrintValidateMetadata(stream, &(map->web.metadata), namespaces, "rootname", OWS_NOERR, "</%s>\n", "msGMLOutput");
if(filename && strlen(filename) > 0) fclose(stream);
msFree(pszMapSRS);
return(MS_SUCCESS);
@@ -1604,12 +1608,9 @@ void msGMLWriteWFSBounds(mapObj *map, FILE *stream, const char *tab,
}
else
{
const char* constsrs;
constsrs = msOWSGetEPSGProj(&(map->projection), NULL, "FGO", MS_TRUE);
if (!constsrs)
constsrs = msOWSGetEPSGProj(&(map->projection), &(map->web.metadata), "FGO", MS_TRUE);
if (constsrs)
srs = msStrdup(constsrs);
msOWSGetEPSGProj(&(map->projection), NULL, "FGO", MS_TRUE, &srs);
if (!srs)
msOWSGetEPSGProj(&(map->projection), &(map->web.metadata), "FGO", MS_TRUE, &srs);
}
gmlWriteBounds(stream, outputformat, &resultBounds, srs, tab,
@@ -1733,14 +1734,11 @@ int msGMLWriteWFSQuery(mapObj *map, FILE *stream, const char *default_namespace_
}
else
{
const char* constsrs;
constsrs = msOWSGetEPSGProj(&(map->projection), NULL, "FGO", MS_TRUE);
if (!constsrs)
constsrs = msOWSGetEPSGProj(&(map->projection), &(map->web.metadata), "FGO", MS_TRUE);
if (!constsrs)
constsrs = msOWSGetEPSGProj(&(lp->projection), &(lp->metadata), "FGO", MS_TRUE);
if (constsrs)
srs = msStrdup(constsrs);
msOWSGetEPSGProj(&(map->projection), NULL, "FGO", MS_TRUE, &srs);
if (!srs)
msOWSGetEPSGProj(&(map->projection), &(map->web.metadata), "FGO", MS_TRUE, &srs);
if (!srs)
msOWSGetEPSGProj(&(lp->projection), &(lp->metadata), "FGO", MS_TRUE, &srs);
}
#endif
View
@@ -350,17 +350,21 @@ void msSOSAddPropertyNode(xmlNsPtr psNsSwe, xmlNsPtr psNsXLink, xmlNodePtr psPar
/* possible. */
/************************************************************************/
void msSOSAddGeometryNode(xmlNsPtr psNsGml, xmlNsPtr psNsMs, xmlNodePtr psParent, mapObj *map, layerObj *lp, shapeObj *psShape,
const char *pszEpsg)
const char *pszEpsg_in)
{
char *pszTmp = NULL;
int i,j = 0;
xmlNodePtr psPointNode, psNode, psLineNode, psPolygonNode;
int *panOuterList = NULL, *panInnerList = NULL;
const char *pszEpsg = pszEpsg_in;
char *pszEpsg_buf = NULL;
if (psParent && psShape) {
if (msProjectionsDiffer(&map->projection, &lp->projection) == MS_TRUE) {
msProjectShape(&lp->projection, &map->projection, psShape);
pszEpsg = msOWSGetEPSGProj(&(map->projection), &(lp->metadata), "SO", MS_TRUE);
msOWSGetEPSGProj(&(map->projection), &(lp->metadata), "SO", MS_TRUE, &pszEpsg_buf);
pszEpsg = pszEpsg_buf;
}
switch(psShape->type) {
case(MS_SHAPE_POINT):
@@ -508,6 +512,7 @@ void msSOSAddGeometryNode(xmlNsPtr psNsGml, xmlNsPtr psNsMs, xmlNodePtr psParen
}
}
msFree(pszEpsg_buf);
}
@@ -626,7 +631,8 @@ void msSOSAddMemberNode(xmlNsPtr psNsGml, xmlNsPtr psNsOm, xmlNsPtr psNsSwe, xml
int iFeatureId, const char *script_url, const char *opLayerName)
{
xmlNodePtr psObsNode, psNode, psLayerNode = NULL;
const char *pszEpsg = NULL, *pszValue = NULL;
const char *pszValue = NULL;
char *pszEpsg = NULL;
int status,i,j;
shapeObj sShape;
char szTmp[256];
@@ -785,9 +791,9 @@ void msSOSAddMemberNode(xmlNsPtr psNsGml, xmlNsPtr psNsOm, xmlNsPtr psNsSwe, xml
/*bbox*/
#ifdef USE_PROJ
pszEpsg = msOWSGetEPSGProj(&(map->projection), &(lp->metadata), "SO", MS_TRUE);
msOWSGetEPSGProj(&(map->projection), &(lp->metadata), "SO", MS_TRUE, &pszEpsg);
if (!pszEpsg)
pszEpsg = msOWSGetEPSGProj(&(lp->projection), &(lp->metadata), "SO", MS_TRUE);
msOWSGetEPSGProj(&(lp->projection), &(lp->metadata), "SO", MS_TRUE, &pszEpsg);
if (msProjectionsDiffer(&map->projection, &lp->projection) == MS_TRUE)
msProjectRect(&lp->projection, &map->projection, &sShape.bounds);
@@ -796,6 +802,7 @@ void msSOSAddMemberNode(xmlNsPtr psNsGml, xmlNsPtr psNsOm, xmlNsPtr psNsSwe, xml
/*geometry*/
msSOSAddGeometryNode(psNsGml, psNsMs, psLayerNode, map, lp, &sShape, pszEpsg);
msFree(pszEpsg);
/*attributes */
/* TODO only output attributes where there is a sos_%s_alias (to be discussed)*/
@@ -1369,18 +1376,19 @@ int msSOSGetCapabilities(mapObj *map, sosParamsObj *sosparams, cgiRequestObj *re
Check also what happen if epsg not present */
value = msOWSLookupMetadata(&(lp->metadata), "S", "offering_extent");
if (value) {
char **tokens;
char **tokens,*pszLayerEPSG;
int n;
tokens = msStringSplit(value, ',', &n);
if (tokens==NULL || n != 4) {
msSetError(MS_SOSERR, "Wrong number of arguments for sos_offering_extent.",
"msSOSGetCapabilities()");
return msSOSException(map, "sos_offering_extent", "InvalidParameterValue");
}
value = msOWSGetEPSGProj(&(lp->projection),
&(lp->metadata), "SO", MS_TRUE);
if (value)
psNode = xmlAddChild(psOfferingNode, msGML3BoundedBy(psNsGml, atof(tokens[0]), atof(tokens[1]), atof(tokens[2]), atof(tokens[3]), value));
msOWSGetEPSGProj(&(lp->projection),&(lp->metadata), "SO", MS_TRUE,&pszLayerEPSG);
if (pszLayerEPSG) {
psNode = xmlAddChild(psOfferingNode, msGML3BoundedBy(psNsGml, atof(tokens[0]), atof(tokens[1]), atof(tokens[2]), atof(tokens[3]), pszLayerEPSG));
msFree(pszLayerEPSG);
}
msFreeCharArray(tokens, n);
}
@@ -2284,10 +2292,11 @@ this request. Check sos/ows_enable_request settings.", "msSOSGetObservation()",
if (pszTmp) {
char **tokens;
char *pszMapEpsg;
int n;
rectObj envelope;
pszTmp2 = msOWSGetEPSGProj(&(map->projection), &(lp->metadata), "SO", MS_TRUE);
msOWSGetEPSGProj(&(map->projection), &(lp->metadata), "SO", MS_TRUE, &pszMapEpsg);
tokens = msStringSplit(pszTmp, ',', &n);
if (tokens==NULL || n != 4) {
@@ -2308,8 +2317,9 @@ this request. Check sos/ows_enable_request settings.", "msSOSGetObservation()",
}
}
psNode = xmlAddChild(psRootNode, msGML3BoundedBy(psNsGml, envelope.minx, envelope.miny, envelope.maxx, envelope.maxy, pszTmp2));
psNode = xmlAddChild(psRootNode, msGML3BoundedBy(psNsGml, envelope.minx, envelope.miny, envelope.maxx, envelope.maxy, pszMapEpsg));
msFreeCharArray(tokens, n);
msFree(pszMapEpsg);
}
/* time
View
@@ -2129,8 +2129,8 @@ void msOWSPrintBoundingBox(FILE *stream, const char *tabspace,
const char *namespaces,
int wms_version)
{
const char *value, *resx, *resy, *wms_bbox_extended, *epsg_str;
char *encoded, *encoded_resx, *encoded_resy;
const char *value, *resx, *resy, *wms_bbox_extended;
char *encoded, *encoded_resx, *encoded_resy, *epsg_str;
char **epsgs;
int i, num_epsgs;
projectionObj proj;
@@ -2141,18 +2141,19 @@ void msOWSPrintBoundingBox(FILE *stream, const char *tabspace,
/* get a list of all projections from the metadata
try the layer metadata first, otherwise use the map's */
if( msOWSLookupMetadata(layer_meta, namespaces, "srs") ) {
epsg_str = msOWSGetEPSGProj(srcproj, layer_meta, namespaces, MS_FALSE);
msOWSGetEPSGProj(srcproj, layer_meta, namespaces, MS_FALSE, &epsg_str);
} else {
epsg_str = msOWSGetEPSGProj(srcproj, map_meta, namespaces, MS_FALSE);
msOWSGetEPSGProj(srcproj, map_meta, namespaces, MS_FALSE, &epsg_str);
}
epsgs = msStringSplit(epsg_str, ' ', &num_epsgs);
msFree(epsg_str);
} else {
/* Look for EPSG code in PROJECTION block only. "wms_srs" metadata cannot be
* used to establish the native projection of a layer for BoundingBox purposes.
*/
epsgs = (char **) msSmallMalloc(sizeof(char *));
num_epsgs = 1;
epsgs[0] = msStrdup( msOWSGetEPSGProj(srcproj, layer_meta, namespaces, MS_TRUE) );
msOWSGetEPSGProj(srcproj, layer_meta, namespaces, MS_TRUE, &(epsgs[0]));
}
for( i = 0; i < num_epsgs; i++) {
@@ -2495,34 +2496,37 @@ char *msOWSBuildURLFilename(const char *pszPath, const char *pszURL,
** then only the first one (which is assumed to be the layer's default
** projection) is returned.
*/
const char *msOWSGetEPSGProj(projectionObj *proj, hashTableObj *metadata, const char *namespaces, int bReturnOnlyFirstOne)
void msOWSGetEPSGProj(projectionObj *proj, hashTableObj *metadata, const char *namespaces, int bReturnOnlyFirstOne, char **epsgCode)
{
static char epsgCode[20] ="";
char *value;
const char *value;
*epsgCode = NULL;
/* metadata value should already be in format "EPSG:n" or "AUTO:..." */
if (metadata && ((value = (char *) msOWSLookupMetadata(metadata, namespaces, "srs")) != NULL)) {
if (!bReturnOnlyFirstOne) return value;
/* caller requested only first projection code */
strlcpy(epsgCode, value, 20);
if ((value=strchr(epsgCode, ' ')) != NULL) *value = '\0';
if (metadata && ((value = msOWSLookupMetadata(metadata, namespaces, "srs")) != NULL)) {
const char *space_ptr;
if (!bReturnOnlyFirstOne || (space_ptr = strchr(value,' ')) == NULL) {
*epsgCode = msStrdup(value);
return;
}
return epsgCode;
} else if (proj && proj->numargs > 0 && (value = strstr(proj->args[0], "init=epsg:")) != NULL && strlen(value) < 20) {
snprintf(epsgCode, sizeof(epsgCode), "EPSG:%s", value+10);
return epsgCode;
} else if (proj && proj->numargs > 0 && (value = strstr(proj->args[0], "init=crs:")) != NULL && strlen(value) < 20) {
snprintf(epsgCode, sizeof(epsgCode), "CRS:%s", value+9);
return epsgCode;
*epsgCode = msSmallMalloc((space_ptr - value + 1)*sizeof(char));
/* caller requested only first projection code, copy up to the first space character*/
strlcpy(*epsgCode, value, space_ptr - value + 1) ;
return;
} else if (proj && proj->numargs > 0 && (value = strstr(proj->args[0], "init=epsg:")) != NULL) {
*epsgCode = msSmallMalloc((strlen("EPSG:")+strlen(value+10)+1)*sizeof(char));
sprintf(*epsgCode, "EPSG:%s", value+10);
return;
} else if (proj && proj->numargs > 0 && (value = strstr(proj->args[0], "init=crs:")) != NULL) {
*epsgCode = msSmallMalloc((strlen("CRS:")+strlen(value+9)+1)*sizeof(char));
sprintf(*epsgCode, "CRS:%s", value+9);
return;
} else if (proj && proj->numargs > 0 && (strncasecmp(proj->args[0], "AUTO:", 5) == 0 ||
strncasecmp(proj->args[0], "AUTO2:", 6) == 0)) {
return proj->args[0];
*epsgCode = msStrdup(proj->args[0]);
return;
}
return NULL;
}
/*
** msOWSGetProjURN()
@@ -2538,16 +2542,18 @@ char *msOWSGetProjURN(projectionObj *proj, hashTableObj *metadata, const char *n
char **tokens;
int numtokens, i;
size_t bufferSize = 0;
const char *oldStyle = msOWSGetEPSGProj( proj, metadata, namespaces,
bReturnOnlyFirstOne );
char *oldStyle;
msOWSGetEPSGProj( proj, metadata, namespaces,
bReturnOnlyFirstOne, &oldStyle );
if( oldStyle == NULL || strncmp(oldStyle,"EPSG:",5) != 0 )
return NULL;
result = msStrdup("");
tokens = msStringSplit(oldStyle, ' ', &numtokens);
msFree(oldStyle);
for(i=0; tokens != NULL && i<numtokens; i++) {
char urn[100];
@@ -2596,16 +2602,18 @@ char *msOWSGetProjURI(projectionObj *proj, hashTableObj *metadata, const char *n
char *result;
char **tokens;
int numtokens, i;
const char *oldStyle = msOWSGetEPSGProj( proj, metadata, namespaces,
bReturnOnlyFirstOne );
char *oldStyle;
msOWSGetEPSGProj( proj, metadata, namespaces,
bReturnOnlyFirstOne, &oldStyle);
if( oldStyle == NULL || !EQUALN(oldStyle,"EPSG:",5) )
return NULL;
result = msStrdup("");
tokens = msStringSplit(oldStyle, ' ', &numtokens);
msFree(oldStyle);
for(i=0; tokens != NULL && i<numtokens; i++) {
char urn[100];
View
@@ -293,7 +293,7 @@ void msOWSProcessException(layerObj *lp, const char *pszFname,
int nErrorCode, const char *pszFuncName);
char *msOWSBuildURLFilename(const char *pszPath, const char *pszURL,
const char *pszExt);
const char *msOWSGetEPSGProj(projectionObj *proj, hashTableObj *metadata, const char *namespaces, int bReturnOnlyFirstOne);
void msOWSGetEPSGProj(projectionObj *proj, hashTableObj *metadata, const char *namespaces, int bReturnOnlyFirstOne, char **epsgProj);
char *msOWSGetProjURN(projectionObj *proj, hashTableObj *metadata, const char *namespaces, int bReturnOnlyFirstOne);
char *msOWSGetProjURI(projectionObj *proj, hashTableObj *metadata, const char *namespaces, int bReturnOnlyFirstOne);
Oops, something went wrong.

0 comments on commit 4dd30cd

Please sign in to comment.