3

I'm trying to add a point to my open layers map from a file.

To start with I loaded the json data from a variable.

var data = '{ "type": "Feature", "geometry" : { "type": "MultiPoint", "coordinates": [ [-252000, -592000], [314000, 64000] ] } }'

var vectorSource = new ol.source.Vector({
    features: (new ol.format.GeoJSON()).readFeatures(data, {
    }),
});

var vectorLayer = new ol.layer.Vector({
    source: vectorSource,
});

var map = new ol.Map({
    target: 'map',
    layers: [
        new ol.layer.Tile({
        source: new ol.source.OSM()}),
        vectorLayer
    ],
    view: new ol.View({
        projection: ukProj,
        center: [0.5*(imageExtent[2]+imageExtent[0]),0.5*(imageExtent[3]+imageExtent[1])],
        zoom: 7,
    })
});

This works successfully. Now I'm trying to load the data from a json file, my file looks like this (same as variable input):

{ "type": "Feature", "geometry" : { "type": "MultiPoint", "coordinates": [ [-252000, -592000], [314000, 64000] ] } }

And I'm loading it like this:

geojsonLayer = new ol.layer.Vector({
    source: new ol.source.Vector({
        url: "https://path_to_data/data.json",
        format: new ol.format.GeoJSON(),
    }),
});

var map = new ol.Map({
    target: 'map',
    layers: [
        new ol.layer.Tile({
        source: new ol.source.OSM()}),
        geojsonLayer
    ],
    view: new ol.View({
        projection: ukProj,
        center: [0.5*(imageExtent[2]+imageExtent[0]),0.5*(imageExtent[3]+imageExtent[1])],
        zoom: 7,
    })
});

But the point is nowhere to be seen on my map. Can anyone tell me what I'm doing wrong?

2
  • your projection is different in your second code block ukProj. Is that supposed to be ukngProj?
    – Binx
    Feb 10 at 17:49
  • Sorry, typo. It was correct in the code.
    – dwright
    Feb 11 at 9:12

1 Answer 1

5

When used in a Vector source the GeoJSON parser will attempt to transform coordinates from the default data projection for GeoJSONs (CRS:84) to the view projection. As your coordinates are already in the view projection you should set that in the format options:

format: new ol.format.GeoJSON({dataProjection: ukProj})
1
  • That's got it working. Thank you!
    – dwright
    Feb 11 at 9:14

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.