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

How to get raw geojson data from addSource request: follow-up #8350

Closed
vgoklani opened this issue Jun 13, 2019 · 3 comments
Closed

How to get raw geojson data from addSource request: follow-up #8350

vgoklani opened this issue Jun 13, 2019 · 3 comments

Comments

@vgoklani
Copy link

vgoklani commented Jun 13, 2019

Hello - thanks for the follow-up on the previous question:

#8333

I tried this:

map.on('load', function() {
	map.addSource("geo_data", {
		"type": "geojson",
		"data": "/data.geojson",
		"buffer": 1,
		"maxzoom": 14,
		"generateId": true
	});
	map.addLayer({
		id: 'locations',
		type: 'symbol',
		source: "geo_data",
		layout: {
			'icon-image': 'restaurant-15',
			'icon-allow-overlap': true,
		}
	});

	const stores = map.querySourceFeatures("geo_data");
	console.log(stores)

but unfortunately it's returning an empty array.

@mourner is the GeoJSON data stored somewhere else (?), even as a private variable?

@planemad the data is visible on the default view of the map - the code was taken from the sweetgreens example, and I'm just passing the data from the server instead of hard-coding it. Thanks!

@vgoklani vgoklani changed the title How to get raw geojson data from addSource request - followup How to get raw geojson data from addSource request: follow-up Jun 13, 2019
@stevage
Copy link
Contributor

stevage commented Jun 14, 2019

I don't think the raw GeoJSON is stored anywhere. Assuming it gets converted to vector tiles then thrown away. If you need to access it (which is a common need), you'll need to fetch it yourself. I find a useful pattern is:

  1. Create source, with empty GeoJSON: { type: 'FeatureCollection', features: [] }
  2. Create layer that uses source.
  3. Fetch the actual data (using d3.json or axios or fetch()).
  4. When the data arrives, store it and call map.setData() to update the source.

@bibeksh9
Copy link

You can try this
map.getSource(sourceName).serialize().data

@mourner
Copy link
Member

mourner commented Jun 14, 2019

but unfortunately it's returning an empty array.

It's probably returning an empty array because it didn't finish loading. I think you have to subscribe to some event before doing querySourceFeatures — e.g. map.once('idle', ...).

is the GeoJSON data stored somewhere else (?), even as a private variable?

The problem is that it's stored on the Web Worker side, not on the main thread, so there's no easy way to fetch it.

Anyway, I'd recommend that you fetch your GeoJSON in the app in addition to loading it as a GL JS GeoJSON source. That removes any gotchas / difficulties from this case.

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

4 participants