[mapserver-commits] r10923 - in trunk/mapserver: . mapscript/php
mapscript/swiginc
svn at osgeo.org
svn at osgeo.org
Tue Feb 1 23:08:36 EST 2011
Author: sdlime
Date: 2011-02-01 20:08:35 -0800 (Tue, 01 Feb 2011)
New Revision: 10923
Modified:
trunk/mapserver/mapquery.c
trunk/mapserver/mapscript/php/map.c
trunk/mapserver/mapscript/php/mapscript_i.c
trunk/mapserver/mapscript/swiginc/map.i
trunk/mapserver/mapserv.c
trunk/mapserver/mapserver.h
Log:
Enabled two means of serializing query results: new style query params and old style result indexes. Changed mapscript saveQuery to take an optional boolean parameter to trigger the old style. The loadQuery method detects the type based on a magic string saved with the query file. Part of RFC 65...
Modified: trunk/mapserver/mapquery.c
===================================================================
--- trunk/mapserver/mapquery.c 2011-02-01 15:43:09 UTC (rev 10922)
+++ trunk/mapserver/mapquery.c 2011-02-02 04:08:35 UTC (rev 10923)
@@ -184,15 +184,17 @@
if(!filename) {
msSetError(MS_MISCERR, "No filename provided to save query results to.", "msSaveQueryResults()");
- return(MS_FAILURE);
+ return MS_FAILURE;
}
- stream = fopen(filename, "wb");
+ stream = fopen(filename, "w");
if(!stream) {
msSetError(MS_IOERR, "(%s)", "msSaveQueryResults()", filename);
- return(MS_FAILURE);
+ return MS_FAILURE;
}
+ fprintf(stream, "%s - Generated by msSaveQueryResults()\n", MS_QUERY_RESULTS_MAGIC_STRING);
+
/* count the number of layers with results */
for(i=0; i<map->numlayers; i++)
if(GET_LAYER(map, i)->resultcache) n++;
@@ -209,30 +211,12 @@
}
}
- fclose(stream);
- return(MS_SUCCESS);
+ return MS_SUCCESS;
}
-int msLoadQueryResults(mapObj *map, char *filename) {
- FILE *stream;
+static int loadQueryResults(mapObj *map, FILE *stream) {
int i, j, k, n=0;
- if(!filename) {
- msSetError(MS_MISCERR, "No filename provided to load query from.", "msLoadQueryResults()");
- return(MS_FAILURE);
- }
-
- /*
- ** Make sure the file at least has the right extension.
- */
- if(msEvalRegex("\\.qy$", filename) != MS_TRUE) return MS_FAILURE;
-
- stream = fopen(filename, "rb");
- if(!stream) {
- msSetError(MS_IOERR, "(%s)", "msLoadQueryResults()", filename);
- return(MS_FAILURE);
- }
-
fread(&n, sizeof(int), 1, stream);
/* now load the result set for each layer found in the query file */
@@ -241,7 +225,7 @@
if(j<0 || j>map->numlayers) {
msSetError(MS_MISCERR, "Invalid layer index loaded from query file.", "msLoadQueryResults()");
- return(MS_FAILURE);
+ return MS_FAILURE;
}
/* inialize the results for this layer */
@@ -266,31 +250,31 @@
for(k=0; k<GET_LAYER(map, j)->resultcache->numresults; k++) {
fread(&(GET_LAYER(map, j)->resultcache->results[k]), sizeof(resultCacheMemberObj), 1, stream); /* each result */
if(!GET_LAYER(map, j)->tileindex) GET_LAYER(map, j)->resultcache->results[k].tileindex = -1; /* reset the tile index for non-tiled layers */
+ GET_LAYER(map, j)->resultcache->results[k].resultindex = -1; /* all results loaded this way have a -1 result index */
}
}
- fclose(stream);
- return(MS_SUCCESS);
+ return MS_SUCCESS;
}
/*
-** Serialize the values necessary to duplicate a query to disk.
+** Serialize the parameters necessary to duplicate a query to disk. (TODO: add filter query...)
*/
-int msSaveQuery(mapObj *map, char *filename) {
+int msSaveQueryParams(mapObj *map, char *filename) {
FILE *stream;
if(!filename) {
- msSetError(MS_MISCERR, "No filename provided to save query to.", "msSaveQuery()");
- return(MS_FAILURE);
+ msSetError(MS_MISCERR, "No filename provided to save query to.", "msSaveQueryParams()");
+ return MS_FAILURE;
}
stream = fopen(filename, "w");
if(!stream) {
- msSetError(MS_IOERR, "(%s)", "msSaveQuery()", filename);
- return(MS_FAILURE);
+ msSetError(MS_IOERR, "(%s)", "msSaveQueryParams()", filename);
+ return MS_FAILURE;
}
- fprintf(stream, "%s - Generated by msSaveQuery()\n", MS_QUERY_MAGIC_STRING);
+ fprintf(stream, "%s - Generated by msSaveQueryParms()\n", MS_QUERY_PARAMS_MAGIC_STRING);
fprintf(stream, "%d %d %d %d\n", map->query.mode, map->query.type, map->query.layer, map->query.slayer); /* all queries */
fprintf(stream, "%.15g %.15g %g %d\n", map->query.point.x, map->query.point.y, map->query.buffer, map->query.maxresults); /* by point */
@@ -317,47 +301,15 @@
fprintf(stream, "%d\n", MS_SHAPE_NULL); /* NULL shape */
}
- fclose(stream);
- return(MS_SUCCESS);
+ return MS_SUCCESS;
}
-int msLoadQuery(mapObj *map, char *filename) {
- FILE *stream;
+static int loadQueryParams(mapObj *map, FILE *stream) {
char buffer[MS_BUFFER_LENGTH];
int lineno;
int shapetype, numlines, numpoints;
- if(!filename) {
- msSetError(MS_MISCERR, "No filename provided to load query from.", "msLoadQuery()");
- return(MS_FAILURE);
- }
-
- /*
- ** Make sure the file at least has the right extension.
- */
- if(msEvalRegex("\\.qy$", filename) != MS_TRUE) {
- msSetError(MS_MISCERR, "Query file extension check failed on %s (extension must be .qy)", "msLoadQuery()", filename);
- return MS_FAILURE;
- }
-
- stream = fopen(filename, "r");
- if(!stream) {
- msSetError(MS_IOERR, "(%s)", "msLoadQuery()", filename);
- return(MS_FAILURE);
- }
-
- /*
- ** Make sure the query magic string is the first line.
- */
- if(fgets(buffer, MS_BUFFER_LENGTH, stream) != NULL) {
- if(!msCaseFindSubstring(buffer, MS_QUERY_MAGIC_STRING)) {
- msSetError(MS_WEBERR, "Missing magic string, %s doesn't look like a MapServer query file.", "msLoadQuery()", filename);
- fclose(stream);
- return MS_FAILURE;
- }
- }
-
msInitQuery(&(map->query)); /* this will free any previous query as well */
lineno = 2; /* line 1 is the magic string */
@@ -429,17 +381,66 @@
if(map->query.layer >= 0 && map->query.layer < map->numlayers) GET_LAYER(map, map->query.layer)->status = MS_ON;
if(map->query.slayer >= 0 && map->query.slayer < map->numlayers) GET_LAYER(map, map->query.slayer)->status = MS_ON;
- fclose(stream);
-
/* now execute the query */
return msExecuteQuery(map);
parse_error:
- msSetError(MS_MISCERR, "Parse error line %d.", "msLoadQuery()", lineno);
- fclose(stream);
+ msSetError(MS_MISCERR, "Parse error line %d.", "loadQueryParameters()", lineno);
return MS_FAILURE;
}
+/*
+** Loads a query file contained either serialized 1) query parameters or 2) query results (e.g. indexes).
+*/
+int msLoadQuery(mapObj *map, char *filename)
+{
+ FILE *stream;
+ char buffer[MS_BUFFER_LENGTH];
+ int retval=MS_FAILURE;
+
+ if(!filename) {
+ msSetError(MS_MISCERR, "No filename provided to load query from.", "msLoadQuery()");
+ return MS_FAILURE;
+ }
+
+ /*
+ ** Make sure the file at least has the right extension.
+ */
+ if(msEvalRegex("\\.qy$", filename) != MS_TRUE) {
+ msSetError(MS_MISCERR, "Queryfile %s has incorrect file extension.", "msLoadQuery()", filename);
+ return MS_FAILURE;
+ }
+
+ /*
+ ** Open the file and inspect the first line.
+ */
+ stream = fopen(filename, "r");
+ if(!stream) {
+ msSetError(MS_IOERR, "(%s)", "msLoadQuery()", filename);
+ return MS_FAILURE;
+ }
+
+ if(fgets(buffer, MS_BUFFER_LENGTH, stream) != NULL) {
+ /*
+ ** Call correct reader based on the magic string.
+ */
+ if(strncasecmp(buffer, MS_QUERY_RESULTS_MAGIC_STRING, strlen(MS_QUERY_RESULTS_MAGIC_STRING)) == 0) {
+ retval = loadQueryResults(map, stream);
+ } else if(strncasecmp(buffer, MS_QUERY_PARAMS_MAGIC_STRING, strlen(MS_QUERY_PARAMS_MAGIC_STRING)) == 0) {
+ retval = loadQueryParams(map, stream);
+ } else {
+ msSetError(MS_WEBERR, "Missing magic string, %s doesn't look like a MapServer query file.", "msLoadQuery()", filename);
+ retval = MS_FAILURE;
+ }
+ } else {
+ msSetError(MS_WEBERR, "Empty file or failed read for %s.", "msLoadQuery()", filename);
+ retval = MS_FAILURE;
+ }
+
+ fclose(stream);
+ return retval;
+}
+
int msQueryByIndex(mapObj *map)
{
layerObj *lp;
Modified: trunk/mapserver/mapscript/php/map.c
===================================================================
--- trunk/mapserver/mapscript/php/map.c 2011-02-01 15:43:09 UTC (rev 10922)
+++ trunk/mapserver/mapscript/php/map.c 2011-02-02 04:08:35 UTC (rev 10923)
@@ -167,6 +167,7 @@
ZEND_BEGIN_ARG_INFO_EX(map_saveQuery_args, 0, 0, 1)
ZEND_ARG_INFO(0, filename)
+ ZEND_ARG_INFO(0, results)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(map_loadQuery_args, 0, 0, 1)
@@ -1987,19 +1988,20 @@
}
/* }}} */
-/* {{{ proto int map.savequery(filename)
+/* {{{ proto int map.savequery(string filename, int results)
Save the current query to a specfied file. Can be used with loadquery */
PHP_METHOD(mapObj, saveQuery)
{
zval *zobj = getThis();
char *filename;
long filename_len;
+ int results = MS_FALSE;
int status = MS_FAILURE;
php_map_object *php_map;
PHP_MAPSCRIPT_ERROR_HANDLING(TRUE);
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
- &filename, &filename_len) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l",
+ &filename, &filename_len, &results) == FAILURE) {
PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
return;
}
@@ -2007,7 +2009,7 @@
php_map = (php_map_object *) zend_object_store_get_object(zobj TSRMLS_CC);
- status = mapObj_saveQuery(php_map->map, filename);
+ status = mapObj_saveQuery(php_map->map, filename, results);
RETURN_LONG(status);
}
Modified: trunk/mapserver/mapscript/php/mapscript_i.c
===================================================================
--- trunk/mapserver/mapscript/php/mapscript_i.c 2011-02-01 15:43:09 UTC (rev 10922)
+++ trunk/mapserver/mapscript/php/mapscript_i.c 2011-02-02 04:08:35 UTC (rev 10923)
@@ -210,8 +210,11 @@
return msQueryByIndex(self);
}
-int mapObj_saveQuery(mapObj *self, char *filename) {
- return msSaveQuery(self, filename);
+int mapObj_saveQuery(mapObj *self, char *filename, int results) {
+ if(results)
+ return msSaveQueryResults(self, filename);
+ else
+ return msSaveQueryParams(self, filename);
}
int mapObj_loadQuery(mapObj *self, char *filename) {
return msLoadQuery(self, filename);
Modified: trunk/mapserver/mapscript/swiginc/map.i
===================================================================
--- trunk/mapserver/mapscript/swiginc/map.i 2011-02-01 15:43:09 UTC (rev 10922)
+++ trunk/mapserver/mapscript/swiginc/map.i 2011-02-02 04:08:35 UTC (rev 10923)
@@ -309,19 +309,20 @@
return msSaveMap(self, filename);
}
- int saveQuery(char *filename) {
- return msSaveQuery(self, filename);
- }
+ int saveQuery(char *filename, int results=MS_FALSE) {
+ if(results)
+ return msSaveQueryResults(self, filename);
+ else
+ return msSaveQueryParams(self, filename);
+ }
- int loadQuery(char *filename)
- {
- return msLoadQuery(self, filename);
- }
+ int loadQuery(char *filename) {
+ return msLoadQuery(self, filename);
+ }
- void freeQuery(int qlayer=-1)
- {
- msQueryFree(self, qlayer);
- }
+ void freeQuery(int qlayer=-1) {
+ msQueryFree(self, qlayer);
+ }
int saveQueryAsGML(char *filename, const char *ns="GOMF") {
return msGMLWriteQuery(self, filename, ns);
Modified: trunk/mapserver/mapserv.c
===================================================================
--- trunk/mapserver/mapserv.c 2011-02-01 15:43:09 UTC (rev 10922)
+++ trunk/mapserver/mapserv.c 2011-02-02 04:08:35 UTC (rev 10923)
@@ -1853,7 +1853,7 @@
if(mapserv->savequery) {
snprintf(buffer, sizeof(buffer), "%s%s%s%s", mapserv->map->web.imagepath, mapserv->map->name, mapserv->Id, MS_QUERY_EXTENSION);
- if((status = msSaveQuery(mapserv->map, buffer)) != MS_SUCCESS) return status;
+ if((status = msSaveQueryParams(mapserv->map, buffer)) != MS_SUCCESS) return status;
}
}
Modified: trunk/mapserver/mapserver.h
===================================================================
--- trunk/mapserver/mapserver.h 2011-02-01 15:43:09 UTC (rev 10922)
+++ trunk/mapserver/mapserver.h 2011-02-02 04:08:35 UTC (rev 10923)
@@ -221,7 +221,8 @@
#define MS_INDEX_EXTENSION ".qix"
-#define MS_QUERY_MAGIC_STRING "MapServer Query"
+#define MS_QUERY_RESULTS_MAGIC_STRING "MapServer Query Results"
+#define MS_QUERY_PARAMS_MAGIC_STRING "MapServer Query Params"
#define MS_QUERY_EXTENSION ".qy"
#define MS_DEG_TO_RAD .0174532925199432958
@@ -1805,8 +1806,7 @@
MS_DLL_EXPORT int msInitQuery(queryObj *query); /* in mapquery.c */
MS_DLL_EXPORT void msFreeQuery(queryObj *query);
MS_DLL_EXPORT int msSaveQueryResults(mapObj *map, char *filename);
-MS_DLL_EXPORT int msLoadQueryResults(mapObj *map, char *filename);
-MS_DLL_EXPORT int msSaveQuery(mapObj *map, char *filename);
+MS_DLL_EXPORT int msSaveQueryParams(mapObj *map, char *filename);
MS_DLL_EXPORT int msLoadQuery(mapObj *map, char *filename);
MS_DLL_EXPORT int msExecuteQuery(mapObj *map);
More information about the mapserver-commits
mailing list