Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get original data from a GeoJSONSource #1762

Closed
nilshoerrmann opened this issue Nov 26, 2015 · 5 comments
Closed

Get original data from a GeoJSONSource #1762

nilshoerrmann opened this issue Nov 26, 2015 · 5 comments

Comments

@nilshoerrmann
Copy link

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.

@mourner
Copy link
Member

mourner commented Nov 26, 2015

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.

@mourner mourner closed this as completed Nov 26, 2015
@nilshoerrmann
Copy link
Author

… 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?

@mourner
Copy link
Member

mourner commented Nov 26, 2015

Yes, that's correct, but that depends on the size of GeoJSON. It's fine for smaller objects.

@nilshoerrmann
Copy link
Author

Okay, thanks @mourner for the explanation, very much appreciated!

kielni added a commit to RagtagOpen/marchon-map that referenced this issue Nov 11, 2017
load GeoJSON directly, then pass to Mapbox
Mapbox won't share if it loads the data: mapbox/mapbox-gl-js#1762
map events only return features in the viewport; does not include all features in the geojson
@shawnmgoulet
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants