[mapserver-users] Re: interesting thread on JSON-ish output from MapServer

Steve Lime sdlime at gmail.com
Sun Sep 11 13:46:35 EDT 2011



Sent from my iPad

On Sep 11, 2011, at 9:36 AM, Mr. Puneet Kishor <punk.kish at gmail.com> wrote:

> Partial solution (phew, for now), but other problems persist --
> 
> (cc-ing to Steve Lime and Rich Greenwood)
> 
> On Sep 10, 2011, at 11:42 PM, Mr. Puneet Kishor wrote:
> 
>> [http://osgeo-org.1803224.n2.nabble.com/GeoJSON-output-from-MapServer-td3188987.html]
>> 
>> If I call my template "template.html" I get the output, but I get mime type warning and a syntax error in parsing the output.
> 
> The above is now partially solved. I still get the mime type incorrectly set, but the syntax error was my mistake. However, it is something worthy of being documented. I was making a JSONP (cross-domain) call. Hence, the response coming back should have been wrapped in the callback as an argument. Since I am stuck using CGI for now, I don't know if I can get MapServer to recognize the callback param that is randomly generated by jQuery, and use that in the template. So, I prescribed a custom callback, and used that in the template.
> 
> Before (doesn't work with JSONP)
> ---------------------------------
> $.ajax({
>    url        : url,
>    type    : "GET",
>    data    : "",
>    dataType: "jsonp",
>    error    : function() { alert("Error loading html document"); },
>    success : function(r) { .. }
> });
> 
> // mapserver template
> {"gid":"[gid]","unit":"[unit_abbre]","rock type":"[rocktype]","lithology":"[lithology]","min age":"[min_age]","max age":"[max_age]","interval":"[interval_name]"}
> 
> After (works fine, note addition of "jsonpCallback" in the request)
> -------------------------------------------------------------------
> $.ajax({
>    url        : url,
>    type    : "GET",
>    data    : "",
>    dataType: "jsonp",
>    jsonpCallback: "foo",
>    error    : function() { alert("Error loading html document"); },
>    success : function(r) { .. }
> });
> 
> // mapserver template
> foo({"gid":"[gid]","unit":"[unit_abbre]","rock type":"[rocktype]","lithology":"[lithology]","min age":"[min_age]","max age":"[max_age]","interval":"[interval_name]"})
> 
> 
> One issue I have not solved -- if no result is found, MapServer sends back the following --
> 
> ----
> <HTML>
> 
> <HEAD><TITLE>MapServer Message</TITLE></HEAD>
> 
> <!-- MapServer version 6.0.1 OUTPUT=GIF OUTPUT=PNG OUTPUT=JPEG SUPPORTS=PROJ SUPPORTS=AGG SUPPORTS=FREETYPE SUPPORTS=ICONV SUPPORTS=WMS_SERVER SUPPORTS=WMS_CLIENT SUPPORTS=GEOS INPUT=POSTGIS INPUT=GDAL INPUT
> =SHAPEFILE -->
> 
> <BODY BGCOLOR="#FFFFFF">
> 
> msQueryByPoint(): Search returned no results. No matching record(s) found.
> 
> </BODY></HTML>
> ----
> 
> 
> Well, that gums up the works. I really want MapServer to just send an empty JSON structure, something like foo({}). Suggestions on how I can prevent this HTML message from MapServer, and force it to use the template instead?
> 
> 
>> 
>> If I rename the template "template.json" MapServer complains that the name is malformed.
> 

Correct, .json is not a valid extension.

> 
> This one is interesting. Per Steve Lime's email earlier today, "Issue 1: only certain extensions can be used for templates. I believe .js is legal but .json is not. We could change that in future releases. Could you file a ticket?" Yet, the docs at [http://mapserver.org/output/template_output.html] very clearly mention the use of ".json" as a file extension. I quote
> 
>> OUTPUTFORMAT
>>  NAME "geojson"
>>  DRIVER "TEMPLATE"
>>  FORMATOPTION "FILE=myTemplate.json"
>> END
> 

The docs/example is wrong or doesn't match the source anyway. The set of legal extensions is defined in mapserver.h and .json is not included. A ticket would help make sure we get that fixed.

> 
> Fwiw, I using `NAME "geojson"` as above in my `OUTPUTFORMAT` block, but I am returning just plain vanilla JSON. If I change the "NAME" to "json", I get back an unprocessed template.
> 
>> 
>> If I rename the template "template.js" MapServer complains that it is "Unable to access file"
> 

Does the file exist? There's nothing cryptic about the error message. 

> ".js" being legal notwithstanding, the fact is, if I change my template.html to template.js, MapServer errors with the above complaint "Unable to access file," whatever that means. Exactly the same file, same file mode, everything identical, just the name change.
> 
> 
>> If I add `qformat=geojson` to my query (as shown in the link above), the output contains the template with the place holders, no substitutions with the values.
> 

I'd need to see the whole mapfile. It sounds like you might be mixing new and old-style template definitions which I would discourage. 

> This is even more bizarre -- per the email thread at [[http://osgeo-org.1803224.n2.nabble.com/GeoJSON-output-from-MapServer-td3188987.html], it seems from Steve's comment that `qformat=geojson` is a valid query param (although I don't see it documented anywhere). Yet, if I add that, I get an unprocessed template.
> 
>> 
>> What gives?
> 

Note there are no predefined template drivers, so if you give it a qformat value that doesn't reference anything valid you should get an error but exactly what that is depends on the way you have errors configured in the web object.

Steve


More information about the mapserver-users mailing list