Mapbox-gl and Supercluster: How to get access to reduce methods of supercluster instance in mapbox-gl - mapbox-gl-js

As I see supercluster uses like peer dependency for mapbox-gl.
Mapbox provides the possibility of clustering from the box and this is thanks supercluster.
Supercluster has very important method - reduce: a reduce function that merges properties of two clusters into one
But i can't find, how can i access to this method from mapbox instance
Is there any way to access to reduce? Or is there any other way to manage the merges properties?

Related

Is there a way to inject a custom clustering algorithm in mapbox-gl

I have a specific use case where each cluster can only have a specific 'type' of geo units. I have written the code to partition them correctly but I'm at a loss on how to subtitute the default clustering algorithm. Is there some sort of plugin architecture to specify a custom clustering algorithm?
Similar to how one can use Supercluster with leaflet (as mentioned in the supercluster readme https://github.com/mapbox/supercluster).
I also am passing in the type data to the algorithm and had to modify the type validation to get it work in my custom build. What's the correct way of notifying mapbox-gl of additional 'valid' parameters to the clustering algorithm.
If the above is not possible is the only other alternative to do a custom build replacing supercluster in mapbox-gl for the build. One final question, if that is the case are there instructions on how to do a mapbox-gl build. When I attempt an npm run build-dev or npm run build-prod I get complaints of a missing rollup config file.

Mapbox: Is there a way to retrieve all coordinates for a specific feature ID?

I'm building a feature where I need to extract all coordinates of a selecetd road/path in Mapbox when it's clicked on. I've attempted to use the queryRenderedFeatures method, but it seems the result list is fragmented. By "fragmented" I mean that if you have a road or path which is clearly just one long path/road when rendered on the map, it often consists up of 4-5-6 or more features, and you cannot really work out from the feature collection how they're supposed to be connected (in order)
I then tried to use the Tilequery API, but it doesn't return any coordinates for LineStrings.
Is there any API - server or client side - in Mapbox, where you can provide an ID of a feature and retrieve the all coordinates for a road or path?
Thanks in advance :-)
I think you're really asking: "is there a way to access complete LineString features for data in Mapbox's tilesets", to which the answer is, no, not really - other than trying to reassemble them in the way you have tried.
For your own data, you could host it using Mapbox's Datasets, rather than Tilesets.

Is there a way to set multiple layer filters in the Mapbox Static Images API?

I am using the mapbox static images API. I have two layers in my style that I'm trying to filter on the request. The documentation seems to only provide a single "setfilter" and a single "layer_id" property, indicating I can only filter one of the layers.
Is it possible to filter two (or three, four) layers in the static images API? I'd love to be able to comma separate the layer_id and/or setfilter parameters in the request to handle this case.
An example request they provide in the docs for setting the filters is:
https://api.mapbox.com/styles/v1/mapbox/streets-v11/static/-91,60,2/800x600?access_token=YOUR_MAPBOX_ACCESS_TOKEN&setfilter=["==","name_en","Canada"]&layer_id=country-label
I am looking for something like:
https://api.mapbox.com/styles/v1/mapbox/streets-v11/static/-91,60,2/800x600?access_token=YOUR_MAPBOX_ACCESS_TOKEN&setfilters=["==","name_en","Canada"],["==","name","New York"]&layer_ids=country-label,state-label
FWIW, my current workaround is to use an overlay for one of the layers to form the geojson objects that I needed filtering, but its hacky and has an upperbound on how many I can add to the request, and I had to simplify the geojson for brevity in the request.
Unfortunately there is not a direct way to reference multiple layers in a request to the Static Images API.
As a workaround you may be able to create a new style layer in Mapbox Studio that combines all of the layers you want to apply a filter to. You could then use setfilter on that layer to achieve filtering across these multiple layers, see the documentation here.

MapBox GL - Render GeoJson Thematically Based on Data in Client

I have a geoJson layer showing U.S. states, with only an ID, and no other properties.
I also have a local array of data in memory with lots of properties about those states: things like population, acres of farmland, sales tax rate etc. One of the properties is the ID that's in the geoJson layer. So I should be able to join the local array with features in the geoJson layer.
I would like to be able to thematically style the states based on any of those properties, allowing the user the ability to choose the property. All the examples I have seen for thematic styling of GeoJson layers in MapBox require (1) that the properties exist in the geoJson itself, and (2) that you define the property and styling rules in advance (I'm not sure if there's a way to change this at a later point).
In other map libraries, I think including Leaflet, you can define a paint function, and pass in the feature. Is there anything like this in MapBox GL?
Thanks.
You can make your life easier by joining the geojson data with your "properties" data into a single geojson feature collection.
Once you got that going, you can use map.setPaintProperty(layerId, ...) to update the layer style based on user interaction: https://docs.mapbox.com/mapbox-gl-js/api/#map#setpaintproperty
As of the documentation:
value(any) The value of the paint property to set. Must be of a type appropriate for the property, as defined in the Mapbox Style Specification .
You can set anything the style spec supports, even interpolated data-driven properties, wich means you can set & update any property of the layer you would be able to configure when newly adding that layer. Sure, you can not set multiple paint/layout properties with a single call, but that is something you can easily abstract away yourself.
This might be a bit far fetched, but assuming what you call a "theme" is just a collection of paint properties and values which you derived from an user interaction. You can just encode your "theme" as a valid mapbox style and use map.setStyle(...) to efficiently change multiple properties with a single call. Remeber setStyle() can receive a a style url but also a complete style object.

Accessing nested properties of a TileSet

I am trying to access all the features of a specific tileset generated threw MapBoxStudio.
Reading the API doc I guess I should be able to get all the nested parameters from the method :
map.querySourceFeatures(IdOfMyLayer);
However, the output of this is empty.
map.querySourceFeatures('words');
Where 'words' is the ID of the layer I got from map.getStyle().layers
I would expect to get access to the properties of this particular layer.
However, I get an empty result.
Sorry for the noob question but I am missing something crucial here in the way MapBox is working, any help from you would be very much appreciated.
If you are trying to access all the features of a tileset that you created in Studio, you should probably consider a different workflow. By definition, a tileset is a transformation of a dataset optimised for displaying a map, not querying.
If you use a Mapbox dataset rather than a tileset, you can access the whole dataset as a clean GeoJSON object - assuming it's small enough to load into the browser. Alternatively, you could create a GeoJSON by some other means, then upload it.
If querySourceFeature() is returning [], then most likely no features have loaded in the current viewport (yet). Perhaps you need to trigger the call at a later time, when the source data has loaded.
I get it now.
The proper way to retrieve all the features of a dataset is to use the DataSet API:
https://docs.mapbox.com/api/maps/#retrieve-a-dataset
This is what I've done :
$.ajax({
url: 'https://api.mapbox.com/datasets/v1/*****/{dataset_id}/features?limit=50&access_token=********',
type:'GET',
success:function(data){console.log(data)},
error:function(error){console.log('ERROR')}
})
With the {dataset_id} taken from mapboxStudio.
I can limit and store the variable to process the JSON response.
Thanks a lot :)