5

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.)

Any suggestions/pointers very welcome.

Thanks.

7

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'
});
  • Yes I can confirm this works for me too, I'm happy to use the Experimental components. I will be sure to run this check in the future. Hopefully this will soon be included in the Stable documentation :) – jayuu Oct 18 '14 at 21:52
  • Samll precision >> "Unstable only" doesn't really mean unstable in the sense "it will not work or is not reliable". Instead, it means "This API part may change in the future, use with caution" – ThomasG77 Oct 18 '14 at 21:56
  • OK thanks for clarifying, and your assistance. It seems clear that on-the-fly reprojection is a common enough task to be managed by OpenLayers simply by setting the projection tag for a data source (somewhere) but you have also shown me I need to familiarise myself more with the docs. – jayuu Oct 19 '14 at 8:07
0

Have you tried to explicitly reproject your data using ol.proj.transform?

For example: var map_centre = ol.proj.transform([lon, lat], 'EPSG:4326', 'EPSG:3857');

  • I was considering doing something like this with the data but ultimately OpenLayers 3 seems to handle on-the-fly reprojection if the view / source etc. are explicitly defined. I followed ThomasG77's instructions and this seems the easiest way to achieve what I need to. – jayuu Oct 18 '14 at 21:46
  • I'm running into the same issue, my geojson data is not reprojected. Which ol code are you using to access the on the fly reprojection? – user2898616 Oct 19 '14 at 18:05
  • Actually works with v3.0.0 release. As above, changed: projection: 4326 -> 3857 So am I correct in deriving that the projection: should be what we want, not what the data currently is? My GeoJSON data is in lonlat. – user2898616 Oct 19 '14 at 18:13
  • As recommended above, read the "experimental" API, and yes it is "Destination projection" – user2898616 Oct 19 '14 at 18:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.