[mapserver-users] Setting up a JSONP service

Lime, Steve D (MNIT) Steve.Lime at state.mn.us
Tue Jan 13 15:41:27 PST 2015


Hi Lars: Most of what you need was in my last reply. Those are snippets from a working configuration. Here's a simple JSON template for a raster query that creates an array of xyz objects:

// MapServer Template
{'result':{
  'points':[
[resultset layer="elev_dem30ra3"]
  [feature trimlast=","]{x:[x], y:[y], z:[value_0]},[/feature]
[/resultset]
  ]
}}

You could turn this into JSONP and not use the include strategy (I wanted both JSON and JSONP) simply by wrapping the object in callback (callback is a value passed through MapServer, e.g. &mode=query&callback=foo):

// MapServer Template (jsonp.js)
[callback]({'result':{
  'points':[
[resultset layer="elev_dem30ra3"]
  [feature trimlast=","]{x:[x], y:[y], z:[value_0]},[/feature]
[/resultset]
  ]
}})

and here's the corresponding OUTPUTFORMAT block:

OUTPUTFORMAT
  NAME 'JSONP'
  DRIVER 'TEMPLATE'
  MIMETYPE 'application/json;'
  FORMATOPTION 'FILE=templates/jsonp.js'
  FORMATOPTION 'ATTACHMENT=service.json'
END

Regarding speed, templates are going to be a bit slower since there are generally two passes through the data, once to identify the candidate records and a second for presentation. Also the template assembly is done in memory so there are limits on how large of datasets you can practically work with. How big a deal this is depends on your situation. It's not perfect but in general I've had great luck with this approach.

Steve

-----Original Message-----
From: mapserver-users-bounces at lists.osgeo.org [mailto:mapserver-users-bounces at lists.osgeo.org] On Behalf Of Lars Fricke
Sent: Friday, January 09, 2015 4:24 AM
To: mapserver-users at lists.osgeo.org
Subject: Re: [mapserver-users] Setting up a JSONP service

Hello,

first of all: Thank you for your support!
Sorry for the late reply but I unfortunately was ill.

@ Steve:
I read about templating but also read that it is slower?
I do not have a lot of experience in writing templates. Would you mind sharing a JSONP template?

@ Even: Thank you for your thoughts. Who would implement that change though? I'm afraid I can't.

@ Jeff: I followed those links but I only found threads referring to GeoServer or that were not related to my question. The GDAL page I did study and that's how I managed to set up the GeoJSON service but thats not the same unfortunately as it conflicts with the same origin policy.

Best

Lars

Am 30.12.2014 um 16:24 schrieb Lime, Steve D (MNIT):
> You can also use MapServer templates to accomplish this. It's a little more work since you have to write the template but it's quite flexible then. Usually I write the template to produce JSON and then use a simple wrapper template to produce JSONP. For example, jsonp.js looks like (callback is passed in):
>
> // MapServer Template
> [callback](
>    [include src="templates/json.js"]
> )
>
> Output formats look like:
>
>    OUTPUTFORMAT
>      NAME 'JSON'
>      DRIVER 'TEMPLATE'
>      MIMETYPE 'application/json;'
>      FORMATOPTION 'FILE=templates/json.js'
>      FORMATOPTION 'ATTACHMENT=service.json'
>    END
>
>    OUTPUTFORMAT
>      NAME 'JSONP'
>      DRIVER 'TEMPLATE'
>      MIMETYPE 'application/json;'
>      FORMATOPTION 'FILE=templates/jsonp.js'
>      FORMATOPTION 'ATTACHMENT=service.json'
>    END
>
> Steve
>
> -----Original Message-----
> From:mapserver-users-bounces at lists.osgeo.org  
> [mailto:mapserver-users-bounces at lists.osgeo.org] On Behalf Of Lars 
> Fricke
> Sent: Monday, December 29, 2014 5:34 AM 
> To:mapserver-users at lists.osgeo.org
> Subject: [mapserver-users] Setting up a JSONP service
>
> Dear List,
>
> I have a WFS service running under MapServer that I would like to operate as JSONP service. I managed to set GEOJSON as output format but I get "Cross-Origin-Request Blocked" if I try to call it with a Javascript client (using Leaflet L.layerJSON.
> The question is: Is it possible to set up a JSONP service from Mapserver and if yes, how? My current mapfile looks like this (relevant parts):
>
> "
> # in WEB - METADATA
> "wfs_getfeature_formatlist" "geojson,csv,ogrgml"
>
> OUTPUTFORMAT
>      NAME "geojson"
>      DRIVER "OGR/GEOJSON"
>      MIMETYPE "application/json; subtype=geojson; charset=utf-8"
>      FORMATOPTION "STORAGE=stream"
>      FORMATOPTION "FORM=SIMPLE"
>      FORMATOPTION "LCO:COORDINATE_PRECISION=5"
>     END
> "
> If this would already be a correct JSONP service, I have to look on the Leaflet side for the error...
> Thanks for your help.
>
> Cheers
>
> Lars
> _______________________________________________
> mapserver-users mailing list
> mapserver-users at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/mapserver-users
> _______________________________________________
> mapserver-users mailing list
> mapserver-users at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/mapserver-users
>

_______________________________________________
mapserver-users mailing list
mapserver-users at lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/mapserver-users


More information about the mapserver-users mailing list