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?

Matt
19.4k4 gold badges25 silver badges64 bronze badges
asked Sep 23, 2021 at 8:19
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 Commented 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. Commented Sep 23, 2021 at 8:46
  • @TomazicM I didn't understand ! what should I do ? Commented Sep 23, 2021 at 9:01

1 Answer 1

1

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)});
answered Sep 23, 2021 at 9:54
3
  • I'm sorry but even when I tried this , I didn't have a result on the console !@TomazicM Commented Sep 23, 2021 at 10:19
  • You have to add vector layer to the map for the features to be loaded into the source. Commented Sep 23, 2021 at 10:22
  • See modified answer. Commented Sep 23, 2021 at 10:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.