[mapserver-users] Setting up a JSONP service
Lars Fricke
fricke at gisberater.com
Wed Jan 14 08:17:06 PST 2015
Hello Andreas,
I confirm your solution is working perfectly well. I succeeded to
integrate the JSONP output into my web application with no conflicts.
Funny this depended so much on the version of code I happened to grab
wrong. Sorry for producing extra work by that.
I'd love to see this go to the repositories, it's really helpful until
someone will extend OGR.
So thanks again from Mecklenburg to Saxony :-)
Best
Lars
Am 13.01.2015 um 16:52 schrieb Eichner, Andreas - SID:
> That's what I get on the console:
>
> $ MS_MAPFILE=$(pwd)/IHK_Handelsflaechen.map ./mapserv QUERY_STRING='service=WFS&version=1.1.0&request=GetFeature&featureid=IHK.71247&outputformat=geojson&callback=foobar'Content-Disposition: attachment; filename=result.dat
> Content-Type: application/json; subtype=geojson; charset=utf-8
>
> foobar({
> "type": "FeatureCollection",
>
> "features": [
> { "type": "Feature", "properties": { "PLZ": "09648", ..., "LANDKREIS": "Mittelsachsen" }, "geometry": { "type": "Point", "coordinates": [ 4568447.14, 5650577.57 ] } }
> ]
> }
> );
>
> In other words, the JSON is enclosed by "foobar(" and ");": The relevant parts of the mapfile are:
>
> OUTPUTFORMAT
> NAME 'geojson'
> # NAME 'jsonp'
> DRIVER 'OGR/GEOJSON'
> MIMETYPE 'application/json; subtype=geojson; charset=utf-8'
> # MIMETYPE 'text/javascript; charset=utf-8'
> FORMATOPTION 'STORAGE=stream'
> FORMATOPTION 'FORM=simple'
> FORMATOPTION 'LCO:COORDINATE_PRECISION=5'
> FORMATOPTION 'JSONP=%callback%'
> END
>
> WEB
> VALIDATION
> callback '.*'
> # default_callback 'jsonp'
> END
> END
>
> The default value does not work, so the substitution parameter must be given.
>
>
>
>> -----Ursprüngliche Nachricht-----
>> Von: Lars Fricke [mailto:fricke at gisberater.com]
>> Gesendet: Dienstag, 13. Januar 2015 16:39
>> An: Eichner, Andreas - SID; mapserver-users at lists.osgeo.org
>> Betreff: Re: [mapserver-users] Setting up a JSONP service
>>
>> Hi Andreas,
>>
>> I guess I'm still doing something wrong. If I use
>> http://localhost:8082/wfs?SERVICE=WFS&VERSION=1.1.0&request=GetFeature&TYP
>> ENAME=HU&outputformat=geojson&SRS=EPSG:3857&bbox=793732,6570204,793765,657
>> 0228&callback=foobar
>>
>> I'm getting the exact same output as before. What are you getting? Can
>> you post an example of your servers response?
>>
>> Thank you so much.
>>
>> Best
>>
>> Lars
>>
>> Am 13.01.2015 um 15:57 schrieb Eichner, Andreas - SID:
>>> Arg.. outputformat=geojson also works, only had a typo...
>>>
>>>> -----Ursprüngliche Nachricht-----
>>>> Von: mapserver-users-bounces at lists.osgeo.org [mailto:mapserver-users-
>>>> bounces at lists.osgeo.org] Im Auftrag von Eichner, Andreas - SID
>>>> Gesendet: Dienstag, 13. Januar 2015 15:18
>>>> An: 'lars.fricke at skendata.de'; mapserver-users at lists.osgeo.org
>>>> Betreff: Re: [mapserver-users] Setting up a JSONP service
>>>>
>>>> You need to append the "callback" parameter to the query string:
>>>>
>>>>
>> http://localhost:8082/wfs?SERVICE=WFS&VERSION=1.1.0&request=GetFeature&TYP
>> ENAME=HU&outputformat=geojson&SRS=EPSG:3857&bbox=793732,6570204,793765,657
>>>> 0228&callback=foobar
>>>>
>>>> You can test on the command line, too:
>>>> MS_MAPFILE=/path/to/mapfile ./mapserv
>>>> QUERY_STRING='service=WFS&version=1.1.0&request=GetFeature&typename=HU&
>>>>
>> SRS=EPSG:3857&bbox=793732,6570204,793765,6570228&outputformat=application/
>>>> json; subtype=geojson; charset=utf-8&callback=foobar'
>>>>
>>>> Please note that IMHO "geojson" alone isn't enough. The full mimetype
>>>> string should be used as reported by GetCapabilities.
>>>>
>>>> HTH
>>>>
>>>>> -----Ursprüngliche Nachricht-----
>>>>> Von: Lars Fricke [mailto:fricke at gisberater.com]
>>>>> Gesendet: Dienstag, 13. Januar 2015 14:22
>>>>> An: Eichner, Andreas - SID; mapserver-users at lists.osgeo.org
>>>>> Betreff: Re: [mapserver-users] Setting up a JSONP service
>>>>>
>>>>> Hi,
>>>>>
>>>>> I compiled MapServer after applying the patch (at least I think it was
>>>>> applied). Unfortunately the output is not JSONP format. Maybe I'm
>>>>> getting something wrong. So here is what I did:
>>>>> Applied
>>>>> patch -p1 < jsonp.diff
>>>>>
>>>>> I got an updated mapogroutput.c.
>>>>>
>>>>> I then compiled with the following options:
>>>>> cmake -DCMAKE_INSTALL_PREFIX=/opt \
>>>>> -DCMAKE_PREFIX_PATH="/usr/local;/opt" \
>>>>> -DWITH_CLIENT_WFS=ON \
>>>>> -DWITH_CLIENT_WMS=ON \
>>>>> -DWITH_CURL=ON \
>>>>> -DWITH_SOS=ON \
>>>>> -DWITH_PHP=0 \
>>>>> -DWITH_PYTHON=ON \
>>>>> -DWITH_SVGCAIRO=OFF \
>>>>> -DWITH_ORACLESPATIAL=0 \
>>>>> -DWITH_MSSQL2008=OFF \
>>>>> -DWITH_THREAD_SAFETY=ON \
>>>>> -DWITH_LIBXML2=ON \
>>>>> -DWITH_MYSQL=ON\
>>>>> -DWITH_SDE=0 .. >../configure.out.txt
>>>>>
>>>>> Which goes through without any warning.
>>>>>
>>>>> I do make next, getting a few warnings about the Python bindings but
>>>>> nothing about mapogroutput (ecept that it was compiled). Then sudo
>> make
>>>>> install. No warnings.
>>>>> I then set a symlink to the new mapserv file inside cgi-bin and start
>> my
>>>>> apache.
>>>>>
>>>>> I changed my mapfile as you indicated:
>>>>> WEB
>>>>> FOOTER "test"
>>>>> IMAGEPATH "/var/www/html/tmp/"
>>>>> TEMPPATH "/var/www/html/tmp/"
>>>>> IMAGEURL "/html/tmp/"
>>>>> METADATA
>>>>> "wfs_title" "Test"
>>>>> "ows_onlineresource" "http://localhost:8082/wfs?"
>>>>> "ows_enable_request" "*"
>>>>> "ows_srs" "EPSG:3857"
>>>>> "wfs_srs" "EPSG:3857"
>>>>> "wfs_getfeature_formatlist" "geojson,csv,ogrgml"
>>>>> "wfs_encoding" "UTF-8"
>>>>> END # METADATA
>>>>> VALIDATION
>>>>> callback ".*"
>>>>> END
>>>>> END # WEB
>>>>>
>>>>> 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"
>>>>> FORMATOPTION "JSONP=%callback%"
>>>>> END
>>>>>
>>>>> Unfortunately calling:
>>>>>
>> http://localhost:8082/wfs?SERVICE=WFS&VERSION=1.1.0&request=GetFeature&TYP
>> ENAME=HU&outputformat=geojson&SRS=EPSG:3857&bbox=793732,6570204,793765,657
>>>>> 0228
>>>>>
>>>>> gives me a result as before without your changes:
>>>>> {
>>>>> "type": "FeatureCollection",
>>>>> "crs": { "type": "name", "properties": { "name":
>>>>> "urn:ogc:def:crs:EPSG::3857" } },
>>>>> "features": [
>>>>> { "type": "Feature", "properties": { "gml_id": "", "UMRING_ID":
>>>>> "36245586"}, "geometry": { "type": "Polygon", "coordinates": [ [ [
>>>>> 793757.73417, 6570202.31642 ], [ 793762.85989, 6570195.63821 ], [
>>>>> 793770.20682, 6570201.30901 ], [ 793765.08276, 6570207.98411 ], [
>>>>> 793757.73417, 6570202.31642 ] ] ] } },
>>>>> { "type": "Feature", "properties": { "gml_id": "", "UMRING_ID":
>>>>> "36245583"}, "geometry": { "type": "Polygon", "coordinates": [ [ [
>>>>> 793752.31968, 6570224.89917 ], [ 793747.96097, 6570230.48534 ], [
>>>>> 793732.07033, 6570218.01607 ], [ 793740.12777, 6570207.69188 ], [
>>>>> 793742.99908, 6570209.94649 ], [ 793747.5696, 6570204.08902 ], [
>>>>> 793761.16105, 6570214.75134 ], [ 793752.94726, 6570225.27397 ], [
>>>>> 793752.31968, 6570224.89917 ] ] ] } },
>>>>> { "type": "Feature", "properties": { "gml_id": "", "UMRING_ID":
>>>>> "36245550"}, "geometry": { "type": "Polygon", "coordinates": [ [ [
>>>>> 793735.55884, 6570231.00753 ], [ 793738.78194, 6570226.81082 ], [
>>>>> 793746.26949, 6570232.5959 ], [ 793743.04798, 6570236.79266 ], [
>>>>> 793735.55884, 6570231.00753 ] ] ] } }
>>>>> ]
>>>>> }
>>>>>
>>>>> I also tried this with an AJAX call from Javascript, giving me the
>>>>> exactly same result.
>>>>>
>>>>> I get an error message trying to use the result in Leaflet telling me
>> a
>>>>> ";" is missing. I assume it should look like this:
>>>>>
>>>>> var testlayer = [{
>>>>> "type": "FeatureCollection",
>>>>> "crs": { "type": "name", "properties": { "name":
>>>>> "urn:ogc:def:crs:EPSG::3857" } },
>>>>> "features": [
>>>>> { "type": "Feature", "properties": { "gml_id": "", "UMRING_ID":
>>>>> "36245586"}, "geometry": { "type": "Polygon", "coordinates": [ [ [
>>>>> 793757.73417, 6570202.31642 ], [ 793762.85989, 6570195.63821 ], [
>>>>> 793770.20682, 6570201.30901 ], [ 793765.08276, 6570207.98411 ], [
>>>>> 793757.73417, 6570202.31642 ] ] ] } },
>>>>> { "type": "Feature", "properties": { "gml_id": "", "UMRING_ID":
>>>>> "36245583"}, "geometry": { "type": "Polygon", "coordinates": [ [ [
>>>>> 793752.31968, 6570224.89917 ], [ 793747.96097, 6570230.48534 ], [
>>>>> 793732.07033, 6570218.01607 ], [ 793740.12777, 6570207.69188 ], [
>>>>> 793742.99908, 6570209.94649 ], [ 793747.5696, 6570204.08902 ], [
>>>>> 793761.16105, 6570214.75134 ], [ 793752.94726, 6570225.27397 ], [
>>>>> 793752.31968, 6570224.89917 ] ] ] } },
>>>>> { "type": "Feature", "properties": { "gml_id": "", "UMRING_ID":
>>>>> "36245550"}, "geometry": { "type": "Polygon", "coordinates": [ [ [
>>>>> 793735.55884, 6570231.00753 ], [ 793738.78194, 6570226.81082 ], [
>>>>> 793746.26949, 6570232.5959 ], [ 793743.04798, 6570236.79266 ], [
>>>>> 793735.55884, 6570231.00753 ] ] ] } }
>>>>> ]
>>>>> }];
>>>>>
>>>>> I'm not sure what is going on. Am I not applying the changes correctly
>>>>> or is my call wrong?
>>>>>
>>>>> Thanks for you great support.
>>>>>
>>>>> Best
>>>>>
>>>>> Lars
>>>>>
>>>>>
>>>>> Am 09.01.2015 um 15:30 schrieb Eichner, Andreas - SID:
>>>>>> Hi,
>>>>>>
>>>>>> I've attached a quick'n dirty solution (against master). When applied
>>>> to
>>>>>> mapogroutput.c you can set a JSONP formatoption to the callback name:
>>>>>>
>>>>>> 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'
>>>>>> FORMATOPTION 'JSONP=%callback%'
>>>>>> END
>>>>>>
>>>>>> This needs of course a WEB.VALIDATION:
>>>>>> WEB
>>>>>> ...
>>>>>> VALIDATION
>>>>>> callback '.*'
>>>>>> END
>>>>>> END
>>>>>>
>>>>>> Would be great if you can test if this works (looks good to me). The
>>>>> Devs might have a look if this could be integrated into master until
>> OGR
>>>>> has an appropriate layer creation option (solution described by Even).
>>>>>> Greetings
>>>>>>
>>>>>>> -----Ursprüngliche Nachricht-----
>>>>>>> Von: mapserver-users-bounces at lists.osgeo.org [mailto:mapserver-
>> users-
>>>>>>> bounces at lists.osgeo.org] Im Auftrag von Lars Fricke
>>>>>>> Gesendet: Freitag, 9. Januar 2015 11:24
>>>>>>> An: mapserver-users at lists.osgeo.org
>>>>>>> Betreff: 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
>>>> _______________________________________________
>>>> 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