Reputation: 1955
I have a source layer like
{"
type:"geojson",
data:"http://www.example.com/markers.geojson"
}
Now, I need to make some calculations on the features. But since the data is a geojson URL, I don't have direct access to it. I might load it aside but it make things more complex.
I know you can get source features using querySourceFeatures, but this only gets features within the viewport bounding box :
The domain of the query includes all currently-loaded vector tiles and GeoJSON source tiles: this function does not check tiles outside the currently visible viewport.
This seems to be a recurrent question, but hell! Is there a solution that works to get all the features of a source ? Looks like an obvious thing to have.
Thanks a lot.
Upvotes: 3
Views: 1418
Reputation: 61
You could consider adding the geojson url to sessionStorage whenever a source is created/updated/deleted.
Then, when you need to work on the full dataset, you retrieve the latest key and use "fetch" to get the data. As the dataset has already been loaded by the browser it should be cached and accessible at a couple ms.
I've tested this with up to 20Mb geojson files without problems.
Example code:
const createSource = (url: string) => {
window.sessionStorage.setItem('geojson-url', url);
map.addSource('my-source', { type: 'geojson', data: url });
};
const makeCalculations = async () => {
const url = window.sessionStorage.getItem('geojson-url');
if (url) {
const data = await fetch(url).then((res) => res.json());
//... work on the data
}
};
Upvotes: 0