2

I'm trying to extract coordinates from a GeoJSON file . I tried this line of code but I didn't have result in the console.

<script type="text/javascript">
    var vectorLayer = new ol.layer.Vector({
        source: new ol.source.Vector({
            url: '/area.geojson',
            format: new ol.format.GeoJSON()
        })
    });

    function getCoordinatesOfCounties() {
        var features = vectorLayer.getSource().getFeatures();
        features.forEach((feature) => {
            var coordinates = feature.getGeometry().getCoordinates();
            console.log(coordinates);
        });
    }
      
    </script> 

What's wrong?

3
  • Welcome to Geographic Information Systems! Welcome to GIS SE! We're a little different from other sites; this isn't a discussion forum but a Q&A site. Your questions should as much as possible describe not just what you want to do, but precisely what you have tried and where you are stuck trying that. Please check out our short tour for more about how the site works
    – Ian Turton
    Sep 23, 2021 at 8:22
  • 1
    The is no call of getCoordinatesOfCounties function in the above code. When did you call it? It should be called after featuresloadend event is triggered on the vector source.
    – TomazicM
    Sep 23, 2021 at 8:46
  • @TomazicM I didn't understand ! what should I do ?
    – Ahmed
    Sep 23, 2021 at 9:01

1 Answer 1

0

You have to wait till features are loaded into the source, then you can get coordinates of the features. You can use vector source event featuresloadend for that (see https://openlayers.org/en/latest/apidoc/module-ol_source_Vector-VectorSource.html and https://openlayers.org/en/latest/apidoc/module-ol_source_Vector.VectorSourceEvent.html).

Event featuresloadend is fired when all the features are loaded into the source. Property evt.features of event processing function parameter evt contains all the loaded features. For this to work you have to add vector layer to the map.

Code could the look something like this:

function getCoordinatesOfCounties(evt) {
  var features = evt.features;
  features.forEach((feature) => {
      var coordinates = feature.getGeometry().getCoordinates();
      console.log(coordinates);
  });
}

var vectorSource = new ol.source.Vector({
  url: '/area.geojson',
  format: new ol.format.GeoJSON()
});
vectorSource.on('featuresloadend', getCoordinatesOfCounties);

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

If you don't want to add layer to the map and just read features, you can use JS fetch to read data and ol.format.GeoJSON().readFeatures to read the features:

function getCoordinatesOfCounties(features) {
  features.forEach((feature) => {
      var coordinates = feature.getGeometry().getCoordinates();
      console.log('coords', coordinates);
  });
}

function readFeatures(data) {
  var features = new ol.format.GeoJSON().readFeatures(data, {
    dataProjection:'EPSG:4326',
    featureProjection: 'EPSG:3857'
  });
  getCoordinatesOfCounties(features);
}

fetch('/area.geojson')
  .then(response => response.json())
  .then(data => {readFeatures(data)});
3
  • I'm sorry but even when I tried this , I didn't have a result on the console !@TomazicM
    – Ahmed
    Sep 23, 2021 at 10:19
  • You have to add vector layer to the map for the features to be loaded into the source.
    – TomazicM
    Sep 23, 2021 at 10:22
  • See modified answer.
    – TomazicM
    Sep 23, 2021 at 10:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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