[GIS] Reprojecting GeoJSON in OpenLayers

coordinate systemgeojsonopenlayers

I'm basically trying (and failing) to replicate this in OpenLayers 3.

I've set up a fiddle here:
http://jsfiddle.net/vu8ecy8v/5/

I'm under the impression that OpenLayers will reproject a GeoJSON source 'on the fly' if the projection of the GeoJSON is specified – which I'm doing in layers: of ol.map:

    ...
    new ol.layer.Vector({
        title: 'Montana',
        source: montanaSource,
        projection: 'EPSG:4326',
    }), 
    ...

This makes no difference. Am I missing something obvious or do I have to reproject the GeoJSON in some other way.

If I change the background / view to be EPSG:4326 (same as the source) the GeoJSON appears without a hitch. These alternatives are in the fiddle (commented out.)

Best Answer

After looking in the API docs at http://openlayers.org/en/v3.0.0/apidoc/ol.source.GeoJSON.html and unchecking the checkbox Stable Only on the top right of the banner, I see a projection option for the source.

It's here you have to set the projection to say to OpenLayers 3 to transform the data coordinates.

Trying changing your fiddle with below part, it works

var montanaSource = new ol.source.GeoJSON({
    object: ......................................,
    projection: 'EPSG:3857'
});
Related Question