Reputation: 332
Using the latest version of Openlayers, 4.3.1
, i set a vector source as follows:
this.vectorSource = new ol.source.Vector({
url: `../assets/data/file.json`,
format: new ol.format.TopoJSON()
});
this.vector = new ol.layer.Vector({
source: this.vectorSource,
style: this.style
});
But the file, in some cases, needed a previous treatement through other function.
How i can load the file.json
, treat him and use in ol.source.vector
?
I trying with a response ajax too, where dataSource
variable is a response from ajax call to same URL
in the first example ../assets/data/file.json
vectorSource = new ol.source.Vector({
features: (new ol.format.TopoJSON()).readFeatures(dataSource)
});
Upvotes: 1
Views: 380
Reputation: 5647
This can be done quite easily. Configure the ol.source.Vector
without a URL. Instead, you can load your json yourself and turn it into an object with
var json = JSON.parse(dataSource);
Then modify the JSON object, and finally call
var features = new ol.format.TopoJSON().readFeatures(dataSource,
{featureProjection: view.getProjection()});
vectorSource.addFeatures(features);
Note the featureProjection
option, which is required unless your view has the same projection as your data source.
Upvotes: 1