3

I'm trying to change my map from using JSON to JSON-P for the source of data from my GeoServer. Unfortunately, what should be a vector layer covering the entire North Atlantic displays as a point in the Gulf of Guinea--so a classic projection error. But I can't see what I'm missing.

TL;DR: If I actually request the data from Geoserver using SRSNAME=EPSG:900913 I don't have a problem, but I can't figure out why I didn't need to do that originally!

The original vector layer is created by:

var vectorLayer = new ol.layer.Vector({
    source: new ol.source.Vector({
      url: '//'+$(location).attr('host')+'/geoserver/SAHFOS/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=SAHFOS:CPRStdAreasIntRes&maxFeatures=50&outputFormat=application/json',
      format: new ol.format.GeoJSON()
    })
});

and works fine. The 41 features fit exactly to the coastlines of a world map. Trying to do it with JSON-P:

    var stdAreas = new ol.layer.Vector({
        // Source retrieving WFS data in GeoJSON format using JSONP technique
        source: new ol.source.Vector({
            loader: function(extent) {
                $.ajax('http://domain/geoserver/SAHFOS/ows', {
                    type: 'GET',
                    data: {
                        service: 'WFS',
                        version: '1.0.0',
                        request: 'GetFeature',
                        typeName: 'SAHFOS:CPRStdAreasIntRes',
                        maxFeatures: '50',
                        outputFormat: 'text/javascript'
                        },
                    dataType: 'jsonp',
                    jsonpCallback:'callback:loadFeatures',
                    jsonp: 'format_options'
                })
            }
        })
    });

    var loadFeatures = function(response) {
        var source = stdAreas.getSource();
        source.addFeatures(new ol.format.GeoJSON().readFeatures(response, {dataProjection: 'EPSG:4326'}));
        var fmt=new ol.format.GeoJSON().readProjection(response)
    };

fmt is EPSG:4326, so that dataProjection shouldn't be necessary, but nevertheless, the layer still ends up being a point in the Gulf of Guinea.

Auspex
  • 153
  • 1
  • 12

1 Answers1

1

You need to set the featureProjection when you read the features:

source.addFeatures(new ol.format.GeoJSON().readFeatures(response, {
  dataProjection: 'EPSG:4326',
  featureProjection: 'EPSG:3857'
}));
oterral
  • 1,129
  • 7
  • 8
  • I don't think that's true (because I didn't have to when asking for JSON, rather than JSONP), and in any case as I pointed out, when I explicitly ask GeoServer to send me SRSNAME=EPSG:900913, I don't need to specify either Projection parameter.

    In any case, the question is why is it different between JSON and JSONP, not how to make it work--as you can see, I have got it working.

    – Auspex Feb 09 '17 at 15:26
  • When you ask for JSON the ol.layer Vector parse for you the json using featureProjection: view.getProjection() so when you ask for JSONP you need to do the same – oterral Feb 09 '17 at 16:14
  • 2nd if your JSONP contains coordinates in EPSG:900913 so the dataProjection paramter must be EPSG:3857 . EPSG:3837 is the official epsg for EPSG 900913 – oterral Feb 09 '17 at 16:17
  • "EPSG:3837 is the official epsg for EPSG 900913" is exactly why I don't understand projections. Why on earth wouldn't EPSG:900913 be the official epsg...? – Auspex Mar 02 '17 at 17:15
  • 1
    See this conversation and the history there – oterral Mar 14 '17 at 14:53
  • Thanks for that! It seems senseless until you know that there were competing companies using the same thing but giving it different names. – Auspex Mar 15 '17 at 17:32