kaycee
kaycee

Reputation: 921

Convert GeoJSON coordinates to other projection to use with Turf.js

I have a GeoJSON feature collection using the NAD-83 UTM projection, so the coordinates are in meters. I want to use this feature collection with the Turf.JS library to do some contouring.

The problem is that Turf.JS only takes WGS84 coordinates. When I'm doing the contouring on my grid with the UTM projection, the resulting isolines have wrong coordinates (-XXXXXXXX, XXXXXXXX).

How can I convert my GeoJSON file to have WGS84 coordinates instead of UTM ?

That's my code:

var receptors1 = new ol.layer.Vector({
        name: "Receptors",
        visible: true,
        source: new ol.source.Vector({
            url: "url.json",
            format: new ol.format.GeoJSON()
        })
    });

map.addLayer(receptors1);

receptors1.getSource().on('change', function(evt){
    var source = evt.target;
    if(source.getState() === 'ready'){  
        var feats = source.getFeatures();

        var newForm = new ol.format.GeoJSON();

        featColl = newForm.writeFeaturesObject(feats);

        var breaks = [0, 1, 2, 3, 4, 5];

        isolined = turf.isolines(featColl, 'Max', 15, breaks);

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

        var isoShow = new ol.layer.Vector({
            source: vectorSource
        })

        map.addLayer(isoShow);
    }
})

Upvotes: 0

Views: 2883

Answers (1)

ahocevar
ahocevar

Reputation: 5647

To pass the GeoJSON to Turf.js, do

var formatOptions = {featureProjection: map.getView().getProjection()};
featColl = newForm.writeFeaturesObject(feats, formatOptions);

To add the result from Turf.js back to your source, do

features: newForm.readFeatures(isolined, formatOptions)

Upvotes: 2

Related Questions