When I add a GeoJSON source to my map, I can point options.data to an URL like so:
var sourceObj = new mapboxgl.GeoJSONSource({
data: 'http://example.com/geo.json'
});
map.addSource('some id', sourceObj);
Mapbox GL takes care of fetching my external source, which is great. But is there a way to access the original GeoJSON data for further processing outside of Mapbox GL? Something like getData() which returns the plain, unchanged GeoJSON object?
As long as I'm not missing the obvious, right now I have to fetch the GeoJSON manually for a second time. It would be nice to circumvent this unnecessary step.
If you need it both as a source and as an object for further processing, you should just pass the object instead of an url to GeoJSONSource, or fetch it the second time to avoid a performance hit — sending JSON data between worker and the main thread is expensive in case of big JSON objects. I don't think we want to handle this on the API level.
… sending JSON data between worker and the main thread is expensive in case of big JSON objects.
I didn't know that. Thanks!
… you should just pass the object instead of an url to GeoJSONSource, or fetch it the second time to avoid a performance hit …
Just so I understand the issue correctly as I'm not familiar with the inner workings of Mapbox GL:
Am I correct that even if I fetch the GeoJSON first and pass the plain object to GeoJSONSource, there will be a performance hit (freezing of the browser, I guess) because the data needs to be forwarded to a worker? So even if it sounds counterintuitive, fetching my data twice will be more performant on the client?
Yes, that's correct, but that depends on the size of GeoJSON. It's fine for smaller objects.
Okay, thanks @mourner for the explanation, very much appreciated!
Most helpful comment
It would be super sweet if this workflow suggestion was added to the docs in the sources section by Mapbox. I am currently implementing - thanks to @kielni for the reference & @mourner for this explanation.