Mapbox-gl-js: Get original data from a GeoJSONSource

Created on 26 Nov 2015  Â·  5Comments  Â·  Source: mapbox/mapbox-gl-js

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.

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.

All 5 comments

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!

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PBrockmann picture PBrockmann  Â·  3Comments

mollymerp picture mollymerp  Â·  3Comments

muesliq picture muesliq  Â·  3Comments

stevage picture stevage  Â·  3Comments

iamdenny picture iamdenny  Â·  3Comments