Thursday, 15 September 2016

coordinate system - Reprojecting GeoJSON in OpenLayers


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



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'
});

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...