I am using OpenLayers 3 to animate the paths of migrating animals tagged by scientists. I load the geoJSON file like so
var whaleSource = new ol.source.Vector({
url: 'data/BW2205005.json',
format: new ol.format.GeoJSON()
});
Instead of loading this directly into a layer, I would like to use and reuse the data in the geoJSON file for different purposes throughout my program. For example, I want to pull the lat & lon coordinates into an array to manipulate them to create interpolated animated tracks. Later I will want to query the geoJSON properties to restyle the tracks of males and females.
How might I load the geoJSON data into various arrays at different stages of my program instead of directly into a layer?
Thanks much
When using the
url
property ofol.source.Vector
the class loads the given url via XHR/AJAX for you:You could load the file yourself using XHR/AJAX using
XMLHttpRequest
or a library like jquery which has XHR/AJAX functionality. When you've retreived the GeoJSON collection you can loop over the features array it holds and split it up into what every you need and put those features into new separate GeoJSON collections. Here's a very crude example to give you and idea of the concept:Assume the following GeoJSON collection:
Here's how to load it (using jQuery's $.getJSON XHR function) and to split it up in to separate collections:
Now you can use the separate collections stored in the whale object to create layers. Note this differs some from using the
url
property:Here's a working example of the concept: http://plnkr.co/edit/rGwhI9vpu8ZYfAWvBZZr?p=preview
Edit after comment:
If you want all the coordinates for Willy:
Now
willysCoordinates
holds a nested array of coordinates:[[0, 0],[2, 2]]